From acb0171e07dad2bb1add64a5ce27d17a37632c2e Mon Sep 17 00:00:00 2001 From: Scott Howe Date: Tue, 1 Oct 2024 09:01:28 -0600 Subject: [PATCH] fix: improve error messaging (#35) fix: implement more detailed cfm-service error messaging to communicate more detailed error information to the client --- cli/pkg/serviceLib/serviceWrap/appliance.go | 110 +++++++---- cli/pkg/serviceLib/serviceWrap/blade.go | 150 ++++++++++----- cli/pkg/serviceLib/serviceWrap/host.go | 110 +++++++---- .../serviceLib/serviceWrap/memory-devices.go | 82 +++++++-- cli/pkg/serviceLib/serviceWrap/memory.go | 171 ++++++++++++++---- cli/pkg/serviceLib/serviceWrap/memoryBlade.go | 17 +- cli/pkg/serviceLib/serviceWrap/ports.go | 128 +++++++++---- cli/pkg/serviceLib/serviceWrap/resources.go | 56 ++++-- 8 files changed, 609 insertions(+), 215 deletions(-) diff --git a/cli/pkg/serviceLib/serviceWrap/appliance.go b/cli/pkg/serviceLib/serviceWrap/appliance.go index 3b707d7..e4956f9 100644 --- a/cli/pkg/serviceLib/serviceWrap/appliance.go +++ b/cli/pkg/serviceLib/serviceWrap/appliance.go @@ -4,6 +4,7 @@ package serviceWrap import ( "context" + "encoding/json" "fmt" service "cfm/pkg/client" @@ -12,64 +13,100 @@ import ( ) func AddAppliance(client *service.APIClient, creds *service.Credentials) (*service.Appliance, error) { - newReqAddAppl := client.DefaultAPI.AppliancesPost(context.Background()) - newReqAddAppl = newReqAddAppl.Credentials(*creds) - addedAppl, response, err := newReqAddAppl.Execute() + request := client.DefaultAPI.AppliancesPost(context.Background()) + request = request.Credentials(*creds) + appliance, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", newReqAddAppl) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: appliances post: %s", err) + // 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 } - klog.V(3).InfoS("AppliancesPost success", "ID", addedAppl.GetId()) + klog.V(3).InfoS("success: AddAppliance", "applianceId", appliance.GetId()) - return addedAppl, nil + return appliance, nil } func DeleteApplianceById(client *service.APIClient, applId string) (*service.Appliance, error) { - newReqDelApplById := client.DefaultAPI.AppliancesDeleteById(context.Background(), applId) - deletedAppl, response, err := newReqDelApplById.Execute() + request := client.DefaultAPI.AppliancesDeleteById(context.Background(), applId) + appliance, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", newReqDelApplById) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: delete appliance by id failure") + // 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 } - klog.V(3).InfoS("AppliancesDeleteById success", "ID", deletedAppl.GetId()) + klog.V(3).InfoS("success: DeleteApplianceById", "applianceId", appliance.GetId()) - return deletedAppl, nil + return appliance, nil } func GetAllAppliances(client *service.APIClient) (*[]*service.Appliance, error) { var appliances []*service.Appliance //Get existing appliances - newReqGetAppls := client.DefaultAPI.AppliancesGet(context.Background()) - collection, response, err := newReqGetAppls.Execute() + requestGetAppls := client.DefaultAPI.AppliancesGet(context.Background()) + collection, response, err := requestGetAppls.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", newReqGetAppls) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: appliances get: %s", err) + // 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 } - klog.V(3).InfoS("AppliancesGet success", "count", collection.GetMemberCount()) + klog.V(4).InfoS("success: AppliancesGet", "count", collection.GetMemberCount()) for _, member := range collection.GetMembers() { id := ReadLastItemFromUri(member.GetUri()) - newReqGetApplById := client.DefaultAPI.AppliancesGetById(context.Background(), id) - appliance, response, err := newReqGetApplById.Execute() + requestGetApplById := client.DefaultAPI.AppliancesGetById(context.Background(), id) + appliance, response, err := requestGetApplById.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", newReqGetApplById) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: appliances get by id: %s", err) + // 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 } - klog.V(3).InfoS("AppliancesGetById success", "ID", appliance.GetId()) + klog.V(4).InfoS("success: AppliancesGetById", "applianceId", appliance.GetId()) appliances = append(appliances, appliance) } - klog.V(4).InfoS("Discovered appliances", "count", len(appliances)) + klog.V(3).InfoS("success: GetAllAppliances", "count", len(appliances)) return &appliances, nil } @@ -78,12 +115,21 @@ func ResyncApplianceById(client *service.APIClient, applianceId string) (*servic request := client.DefaultAPI.AppliancesResyncById(context.Background(), applianceId) appliance, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", appliance) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: resync appliance by id failure") + // 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 } - klog.V(3).InfoS("BladesResyncById success", "applianceId", appliance.GetId()) + klog.V(3).InfoS("success: ResyncApplianceById", "applianceId", appliance.GetId()) return appliance, nil } diff --git a/cli/pkg/serviceLib/serviceWrap/blade.go b/cli/pkg/serviceLib/serviceWrap/blade.go index 3c86aaa..72ed691 100644 --- a/cli/pkg/serviceLib/serviceWrap/blade.go +++ b/cli/pkg/serviceLib/serviceWrap/blade.go @@ -4,6 +4,7 @@ package serviceWrap import ( "context" + "encoding/json" "fmt" service "cfm/pkg/client" @@ -50,47 +51,74 @@ func (s *ApplianceBladeSummary) ApplianceCount() int { } func AddBlade(client *service.APIClient, applianceId string, bladeCreds *service.Credentials) (*service.Blade, error) { - addRequest := client.DefaultAPI.BladesPost(context.Background(), applianceId) - addRequest = addRequest.Credentials(*bladeCreds) - addedBlade, response, err := addRequest.Execute() + request := client.DefaultAPI.BladesPost(context.Background(), applianceId) + request = request.Credentials(*bladeCreds) + blade, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", addRequest) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: blades post: %s", err) + // 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 } - klog.V(3).InfoS("BladesPost success", "bladeId", addedBlade.GetId(), "applianceId", applianceId) + klog.V(3).InfoS("success: AddBlade", "bladeId", blade.GetId(), "applianceId", applianceId) - return addedBlade, nil + return blade, nil } func DeleteBladeById(client *service.APIClient, applId, bladeId string) (*service.Blade, error) { - deleteRequest := client.DefaultAPI.BladesDeleteById(context.Background(), applId, bladeId) - deletedBlade, response, err := deleteRequest.Execute() + request := client.DefaultAPI.BladesDeleteById(context.Background(), applId, bladeId) + blade, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", deleteRequest) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: delete blade by id failure") + // 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 } - klog.V(3).InfoS("BladesDeleteById success", "ApplianceID", applId, "BladeID", deletedBlade.GetId()) + klog.V(3).InfoS("success: DeleteBladeById", "applianceId", applId, "bladeID", blade.GetId()) - return deletedBlade, nil + return blade, nil } // Find a specific Blade by ID on a specific Appliance func FindBladeById_SingleAppl(client *service.APIClient, applId, bladeId string) (*service.Blade, error) { - requestBlade := client.DefaultAPI.BladesGetById(context.Background(), applId, bladeId) + request := client.DefaultAPI.BladesGetById(context.Background(), applId, bladeId) //TODO: What does this api do when the blade is empty - blade, response, err := requestBlade.Execute() + blade, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestBlade) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: get appliance blade by id: %s", err) + // 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 } - klog.V(3).InfoS("BladesGetById success", "applId", applId, "bladeId", blade.GetId()) + klog.V(3).InfoS("success: FindBladeById_SingleAppl", "applianceId", applId, "bladeId", blade.GetId()) return blade, nil } @@ -100,15 +128,24 @@ func FindBladeById_AllAppls(client *service.APIClient, bladeId string) (*Applian summary := NewApplianceBladeSummary() //Get all existing appliances - requestAppliances := client.DefaultAPI.AppliancesGet(context.Background()) - applColl, response, err := requestAppliances.Execute() + request := client.DefaultAPI.AppliancesGet(context.Background()) + applColl, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestAppliances) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: get appliances: %s", err) + // 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 } - klog.V(3).InfoS("AppliancesGet success", "applCount", applColl.GetMemberCount()) + klog.V(4).InfoS("success: AppliancesGet", "applCount", applColl.GetMemberCount()) if applColl.GetMemberCount() == 0 { klog.V(3).InfoS("FindBladeById_AllAppls: no appliances found") @@ -134,15 +171,24 @@ func FindBladeById_AllAppls(client *service.APIClient, bladeId string) (*Applian func GetAllBlades_SingleAppl(client *service.APIClient, applId string) (*[]*service.Blade, error) { var blades []*service.Blade - requestBlades := client.DefaultAPI.BladesGet(context.Background(), applId) - bladeColl, response, err := requestBlades.Execute() + request := client.DefaultAPI.BladesGet(context.Background(), applId) + bladeColl, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestBlades) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: get appliance blades: %s", err) + // 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 } - klog.V(3).InfoS("BladesGet success", "applId", applId, "bladeColl", bladeColl.GetMemberCount()) + klog.V(4).InfoS("success: BladesGet", "applianceId", applId, "bladeCount", bladeColl.GetMemberCount()) for _, bladeMember := range bladeColl.GetMembers() { bladeId := ReadLastItemFromUri(bladeMember.GetUri()) @@ -163,15 +209,24 @@ func GetAllBlades_AllAppls(client *service.APIClient) (*ApplianceBladeSummary, e summary := NewApplianceBladeSummary() //Get all existing appliances - requestAppliances := client.DefaultAPI.AppliancesGet(context.Background()) - applColl, response, err := requestAppliances.Execute() + request := client.DefaultAPI.AppliancesGet(context.Background()) + applColl, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestAppliances) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: get appliances: %s", err) + // 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 } - klog.V(3).InfoS("AppliancesGet success", "applCount", applColl.GetMemberCount()) + klog.V(4).InfoS("success: AppliancesGet", "applCount", applColl.GetMemberCount()) //Scan collection members for target appliance id for _, appl := range applColl.GetMembers() { @@ -192,12 +247,21 @@ func ResyncBladeById(client *service.APIClient, applId, bladeId string) (*servic request := client.DefaultAPI.BladesResyncById(context.Background(), applId, bladeId) blade, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", blade) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: resync blade by id failure") + // 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 } - klog.V(3).InfoS("BladesResyncById success", "ApplianceID", applId, "BladeID", blade.GetId()) + klog.V(3).InfoS("success: ResyncBladeById", "applianceId", applId, "bladeID", blade.GetId()) return blade, nil } diff --git a/cli/pkg/serviceLib/serviceWrap/host.go b/cli/pkg/serviceLib/serviceWrap/host.go index 53ccc2d..8740bd2 100644 --- a/cli/pkg/serviceLib/serviceWrap/host.go +++ b/cli/pkg/serviceLib/serviceWrap/host.go @@ -4,6 +4,7 @@ package serviceWrap import ( "context" + "encoding/json" "fmt" service "cfm/pkg/client" @@ -12,64 +13,100 @@ import ( ) func AddHost(client *service.APIClient, creds *service.Credentials) (*service.Host, error) { - newReqAddHost := client.DefaultAPI.HostsPost(context.Background()) - newReqAddHost = newReqAddHost.Credentials(*creds) - addedHost, response, err := newReqAddHost.Execute() + request := client.DefaultAPI.HostsPost(context.Background()) + request = request.Credentials(*creds) + host, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", newReqAddHost) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: hosts post: %s", err) + // 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 } - klog.V(3).InfoS("HostsPost success", "hostId", addedHost.GetId()) + klog.V(3).InfoS("success: AddHost", "hostId", host.GetId()) - return addedHost, nil + return host, nil } func DeleteHostById(client *service.APIClient, hostId string) (*service.Host, error) { - newReqDelHostById := client.DefaultAPI.HostsDeleteById(context.Background(), hostId) - deletedHost, response, err := newReqDelHostById.Execute() + request := client.DefaultAPI.HostsDeleteById(context.Background(), hostId) + host, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", newReqDelHostById) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: delete host by id failure") + // 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 } - klog.V(3).InfoS("HostsDeleteById success", "hostId", deletedHost.GetId()) + klog.V(3).InfoS("success: DeleteHostById", "hostId", host.GetId()) - return deletedHost, nil + return host, nil } func GetAllHosts(client *service.APIClient) (*[]*service.Host, error) { var hosts []*service.Host //Get existing hosts - newReqGetHosts := client.DefaultAPI.HostsGet(context.Background()) - collection, response, err := newReqGetHosts.Execute() + requestGetHosts := client.DefaultAPI.HostsGet(context.Background()) + collection, response, err := requestGetHosts.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", newReqGetHosts) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: hosts get: %s", err) + // 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 } - klog.V(3).InfoS("HostsGet success", "count", collection.GetMemberCount()) + klog.V(4).InfoS("success: HostsGet", "count", collection.GetMemberCount()) for _, member := range collection.GetMembers() { id := ReadLastItemFromUri(member.GetUri()) - newReqGetHostById := client.DefaultAPI.HostsGetById(context.Background(), id) - host, response, err := newReqGetHostById.Execute() + requestGetHostById := client.DefaultAPI.HostsGetById(context.Background(), id) + host, response, err := requestGetHostById.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", newReqGetHostById) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: hosts get by id: %s", err) + // 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 } - klog.V(3).InfoS("HostsGetById success", "hostId", host.GetId()) + klog.V(4).InfoS("success: HostsGetById", "hostId", host.GetId()) hosts = append(hosts, host) } - klog.V(3).InfoS("Discovered hosts", "count", len(hosts)) + klog.V(3).InfoS("success: GetAllHosts", "Host Count", len(hosts)) return &hosts, nil } @@ -78,12 +115,21 @@ func ResyncHostById(client *service.APIClient, hostId string) (*service.Host, er request := client.DefaultAPI.HostsResyncById(context.Background(), hostId) host, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", host) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: resync host by id failure") + // 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 } - klog.V(3).InfoS("BladesResyncById success", "hostID", host.GetId()) + klog.V(3).InfoS("success: ResyncHostById", "hostID", host.GetId()) return host, nil } diff --git a/cli/pkg/serviceLib/serviceWrap/memory-devices.go b/cli/pkg/serviceLib/serviceWrap/memory-devices.go index db829cb..486a2f0 100644 --- a/cli/pkg/serviceLib/serviceWrap/memory-devices.go +++ b/cli/pkg/serviceLib/serviceWrap/memory-devices.go @@ -4,6 +4,7 @@ package serviceWrap import ( "context" + "encoding/json" "fmt" service "cfm/pkg/client" @@ -57,12 +58,21 @@ func FindMemoryDeviceOnHost(client *service.APIClient, hostId, memoryDeviceId st request := client.DefaultAPI.HostsGetMemoryDeviceById(context.Background(), hostId, memoryDeviceId) memoryDevice, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", request) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: get host memoryDevice by id: %s", err) + // 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 } - klog.V(3).InfoS("HostsGetMemoryById success", "hostId", hostId, "memoryDeviceId", memoryDevice.GetId()) + klog.V(3).InfoS("success: FindMemoryDeviceOnHost", "hostId", hostId, "memoryDeviceId", memoryDevice.GetId()) return memoryDevice, nil } @@ -73,26 +83,44 @@ func GetAllMemoryDevicesForHost(client *service.APIClient, hostId string) (*[]*s request := client.DefaultAPI.HostsGetMemoryDevices(context.Background(), hostId) memoryDeviceColl, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", request) - klog.ErrorS(err, msg, "response", response) - // return nil, fmt.Errorf("failure: get host memoryDevices: %s", err) + // 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 return &memoryDevices, nil //TODO: Error here instead? } - klog.V(3).InfoS("HostGetMemory success", "hostId", hostId, "memoryDeviceColl", memoryDeviceColl.GetMemberCount()) + klog.V(4).InfoS("success: HostsGetMemoryDevices", "hostId", hostId, "memoryDeviceColl", memoryDeviceColl.GetMemberCount()) for _, member := range memoryDeviceColl.GetMembers() { memoryDeviceId := ReadLastItemFromUri(member.GetUri()) request := client.DefaultAPI.HostsGetMemoryDeviceById(context.Background(), hostId, memoryDeviceId) memoryDevice, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", request) - klog.ErrorS(err, msg, "response", response) - // return nil, fmt.Errorf("failure: get host memoryDevice by id: %s", err) + // 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 continue //TODO: Error here instead? } - klog.V(3).InfoS("HostsGetMemoryById success", "hostId", hostId, "memoryDeviceId", memoryDevice.GetId()) + klog.V(4).InfoS("success: HostsGetMemoryById", "hostId", hostId, "memoryDeviceId", memoryDevice.GetId()) memoryDevices = append(memoryDevices, memoryDevice) } @@ -107,9 +135,18 @@ func GetMemoryDevices_AllHosts(client *service.APIClient) (*HostMemoryDeviceSumm request := client.DefaultAPI.HostsGet(context.Background()) hostColl, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", request) - klog.ErrorS(err, msg, "response", response) - // return nil, fmt.Errorf("failure: get host memoryDevices: %s", err) + // 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 return summary, nil //TODO: Error here instead? } @@ -134,9 +171,18 @@ func FindMemoryDevice_AllHosts(client *service.APIClient, memoryDeviceId string) request := client.DefaultAPI.HostsGet(context.Background()) hostColl, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", request) - klog.ErrorS(err, msg, "response", response) - // return nil, fmt.Errorf("failure: get host memoryDevices: %s", err) + // 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 return summary, nil //TODO: Error here instead? } diff --git a/cli/pkg/serviceLib/serviceWrap/memory.go b/cli/pkg/serviceLib/serviceWrap/memory.go index f9d374f..48ca48a 100644 --- a/cli/pkg/serviceLib/serviceWrap/memory.go +++ b/cli/pkg/serviceLib/serviceWrap/memory.go @@ -4,6 +4,7 @@ package serviceWrap import ( "context" + "encoding/json" "fmt" service "cfm/pkg/client" @@ -53,15 +54,24 @@ func (s *BladeMemoryRegionSummary) AddMemoryRegionSlice(applId, bladeId string, func FindMemoryRegionOnBlade(client *service.APIClient, applId, bladeId, memoryId string) (*service.MemoryRegion, error) { var memoryRegion *service.MemoryRegion - requestMemoryRegionById := client.DefaultAPI.BladesGetMemoryById(context.Background(), applId, bladeId, memoryId) - memoryRegion, response, err := requestMemoryRegionById.Execute() + request := client.DefaultAPI.BladesGetMemoryById(context.Background(), applId, bladeId, memoryId) + memoryRegion, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestMemoryRegionById) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: get appliance blade memoryRegion by id: %s", err) + // 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 } - klog.V(3).InfoS("BladesGetMemoryRegionById success", "applId", applId, "bladeId", bladeId, "memoryId", memoryRegion.GetId()) + klog.V(3).InfoS("success: FindMemoryRegionOnBlade", "applId", applId, "bladeId", bladeId, "memoryId", memoryRegion.GetId()) return memoryRegion, nil } @@ -72,26 +82,44 @@ func GetAllMemoryRegionsForBlade(client *service.APIClient, applId, bladeId stri requestMemoryRegions := client.DefaultAPI.BladesGetMemory(context.Background(), applId, bladeId) memoryRegionColl, response, err := requestMemoryRegions.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestMemoryRegions) - klog.ErrorS(err, msg, "response", response) - // return nil, fmt.Errorf("failure: get appliance blade memoryRegions: %s", err) + // 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 return &memoryRegions, nil //TODO: Error here instead? } - klog.V(3).InfoS("BladesGetMemoryRegions success", "applId", applId, "bladeId", bladeId, "memoryRegionColl", memoryRegionColl.GetMemberCount()) + klog.V(4).InfoS("success: BladesGetMemory", "applId", applId, "bladeId", bladeId, "memoryRegionColl", memoryRegionColl.GetMemberCount()) for _, res := range memoryRegionColl.GetMembers() { memoryId := ReadLastItemFromUri(res.GetUri()) requestMemoryRegionById := client.DefaultAPI.BladesGetMemoryById(context.Background(), applId, bladeId, memoryId) memoryRegion, response, err := requestMemoryRegionById.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestMemoryRegionById) - klog.ErrorS(err, msg, "response", response) - // return nil, fmt.Errorf("failure: get appliance blade memoryRegion by id: %s", err) + // 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 continue //TODO: Error here instead? } - klog.V(3).InfoS("BladesGetMemoryRegionById success", "applId", applId, "bladeId", bladeId, "memoryId", memoryRegion.GetId()) + klog.V(4).InfoS("success: BladesGetMemoryById", "applId", applId, "bladeId", bladeId, "memoryId", memoryRegion.GetId()) memoryRegions = append(memoryRegions, memoryRegion) } @@ -301,12 +329,21 @@ func ComposeMemory(client *service.APIClient, applianceId, bladeId, portId strin // Execute ApiAppliancesComposeMemoryRequest region, response, err := requestApiCompose.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestApiCompose) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: appliance blade compose memory: %s", err) + // 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 } - klog.V(3).InfoS("BladesComposeMemory success", "region", region.GetId(), "size", region.GetSizeMiB(), "applId", applianceId, "bladeId", bladeId) + klog.V(3).InfoS("success: ComposeMemory", "region", region.GetId(), "size", region.GetSizeMiB(), "applId", applianceId, "bladeId", bladeId) return region, nil } @@ -319,12 +356,21 @@ func FreeMemory(client *service.APIClient, applianceId, bladeId, memoryId string // Execute ApiAppliancesFreeMemoryByIdRequest region, response, err := requestFree.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestFree) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: appliances free memory: %s", err) + // 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 } - klog.V(3).InfoS("BladesFreeMemoryById success", "region", region.GetId(), "size", region.GetSizeMiB(), "applId", applianceId, "bladeId", bladeId) + klog.V(3).InfoS("success: FreeMemory", "region", region.GetId(), "size", region.GetSizeMiB(), "applId", applianceId, "bladeId", bladeId) return region, nil } @@ -378,12 +424,21 @@ func FindMemoryRegionOnHost(client *service.APIClient, hostId, memoryId string) request := client.DefaultAPI.HostsGetMemoryById(context.Background(), hostId, memoryId) memoryRegion, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", request) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: get host memoryRegion by id: %s", err) + // 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 } - klog.V(3).InfoS("HostsGetMemoryById success", "hostId", hostId, "memoryId", memoryRegion.GetId()) + klog.V(3).InfoS("success: FindMemoryRegionOnHost", "hostId", hostId, "memoryId", memoryRegion.GetId()) return memoryRegion, nil } @@ -394,26 +449,44 @@ func GetAllMemoryRegionsForHost(client *service.APIClient, hostId string) (*[]*s request := client.DefaultAPI.HostGetMemory(context.Background(), hostId) memoryRegionColl, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", request) - klog.ErrorS(err, msg, "response", response) - // return nil, fmt.Errorf("failure: get host memoryRegions: %s", err) + // 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 return &memoryRegions, nil //TODO: Error here instead? } - klog.V(3).InfoS("HostGetMemory success", "hostId", hostId, "memoryRegionColl", memoryRegionColl.GetMemberCount()) + klog.V(4).InfoS("success: HostGetMemory", "hostId", hostId, "memoryRegionColl", memoryRegionColl.GetMemberCount()) for _, member := range memoryRegionColl.GetMembers() { memoryId := ReadLastItemFromUri(member.GetUri()) request := client.DefaultAPI.HostsGetMemoryById(context.Background(), hostId, memoryId) memoryRegion, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", request) - klog.ErrorS(err, msg, "response", response) - // return nil, fmt.Errorf("failure: get host memoryRegion by id: %s", err) + // 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 continue //TODO: Error here instead? } - klog.V(3).InfoS("HostsGetMemoryById success", "hostId", hostId, "memoryId", memoryRegion.GetId()) + klog.V(4).InfoS("success: HostsGetMemoryById", "hostId", hostId, "memoryId", memoryRegion.GetId()) memoryRegions = append(memoryRegions, memoryRegion) } @@ -428,9 +501,18 @@ func GetMemoryRegions_AllHosts(client *service.APIClient) (*HostMemoryRegionSumm request := client.DefaultAPI.HostsGet(context.Background()) hostColl, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", request) - klog.ErrorS(err, msg, "response", response) - // return nil, fmt.Errorf("failure: get host memoryRegions: %s", err) + // 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 return summary, nil //TODO: Error here instead? } @@ -455,9 +537,18 @@ func FindMemoryRegion_AllHosts(client *service.APIClient, memoryId string) (*Hos request := client.DefaultAPI.HostsGet(context.Background()) hostColl, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", request) - klog.ErrorS(err, msg, "response", response) - // return nil, fmt.Errorf("failure: get host memoryRegions: %s", err) + // 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 return summary, nil //TODO: Error here instead? } diff --git a/cli/pkg/serviceLib/serviceWrap/memoryBlade.go b/cli/pkg/serviceLib/serviceWrap/memoryBlade.go index 90603b7..fbaca44 100644 --- a/cli/pkg/serviceLib/serviceWrap/memoryBlade.go +++ b/cli/pkg/serviceLib/serviceWrap/memoryBlade.go @@ -4,6 +4,7 @@ package serviceWrap import ( "context" + "encoding/json" "fmt" service "cfm/pkg/client" @@ -26,13 +27,21 @@ func BladesAssignMemory(client *service.APIClient, applianceId, bladeId, memoryI // Execute ApiBladesAssignMemoryByIdRequest region, response, err := bladeRequest.Execute() if err != nil { - newErr := fmt.Errorf("failure: blade %s memory: %w", operation, err) - msg := fmt.Sprintf("%T: Execute FAILURE", bladeRequest) - klog.ErrorS(newErr, msg, "response", response, "bladeRequest", bladeRequest) + // 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 } - klog.V(3).InfoS("BladesAssignMemory success", "operation", operation, "memoryId", region.GetId(), "portId", region.GetMemoryAppliancePort(), "size", region.GetSizeMiB(), "applId", region.GetMemoryApplianceId(), "bladeId", region.GetMemoryBladeId()) + klog.V(3).InfoS("sucess: BladesAssignMemory", "operation", operation, "memoryId", region.GetId(), "portId", region.GetMemoryAppliancePort(), "size", region.GetSizeMiB(), "applId", region.GetMemoryApplianceId(), "bladeId", region.GetMemoryBladeId()) return region, nil } diff --git a/cli/pkg/serviceLib/serviceWrap/ports.go b/cli/pkg/serviceLib/serviceWrap/ports.go index 3fedcc8..982272c 100644 --- a/cli/pkg/serviceLib/serviceWrap/ports.go +++ b/cli/pkg/serviceLib/serviceWrap/ports.go @@ -4,6 +4,7 @@ package serviceWrap import ( "context" + "encoding/json" "fmt" service "cfm/pkg/client" @@ -53,15 +54,24 @@ func (s *BladePortsSummary) AddPortSlice(applId, bladeId string, ports *[]*servi func FindPortOnBlade(client *service.APIClient, applId, bladeId, portId string) (*service.PortInformation, error) { var port *service.PortInformation - requestPortById := client.DefaultAPI.BladesGetPortById(context.Background(), applId, bladeId, portId) - port, response, err := requestPortById.Execute() + request := client.DefaultAPI.BladesGetPortById(context.Background(), applId, bladeId, portId) + port, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestPortById) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: get appliance blade port by id: %s", err) + // 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 } - klog.V(3).InfoS("BladesGetPortById success", "applId", applId, "bladeId", bladeId, "portId", port.GetId()) + klog.V(3).InfoS("success: FindPortOnBlade", "applId", applId, "bladeId", bladeId, "portId", port.GetId()) return port, nil } @@ -72,26 +82,44 @@ func GetAllPortsForBlade(client *service.APIClient, applId, bladeId string) (*[] requestPorts := client.DefaultAPI.BladesGetPorts(context.Background(), applId, bladeId) portColl, response, err := requestPorts.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestPorts) - klog.ErrorS(err, msg, "response", response) - // return nil, fmt.Errorf("failure: get appliance blade ports: %s", err) + // 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 return &ports, nil //TODO: Error here instead? } - klog.V(3).InfoS("BladesGetPorts success", "applId", applId, "bladeId", bladeId, "portColl", portColl.GetMemberCount()) + klog.V(4).InfoS("success: BladesGetPorts", "applId", applId, "bladeId", bladeId, "portColl", portColl.GetMemberCount()) for _, res := range portColl.GetMembers() { portId := ReadLastItemFromUri(res.GetUri()) requestPortById := client.DefaultAPI.BladesGetPortById(context.Background(), applId, bladeId, portId) port, response, err := requestPortById.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestPortById) - klog.ErrorS(err, msg, "response", response) - // return nil, fmt.Errorf("failure: get appliance blade port by id: %s", err) + // 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 continue //TODO: Error here instead? } - klog.V(3).InfoS("BladesGetPortById success", "applId", applId, "bladeId", bladeId, "portId", port.GetId()) + klog.V(4).InfoS("success: BladesGetPortById", "applId", applId, "bladeId", bladeId, "portId", port.GetId()) ports = append(ports, port) } @@ -323,15 +351,24 @@ func (s *HostPortSummary) HostCount() int { // Find a specific Port by ID on a specific host func FindPortById_SingleHost(client *service.APIClient, hostId, portId string) (*service.PortInformation, error) { - requestPort := client.DefaultAPI.HostsGetPortById(context.Background(), hostId, portId) - port, response, err := requestPort.Execute() + request := client.DefaultAPI.HostsGetPortById(context.Background(), hostId, portId) + port, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestPort) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: get host port by id: %s", err) + // 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 } - klog.V(3).InfoS("HostsGetPortById success", "hostId", hostId, "portId", port.GetId()) + klog.V(3).InfoS("success: HostsGetPortById", "hostId", hostId, "portId", port.GetId()) return port, nil } @@ -344,12 +381,21 @@ func FindPortById_AllHosts(client *service.APIClient, portId string) (*HostPortS requestHosts := client.DefaultAPI.HostsGet(context.Background()) hostsColl, response, err := requestHosts.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestHosts) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: get hosts: %s", err) + // 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 } - klog.V(3).InfoS("HostsGet success", "hostsColl", hostsColl.GetMemberCount()) + klog.V(4).InfoS("success: HostsGet", "hostsColl", hostsColl.GetMemberCount()) if hostsColl.GetMemberCount() == 0 { klog.V(3).InfoS("FindPortById_AllHosts: no hosts found") @@ -378,12 +424,21 @@ func GetAllPorts_SingleHost(client *service.APIClient, hostId string) (*[]*servi requestPorts := client.DefaultAPI.HostsGetPorts(context.Background(), hostId) portsColl, response, err := requestPorts.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestPorts) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: get host ports: %s", err) + // 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 } - klog.V(3).InfoS("PortsGet success", "hostId", hostId, "portsColl", portsColl.GetMemberCount()) + klog.V(4).InfoS("success: PortsGet", "hostId", hostId, "portsColl", portsColl.GetMemberCount()) for _, portMember := range portsColl.GetMembers() { portId := ReadLastItemFromUri(portMember.GetUri()) @@ -407,12 +462,21 @@ func GetAllPorts_AllHosts(client *service.APIClient) (*HostPortSummary, error) { requestHosts := client.DefaultAPI.HostsGet(context.Background()) hostColl, response, err := requestHosts.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestHosts) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: get all hosts and ports: %s", err) + // 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 } - klog.V(3).InfoS("HostsGet success", "hostColl", hostColl.GetMemberCount()) + klog.V(4).InfoS("success: HostsGet", "hostColl", hostColl.GetMemberCount()) //Scan collection members for target host id for _, host := range hostColl.GetMembers() { diff --git a/cli/pkg/serviceLib/serviceWrap/resources.go b/cli/pkg/serviceLib/serviceWrap/resources.go index ef71b00..667a46b 100644 --- a/cli/pkg/serviceLib/serviceWrap/resources.go +++ b/cli/pkg/serviceLib/serviceWrap/resources.go @@ -4,6 +4,7 @@ package serviceWrap import ( "context" + "encoding/json" "fmt" service "cfm/pkg/client" @@ -53,15 +54,24 @@ func (s *ResourceBlockSummary) AddResourceSlice(applId, bladeId string, resource func FindResourceBlockOnBlade(client *service.APIClient, applId, bladeId, resourceId string) (*service.MemoryResourceBlock, error) { var resourceBlock *service.MemoryResourceBlock - requestResourceById := client.DefaultAPI.BladesGetResourceById(context.Background(), applId, bladeId, resourceId) - resourceBlock, response, err := requestResourceById.Execute() + request := client.DefaultAPI.BladesGetResourceById(context.Background(), applId, bladeId, resourceId) + resourceBlock, response, err := request.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestResourceById) - klog.ErrorS(err, msg, "response", response) - return nil, fmt.Errorf("failure: get appliance blade resource by id: %s", err) + // 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 } - klog.V(3).InfoS("BladesGetResourceById success", "applId", applId, "bladeId", bladeId, "resourceId", resourceBlock.GetId()) + klog.V(3).InfoS("FindResourceBlockOnBlade success", "applId", applId, "bladeId", bladeId, "resourceId", resourceBlock.GetId()) return resourceBlock, nil } @@ -72,26 +82,44 @@ func GetAllResourceBlocksForBlade(client *service.APIClient, applId, bladeId str requestResources := client.DefaultAPI.BladesGetResources(context.Background(), applId, bladeId) resourceColl, response, err := requestResources.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestResources) - klog.ErrorS(err, msg, "response", response) - // return nil, fmt.Errorf("failure: get appliance blade resources: %s", err) + // 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 return &resources, nil //TODO: Error here instead? } - klog.V(3).InfoS("BladesGetResources success", "applId", applId, "bladeId", bladeId, "resourceColl", resourceColl.GetMemberCount()) + klog.V(4).InfoS("BladesGetResources success", "applId", applId, "bladeId", bladeId, "resourceColl", resourceColl.GetMemberCount()) for _, res := range resourceColl.GetMembers() { resourceId := ReadLastItemFromUri(res.GetUri()) requestResourceById := client.DefaultAPI.BladesGetResourceById(context.Background(), applId, bladeId, resourceId) resourceBlock, response, err := requestResourceById.Execute() if err != nil { - msg := fmt.Sprintf("%T: Execute FAILURE", requestResourceById) - klog.ErrorS(err, msg, "response", response) - // return nil, fmt.Errorf("failure: get appliance blade resource by id: %s", err) + // 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 continue //TODO: Error here instead? } - klog.V(3).InfoS("BladesGetResourceById success", "applId", applId, "bladeId", bladeId, "resourceId", resourceBlock.GetId()) + klog.V(4).InfoS("BladesGetResourceById success", "applId", applId, "bladeId", bladeId, "resourceId", resourceBlock.GetId()) resources = append(resources, resourceBlock) }