Skip to content

Commit

Permalink
Add functional arguments and options:
Browse files Browse the repository at this point in the history
This uniforms providers to using func args.
Updates the client to work with this.

Signed-off-by: Jacob Weinstock <jakobweinstock@gmail.com>
  • Loading branch information
jacobweinstock committed May 5, 2023
1 parent f028714 commit 0fa5f77
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 46 deletions.
25 changes: 14 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,14 @@ type providerConfig struct {
}

// NewClient returns a new Client struct
func NewClient(host, port, user, pass string, opts ...Option) *Client {
hc, _ := httpclient.Build()
var defaultClient = &Client{
func NewClient(host, user, pass string, opts ...Option) *Client {
defaultClient := &Client{
Logger: logr.Discard(),
Registry: registrar.NewRegistry(),
httpClient: hc,
httpClient: httpclient.Build(),
providerConfig: providerConfig{
ipmitool: ipmitool.Config{
CipherSuite: 3,
Port: "623",
Port: "623",
},
asrock: asrockrack.Config{},
gofish: redfish.Config{
Expand All @@ -89,7 +87,7 @@ func NewClient(host, port, user, pass string, opts ...Option) *Client {
defaultClient.Auth.Host = host
defaultClient.Auth.User = user
defaultClient.Auth.Pass = pass
// len of 0 means that no Registry, with any registered providers was passed in.
// len of 0 means that no Registry, with any registered providers, was passed in.
if len(defaultClient.Registry.Drivers) == 0 {
defaultClient.registerProviders()
}
Expand Down Expand Up @@ -120,13 +118,17 @@ func (c *Client) registerProviders() {
ipmitool.WithLogger(c.Logger),
ipmitool.WithPort(c.providerConfig.ipmitool.Port),
ipmitool.WithCipherSuite(c.providerConfig.ipmitool.CipherSuite),
ipmitool.WithIpmitoolPath(c.providerConfig.ipmitool.IpmitoolPath),
}
if driverIpmitool, err := ipmitool.New(c.Auth.Host, c.Auth.User, c.Auth.Pass, ipmiOpts...); err == nil {
c.Registry.Register(ipmitool.ProviderName, ipmitool.ProviderProtocol, ipmitool.Features, nil, driverIpmitool)
} else {
c.Logger.Info("ipmitool provider not available", "error", err.Error())
}
driverIpmitool, _ := ipmitool.New(c.Auth.Host, c.Auth.User, c.Auth.Pass, ipmiOpts...)
c.Registry.Register(ipmitool.ProviderName, ipmitool.ProviderProtocol, ipmitool.Features, nil, driverIpmitool)

// register ASRR vendorapi provider
asrHttpClient := *c.httpClient
driverAsrockrack, _ := asrockrack.NewWithOptions(c.Auth.Host, c.Auth.User, c.Auth.Pass, c.Logger, asrockrack.WithHTTPClient(&asrHttpClient))
driverAsrockrack := asrockrack.NewWithOptions(c.Auth.Host, c.Auth.User, c.Auth.Pass, c.Logger, asrockrack.WithHTTPClient(&asrHttpClient))
c.Registry.Register(asrockrack.ProviderName, asrockrack.ProviderProtocol, asrockrack.Features, nil, driverAsrockrack)

// register gofish provider
Expand Down Expand Up @@ -176,6 +178,7 @@ func (c *Client) setMetadata(metadata bmc.Metadata) {
// being empty then we error.
func (c *Client) Open(ctx context.Context) error {
ifs, metadata, err := bmc.OpenConnectionFromInterfaces(ctx, c.perProviderTimeout(ctx), c.Registry.GetDriverInterfaces())
defer c.setMetadata(metadata)
if err != nil {
return err
}
Expand All @@ -189,7 +192,7 @@ func (c *Client) Open(ctx context.Context) error {
}
}
c.Registry.Drivers = reg
c.setMetadata(metadata)

return nil
}

Expand Down
12 changes: 5 additions & 7 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ import (
func TestBMC(t *testing.T) {
t.Skip("needs ipmitool and real ipmi server")
host := "127.0.0.1"
port := "623"
user := "admin"
pass := "admin"

log := logging.DefaultLogger()
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
cl := NewClient(host, port, user, pass, WithLogger(log), WithPerProviderTimeout(5*time.Second))
cl := NewClient(host, user, pass, WithLogger(log), WithPerProviderTimeout(5*time.Second))
cl.FilterForCompatible(ctx)
var err error
err = cl.Open(ctx)
if err != nil {
t.Logf("%+v", cl.GetMetadata())
t.Fatal(err)
}
defer cl.Close(ctx)
Expand Down Expand Up @@ -58,7 +58,6 @@ func TestBMC(t *testing.T) {

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

Expand All @@ -77,15 +76,14 @@ func TestWithRedfishVersionsNotCompatible(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cl := NewClient(host, port, user, pass, WithGofishVersionsNotCompatible(tt.versions))
cl := NewClient(host, user, pass, WithGofishVersionsNotCompatible(tt.versions))
assert.Equal(t, tt.versions, cl.providerConfig.gofish.VersionsNotCompatible)
})
}
}

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

Expand All @@ -104,7 +102,7 @@ func TestWithConnectionTimeout(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cl := NewClient(host, port, user, pass, WithPerProviderTimeout(tt.timeout))
cl := NewClient(host, user, pass, WithPerProviderTimeout(tt.timeout))
assert.Equal(t, tt.timeout, cl.perProviderTimeout(nil))
})
}
Expand All @@ -130,7 +128,7 @@ func TestDefaultTimeout(t *testing.T) {
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
c := NewClient("", "", "", "")
c := NewClient("", "", "")
got := c.defaultTimeout(tt.ctx)
if diff := cmp.Diff(got.Round(time.Millisecond), tt.want); diff != "" {
t.Errorf("unexpected timeout (-want +got):\n%s", diff)
Expand Down
2 changes: 1 addition & 1 deletion examples/bios/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {
bmcPass := os.Getenv("BMC_PASSWORD")
bmcUser := os.Getenv("BMC_USERNAME")
// init client
client := bmclib.NewClient(host, "", bmcUser, bmcPass, clientOpts...)
client := bmclib.NewClient(host, bmcUser, bmcPass, clientOpts...)

ctx := context.TODO()
// open BMC session
Expand Down
4 changes: 1 addition & 3 deletions examples/create-users/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"io/ioutil"
"os"
"strconv"
"time"

bmclib "github.com/bmc-toolbox/bmclib/v2"
Expand All @@ -20,7 +19,6 @@ func main() {
user := flag.String("user", "", "Username to login with")
pass := flag.String("password", "", "Username to login with")
host := flag.String("host", "", "BMC hostname to connect to")
port := flag.Int("port", 443, "BMC port to connect to")
withSecureTLS := flag.Bool("secure-tls", false, "Enable secure TLS")
certPoolFile := flag.String("cert-pool", "", "Path to an file containing x509 CAs. An empty string uses the system CAs. Only takes effect when --secure-tls=true")
userCSV := flag.String("user-csv", "", "A CSV file of users to create containing 3 columns: username, password, role")
Expand Down Expand Up @@ -51,7 +49,7 @@ func main() {
clientOpts = append(clientOpts, bmclib.WithSecureTLS(pool))
}

cl := bmclib.NewClient(*host, strconv.Itoa(*port), *user, *pass, clientOpts...)
cl := bmclib.NewClient(*host, *user, *pass, clientOpts...)
cl.Registry.Drivers = cl.Registry.Using("redfish")

ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
Expand Down
4 changes: 1 addition & 3 deletions examples/install-firmware/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"log"
"os"
"strconv"
"time"

bmclib "github.com/bmc-toolbox/bmclib/v2"
Expand All @@ -21,7 +20,6 @@ func main() {
user := flag.String("user", "", "Username to login with")
pass := flag.String("password", "", "Username to login with")
host := flag.String("host", "", "BMC hostname to connect to")
port := flag.Int("port", 443, "BMC port to connect to")
withSecureTLS := flag.Bool("secure-tls", false, "Enable secure TLS")
certPoolPath := flag.String("cert-pool", "", "Path to an file containing x509 CAs. An empty string uses the system CAs. Only takes effect when --secure-tls=true")
firmwarePath := flag.String("firmware", "", "The local path of the firmware to install")
Expand Down Expand Up @@ -55,7 +53,7 @@ func main() {
clientOpts = append(clientOpts, bmclib.WithSecureTLS(pool))
}

cl := bmclib.NewClient(*host, strconv.Itoa(*port), *user, *pass, clientOpts...)
cl := bmclib.NewClient(*host, *user, *pass, clientOpts...)
err := cl.Open(ctx)
if err != nil {
l.Fatal(err, "bmc login failed")
Expand Down
4 changes: 1 addition & 3 deletions examples/inventory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"

Expand All @@ -24,7 +23,6 @@ func main() {
user := flag.String("user", "", "Username to login with")
pass := flag.String("password", "", "Username to login with")
host := flag.String("host", "", "BMC hostname to connect to")
port := flag.Int("port", 443, "BMC port to connect to")
incompatibleRedfishVersions := flag.String("incompatible-redfish-versions", "", "Comma separated list of redfish versions to deem incompatible")
withSecureTLS := flag.Bool("secure-tls", false, "Enable secure TLS")
certPoolFile := flag.String("cert-pool", "", "Path to an file containing x509 CAs. An empty string uses the system CAs. Only takes effect when --secure-tls=true")
Expand Down Expand Up @@ -57,7 +55,7 @@ func main() {
bmclib.WithGofishVersionsNotCompatible(strings.Split(*incompatibleRedfishVersions, ",")))
}

cl := bmclib.NewClient(*host, strconv.Itoa(*port), *user, *pass, clientOpts...)
cl := bmclib.NewClient(*host, *user, *pass, clientOpts...)

cl.Registry.Drivers = cl.Registry.FilterForCompatible(ctx)

Expand Down
4 changes: 1 addition & 3 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"log"
"os"
"strconv"
"time"

bmclib "github.com/bmc-toolbox/bmclib/v2"
Expand All @@ -21,7 +20,6 @@ func main() {
user := flag.String("user", "", "Username to login with")
pass := flag.String("password", "", "Username to login with")
host := flag.String("host", "", "BMC hostname to connect to")
port := flag.Int("port", 443, "BMC port to connect to")
withSecureTLS := flag.Bool("secure-tls", false, "Enable secure TLS")
certPoolPath := flag.String("cert-pool", "", "Path to an file containing x509 CAs. An empty string uses the system CAs. Only takes effect when --secure-tls=true")
firmwarePath := flag.String("firmware", "", "The local path of the firmware to install")
Expand Down Expand Up @@ -55,7 +53,7 @@ func main() {
clientOpts = append(clientOpts, bmclib.WithSecureTLS(pool))
}

cl := bmclib.NewClient(*host, strconv.Itoa(*port), *user, *pass, clientOpts...)
cl := bmclib.NewClient(*host, *user, *pass, clientOpts...)
err := cl.Open(ctx)
if err != nil {
l.Fatal(err, "bmc login failed")
Expand Down
3 changes: 1 addition & 2 deletions examples/reset_bmc/reset_bmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func main() {

// set BMC parameters here
host := "10.211.132.157"
port := ""
user := "root"
pass := "yxvZdxAQ38ZWlZ"

Expand All @@ -32,7 +31,7 @@ func main() {
os.Setenv("DEBUG_BMCLIB", "true")
defer os.Unsetenv("DEBUG_BMCLIB")

cl := bmclib.NewClient(host, port, user, pass, bmclib.WithLogger(logger))
cl := bmclib.NewClient(host, user, pass, bmclib.WithLogger(logger))

err := cl.Open(ctx)
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions examples/status/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/x509"
"flag"
"io/ioutil"
"strconv"
"time"

"github.com/bmc-toolbox/bmclib/v2"
Expand All @@ -17,7 +16,6 @@ func main() {
user := flag.String("user", "", "Username to login with")
pass := flag.String("password", "", "Username to login with")
host := flag.String("host", "", "BMC hostname to connect to")
port := flag.Int("port", 443, "BMC port to connect to")
withSecureTLS := flag.Bool("secure-tls", false, "Enable secure TLS")
certPoolFile := flag.String("cert-pool", "", "Path to an file containing x509 CAs. An empty string uses the system CAs. Only takes effect when --secure-tls=true")
flag.Parse()
Expand Down Expand Up @@ -46,7 +44,7 @@ func main() {
clientOpts = append(clientOpts, bmclib.WithSecureTLS(pool))
}

cl := bmclib.NewClient(*host, strconv.Itoa(*port), *user, *pass, clientOpts...)
cl := bmclib.NewClient(*host, *user, *pass, clientOpts...)
cl.Registry.Drivers = cl.Registry.Using("redfish")

ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
Expand Down
8 changes: 7 additions & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func WithPerProviderTimeout(timeout time.Duration) Option {
}
}

func WithIpmitoolCipherSuite(cipherSuite int) Option {
func WithIpmitoolCipherSuite(cipherSuite string) Option {
return func(args *Client) {
args.providerConfig.ipmitool.CipherSuite = cipherSuite
}
Expand All @@ -64,6 +64,12 @@ func WithIpmitoolPort(port string) Option {
}
}

func WithIpmitoolPath(path string) Option {
return func(args *Client) {
args.providerConfig.ipmitool.IpmitoolPath = path
}
}

func WithAsrockrackHTTPClient(httpClient *http.Client) Option {
return func(args *Client) {
args.providerConfig.asrock.HttpClient = httpClient
Expand Down
7 changes: 6 additions & 1 deletion providers/intelamt/intelamt.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,13 @@ func New(host string, user string, pass string, opts ...Option) *Conn {
opt(defaultClient)
}

iopts := []iamt.Option{
iamt.WithLogger(defaultClient.Logger),
iamt.WithPort(defaultClient.Port),
iamt.WithScheme(defaultClient.HostScheme),
}
return &Conn{
client: iamt.NewClient(host, user, pass, iamt.WithLogger(defaultClient.Logger), iamt.WithPort(defaultClient.Port), iamt.WithScheme(defaultClient.HostScheme)),
client: iamt.NewClient(host, user, pass, iopts...),
}
}

Expand Down
27 changes: 19 additions & 8 deletions providers/ipmitool/ipmitool.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ type Conn struct {
}

type Config struct {
CipherSuite int
Port string
Log logr.Logger
CipherSuite string
IpmitoolPath string
Log logr.Logger
Port string
}

// Option for setting optional Client values
Expand All @@ -57,24 +58,34 @@ func WithPort(port string) Option {
}
}

func WithCipherSuite(cipherSuite int) Option {
func WithCipherSuite(cipherSuite string) Option {
return func(c *Config) {
c.CipherSuite = cipherSuite
}
}

func WithIpmitoolPath(ipmitoolPath string) Option {
return func(c *Config) {
c.IpmitoolPath = ipmitoolPath
}
}

func New(host, user, pass string, opts ...Option) (*Conn, error) {
defaultConfig := &Config{
CipherSuite: 3,
Port: "623",
Log: logr.Discard(),
Port: "623",
Log: logr.Discard(),
}

for _, opt := range opts {
opt(defaultConfig)
}

ipt, err := ipmi.New(user, pass, host+":"+defaultConfig.Port)
iopts := []ipmi.Option{
ipmi.WithIpmitoolPath(defaultConfig.IpmitoolPath),
ipmi.WithCipherSuite(defaultConfig.CipherSuite),
ipmi.WithLogger(defaultConfig.Log),
}
ipt, err := ipmi.New(user, pass, host+":"+defaultConfig.Port, iopts...)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 0fa5f77

Please sign in to comment.