Skip to content

Commit

Permalink
Add option to clean etag before PATCH and POST (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
TyphonHill authored Sep 15, 2023
1 parent 9c6205f commit db55696
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions common/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"strings"
)

// Entity provides the common basis for all Redfish and Swordfish objects.
Expand All @@ -24,6 +25,9 @@ type Entity struct {
// 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
// Removes surrounding quotes of etag used in If-Match header of PATCH and POST requests.
// Only use this option to resolve bad vendor implementation where If-Match only matches the unquoted etag string.
stripEtagQuotes bool
}

// SetClient sets the API client connection to use for accessing this
Expand All @@ -38,6 +42,11 @@ func (e *Entity) GetClient() Client {
return e.client
}

// Set stripEtagQuotes to enable/disable strupping etag quotes
func (e *Entity) StripEtagQuotes(b bool) {
e.stripEtagQuotes = b
}

// Update commits changes to an entity.
func (e *Entity) Update(originalEntity, updatedEntity reflect.Value, allowedUpdates []string) error {
payload := getPatchPayloadFromUpdate(originalEntity, updatedEntity)
Expand Down Expand Up @@ -90,6 +99,10 @@ func (e *Entity) Get(c Client, uri string, payload interface{}) error {
func (e *Entity) Patch(uri string, payload interface{}) error {
header := make(map[string]string)
if e.etag != "" {
if e.stripEtagQuotes {
e.etag = strings.Trim(e.etag, "\"")
}

header["If-Match"] = e.etag
}

Expand All @@ -104,6 +117,10 @@ func (e *Entity) Patch(uri string, payload interface{}) error {
func (e *Entity) Post(uri string, payload interface{}) error {
header := make(map[string]string)
if e.etag != "" {
if e.stripEtagQuotes {
e.etag = strings.Trim(e.etag, "\"")
}

header["If-Match"] = e.etag
}

Expand Down

0 comments on commit db55696

Please sign in to comment.