Skip to content

Commit

Permalink
Redfish disable Etag match header (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Sep 22, 2023
2 parents 44c2961 + 93480d6 commit 1f97b35
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.30.0
github.com/sirupsen/logrus v1.8.1
github.com/stmcginnis/gofish v0.14.0
github.com/stmcginnis/gofish v0.14.1-0.20230920133920-77490fd98fa2
github.com/stretchr/testify v1.8.0
go.uber.org/goleak v1.2.1
golang.org/x/crypto v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stmcginnis/gofish v0.14.0 h1:geECNAiG33JDB2x2xDkerpOOuXFqxp5YP3EFE3vd5iM=
github.com/stmcginnis/gofish v0.14.0/go.mod h1:BLDSFTp8pDlf/xDbLZa+F7f7eW0E/CHCboggsu8CznI=
github.com/stmcginnis/gofish v0.14.1-0.20230920133920-77490fd98fa2 h1:R0N4G786trm1dHBwJftzaupRrwhY1T+rBrTBC8eqiRQ=
github.com/stmcginnis/gofish v0.14.1-0.20230920133920-77490fd98fa2/go.mod h1:BLDSFTp8pDlf/xDbLZa+F7f7eW0E/CHCboggsu8CznI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
Expand Down
10 changes: 10 additions & 0 deletions internal/redfishwrapper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Client struct {
user string
pass string
basicAuth bool
disableEtagMatch bool
versionsNotCompatible []string // a slice of redfish versions to ignore as incompatible
client *gofish.APIClient
httpClient *http.Client
Expand Down Expand Up @@ -64,6 +65,15 @@ func WithBasicAuthEnabled(e bool) Option {
}
}

// WithEtagMatchDisabled disables the If-Match Etag header from being included by the Gofish driver.
//
// As of the current implementation this disables the header for POST/PATCH requests to the System entity endpoints.
func WithEtagMatchDisabled(d bool) Option {
return func(c *Client) {
c.disableEtagMatch = d
}
}

// NewClient returns a redfishwrapper client
func NewClient(host, port, user, pass string, opts ...Option) *Client {
if !strings.HasPrefix(host, "https://") && !strings.HasPrefix(host, "http://") {
Expand Down
26 changes: 26 additions & 0 deletions internal/redfishwrapper/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,29 @@ func TestWithBasicAuthEnabled(t *testing.T) {
})
}
}

func TestWithEtagMatchDisabled(t *testing.T) {
host := "127.0.0.1"
user := "ADMIN"
pass := "ADMIN"

tests := []struct {
name string
disabled bool
}{
{
"disabled",
true,
},
{
"enabled",
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := NewClient(host, "", user, pass, WithEtagMatchDisabled(tt.disabled))
assert.Equal(t, tt.disabled, client.disableEtagMatch)
})
}
}
12 changes: 12 additions & 0 deletions internal/redfishwrapper/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func (c *Client) SystemPowerOn(ctx context.Context) (ok bool, err error) {
if system.PowerState == rf.OnPowerState {
break
}

system.DisableEtagMatch(c.disableEtagMatch)

err = system.Reset(rf.OnResetType)
if err != nil {
return false, err
Expand All @@ -72,6 +75,9 @@ func (c *Client) SystemPowerOff(ctx context.Context) (ok bool, err error) {
if system.PowerState == rf.OffPowerState {
break
}

system.DisableEtagMatch(c.disableEtagMatch)

err = system.Reset(rf.GracefulShutdownResetType)
if err != nil {
return false, err
Expand All @@ -94,6 +100,7 @@ func (c *Client) SystemReset(ctx context.Context) (ok bool, err error) {
}

for _, system := range ss {
system.DisableEtagMatch(c.disableEtagMatch)
err = system.Reset(rf.PowerCycleResetType)
if err != nil {

Expand Down Expand Up @@ -137,6 +144,8 @@ func (c *Client) SystemPowerCycle(ctx context.Context) (ok bool, err error) {
}

for _, system := range ss {
system.DisableEtagMatch(c.disableEtagMatch)

err = system.Reset(rf.ForceRestartResetType)
if err != nil {
return false, errors.WithMessage(err, "power cycle failed")
Expand Down Expand Up @@ -181,6 +190,9 @@ func (c *Client) SystemForceOff(ctx context.Context) (ok bool, err error) {
if system.PowerState == rf.OffPowerState {
break
}

system.DisableEtagMatch(c.disableEtagMatch)

err = system.Reset(rf.ForceOffResetType)
if err != nil {
return false, err
Expand Down
14 changes: 14 additions & 0 deletions providers/redfish/redfish.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type Config struct {
VersionsNotCompatible []string
RootCAs *x509.CertPool
UseBasicAuth bool
// WithEtagMatchDisabled disables the If-Match Etag header from being included by the Gofish driver.
disableEtagMatch bool
}

// Option for setting optional Client values
Expand Down Expand Up @@ -92,6 +94,15 @@ func WithUseBasicAuth(useBasicAuth bool) Option {
}
}

// WithEtagMatchDisabled disables the If-Match Etag header from being included by the Gofish driver.
//
// As of the current implementation this disables the header for POST/PATCH requests to the System entity endpoints.
func WithEtagMatchDisabled(d bool) Option {
return func(c *Config) {
c.disableEtagMatch = d
}
}

// New returns connection with a redfish client initialized
func New(host, user, pass string, log logr.Logger, opts ...Option) *Conn {
defaultConfig := &Config{
Expand All @@ -106,10 +117,13 @@ func New(host, user, pass string, log logr.Logger, opts ...Option) *Conn {
rfOpts := []redfishwrapper.Option{
redfishwrapper.WithHTTPClient(defaultConfig.HttpClient),
redfishwrapper.WithVersionsNotCompatible(defaultConfig.VersionsNotCompatible),
redfishwrapper.WithEtagMatchDisabled(defaultConfig.disableEtagMatch),
}

if defaultConfig.RootCAs != nil {
rfOpts = append(rfOpts, redfishwrapper.WithSecureTLS(defaultConfig.RootCAs))
}

return &Conn{
Log: log,
failInventoryOnError: false,
Expand Down

0 comments on commit 1f97b35

Please sign in to comment.