Skip to content

Commit

Permalink
fix firmware versioned attribute compatibility (#111)
Browse files Browse the repository at this point in the history
The firmware payload written by the older style APIs was inconsistent
with the way that the setInventory and getInventory APIs were reading
it. This change provides an internal adapter structure to make sure the
data formats are consistent.
  • Loading branch information
DoctorVin authored Sep 26, 2024
1 parent 768e52a commit df00198
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions internal/inventory/component_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@ func componentAttributesFromJSON(byt []byte) (*rivets.ComponentAttributes, error
return ca, nil
}

// for historical reasons, alloy used to store firmware data like this using a different API
// we create this here to maintain compatibility with old data
type firmwareContainer struct {
Firmware *common.Firmware `json:"firmware,omitempty"`
}

func mustFirmwareJSON(fw *common.Firmware) []byte {
if fw == nil {
return nil
}
byt, err := json.Marshal(fw)

fwc := &firmwareContainer{
Firmware: fw,
}
byt, err := json.Marshal(fwc)
if err != nil {
panic("bad firmware payload")
}
Expand All @@ -44,12 +54,12 @@ func firmwareFromJSON(byt []byte) (*common.Firmware, error) {
if byt == nil {
return nil, nil
}
fw := &common.Firmware{}
err := json.Unmarshal(byt, fw)
fwc := &firmwareContainer{}
err := json.Unmarshal(byt, fwc)
if err != nil {
return nil, err
}
return fw, nil
return fwc.Firmware, nil
}

type statusContainer struct {
Expand Down

0 comments on commit df00198

Please sign in to comment.