Skip to content

Commit

Permalink
only add image usage if explicitly specified (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Dec 3, 2021
1 parent 785a075 commit 4408b1c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
30 changes: 22 additions & 8 deletions cmd/metal-api/internal/service/image-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/http"
"strconv"
"time"

"github.com/metal-stack/metal-api/cmd/metal-api/internal/datastore"
Expand Down Expand Up @@ -82,6 +83,7 @@ func (ir imageResource) webService() *restful.WebService {
Operation("listImages").
Doc("get all images").
Metadata(restfulspec.KeyOpenAPITags, tags).
Param(ws.QueryParameter("show-usage", "include image usage into response").DataType("bool").DefaultValue("false")).
Writes([]v1.ImageResponse{}).
Returns(http.StatusOK, "OK", []v1.ImageResponse{}).
DefaultReturns("Error", httperrors.HTTPErrorResponse{}))
Expand Down Expand Up @@ -172,19 +174,31 @@ func (ir imageResource) listImages(request *restful.Request, response *restful.R
return
}

ms, err := ir.ds.ListMachines()
if checkError(request, response, utils.CurrentFuncName(), err) {
return
ms := metal.Machines{}
showUsage := false
if request.QueryParameter("show-usage") != "" {
showUsage, err = strconv.ParseBool(request.QueryParameter("show-usage"))
if checkError(request, response, utils.CurrentFuncName(), err) {
return
}
if showUsage {
ms, err = ir.ds.ListMachines()
if checkError(request, response, utils.CurrentFuncName(), err) {
return
}
}
}

result := []*v1.ImageResponse{}
for i := range imgs {
machines := ir.machinesByImage(ms, imgs[i].ID)
ir := v1.NewImageResponse(&imgs[i])
if len(machines) > 0 {
ir.UsedBy = machines
img := v1.NewImageResponse(&imgs[i])
if showUsage {
machines := ir.machinesByImage(ms, imgs[i].ID)
if len(machines) > 0 {
img.UsedBy = machines
}
}
result = append(result, ir)
result = append(result, img)
}
err = response.WriteHeaderAndEntity(http.StatusOK, result)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions spec/metal-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -4964,6 +4964,15 @@
"application/json"
],
"operationId": "listImages",
"parameters": [
{
"default": false,
"description": "include image usage into response",
"in": "query",
"name": "show-usage",
"type": "bool"
}
],
"produces": [
"application/json"
],
Expand Down

0 comments on commit 4408b1c

Please sign in to comment.