Skip to content

Commit

Permalink
Capture Bios etag for later updates (#181)
Browse files Browse the repository at this point in the history
This updates our handling of an instances etag so we capture it on
initial retrieval of the object. Then this value can be used later for
any update calls to enforce that the object we are trying to update has
not been modified since our local version of it was retrieved.

Signed-off-by: Sean McGinnis <[email protected]>
  • Loading branch information
stmcginnis authored Jun 6, 2022
1 parent 993ec02 commit 9d4d64f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
21 changes: 13 additions & 8 deletions redfish/bios.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 89,10 @@ type Bios struct {
activeSoftwareImage string
// rawData holds the original serialized JSON so we can compare updates.
rawData []byte
// etag contains the etag header when fetching the object. This is used to
// control updates to make sure the object has not been modified my a different
// process between fetching and updating that could cause conflicts.
etag string
}

// UnmarshalJSON unmarshals an Bios object from the raw JSON.
Expand Down Expand Up @@ -154,6 158,10 @@ func GetBios(c common.Client, uri string) (*Bios, error) {
return nil, err
}

if resp.Header["Etag"] != nil {
bios.etag = resp.Header["Etag"][0]
}

bios.SetClient(c)
return &bios, nil
}
Expand Down Expand Up @@ -263,23 271,20 @@ func (bios *Bios) UpdateBiosAttributesApplyAt(attrs BiosAttributes, applyTime co
}
}

resp, err := bios.Client.Get(bios.settingsTarget)
if err != nil {
return err
}
defer resp.Body.Close()
// If there are any allowed updates, try to send updates to the system and
// return the result.
if len(payload) > 0 {
data := map[string]interface{}{"Attributes": payload}
if applyTime != "" {
data["@Redfish.SettingsApplyTime"] = map[string]string{"ApplyTime": string(applyTime)}
}

var header = make(map[string]string)
if resp.Header["Etag"] != nil {

This comment has been minimized.

Copy link
@Sn0rt

Sn0rt Aug 14, 2022

Contributor

this's doesn't work.

Only can set the Etag from the value query from GET /redfish/v1/Systems/1/Bios/Settings not /redfish/v1/Systems/1/Bios

header["If-Match"] = resp.Header["Etag"][0]
if bios.etag != "" {
header["If-Match"] = bios.etag
}
resp, err = bios.Client.PatchWithHeaders(bios.settingsTarget, data, header)

resp, err := bios.Client.PatchWithHeaders(bios.settingsTarget, data, header)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions redfish/bios_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 206,15 @@ func TestUpdateBiosAttributes(t *testing.T) {

calls := testClient.CapturedCalls()

if len(calls) != 2 {
if len(calls) != 1 {
t.Errorf("Expected one call to be made, captured: %v", calls)
}

if !strings.Contains(calls[1].Payload, "AssetTag") {
if !strings.Contains(calls[0].Payload, "AssetTag") {
t.Errorf("Unexpected update payload: %s", calls[0].Payload)
}

if strings.Contains(calls[1].Payload, "@Redfish.SettingsApplyTime") {
if strings.Contains(calls[0].Payload, "@Redfish.SettingsApplyTime") {
t.Error("Expected 'SettingsApplyTime' to not be present")
}
}
Expand All @@ -240,15 240,15 @@ func TestUpdateBiosAttributesApplyAt(t *testing.T) {

calls := testClient.CapturedCalls()

if len(calls) != 2 {
if len(calls) != 1 {
t.Errorf("Expected one call to be made, captured: %v", calls)
}

if !strings.Contains(calls[1].Payload, "AssetTag") {
if !strings.Contains(calls[0].Payload, "AssetTag") {
t.Errorf("Unexpected update payload: %s", calls[0].Payload)
}

if !strings.Contains(calls[1].Payload, "@Redfish.SettingsApplyTime") {
if !strings.Contains(calls[0].Payload, "@Redfish.SettingsApplyTime") {
t.Error("Expected 'SettingsApplyTime' to be present")
}
}

0 comments on commit 9d4d64f

Please sign in to comment.