2019-09-19 16:49:55 +03:00
|
|
|
package redfish
|
|
|
|
|
|
|
|
import (
|
2020-03-09 22:00:10 +00:00
|
|
|
"encoding/json"
|
2019-09-19 16:49:55 +03:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2020-03-09 22:00:10 +00:00
|
|
|
redfishClient "opendev.org/airship/go-redfish/client"
|
|
|
|
|
2019-12-01 14:14:09 +04:00
|
|
|
aerror "opendev.org/airship/airshipctl/pkg/errors"
|
2019-09-19 16:49:55 +03:00
|
|
|
)
|
|
|
|
|
2020-03-17 15:35:02 +00:00
|
|
|
// ErrRedfishClient describes an error encountered by the go-redfish client.
|
|
|
|
type ErrRedfishClient struct {
|
2019-09-19 16:49:55 +03:00
|
|
|
aerror.AirshipError
|
2020-03-17 15:35:02 +00:00
|
|
|
Message string
|
2019-09-19 16:49:55 +03:00
|
|
|
}
|
|
|
|
|
2020-03-17 15:35:02 +00:00
|
|
|
func (e ErrRedfishClient) Error() string {
|
|
|
|
return fmt.Sprintf("redfish client encountered an error: %s", e.Message)
|
2019-09-19 16:49:55 +03:00
|
|
|
}
|
|
|
|
|
2020-03-17 15:35:02 +00:00
|
|
|
// ErrRedfishMissingConfig describes an error encountered due to a missing configuration option.
|
2020-02-11 11:54:18 -05:00
|
|
|
type ErrRedfishMissingConfig struct {
|
|
|
|
What string
|
2019-09-19 16:49:55 +03:00
|
|
|
}
|
|
|
|
|
2020-02-11 11:54:18 -05:00
|
|
|
func (e ErrRedfishMissingConfig) Error() string {
|
|
|
|
return "missing configuration: " + e.What
|
2019-09-19 16:49:55 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 22:00:10 +00:00
|
|
|
// ScreenRedfishError provides detailed error checking on a Redfish client response.
|
|
|
|
func ScreenRedfishError(httpResp *http.Response, clientErr error) error {
|
|
|
|
// NOTE(drewwalters96): clientErr may not be nil even though the request was successful. The HTTP status code
|
|
|
|
// has to be verified for success on each request. The Redfish client uses HTTP codes 200 and 204 to indicate
|
|
|
|
// success.
|
|
|
|
if httpResp != nil && httpResp.StatusCode != 200 && httpResp.StatusCode != 204 {
|
|
|
|
if clientErr == nil {
|
|
|
|
return ErrRedfishClient{Message: http.StatusText(httpResp.StatusCode)}
|
|
|
|
}
|
|
|
|
|
|
|
|
oAPIErr, ok := clientErr.(redfishClient.GenericOpenAPIError)
|
|
|
|
if !ok {
|
|
|
|
return ErrRedfishClient{Message: "Unable to decode client error."}
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp redfishClient.RedfishError
|
|
|
|
if err := json.Unmarshal(oAPIErr.Body(), &resp); err != nil {
|
|
|
|
// No JSON response included; use generic error text.
|
2020-03-17 15:35:02 +00:00
|
|
|
return ErrRedfishClient{Message: err.Error()}
|
2019-09-19 16:49:55 +03:00
|
|
|
}
|
2020-03-09 22:00:10 +00:00
|
|
|
} else if httpResp == nil {
|
|
|
|
return ErrRedfishClient{Message: "HTTP request failed. Please try again."}
|
2019-09-19 16:49:55 +03:00
|
|
|
}
|
2020-03-09 22:00:10 +00:00
|
|
|
|
2019-09-19 16:49:55 +03:00
|
|
|
return nil
|
|
|
|
}
|