Skip to content

Commit

Permalink
implement firmware response mapping (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandro Koll authored Mar 23, 2021
1 parent f40cca8 commit e448777
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 24 deletions.
31 changes: 25 additions & 6 deletions cmd/metal-api/internal/service/firmware-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,10 @@ func (r firmwareResource) listFirmwares(request *restful.Request, response *rest
kk = append(kk, kind)
}

resp := v1.FirmwaresResponse{
Revisions: make(map[string]map[string]map[string][]string),
}
rr := make(map[string]map[string]map[string][]string)
for i := range kk {
k := kk[i]
resp.Revisions[k] = make(map[string]map[string][]string)
rr[k] = make(map[string]map[string][]string)
machineID := request.QueryParameter("machine-id")
switch machineID {
case "":
Expand All @@ -211,7 +209,7 @@ func (r firmwareResource) listFirmwares(request *restful.Request, response *rest
Prefix: &k,
}, func(page *s3.ListObjectsOutput, last bool) bool {
for _, p := range page.Contents {
insertRevisions(*p.Key, resp.Revisions[k], vendor, board)
insertRevisions(*p.Key, rr[k], vendor, board)
}
return true
})
Expand All @@ -230,10 +228,11 @@ func (r firmwareResource) listFirmwares(request *restful.Request, response *rest
case bmc:
bb[f.Board] = []string{f.BmcVersion}
}
resp.Revisions[k][f.Vendor] = bb
rr[k][f.Vendor] = bb
}
}

resp := mapToFirmwareResponse(rr)
err := response.WriteHeaderAndEntity(http.StatusOK, resp)
if err != nil {
utils.Logger(request).Sugar().Error("Failed to send response", zap.Error(err))
Expand Down Expand Up @@ -335,3 +334,23 @@ func toFirmwareKind(kind string) (string, error) {
}
return "", fmt.Errorf("unknown firmware kind %q", kind)
}

func mapToFirmwareResponse(m map[string]map[string]map[string][]string) *v1.FirmwaresResponse {
resp := &v1.FirmwaresResponse{
Revisions: make(map[string]v1.VendorRevisions),
}
for k, vv := range m {
resp.Revisions[k] = v1.VendorRevisions{
VendorRevisions: make(map[string]v1.BoardRevisions),
}
for v, bb := range vv {
resp.Revisions[k].VendorRevisions[v] = v1.BoardRevisions{
BoardRevisions: make(map[string][]string),
}
for b, rr := range bb {
resp.Revisions[k].VendorRevisions[v].BoardRevisions[b] = rr
}
}
}
return resp
}
10 changes: 9 additions & 1 deletion cmd/metal-api/internal/service/v1/firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ type Firmware struct {
}

type FirmwaresResponse struct {
Revisions map[string]map[string]map[string][]string `json:"revisions" description:"list of firmwares per board per vendor per kind"`
Revisions map[string]VendorRevisions `json:"revisions" description:"list of firmwares per board per vendor per kind"`
}

type VendorRevisions struct {
VendorRevisions map[string]BoardRevisions
}

type BoardRevisions struct {
BoardRevisions map[string][]string
}

type MachineUpdateFirmwareRequest struct {
Expand Down
48 changes: 31 additions & 17 deletions spec/metal-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@
"cidrs"
]
},
"v1.BoardRevisions": {
"properties": {
"BoardRevisions": {
"additionalProperties": {
"items": {
"type": "string"
},
"type": "array"
},
"type": "object"
}
},
"required": [
"BoardRevisions"
]
},
"v1.BootInfo": {
"properties": {
"bootloaderid": {
Expand Down Expand Up @@ -495,7 +511,7 @@
"properties": {
"revisions": {
"additionalProperties": {
"$ref": "#/definitions/v1.FirmwaresResponse.revisions"
"$ref": "#/definitions/v1.VendorRevisions"
},
"description": "list of firmwares per board per vendor per kind",
"type": "object"
Expand All @@ -505,21 +521,6 @@
"revisions"
]
},
"v1.FirmwaresResponse.revisions": {
"additionalProperties": {
"$ref": "#/definitions/v1.FirmwaresResponse.revisions.value"
},
"type": "object"
},
"v1.FirmwaresResponse.revisions.value": {
"additionalProperties": {
"items": {
"type": "string"
},
"type": "array"
},
"type": "object"
},
"v1.IPAllocateRequest": {
"properties": {
"description": {
Expand Down Expand Up @@ -1993,7 +1994,7 @@
"type": "string"
},
"kind": {
"description": "the firmware kind, i.e. 'bios' of 'bmc'",
"description": "the firmware kind, i.e. [bios|bmc]",
"type": "string"
},
"revision": {
Expand Down Expand Up @@ -3089,6 +3090,19 @@
"id",
"rack_id"
]
},
"v1.VendorRevisions": {
"properties": {
"VendorRevisions": {
"additionalProperties": {
"$ref": "#/definitions/v1.BoardRevisions"
},
"type": "object"
}
},
"required": [
"VendorRevisions"
]
}
},
"info": {
Expand Down

0 comments on commit e448777

Please sign in to comment.