From bf8b70214dd8eba138d2240f7c145c4d8088b01a Mon Sep 17 00:00:00 2001 From: Scott Howe Date: Fri, 22 Nov 2024 09:49:21 -0700 Subject: [PATCH] fix(cli): consolidate and update backend error handling (#55) * fix(cli): consolidate and repair backend error handling The same error handling code is copy-pasted all over the backend. Consolidate this code in 1 common error handler function so it's easier to update next time. Then, fixed the code that's not handling non-cfm-service errors correctly. Also, need to fix potential memory leaks that comes from NOT using "defer response.Body.Close()" after a call into cfm-service. * fix(cli): Remove unnecessary log message. * docs(cli): add function description * fix(cli): fix wrong variable name --- cli/pkg/serviceLib/serviceRequests/blades.go | 1 - cli/pkg/serviceLib/serviceWrap/appliance.go | 111 +++------- cli/pkg/serviceLib/serviceWrap/blade.go | 137 ++++-------- cli/pkg/serviceLib/serviceWrap/common.go | 31 +++ cli/pkg/serviceLib/serviceWrap/host.go | 111 +++------- .../serviceLib/serviceWrap/memory-devices.go | 94 +++------ cli/pkg/serviceLib/serviceWrap/memory.go | 199 ++++++------------ cli/pkg/serviceLib/serviceWrap/memoryBlade.go | 24 +-- cli/pkg/serviceLib/serviceWrap/ports.go | 142 +++++-------- cli/pkg/serviceLib/serviceWrap/resources.go | 62 ++---- 10 files changed, 313 insertions(+), 599 deletions(-) diff --git a/cli/pkg/serviceLib/serviceRequests/blades.go b/cli/pkg/serviceLib/serviceRequests/blades.go index f78c9ba..5b0911e 100644 --- a/cli/pkg/serviceLib/serviceRequests/blades.go +++ b/cli/pkg/serviceLib/serviceRequests/blades.go @@ -122,7 +122,6 @@ func (r *ServiceRequestListBlades) Execute() (*serviceWrap.ApplianceBladeSummary klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ServiceTcp", fmt.Sprintf("%+v", *r.ServiceTcp)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ApplianceId", fmt.Sprintf("%+v", *r.ApplianceId)) - klog.V(4).InfoS(fmt.Sprintf("%T", *r), "BladeId", fmt.Sprintf("%+v", *r.BladeId)) serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort()) diff --git a/cli/pkg/serviceLib/serviceWrap/appliance.go b/cli/pkg/serviceLib/serviceWrap/appliance.go index 75085ad..77c02ea 100644 --- a/cli/pkg/serviceLib/serviceWrap/appliance.go +++ b/cli/pkg/serviceLib/serviceWrap/appliance.go @@ -4,7 +4,6 @@ package serviceWrap import ( "context" - "encoding/json" "fmt" service "cfm/pkg/client" @@ -16,19 +15,12 @@ func AddAppliance(client *service.APIClient, creds *service.Credentials) (*servi request := client.DefaultAPI.AppliancesPost(context.Background()) request = request.Credentials(*creds) appliance, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: AddAppliance", "applianceId", appliance.GetId()) @@ -39,19 +31,12 @@ func AddAppliance(client *service.APIClient, creds *service.Credentials) (*servi func DeleteApplianceById(client *service.APIClient, applId string) (*service.Appliance, error) { request := client.DefaultAPI.AppliancesDeleteById(context.Background(), applId) appliance, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: DeleteApplianceById", "applianceId", appliance.GetId()) @@ -63,42 +48,28 @@ func GetAllAppliances(client *service.APIClient) (*[]*service.Appliance, error) var appliances []*service.Appliance //Get existing appliances - requestGetAppls := client.DefaultAPI.AppliancesGet(context.Background()) - collection, response, err := requestGetAppls.Execute() + request := client.DefaultAPI.AppliancesGet(context.Background()) + collection, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", requestGetAppls, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - requestGetAppls, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(4).InfoS("success: AppliancesGet", "count", collection.GetMemberCount()) for _, member := range collection.GetMembers() { id := ReadLastItemFromUri(member.GetUri()) - requestGetApplById := client.DefaultAPI.AppliancesGetById(context.Background(), id) - appliance, response, err := requestGetApplById.Execute() + request2 := client.DefaultAPI.AppliancesGetById(context.Background(), id) + appliance, response2, err := request2.Execute() + if response2 != nil { + defer response2.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", requestGetApplById, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - requestGetApplById, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response2, err) + return nil, fmt.Errorf("execute failure(%T): %w", request2, newErr) } klog.V(4).InfoS("success: AppliancesGetById", "applianceId", appliance.GetId()) @@ -115,19 +86,12 @@ func RenameApplianceById(client *service.APIClient, applianceId string, newAppli request := client.DefaultAPI.AppliancesUpdateById(context.Background(), applianceId) request = request.NewApplianceId(newApplianceId) appliance, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: RenameApplianceById", "request", request) @@ -138,19 +102,12 @@ func RenameApplianceById(client *service.APIClient, applianceId string, newAppli func ResyncApplianceById(client *service.APIClient, applianceId string) (*service.Appliance, error) { request := client.DefaultAPI.AppliancesResyncById(context.Background(), applianceId) appliance, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: ResyncApplianceById", "applianceId", appliance.GetId()) diff --git a/cli/pkg/serviceLib/serviceWrap/blade.go b/cli/pkg/serviceLib/serviceWrap/blade.go index d56a4db..42a40dc 100644 --- a/cli/pkg/serviceLib/serviceWrap/blade.go +++ b/cli/pkg/serviceLib/serviceWrap/blade.go @@ -4,7 +4,6 @@ package serviceWrap import ( "context" - "encoding/json" "fmt" service "cfm/pkg/client" @@ -54,19 +53,12 @@ func AddBlade(client *service.APIClient, applianceId string, bladeCreds *service request := client.DefaultAPI.BladesPost(context.Background(), applianceId) request = request.Credentials(*bladeCreds) blade, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: AddBlade", "bladeId", blade.GetId(), "applianceId", applianceId) @@ -77,19 +69,12 @@ func AddBlade(client *service.APIClient, applianceId string, bladeCreds *service func DeleteBladeById(client *service.APIClient, applId, bladeId string) (*service.Blade, error) { request := client.DefaultAPI.BladesDeleteById(context.Background(), applId, bladeId) blade, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: DeleteBladeById", "applianceId", applId, "bladeID", blade.GetId()) @@ -103,19 +88,12 @@ func FindBladeById_SingleAppl(client *service.APIClient, applId, bladeId string) request := client.DefaultAPI.BladesGetById(context.Background(), applId, bladeId) //TODO: What does this api do when the blade is empty blade, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: FindBladeById_SingleAppl", "applianceId", applId, "bladeId", blade.GetId()) @@ -130,19 +108,12 @@ func FindBladeById_AllAppls(client *service.APIClient, bladeId string) (*Applian //Get all existing appliances request := client.DefaultAPI.AppliancesGet(context.Background()) applColl, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(4).InfoS("success: AppliancesGet", "applCount", applColl.GetMemberCount()) @@ -173,19 +144,12 @@ func GetAllBlades_SingleAppl(client *service.APIClient, applId string) (*[]*serv request := client.DefaultAPI.BladesGet(context.Background(), applId) bladeColl, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(4).InfoS("success: BladesGet", "applianceId", applId, "bladeCount", bladeColl.GetMemberCount()) @@ -211,19 +175,12 @@ func GetAllBlades_AllAppls(client *service.APIClient) (*ApplianceBladeSummary, e //Get all existing appliances request := client.DefaultAPI.AppliancesGet(context.Background()) applColl, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(4).InfoS("success: AppliancesGet", "applCount", applColl.GetMemberCount()) @@ -247,19 +204,12 @@ func RenameBladeById(client *service.APIClient, applId string, bladeId string, n request := client.DefaultAPI.BladesUpdateById(context.Background(), applId, bladeId) request = request.NewBladeId(newBladeId) blade, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: RenameBladeById", "request", request) @@ -270,19 +220,12 @@ func RenameBladeById(client *service.APIClient, applId string, bladeId string, n func ResyncBladeById(client *service.APIClient, applId, bladeId string) (*service.Blade, error) { request := client.DefaultAPI.BladesResyncById(context.Background(), applId, bladeId) blade, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: ResyncBladeById", "applianceId", applId, "bladeID", blade.GetId()) diff --git a/cli/pkg/serviceLib/serviceWrap/common.go b/cli/pkg/serviceLib/serviceWrap/common.go index dcfe671..206ec1c 100644 --- a/cli/pkg/serviceLib/serviceWrap/common.go +++ b/cli/pkg/serviceLib/serviceWrap/common.go @@ -3,13 +3,17 @@ package serviceWrap import ( + "encoding/json" + "errors" "fmt" + "net/http" "net/netip" "strings" service "cfm/pkg/client" "github.com/google/uuid" + "k8s.io/klog/v2" ) func GetServiceClient(ip string, networkPort uint16) *service.APIClient { @@ -134,3 +138,30 @@ func NewApplianceBladeKey(applId, bladeId string) *ApplianceBladeKey { BladeId: bladeId, } } + +//handleServiceError - common error handler function used after all cfm-service client calls. +func handleServiceError(response *http.Response, err error) error { + var status service.StatusMessage + + if response == nil { + return fmt.Errorf("no response body to interrogate during error, just return the error: %s", err) + } + + //Purposefully NOT using key\value pairing here for the "response object". + // ErrorS() is interrogating the response object, in the "key" location, in such a way that it displays extra error information from within the response body that isn't visible using a basic string cast of the object. + // Currently, unsure of another way to get at this info. + // Note: After the json Decode() call just below, the extra error information is gone, so, must dump here. + // So far, not normally needed. Put on the verbosity flag so it's only visible when requested. + klog.V(4).ErrorS(errors.New(""), "failure: raw response dump: ", response) + + decodeError := json.NewDecoder(response.Body).Decode(&status) + if decodeError != nil { + // code for when Decode of the service's StatusMessage type fails, which means it's another type of error (like http). + return fmt.Errorf("error decoding response JSON: %s: ", decodeError) + } + + newErr := fmt.Errorf("failure: err(%s), uri(%s), details(%s), code(%d), message(%s)", + err, status.Uri, status.Details, status.Status.Code, status.Status.Message) + + return fmt.Errorf("response error info: %s", newErr) +} diff --git a/cli/pkg/serviceLib/serviceWrap/host.go b/cli/pkg/serviceLib/serviceWrap/host.go index 1103f2b..1e82233 100644 --- a/cli/pkg/serviceLib/serviceWrap/host.go +++ b/cli/pkg/serviceLib/serviceWrap/host.go @@ -4,7 +4,6 @@ package serviceWrap import ( "context" - "encoding/json" "fmt" service "cfm/pkg/client" @@ -16,19 +15,12 @@ func AddHost(client *service.APIClient, creds *service.Credentials) (*service.Ho request := client.DefaultAPI.HostsPost(context.Background()) request = request.Credentials(*creds) host, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: AddHost", "hostId", host.GetId()) @@ -39,19 +31,12 @@ func AddHost(client *service.APIClient, creds *service.Credentials) (*service.Ho func DeleteHostById(client *service.APIClient, hostId string) (*service.Host, error) { request := client.DefaultAPI.HostsDeleteById(context.Background(), hostId) host, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: DeleteHostById", "hostId", host.GetId()) @@ -63,42 +48,28 @@ func GetAllHosts(client *service.APIClient) (*[]*service.Host, error) { var hosts []*service.Host //Get existing hosts - requestGetHosts := client.DefaultAPI.HostsGet(context.Background()) - collection, response, err := requestGetHosts.Execute() + request := client.DefaultAPI.HostsGet(context.Background()) + collection, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", requestGetHosts, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - requestGetHosts, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(4).InfoS("success: HostsGet", "count", collection.GetMemberCount()) for _, member := range collection.GetMembers() { id := ReadLastItemFromUri(member.GetUri()) - requestGetHostById := client.DefaultAPI.HostsGetById(context.Background(), id) - host, response, err := requestGetHostById.Execute() + request2 := client.DefaultAPI.HostsGetById(context.Background(), id) + host, response2, err := request2.Execute() + if response2 != nil { + defer response2.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", requestGetHostById, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - requestGetHostById, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response2, err) + return nil, fmt.Errorf("execute failure(%T): %w", request2, newErr) } klog.V(4).InfoS("success: HostsGetById", "hostId", host.GetId()) @@ -115,19 +86,12 @@ func RenameHostById(client *service.APIClient, hostId string, newHostId string) request := client.DefaultAPI.HostsUpdateById(context.Background(), hostId) request = request.NewHostId(newHostId) host, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: RenameHostById", "request", request) @@ -138,19 +102,12 @@ func RenameHostById(client *service.APIClient, hostId string, newHostId string) func ResyncHostById(client *service.APIClient, hostId string) (*service.Host, error) { request := client.DefaultAPI.HostsResyncById(context.Background(), hostId) host, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: ResyncHostById", "hostID", host.GetId()) diff --git a/cli/pkg/serviceLib/serviceWrap/memory-devices.go b/cli/pkg/serviceLib/serviceWrap/memory-devices.go index 486a2f0..4a836b2 100644 --- a/cli/pkg/serviceLib/serviceWrap/memory-devices.go +++ b/cli/pkg/serviceLib/serviceWrap/memory-devices.go @@ -4,7 +4,6 @@ package serviceWrap import ( "context" - "encoding/json" "fmt" service "cfm/pkg/client" @@ -57,19 +56,12 @@ func FindMemoryDeviceOnHost(client *service.APIClient, hostId, memoryDeviceId st request := client.DefaultAPI.HostsGetMemoryDeviceById(context.Background(), hostId, memoryDeviceId) memoryDevice, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: FindMemoryDeviceOnHost", "hostId", hostId, "memoryDeviceId", memoryDevice.GetId()) @@ -82,19 +74,13 @@ func GetAllMemoryDevicesForHost(client *service.APIClient, hostId string) (*[]*s request := client.DefaultAPI.HostsGetMemoryDevices(context.Background(), hostId) memoryDeviceColl, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - // return nil, newErr + // newErr := handleServiceError(response, err) + // return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) + handleServiceError(response, err) return &memoryDevices, nil //TODO: Error here instead? } @@ -102,21 +88,15 @@ func GetAllMemoryDevicesForHost(client *service.APIClient, hostId string) (*[]*s for _, member := range memoryDeviceColl.GetMembers() { memoryDeviceId := ReadLastItemFromUri(member.GetUri()) - request := client.DefaultAPI.HostsGetMemoryDeviceById(context.Background(), hostId, memoryDeviceId) - memoryDevice, response, err := request.Execute() + request2 := client.DefaultAPI.HostsGetMemoryDeviceById(context.Background(), hostId, memoryDeviceId) + memoryDevice, response2, err := request2.Execute() + if response2 != nil { + defer response2.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - // return nil, newErr + // newErr := handleServiceError(response2, err) + // return nil, fmt.Errorf("execute failure(%T): %w", request2, newErr) + handleServiceError(response2, err) continue //TODO: Error here instead? } @@ -134,19 +114,13 @@ func GetMemoryDevices_AllHosts(client *service.APIClient) (*HostMemoryDeviceSumm request := client.DefaultAPI.HostsGet(context.Background()) hostColl, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - // return nil, newErr + // newErr := handleServiceError(response, err) + // return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) + handleServiceError(response, err) return summary, nil //TODO: Error here instead? } @@ -170,19 +144,13 @@ func FindMemoryDevice_AllHosts(client *service.APIClient, memoryDeviceId string) request := client.DefaultAPI.HostsGet(context.Background()) hostColl, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - // return nil, newErr + // newErr := handleServiceError(response, err) + // return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) + handleServiceError(response, err) return summary, nil //TODO: Error here instead? } diff --git a/cli/pkg/serviceLib/serviceWrap/memory.go b/cli/pkg/serviceLib/serviceWrap/memory.go index 48ca48a..11aba1c 100644 --- a/cli/pkg/serviceLib/serviceWrap/memory.go +++ b/cli/pkg/serviceLib/serviceWrap/memory.go @@ -4,7 +4,6 @@ package serviceWrap import ( "context" - "encoding/json" "fmt" service "cfm/pkg/client" @@ -56,19 +55,12 @@ func FindMemoryRegionOnBlade(client *service.APIClient, applId, bladeId, memoryI request := client.DefaultAPI.BladesGetMemoryById(context.Background(), applId, bladeId, memoryId) memoryRegion, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: FindMemoryRegionOnBlade", "applId", applId, "bladeId", bladeId, "memoryId", memoryRegion.GetId()) @@ -79,21 +71,15 @@ func FindMemoryRegionOnBlade(client *service.APIClient, applId, bladeId, memoryI func GetAllMemoryRegionsForBlade(client *service.APIClient, applId, bladeId string) (*[]*service.MemoryRegion, error) { var memoryRegions []*service.MemoryRegion - requestMemoryRegions := client.DefaultAPI.BladesGetMemory(context.Background(), applId, bladeId) - memoryRegionColl, response, err := requestMemoryRegions.Execute() + request := client.DefaultAPI.BladesGetMemory(context.Background(), applId, bladeId) + memoryRegionColl, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", requestMemoryRegions, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - requestMemoryRegions, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - // return nil, newErr + // newErr := handleServiceError(response, err) + // return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) + handleServiceError(response, err) return &memoryRegions, nil //TODO: Error here instead? } @@ -101,21 +87,15 @@ func GetAllMemoryRegionsForBlade(client *service.APIClient, applId, bladeId stri for _, res := range memoryRegionColl.GetMembers() { memoryId := ReadLastItemFromUri(res.GetUri()) - requestMemoryRegionById := client.DefaultAPI.BladesGetMemoryById(context.Background(), applId, bladeId, memoryId) - memoryRegion, response, err := requestMemoryRegionById.Execute() + request2 := client.DefaultAPI.BladesGetMemoryById(context.Background(), applId, bladeId, memoryId) + memoryRegion, response2, err := request2.Execute() + if response2 != nil { + defer response2.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", requestMemoryRegionById, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - requestMemoryRegionById, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - // return nil, newErr + // newErr := handleServiceError(response2, err) + // return nil, fmt.Errorf("execute failure(%T): %w", request2, newErr) + handleServiceError(response2, err) continue //TODO: Error here instead? } @@ -321,26 +301,19 @@ func ComposeMemory(client *service.APIClient, applianceId, bladeId, portId strin requestCompose.SetPort(portId) // create new ApiBladesComposeMemoryRequest - requestApiCompose := client.DefaultAPI.BladesComposeMemory(context.Background(), applianceId, bladeId) + request := client.DefaultAPI.BladesComposeMemory(context.Background(), applianceId, bladeId) // add ComposeMemoryRequest to ApiAppliancesComposeMemoryRequest - requestApiCompose = requestApiCompose.ComposeMemoryRequest(*requestCompose) + request = request.ComposeMemoryRequest(*requestCompose) // Execute ApiAppliancesComposeMemoryRequest - region, response, err := requestApiCompose.Execute() + region, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", requestApiCompose, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - requestApiCompose, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: ComposeMemory", "region", region.GetId(), "size", region.GetSizeMiB(), "applId", applianceId, "bladeId", bladeId) @@ -352,22 +325,15 @@ func ComposeMemory(client *service.APIClient, applianceId, bladeId, portId strin func FreeMemory(client *service.APIClient, applianceId, bladeId, memoryId string) (*service.MemoryRegion, error) { // create new ApiAppliancesFreeMemoryByIdRequest - requestFree := client.DefaultAPI.BladesFreeMemoryById(context.Background(), applianceId, bladeId, memoryId) + request := client.DefaultAPI.BladesFreeMemoryById(context.Background(), applianceId, bladeId, memoryId) // Execute ApiAppliancesFreeMemoryByIdRequest - region, response, err := requestFree.Execute() + region, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", requestFree, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - requestFree, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: FreeMemory", "region", region.GetId(), "size", region.GetSizeMiB(), "applId", applianceId, "bladeId", bladeId) @@ -423,19 +389,12 @@ func FindMemoryRegionOnHost(client *service.APIClient, hostId, memoryId string) request := client.DefaultAPI.HostsGetMemoryById(context.Background(), hostId, memoryId) memoryRegion, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: FindMemoryRegionOnHost", "hostId", hostId, "memoryId", memoryRegion.GetId()) @@ -448,19 +407,13 @@ func GetAllMemoryRegionsForHost(client *service.APIClient, hostId string) (*[]*s request := client.DefaultAPI.HostGetMemory(context.Background(), hostId) memoryRegionColl, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - // return nil, newErr + // newErr := handleServiceError(response, err) + // return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) + handleServiceError(response, err) return &memoryRegions, nil //TODO: Error here instead? } @@ -468,21 +421,15 @@ func GetAllMemoryRegionsForHost(client *service.APIClient, hostId string) (*[]*s for _, member := range memoryRegionColl.GetMembers() { memoryId := ReadLastItemFromUri(member.GetUri()) - request := client.DefaultAPI.HostsGetMemoryById(context.Background(), hostId, memoryId) - memoryRegion, response, err := request.Execute() + request2 := client.DefaultAPI.HostsGetMemoryById(context.Background(), hostId, memoryId) + memoryRegion, response2, err := request2.Execute() + if response2 != nil { + defer response2.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - // return nil, newErr + // newErr := handleServiceError(response2, err) + // return nil, fmt.Errorf("execute failure(%T): %w", request2, newErr) + handleServiceError(response2, err) continue //TODO: Error here instead? } @@ -500,19 +447,13 @@ func GetMemoryRegions_AllHosts(client *service.APIClient) (*HostMemoryRegionSumm request := client.DefaultAPI.HostsGet(context.Background()) hostColl, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - // return nil, newErr + // newErr := handleServiceError(response, err) + // return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) + handleServiceError(response, err) return summary, nil //TODO: Error here instead? } @@ -536,19 +477,13 @@ func FindMemoryRegion_AllHosts(client *service.APIClient, memoryId string) (*Hos request := client.DefaultAPI.HostsGet(context.Background()) hostColl, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - // return nil, newErr + // newErr := handleServiceError(response, err) + // return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) + handleServiceError(response, err) return summary, nil //TODO: Error here instead? } diff --git a/cli/pkg/serviceLib/serviceWrap/memoryBlade.go b/cli/pkg/serviceLib/serviceWrap/memoryBlade.go index fbaca44..d8bc40f 100644 --- a/cli/pkg/serviceLib/serviceWrap/memoryBlade.go +++ b/cli/pkg/serviceLib/serviceWrap/memoryBlade.go @@ -4,7 +4,6 @@ package serviceWrap import ( "context" - "encoding/json" "fmt" service "cfm/pkg/client" @@ -19,26 +18,19 @@ func BladesAssignMemory(client *service.APIClient, applianceId, bladeId, memoryI assignRequest := service.NewAssignMemoryRequest(portId, operation) // create new ApiBladesAssignMemoryByIdRequest - bladeRequest := client.DefaultAPI.BladesAssignMemoryById(context.Background(), applianceId, bladeId, memoryId) + request := client.DefaultAPI.BladesAssignMemoryById(context.Background(), applianceId, bladeId, memoryId) // add AssignMemoryRequest to ApiBladesAssignMemoryByIdRequest - bladeRequest = bladeRequest.AssignMemoryRequest(*assignRequest) + request = request.AssignMemoryRequest(*assignRequest) // Execute ApiBladesAssignMemoryByIdRequest - region, response, err := bladeRequest.Execute() + region, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", bladeRequest, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - bladeRequest, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("sucess: BladesAssignMemory", "operation", operation, "memoryId", region.GetId(), "portId", region.GetMemoryAppliancePort(), "size", region.GetSizeMiB(), "applId", region.GetMemoryApplianceId(), "bladeId", region.GetMemoryBladeId()) diff --git a/cli/pkg/serviceLib/serviceWrap/ports.go b/cli/pkg/serviceLib/serviceWrap/ports.go index 982272c..d3357e5 100644 --- a/cli/pkg/serviceLib/serviceWrap/ports.go +++ b/cli/pkg/serviceLib/serviceWrap/ports.go @@ -4,7 +4,6 @@ package serviceWrap import ( "context" - "encoding/json" "fmt" service "cfm/pkg/client" @@ -56,19 +55,12 @@ func FindPortOnBlade(client *service.APIClient, applId, bladeId, portId string) request := client.DefaultAPI.BladesGetPortById(context.Background(), applId, bladeId, portId) port, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: FindPortOnBlade", "applId", applId, "bladeId", bladeId, "portId", port.GetId()) @@ -79,21 +71,15 @@ func FindPortOnBlade(client *service.APIClient, applId, bladeId, portId string) func GetAllPortsForBlade(client *service.APIClient, applId, bladeId string) (*[]*service.PortInformation, error) { var ports []*service.PortInformation - requestPorts := client.DefaultAPI.BladesGetPorts(context.Background(), applId, bladeId) - portColl, response, err := requestPorts.Execute() + request := client.DefaultAPI.BladesGetPorts(context.Background(), applId, bladeId) + portColl, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", requestPorts, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - requestPorts, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - // return nil, newErr + // newErr := handleServiceError(response, err) + // return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) + handleServiceError(response, err) return &ports, nil //TODO: Error here instead? } @@ -101,21 +87,15 @@ func GetAllPortsForBlade(client *service.APIClient, applId, bladeId string) (*[] for _, res := range portColl.GetMembers() { portId := ReadLastItemFromUri(res.GetUri()) - requestPortById := client.DefaultAPI.BladesGetPortById(context.Background(), applId, bladeId, portId) - port, response, err := requestPortById.Execute() + request2 := client.DefaultAPI.BladesGetPortById(context.Background(), applId, bladeId, portId) + port, response2, err := request2.Execute() + if response2 != nil { + defer response2.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", requestPortById, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - requestPortById, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - // return nil, newErr + // newErr := handleServiceError(response2, err) + // return nil, fmt.Errorf("execute failure(%T): %w", request2, newErr) + handleServiceError(response2, err) continue //TODO: Error here instead? } @@ -353,19 +333,12 @@ func FindPortById_SingleHost(client *service.APIClient, hostId, portId string) ( request := client.DefaultAPI.HostsGetPortById(context.Background(), hostId, portId) port, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("success: HostsGetPortById", "hostId", hostId, "portId", port.GetId()) @@ -378,21 +351,14 @@ func FindPortById_AllHosts(client *service.APIClient, portId string) (*HostPortS summary := NewHostPortSummary() //Get all existing hosts - requestHosts := client.DefaultAPI.HostsGet(context.Background()) - hostsColl, response, err := requestHosts.Execute() + request := client.DefaultAPI.HostsGet(context.Background()) + hostsColl, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", requestHosts, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - requestHosts, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(4).InfoS("success: HostsGet", "hostsColl", hostsColl.GetMemberCount()) @@ -421,21 +387,14 @@ func FindPortById_AllHosts(client *service.APIClient, portId string) (*HostPortS func GetAllPorts_SingleHost(client *service.APIClient, hostId string) (*[]*service.PortInformation, error) { var ports []*service.PortInformation - requestPorts := client.DefaultAPI.HostsGetPorts(context.Background(), hostId) - portsColl, response, err := requestPorts.Execute() + request := client.DefaultAPI.HostsGetPorts(context.Background(), hostId) + portsColl, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", requestPorts, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - requestPorts, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(4).InfoS("success: PortsGet", "hostId", hostId, "portsColl", portsColl.GetMemberCount()) @@ -459,21 +418,14 @@ func GetAllPorts_AllHosts(client *service.APIClient) (*HostPortSummary, error) { summary := NewHostPortSummary() //Get all existing hosts - requestHosts := client.DefaultAPI.HostsGet(context.Background()) - hostColl, response, err := requestHosts.Execute() + request := client.DefaultAPI.HostsGet(context.Background()) + hostColl, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", requestHosts, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - requestHosts, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(4).InfoS("success: HostsGet", "hostColl", hostColl.GetMemberCount()) diff --git a/cli/pkg/serviceLib/serviceWrap/resources.go b/cli/pkg/serviceLib/serviceWrap/resources.go index 667a46b..7a8a463 100644 --- a/cli/pkg/serviceLib/serviceWrap/resources.go +++ b/cli/pkg/serviceLib/serviceWrap/resources.go @@ -4,7 +4,6 @@ package serviceWrap import ( "context" - "encoding/json" "fmt" service "cfm/pkg/client" @@ -56,19 +55,12 @@ func FindResourceBlockOnBlade(client *service.APIClient, applId, bladeId, resour request := client.DefaultAPI.BladesGetResourceById(context.Background(), applId, bladeId, resourceId) resourceBlock, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", request, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - request, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - return nil, newErr + newErr := handleServiceError(response, err) + return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) } klog.V(3).InfoS("FindResourceBlockOnBlade success", "applId", applId, "bladeId", bladeId, "resourceId", resourceBlock.GetId()) @@ -79,21 +71,15 @@ func FindResourceBlockOnBlade(client *service.APIClient, applId, bladeId, resour func GetAllResourceBlocksForBlade(client *service.APIClient, applId, bladeId string) (*[]*service.MemoryResourceBlock, error) { var resources []*service.MemoryResourceBlock - requestResources := client.DefaultAPI.BladesGetResources(context.Background(), applId, bladeId) - resourceColl, response, err := requestResources.Execute() + request := client.DefaultAPI.BladesGetResources(context.Background(), applId, bladeId) + resourceColl, response, err := request.Execute() + if response != nil { + defer response.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", requestResources, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - requestResources, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - // return nil, newErr + // newErr := handleServiceError(response, err) + // return nil, fmt.Errorf("execute failure(%T): %w", request, newErr) + handleServiceError(response, err) return &resources, nil //TODO: Error here instead? } @@ -101,21 +87,15 @@ func GetAllResourceBlocksForBlade(client *service.APIClient, applId, bladeId str for _, res := range resourceColl.GetMembers() { resourceId := ReadLastItemFromUri(res.GetUri()) - requestResourceById := client.DefaultAPI.BladesGetResourceById(context.Background(), applId, bladeId, resourceId) - resourceBlock, response, err := requestResourceById.Execute() + request2 := client.DefaultAPI.BladesGetResourceById(context.Background(), applId, bladeId, resourceId) + resourceBlock, response2, err := request2.Execute() + if response2 != nil { + defer response2.Body.Close() // Required by http lib implementation. + } if err != nil { - // Decode the JSON response into a struct - var status service.StatusMessage - if err := json.NewDecoder(response.Body).Decode(&status); err != nil { - newErr := fmt.Errorf("failure: Execute(%T): err(%s), error decoding response JSON", requestResourceById, err) - klog.V(4).Info(newErr) - return nil, newErr - } - - newErr := fmt.Errorf("failure: Execute(%T): err(%s), uri(%s), details(%s), code(%d), message(%s)", - requestResourceById, err, status.Uri, status.Details, status.Status.Code, status.Status.Message) - klog.V(4).Info(newErr) - // return nil, newErr + // newErr := handleServiceError(response2, err) + // return nil, fmt.Errorf("execute failure(%T): %w", request2, newErr) + handleServiceError(response2, err) continue //TODO: Error here instead? }