weldr: Fix blueprints/info response for unknown blueprints
The response is different for JSON and TOML requests. If it is JSON it will always return a 200, but any blueprints with errors will be in the errors list. If TOML has an error it will return an error 400 with the error in a standard API error response with status set to false.
This commit is contained in:
parent
7bd020ca11
commit
e13ebd46ee
3 changed files with 38 additions and 30 deletions
|
|
@ -732,29 +732,7 @@ func (api *API) blueprintsInfoHandler(writer http.ResponseWriter, request *http.
|
|||
return
|
||||
}
|
||||
|
||||
blueprints := []blueprint.Blueprint{}
|
||||
changes := []change{}
|
||||
|
||||
for i, name := range names {
|
||||
// remove leading / from first name
|
||||
if i == 0 {
|
||||
name = name[1:]
|
||||
}
|
||||
|
||||
blueprint, changed := api.store.GetBlueprint(name)
|
||||
if blueprint == nil {
|
||||
errors := responseError{
|
||||
ID: "UnknownBlueprint",
|
||||
Msg: fmt.Sprintf("%s: ", name),
|
||||
}
|
||||
statusResponseError(writer, http.StatusBadRequest, errors)
|
||||
return
|
||||
}
|
||||
blueprints = append(blueprints, *blueprint)
|
||||
changes = append(changes, change{changed, blueprint.Name})
|
||||
}
|
||||
|
||||
q, err := url.ParseQuery(request.URL.RawQuery)
|
||||
query, err := url.ParseQuery(request.URL.RawQuery)
|
||||
if err != nil {
|
||||
errors := responseError{
|
||||
ID: "InvalidChars",
|
||||
|
|
@ -764,12 +742,34 @@ func (api *API) blueprintsInfoHandler(writer http.ResponseWriter, request *http.
|
|||
return
|
||||
}
|
||||
|
||||
format := q.Get("format")
|
||||
blueprints := []blueprint.Blueprint{}
|
||||
changes := []change{}
|
||||
blueprintErrors := []responseError{}
|
||||
|
||||
for i, name := range names {
|
||||
// remove leading / from first name
|
||||
if i == 0 {
|
||||
name = name[1:]
|
||||
}
|
||||
|
||||
blueprint, changed := api.store.GetBlueprint(name)
|
||||
if blueprint == nil {
|
||||
blueprintErrors = append(blueprintErrors, responseError{
|
||||
ID: "UnknownBlueprint",
|
||||
Msg: fmt.Sprintf("%s: ", name),
|
||||
})
|
||||
continue
|
||||
}
|
||||
blueprints = append(blueprints, *blueprint)
|
||||
changes = append(changes, change{changed, blueprint.Name})
|
||||
}
|
||||
|
||||
format := query.Get("format")
|
||||
if format == "json" || format == "" {
|
||||
err := json.NewEncoder(writer).Encode(reply{
|
||||
Blueprints: blueprints,
|
||||
Changes: changes,
|
||||
Errors: []responseError{},
|
||||
Errors: blueprintErrors,
|
||||
})
|
||||
common.PanicOnError(err)
|
||||
} else if format == "toml" {
|
||||
|
|
@ -784,6 +784,10 @@ func (api *API) blueprintsInfoHandler(writer http.ResponseWriter, request *http.
|
|||
statusResponseError(writer, http.StatusBadRequest, errors)
|
||||
return
|
||||
}
|
||||
if len(blueprintErrors) > 0 {
|
||||
statusResponseError(writer, http.StatusBadRequest, blueprintErrors...)
|
||||
return
|
||||
}
|
||||
encoder := toml.NewEncoder(writer)
|
||||
encoder.Indent = ""
|
||||
err := encoder.Encode(blueprints[0])
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ func TestBasic(t *testing.T) {
|
|||
|
||||
{"/api/v0/blueprints/list", http.StatusOK, `{"total":1,"offset":0,"limit":1,"blueprints":["test"]}`},
|
||||
{"/api/v0/blueprints/info/", http.StatusNotFound, `{"errors":[{"code":404,"id":"HTTPError","msg":"Not Found"}],"status":false}`},
|
||||
{"/api/v0/blueprints/info/foo", http.StatusBadRequest, `{"errors":[{"id":"UnknownBlueprint","msg":"foo: "}],"status":false}`},
|
||||
{"/api/v0/blueprints/info/foo", http.StatusOK, `{"blueprints":[],"changes":[],"errors":[{"id":"UnknownBlueprint","msg":"foo: "}]}`},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
|
|
@ -234,7 +234,7 @@ func TestBlueprintsInfo(t *testing.T) {
|
|||
"changes":[{"name":"test1","changed":false}], "errors":[]}`},
|
||||
{"GET", "/api/v0/blueprints/info/test2", ``, http.StatusOK, `{"blueprints":[{"name":"test2","description":"Test","modules":[],"packages":[{"name":"systemd","version":"123"}],"groups":[],"version":"0.0.0"}],
|
||||
"changes":[{"name":"test2","changed":true}], "errors":[]}`},
|
||||
{"GET", "/api/v0/blueprints/info/test3-non", ``, http.StatusBadRequest, `{"status":false,"errors":[{"id":"UnknownBlueprint","msg":"test3-non: "}]}`},
|
||||
{"GET", "/api/v0/blueprints/info/test3-non", ``, http.StatusOK, `{"blueprints":[],"changes":[],"errors":[{"id":"UnknownBlueprint","msg":"test3-non: "}]}`},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue