mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-19 19:17:02 -07:00
* feat: initial implementation of command split * feat: update collect and new send cmd * chore: cleanup unused code * chore: refactored getting username * chore: more refactoring and cleanup * feat: update send cmd implementation * chore: changed/updated example config * chore: made cmd more consistent and added formatting * refactor: removed --host flag from scan * chore: cleaned up and fixed issue with client * chore: cleaned up CLI flags in collect cmd * feat: updated crawl to include managers and output YAML optionally * refactor: updated and improved send implementation * refactor: minor improvements * refactor: added util func to check for empty slices * fix: issue with reading from stdin * refactor: added scheme trimming function for URIs * refactor: changed host arg back to positional * refactor: removed unused vars and added --output-dir flag * fix: make -f for secrets persistent * refactor: removed --host flag and request in collect * refactor: changed --output flag to --output-file * fix: updated flags for collect * fix: typo in crawler error * fix: dir being created when outputDir not set * fix: reading stdin and data args * fix: made output using -v and -o consistent * readme: added info about command split * updated changelog adding missing version entries * chore: updated example to use host as positional arg * fix: issue with reading --data arg * fix: remove unused import from collect pkg Signed-off-by: Devon Bautista <devonb@lanl.gov> --------- Signed-off-by: David Allen <16520934+davidallendj@users.noreply.github.com> Signed-off-by: Devon Bautista <devonb@lanl.gov> Co-authored-by: Devon Bautista <devonb@lanl.gov>
100 lines
2.6 KiB
Go
100 lines
2.6 KiB
Go
package client
|
|
|
|
// See ref for API docs:
|
|
// https://github.com/OpenCHAMI/hms-smd/blob/master/docs/examples.adoc
|
|
// https://github.com/OpenCHAMI/hms-smd
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
type SmdClient struct {
|
|
*http.Client
|
|
URI string
|
|
Xname string
|
|
}
|
|
|
|
func NewSmdClient() *SmdClient {
|
|
return &SmdClient{
|
|
Client: &http.Client{},
|
|
}
|
|
}
|
|
|
|
func (c *SmdClient) Init() {
|
|
c.Client = &http.Client{}
|
|
}
|
|
|
|
func (c *SmdClient) Name() string {
|
|
return "smd"
|
|
}
|
|
|
|
func (c *SmdClient) RootEndpoint(endpoint string) string {
|
|
return fmt.Sprintf("%s/hsm/v2%s", c.URI, endpoint)
|
|
}
|
|
|
|
func (c *SmdClient) GetInternalClient() *http.Client {
|
|
return c.Client
|
|
}
|
|
|
|
// Add() has a similar function definition to that of the default implementation,
|
|
// but also allows further customization and data/header manipulation that would
|
|
// be specific and/or unique to SMD's API.
|
|
func (c *SmdClient) Add(data HTTPBody, headers HTTPHeader) error {
|
|
if data == nil {
|
|
return fmt.Errorf("failed to add redfish endpoint: no data found")
|
|
}
|
|
|
|
// Add redfish endpoint via POST `/hsm/v2/Inventory/RedfishEndpoints` endpoint
|
|
url := c.RootEndpoint("/Inventory/RedfishEndpoints")
|
|
res, body, err := MakeRequest(c.Client, url, http.MethodPost, data, headers)
|
|
if res != nil {
|
|
statusOk := res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusMultipleChoices
|
|
if !statusOk {
|
|
if len(body) > 0 {
|
|
return fmt.Errorf("%d: %s", res.StatusCode, string(body))
|
|
} else {
|
|
return fmt.Errorf("returned status code %d when adding endpoint", res.StatusCode)
|
|
}
|
|
}
|
|
log.Debug().Msgf("%v (%v)\n%s\n", url, res.Status, string(body))
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (c *SmdClient) Update(data HTTPBody, headers HTTPHeader) error {
|
|
if data == nil {
|
|
return fmt.Errorf("failed to add redfish endpoint: no data found")
|
|
}
|
|
// Update redfish endpoint via PUT `/hsm/v2/Inventory/RedfishEndpoints` endpoint
|
|
url := c.RootEndpoint("/Inventory/RedfishEndpoints/" + c.Xname)
|
|
res, body, err := MakeRequest(c.Client, url, http.MethodPut, data, headers)
|
|
if res != nil {
|
|
statusOk := res.StatusCode >= 200 && res.StatusCode < 300
|
|
if !statusOk {
|
|
if len(body) > 0 {
|
|
return fmt.Errorf("%d: %s", res.StatusCode, string(body))
|
|
} else {
|
|
return fmt.Errorf("failed to update redfish endpoint (returned %s)", res.Status)
|
|
}
|
|
}
|
|
log.Debug().Msgf("%v (%v)\n%s\n", url, res.Status, string(body))
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (c *SmdClient) SetXnameFromJSON(contents []byte, key string) error {
|
|
var (
|
|
data map[string]any
|
|
err error
|
|
)
|
|
|
|
err = json.Unmarshal(contents, &data)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to unmarshal xname: %v", err)
|
|
}
|
|
c.Xname = data[key].(string)
|
|
return nil
|
|
}
|