mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
Added client for SMD for using certs
This commit is contained in:
parent
f1db827c61
commit
8c19608966
1 changed files with 72 additions and 13 deletions
|
|
@ -1,13 +1,18 @@
|
||||||
package smd
|
package smd
|
||||||
|
|
||||||
// See ref for API docs:
|
// See ref for API docs:
|
||||||
// https://github.com/Cray-HPE/hms-smd/blob/master/docs/examples.adoc
|
// https://github.com/OpenCHAMI/hms-smd/blob/master/docs/examples.adoc
|
||||||
// https://github.com/alexlovelltroy/hms-smd
|
// https://github.com/OpenCHAMI/hms-smd
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/OpenCHAMI/magellan/internal/util"
|
"github.com/OpenCHAMI/magellan/internal/util"
|
||||||
// hms "github.com/alexlovelltroy/hms-smd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -16,13 +21,63 @@ var (
|
||||||
Port = 27779
|
Port = 27779
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeEndpointUrl(endpoint string) string {
|
type Option func(*Client)
|
||||||
return Host + ":" + fmt.Sprint(Port) + BaseEndpoint + endpoint
|
|
||||||
|
type Client struct {
|
||||||
|
*http.Client
|
||||||
|
CACertPool *x509.CertPool
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRedfishEndpoints() error {
|
func NewClient(opts ...Option) *Client {
|
||||||
|
client := &Client{
|
||||||
|
Client: http.DefaultClient,
|
||||||
|
}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(client)
|
||||||
|
}
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithHttpClient(httpClient *http.Client) Option {
|
||||||
|
return func(c *Client) {
|
||||||
|
c.Client = httpClient
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This MakeRequest function is a wrapper around the util.MakeRequest function
|
||||||
|
// with a couple of niceties with using a smd.Client
|
||||||
|
func (c *Client) MakeRequest(url string, method string, body []byte, headers map[string]string) (*http.Response, []byte, error) {
|
||||||
|
return util.MakeRequest(c.Client, url, method, body, headers)
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithCertPool(certPool *x509.CertPool) Option {
|
||||||
|
return func(c *Client) {
|
||||||
|
c.Client.Transport = &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{
|
||||||
|
RootCAs: certPool,
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
},
|
||||||
|
DisableKeepAlives: true,
|
||||||
|
Dial: (&net.Dialer{
|
||||||
|
Timeout: 120 * time.Second,
|
||||||
|
KeepAlive: 120 * time.Second,
|
||||||
|
}).Dial,
|
||||||
|
TLSHandshakeTimeout: 120 * time.Second,
|
||||||
|
ResponseHeaderTimeout: 120 * time.Second,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithSecureTLS(certPath string) Option {
|
||||||
|
cacert, _ := os.ReadFile(certPath)
|
||||||
|
certPool := x509.NewCertPool()
|
||||||
|
certPool.AppendCertsFromPEM(cacert)
|
||||||
|
return WithCertPool(certPool)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetRedfishEndpoints(headers map[string]string, opts ...Option) error {
|
||||||
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
|
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
|
||||||
_, body, err := util.MakeRequest(url, "GET", nil, nil)
|
_, body, err := c.MakeRequest(url, "GET", nil, headers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not get endpoint: %v", err)
|
return fmt.Errorf("could not get endpoint: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -31,9 +86,9 @@ func GetRedfishEndpoints() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetComponentEndpoint(xname string) error {
|
func (c *Client) GetComponentEndpoint(xname string) error {
|
||||||
url := makeEndpointUrl("/Inventory/ComponentsEndpoints/" + xname)
|
url := makeEndpointUrl("/Inventory/ComponentsEndpoints/" + xname)
|
||||||
res, body, err := util.MakeRequest(url, "GET", nil, nil)
|
res, body, err := c.MakeRequest(url, "GET", nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not get endpoint: %v", err)
|
return fmt.Errorf("could not get endpoint: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -42,14 +97,14 @@ func GetComponentEndpoint(xname string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddRedfishEndpoint(data []byte, headers map[string]string) error {
|
func (c *Client) AddRedfishEndpoint(data []byte, headers map[string]string) error {
|
||||||
if data == nil {
|
if data == nil {
|
||||||
return fmt.Errorf("could not add redfish endpoint: no data found")
|
return fmt.Errorf("could not add redfish endpoint: no data found")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add redfish endpoint via POST `/hsm/v2/Inventory/RedfishEndpoints` endpoint
|
// Add redfish endpoint via POST `/hsm/v2/Inventory/RedfishEndpoints` endpoint
|
||||||
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
|
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
|
||||||
res, body, err := util.MakeRequest(url, "POST", data, headers)
|
res, body, err := c.MakeRequest(url, "POST", data, headers)
|
||||||
if res != nil {
|
if res != nil {
|
||||||
statusOk := res.StatusCode >= 200 && res.StatusCode < 300
|
statusOk := res.StatusCode >= 200 && res.StatusCode < 300
|
||||||
if !statusOk {
|
if !statusOk {
|
||||||
|
|
@ -60,13 +115,13 @@ func AddRedfishEndpoint(data []byte, headers map[string]string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateRedfishEndpoint(xname string, data []byte, headers map[string]string) error {
|
func (c *Client) UpdateRedfishEndpoint(xname string, data []byte, headers map[string]string) error {
|
||||||
if data == nil {
|
if data == nil {
|
||||||
return fmt.Errorf("could not add redfish endpoint: no data found")
|
return fmt.Errorf("could not add redfish endpoint: no data found")
|
||||||
}
|
}
|
||||||
// Update redfish endpoint via PUT `/hsm/v2/Inventory/RedfishEndpoints` endpoint
|
// Update redfish endpoint via PUT `/hsm/v2/Inventory/RedfishEndpoints` endpoint
|
||||||
url := makeEndpointUrl("/Inventory/RedfishEndpoints/" + xname)
|
url := makeEndpointUrl("/Inventory/RedfishEndpoints/" + xname)
|
||||||
res, body, err := util.MakeRequest(url, "PUT", data, headers)
|
res, body, err := c.MakeRequest(url, "PUT", data, headers)
|
||||||
fmt.Printf("%v (%v)\n%s\n", url, res.Status, string(body))
|
fmt.Printf("%v (%v)\n%s\n", url, res.Status, string(body))
|
||||||
if res != nil {
|
if res != nil {
|
||||||
statusOk := res.StatusCode >= 200 && res.StatusCode < 300
|
statusOk := res.StatusCode >= 200 && res.StatusCode < 300
|
||||||
|
|
@ -76,3 +131,7 @@ func UpdateRedfishEndpoint(xname string, data []byte, headers map[string]string)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func makeEndpointUrl(endpoint string) string {
|
||||||
|
return Host + ":" + fmt.Sprint(Port) + BaseEndpoint + endpoint
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue