Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture Bios etag for later updates #181

Merged
merged 1 commit into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
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")
}
}