bmclib/examples/v1/inventory/main.go
Micah Hausler cc2ec72571 Updated examples with docs
* Renamed user creation example
* Fixed create-user to use the CSV-provided values
* ran `go mod tidy`

Signed-off-by: Micah Hausler <mhausler@amazon.com>
2022-05-10 14:41:30 +00:00

73 lines
1.7 KiB
Go

package main
import (
"context"
"crypto/x509"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"strconv"
"time"
"github.com/bmc-toolbox/bmclib"
"github.com/bombsimon/logrusr/v2"
"github.com/sirupsen/logrus"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
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()
l := logrus.New()
l.Level = logrus.DebugLevel
logger := logrusr.New(l)
clientOpts := []bmclib.Option{bmclib.WithLogger(logger)}
if *withSecureTLS {
var pool *x509.CertPool
if *certPoolFile != "" {
pool = x509.NewCertPool()
data, err := ioutil.ReadFile(*certPoolFile)
if err != nil {
l.Fatal(err)
}
pool.AppendCertsFromPEM(data)
}
// a nil pool uses the system certs
clientOpts = append(clientOpts, bmclib.WithSecureTLS(pool))
}
cl := bmclib.NewClient(*host, strconv.Itoa(*port), *user, *pass, clientOpts...)
cl.Registry.Drivers = cl.Registry.Using("redfish")
err := cl.Open(ctx)
if err != nil {
log.Fatal(err, "bmc login failed")
}
defer cl.Close(ctx)
inv, err := cl.Inventory(ctx)
if err != nil {
l.Error(err)
}
b, err := json.MarshalIndent(inv, "", " ")
if err != nil {
l.Error(err)
}
fmt.Println(string(b))
}