-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathredfish.go
137 lines (116 loc) · 3.48 KB
/
redfish.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package redfish
import (
"context"
"strings"
"github.com/bmc-toolbox/bmclib/v2/internal/redfishwrapper"
"github.com/bmc-toolbox/bmclib/v2/providers"
"github.com/go-logr/logr"
"github.com/jacobweinstock/registrar"
"github.com/pkg/errors"
bmclibErrs "github.com/bmc-toolbox/bmclib/v2/errors"
)
const (
// ProviderName for the provider implementation
ProviderName = "gofish"
// ProviderProtocol for the provider implementation
ProviderProtocol = "redfish"
)
var (
// Features implemented by gofish
Features = registrar.Features{
providers.FeaturePowerSet,
providers.FeaturePowerState,
providers.FeatureUserCreate,
providers.FeatureUserUpdate,
providers.FeatureUserDelete,
providers.FeatureInventoryRead,
providers.FeatureFirmwareInstall,
providers.FeatureFirmwareInstallStatus,
providers.FeatureBmcReset,
}
)
// Conn details for redfish client
type Conn struct {
redfishwrapper *redfishwrapper.Client
failInventoryOnError bool
Log logr.Logger
}
// New returns connection with a redfish client initialized
func New(host, port, user, pass string, log logr.Logger, opts ...redfishwrapper.Option) *Conn {
return &Conn{
Log: log,
failInventoryOnError: false,
redfishwrapper: redfishwrapper.NewClient(host, port, user, pass, opts...),
}
}
// Open a connection to a BMC via redfish
func (c *Conn) Open(ctx context.Context) (err error) {
return c.redfishwrapper.Open(ctx)
}
// Close a connection to a BMC via redfish
func (c *Conn) Close(ctx context.Context) error {
return c.redfishwrapper.Close(ctx)
}
// Name returns the client provider name.
func (c *Conn) Name() string {
return ProviderName
}
// Compatible tests whether a BMC is compatible with the gofish provider
func (c *Conn) Compatible(ctx context.Context) bool {
err := c.Open(ctx)
if err != nil {
c.Log.V(2).WithValues(
"provider",
c.Name(),
).Info("warn", bmclibErrs.ErrCompatibilityCheck.Error(), err.Error())
return false
}
defer c.Close(ctx)
_, err = c.PowerStateGet(ctx)
if err != nil {
c.Log.V(2).WithValues(
"provider",
c.Name(),
).Info("warn", bmclibErrs.ErrCompatibilityCheck.Error(), err.Error())
}
return err == nil
}
// DeviceVendorModel returns the device manufacturer and model attributes
func (c *Conn) DeviceVendorModel(ctx context.Context) (vendor, model string, err error) {
systems, err := c.redfishwrapper.Systems()
if err != nil {
return "", "", err
}
for _, sys := range systems {
if !compatibleOdataID(sys.ODataID, systemsOdataIDs) {
continue
}
return sys.Manufacturer, sys.Model, nil
}
return vendor, model, bmclibErrs.ErrRedfishSystemOdataID
}
// BmcReset power cycles the BMC
func (c *Conn) BmcReset(ctx context.Context, resetType string) (ok bool, err error) {
return c.redfishwrapper.BMCReset(ctx, resetType)
}
// PowerStateGet gets the power state of a BMC machine
func (c *Conn) PowerStateGet(ctx context.Context) (state string, err error) {
return c.redfishwrapper.SystemPowerStatus(ctx)
}
// PowerSet sets the power state of a server
func (c *Conn) PowerSet(ctx context.Context, state string) (ok bool, err error) {
switch strings.ToLower(state) {
case "on":
return c.redfishwrapper.SystemPowerOn(ctx)
case "off":
return c.redfishwrapper.SystemForceOff(ctx)
case "soft":
return c.redfishwrapper.SystemPowerOff(ctx)
case "reset":
return c.redfishwrapper.SystemReset(ctx)
case "cycle":
return c.redfishwrapper.SystemPowerCycle(ctx)
default:
return false, errors.New("unknown power action")
}
}