mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-11 22:14:02 +01:00
orchestrator: provide configuration for other components as YAML
JSON does not allow to marshal fields as embedded (like YAML "inline" tag).
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
@@ -44,15 +44,17 @@ func (c ConfigRelatedOptions) Parse(out io.Writer, component string, config inte
|
||||
return fmt.Errorf("unable to fetch configuration file: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if contentType := resp.Header.Get("Content-Type"); contentType != "application/json" && !strings.HasPrefix(contentType, "application/json;") {
|
||||
return fmt.Errorf("received configuration file is not JSON (%s)", contentType)
|
||||
contentType := resp.Header.Get("Content-Type")
|
||||
mediaType, _, err := mime.ParseMediaType(contentType)
|
||||
if mediaType != "application/x-yaml" || err != nil {
|
||||
return fmt.Errorf("received configuration file is not YAML (%s)", contentType)
|
||||
}
|
||||
input, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to read configuration file: %w", err)
|
||||
}
|
||||
if err := json.Unmarshal(input, &rawConfig); err != nil {
|
||||
return fmt.Errorf("unable to parse JSON configuration file: %w", err)
|
||||
if err := yaml.Unmarshal(input, &rawConfig); err != nil {
|
||||
return fmt.Errorf("unable to parse YAML configuration file: %w", err)
|
||||
}
|
||||
} else {
|
||||
input, err := ioutil.ReadFile(cfgFile)
|
||||
@@ -60,7 +62,7 @@ func (c ConfigRelatedOptions) Parse(out io.Writer, component string, config inte
|
||||
return fmt.Errorf("unable to read configuration file: %w", err)
|
||||
}
|
||||
if err := yaml.Unmarshal(input, &rawConfig); err != nil {
|
||||
return fmt.Errorf("unable to parse configuration file: %w", err)
|
||||
return fmt.Errorf("unable to parse YAML configuration file: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,24 +219,18 @@ module2:
|
||||
|
||||
func TestHTTPConfiguration(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `
|
||||
{
|
||||
"module1": {
|
||||
"topic": "flows"
|
||||
},
|
||||
"module2": {
|
||||
"details": {
|
||||
"workers": 5,
|
||||
"interval-value": "20m"
|
||||
},
|
||||
"stuff": "bye",
|
||||
"elements": [
|
||||
{"name": "first", "gauge": 67},
|
||||
{"name": "second"}
|
||||
]
|
||||
}
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/x-yaml; charset=utf-8")
|
||||
fmt.Fprint(w, `---
|
||||
module1:
|
||||
topic: flows
|
||||
module2:
|
||||
details:
|
||||
workers: 5
|
||||
interval-value: 20m
|
||||
stuff: bye
|
||||
elements:
|
||||
- {"name": "first", "gauge": 67}
|
||||
- {"name": "second"}
|
||||
`)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
@@ -14,10 +14,10 @@ func (c *Component) configurationHandlerFunc(gc *gin.Context) {
|
||||
c.serviceLock.Unlock()
|
||||
|
||||
if !ok {
|
||||
gc.JSON(http.StatusNotFound, gin.H{"message": "Configuration not found."})
|
||||
gc.YAML(http.StatusNotFound, gin.H{"message": "Configuration not found."})
|
||||
return
|
||||
}
|
||||
gc.IndentedJSON(http.StatusOK, configuration)
|
||||
gc.YAML(http.StatusOK, configuration)
|
||||
|
||||
c.serviceLock.Lock()
|
||||
if c.registeredServices[ServiceType(service)] == nil {
|
||||
|
||||
@@ -6,8 +6,6 @@ import (
|
||||
"akvorado/common/helpers"
|
||||
"akvorado/common/http"
|
||||
"akvorado/common/reporter"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestConfigurationEndpoint(t *testing.T) {
|
||||
@@ -26,10 +24,11 @@ func TestConfigurationEndpoint(t *testing.T) {
|
||||
|
||||
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
|
||||
{
|
||||
URL: "/api/v0/orchestrator/configuration/inlet",
|
||||
JSONOutput: gin.H{
|
||||
"bye": "Goodbye world!",
|
||||
"hello": "Hello world!",
|
||||
URL: "/api/v0/orchestrator/configuration/inlet",
|
||||
ContentType: "application/x-yaml; charset=utf-8",
|
||||
FirstLines: []string{
|
||||
`bye: Goodbye world!`,
|
||||
`hello: Hello world!`,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user