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

Add option to clean etag before PATCH and POST #275

Merged
merged 1 commit into from
Sep 15, 2023
Merged
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
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