diff --git a/cmd/generate.go b/cmd/generate.go index d9b7fa0..ef9a666 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -32,6 +32,22 @@ var generateCmd = &cobra.Command{ } } + // make sure that we have a token present before trying to make request + if config.AccessToken == "" { + // TODO: make request to check if request will need token + + // check if OCHAMI_ACCESS_TOKEN env var is set if no access token is provided and use that instead + accessToken := os.Getenv("ACCESS_TOKEN") + if accessToken != "" { + config.AccessToken = accessToken + } else { + // TODO: try and fetch token first if it is needed + if verbose { + fmt.Printf("No token found. Attempting to generate config without one...\n") + } + } + } + // use config plugins if none supplied via CLI if len(pluginPaths) <= 0 { pluginPaths = append(pluginPaths, config.PluginDirs...) diff --git a/cmd/serve.go b/cmd/serve.go index 998ff36..e150b34 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -19,6 +19,22 @@ var serveCmd = &cobra.Command{ Use: "serve", Short: "Start configurator as a server and listen for requests", Run: func(cmd *cobra.Command, args []string) { + // make sure that we have a token present before trying to make request + if config.AccessToken == "" { + // TODO: make request to check if request will need token + + // check if OCHAMI_ACCESS_TOKEN env var is set if no access token is provided and use that instead + accessToken := os.Getenv("ACCESS_TOKEN") + if accessToken != "" { + config.AccessToken = accessToken + } else { + // TODO: try and fetch token first if it is needed + if verbose { + fmt.Printf("No token found. Attempting to generate config without one...\n") + } + } + } + // use config plugins if none supplied via CLI if len(pluginPaths) <= 0 { pluginPaths = append(pluginPaths, config.PluginDirs...) diff --git a/internal/client.go b/internal/client.go index 4b750c6..42a7e6e 100644 --- a/internal/client.go +++ b/internal/client.go @@ -76,7 +76,21 @@ func (client *SmdClient) FetchComponents(opts ...util.Option) ([]Component, erro return nil, fmt.Errorf("failed to make HTTP request: %v", err) } + // make sure our response is actually JSON + if !json.Valid(b) { + return nil, fmt.Errorf("expected valid JSON response: %v", string(b)) + } + // unmarshal response body JSON and extract in object + var tmp map[string]any + err = json.Unmarshal(b, &tmp) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal response: %v", err) + } + b, err = json.Marshal(tmp["RedfishEndpoints"].([]any)) + if err != nil { + return nil, fmt.Errorf("failed to marshal JSON: %v", err) + } err = json.Unmarshal(b, &comps) if err != nil { return nil, fmt.Errorf("failed to unmarshal response: %v", err) @@ -96,15 +110,27 @@ func (client *SmdClient) FetchRedfishEndpoints(opts ...util.Option) ([]RedfishEn var ( params = util.GetParams(opts...) verbose = util.Get[bool](params, "verbose") - rfs = []RedfishEndpoint{} + eps = []RedfishEndpoint{} ) b, err := client.makeRequest("/Inventory/RedfishEndpoints") if err != nil { return nil, fmt.Errorf("failed to make HTTP resquest: %v", err) } + if !json.Valid(b) { + return nil, fmt.Errorf("expected valid JSON response: %v", string(b)) + } + var tmp map[string]any + err = json.Unmarshal(b, &tmp) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal response: %v", err) + } - err = json.Unmarshal(b, &rfs) + b, err = json.Marshal(tmp["RedfishEndpoints"].([]any)) + if err != nil { + return nil, fmt.Errorf("failed to marshal JSON: %v", err) + } + err = json.Unmarshal(b, &eps) if err != nil { return nil, fmt.Errorf("failed to unmarshal response: %v", err) } @@ -115,7 +141,7 @@ func (client *SmdClient) FetchRedfishEndpoints(opts ...util.Option) ([]RedfishEn } } - return rfs, nil + return eps, nil } func (client *SmdClient) makeRequest(endpoint string) ([]byte, error) {