Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper Api error #34

Merged
merged 2 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var getBackupCmd = &cobra.Command{
}
resp, r, err := listBackupRequest.Execute()
if err != nil {
logrus.Errorf("Error when calling `BackupApi.ListBackups`: %v", err)
logrus.Errorf("Error when calling `BackupApi.ListBackups`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
shinji62 marked this conversation as resolved.
Show resolved Hide resolved
return
}
Expand Down Expand Up @@ -74,7 +74,7 @@ var restoreBackupCmd = &cobra.Command{

_, r, err := authApi.RestoreBackup().RestoreSpec(*restoreSpec).Execute()
if err != nil {
logrus.Errorf("Error when calling `BackupApi.RestoreBackup``: %v", err)
logrus.Errorf("Error when calling `BackupApi.RestoreBackup`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down Expand Up @@ -119,7 +119,7 @@ var createBackupCmd = &cobra.Command{
backupResp, response, err := authApi.CreateBackup().BackupSpec(createBackupSpec).Execute()

if err != nil {
logrus.Errorf("Error when calling `BackupApi.CreateBackup``: %v", err)
logrus.Errorf("Error when calling `BackupApi.CreateBackup`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", response)
return
}
Expand Down Expand Up @@ -151,7 +151,7 @@ var deleteBackupCmd = &cobra.Command{
response, err := authApi.DeleteBackup(backupID).Execute()

if err != nil {
logrus.Errorf("Error when calling `BackupApi.DeleteBackup``: %v", err)
logrus.Errorf("Error when calling `BackupApi.DeleteBackup`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", response)
return
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/cdc_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var getCdcSinkCmd = &cobra.Command{

resp, r, err := authApi.GetCdcSink(cdcSinkID).Execute()
if err != nil {
logrus.Errorf("Error when calling `CdcApi.GetCdcSink`: %v", err)
logrus.Errorf("Error when calling `CdcApi.GetCdcSink`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down Expand Up @@ -80,7 +80,7 @@ var createCdcSinkCmd = &cobra.Command{

resp, r, err := authApi.CreateCdcSink().CreateCdcSinkRequest(*createSinkRequest).Execute()
if err != nil {
logrus.Errorf("Error when calling `CdcApi.CreateeCdcSink`: %v", err)
logrus.Errorf("Error when calling `CdcApi.CreateCdcSink`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down Expand Up @@ -133,7 +133,7 @@ var editCdcSinkCmd = &cobra.Command{

resp, r, err := authApi.EditCdcSink(cdcSinkID).EditCdcSinkRequest(*editCdcSinkRequest).Execute()
if err != nil {
logrus.Errorf("Error when calling `CdcApi.EditCdcSink`: %v", err)
logrus.Errorf("Error when calling `CdcApi.EditCdcSink`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down Expand Up @@ -164,7 +164,7 @@ var deleteCdcSinkCmd = &cobra.Command{

resp, err := authApi.DeleteCdcSink(cdcSinkID).Execute()
if err != nil {
logrus.Errorf("Error when calling `CdcApi.DeleteeCdcSink`: %v", err)
logrus.Errorf("Error when calling `CdcApi.DeleteCdcSink`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", resp)
return
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/cdc_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var getCdcStreamCmd = &cobra.Command{

resp, r, err := authApi.GetCdcStream(clusterID, cdcStreamID).Execute()
if err != nil {
logrus.Errorf("Error when calling `CdcApi.GetCdcStream`: %v", err)
logrus.Errorf("Error when calling `CdcApi.GetCdcStream`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down Expand Up @@ -85,7 +85,7 @@ var createCdcStreamCmd = &cobra.Command{

resp, r, err := authApi.CreateCdcStream(clusterID).CdcStreamSpec(cdcStreamSpec).Execute()
if err != nil {
logrus.Errorf("Error when calling `CdcApi.CreateCdcStream`: %v", err)
logrus.Errorf("Error when calling `CdcApi.CreateCdcStream`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down Expand Up @@ -132,7 +132,7 @@ var editCdcStreamCmd = &cobra.Command{

resp, r, err := authApi.EditCdcStream(clusterID, cdcStreamID).EditCdcStreamRequest(*editCdcStreamRequest).Execute()
if err != nil {
logrus.Errorf("Error when calling `CdcApi.EditCdcStream`: %v", err)
logrus.Errorf("Error when calling `CdcApi.EditCdcStream`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down Expand Up @@ -167,7 +167,7 @@ var deleteCdcStreamCmd = &cobra.Command{
}
resp, err := authApi.DeleteCdcStream(clusterID, cdcStreamID).Execute()
if err != nil {
logrus.Errorf("Error when calling `CdcApi.DeleteCdcStream`: %v", err)
logrus.Errorf("Error when calling `CdcApi.DeleteCdcStream`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", resp)
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cloud_regions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var getCloudRegionsCmd = &cobra.Command{
cloudProvider, _ := cmd.Flags().GetString("cloud-provider")
cloudRegionsResp, resp, err := authApi.GetSupportedCloudRegions().Cloud(cloudProvider).Execute()
if err != nil {
logrus.Errorf("Error when calling `ClusterApi.GetSupportedCloudRegions`: %v", err)
logrus.Errorf("Error when calling `ClusterApi.GetSupportedCloudRegions`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", resp)
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var createClusterCmd = &cobra.Command{

resp, r, err := authApi.CreateCluster().CreateClusterRequest(*createClusterRequest).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `ClusterApi.CreateCluster``: %v", err)
fmt.Fprintf(os.Stderr, "Error when calling `ClusterApi.CreateCluster`: %s", ybmAuthClient.GetApiErrorDetails(err))
fmt.Fprintf(os.Stderr, "Full HTTP response: %v", r)
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/delete_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var deleteClusterCmd = &cobra.Command{

r, err := authApi.DeleteCluster(clusterID).Execute()
if err != nil {
logrus.Errorf("Error when calling `ClusterApi.DeleteCluster`: %v", err)
logrus.Errorf("Error when calling `ClusterApi.DeleteCluster`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/get_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var getClusterCmd = &cobra.Command{
resp, r, err := clusterListRequest.Execute()

if err != nil {
logrus.Errorf("Error when calling `ClusterApi.ListClusters`: %v", err)
logrus.Errorf("Error when calling `ClusterApi.ListClusters`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/instance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var getInstanceTypesCmd = &cobra.Command{
showDisabled, _ := cmd.Flags().GetBool("show-disabled")
instanceTypesResp, resp, err := authApi.GetSupportedInstanceTypes(cloudProvider, tier, cloudRegion).ShowDisabled(showDisabled).Execute()
if err != nil {
logrus.Errorf("Error when calling `ClusterApi.GetSupportedInstanceTypes`: %v", err)
logrus.Errorf("Error when calling `ClusterApi.GetSupportedInstanceTypes`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", resp)
return
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/network_allow_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var getNetworkAllowListCmd = &cobra.Command{
// No option to filter by name :(
resp, r, err := authApi.ListNetworkAllowLists().Execute()
if err != nil {
logrus.Errorf("Error when calling `NetworkApi.ListNetworkAllowLists`: %v", err)
logrus.Errorf("Error when calling `NetworkApi.ListNetworkAllowLists`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down Expand Up @@ -90,7 +90,7 @@ var createNetworkAllowListCmd = &cobra.Command{

resp, r, err := authApi.CreateNetworkAllowList().NetworkAllowListSpec(nalSpec).Execute()
if err != nil {
logrus.Errorf("Error when calling `NetworkApi.ListNetworkAllowLists`: %v", err)
logrus.Errorf("Error when calling `NetworkApi.ListNetworkAllowLists`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down Expand Up @@ -121,7 +121,7 @@ var deleteNetworkAllowListCmd = &cobra.Command{

resp, r, err := authApi.ListNetworkAllowLists().Execute()
if err != nil {
logrus.Errorf("Error when calling `NetworkApi.ListNetworkAllowLists`: %v", err)
logrus.Errorf("Error when calling `NetworkApi.ListNetworkAllowLists`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand All @@ -134,7 +134,7 @@ var deleteNetworkAllowListCmd = &cobra.Command{

r, err = authApi.DeleteNetworkAllowList(allowList.Info.Id).Execute()
if err != nil {
logrus.Errorf("Error when calling `NetworkApi.DeleteNetworkAllowList`: %v", err)
logrus.Errorf("Error when calling `NetworkApi.DeleteNetworkAllowList`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/pause_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var pauseClusterCmd = &cobra.Command{
}
resp, r, err := authApi.PauseCluster(clusterID).Execute()
if err != nil {
logrus.Errorf("Error when calling `ClusterApi.PauseCluster`: %v", err)
logrus.Errorf("Error when calling `ClusterApi.PauseCluster`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/read_replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ var getReadReplicaCmd = &cobra.Command{
}
resp, r, err := authApi.ListReadReplicas(clusterID).Execute()
if err != nil {
logrus.Errorf("Error when calling `ReadReplicaApi.ListReadReplicas`: %v", err)
logrus.Errorf("Error when calling `ReadReplicaApi.ListReadReplicas`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down Expand Up @@ -147,7 +147,7 @@ var createReadReplicaCmd = &cobra.Command{

resp, r, err := authApi.CreateReadReplica(clusterID).ReadReplicaSpec(readReplicaSpecs).Execute()
if err != nil {
logrus.Errorf("Error when calling `ReadReplicaApi.CreateReadReplica`: %v", err)
logrus.Errorf("Error when calling `ReadReplicaApi.CreateReadReplica`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down Expand Up @@ -180,7 +180,7 @@ var updateReadReplicaCmd = &cobra.Command{

resp, r, err := authApi.EditReadReplicas(clusterID).ReadReplicaSpec(readReplicaSpecs).Execute()
if err != nil {
logrus.Errorf("Error when calling `ReadReplicaApi.EditReadReplicas`: %v", err)
logrus.Errorf("Error when calling `ReadReplicaApi.EditReadReplicas`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand All @@ -207,7 +207,7 @@ var deleteReadReplicaCmd = &cobra.Command{
}
r, err := authApi.DeleteReadReplica(clusterID).Execute()
if err != nil {
logrus.Errorf("Error when calling `ReadReplicaApi.DeleteReadReplica`: %v", err)
logrus.Errorf("Error when calling `ReadReplicaApi.DeleteReadReplica`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/resume_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var resumeClusterCmd = &cobra.Command{
}
resp, r, err := authApi.ResumeCluster(clusterID).Execute()
if err != nil {
logrus.Errorf("Error when calling `ClusterApi.ResumeCluster`: %v", err)
logrus.Errorf("Error when calling `ClusterApi.ResumeCluster`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/update_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var updateClusterCmd = &cobra.Command{
}
resp, r, err := authApi.GetCluster(clusterID).Execute()
if err != nil {
logrus.Errorf("Error when calling `ClusterApi.GetCluster`: %v", err)
logrus.Errorf("Error when calling `ClusterApi.GetCluster`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand All @@ -45,14 +45,14 @@ var updateClusterCmd = &cobra.Command{
trackID := originalSpec.SoftwareInfo.GetTrackId()
trackName, err := authApi.GetTrackNameById(trackID)
if err != nil {
logrus.Errorf("Error when calling `getTrackName`: %v", err)
logrus.Errorf("Error when calling `getTrackName`: %s", ybmAuthClient.GetApiErrorDetails(err))
return
}
vpcName := ""
if vpcID, ok := originalSpec.ClusterRegionInfo[0].PlacementInfo.GetVpcIdOk(); ok {
vpcName, err = authApi.GetVpcNameById(*vpcID)
if err != nil {
logrus.Errorf("Error when calling `getVpcName`: %v", err)
logrus.Errorf("Error when calling `getVpcName`: %s", ybmAuthClient.GetApiErrorDetails(err))
return
}
}
Expand Down Expand Up @@ -93,7 +93,7 @@ var updateClusterCmd = &cobra.Command{

resp, r, err = authApi.EditCluster(clusterID).ClusterSpec(*clusterSpec).Execute()
if err != nil {
logrus.Errorf("Error when calling `ClusterApi.UpdateCluster`: %v", err)
logrus.Errorf("Error when calling `ClusterApi.UpdateCluster`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var getVpcCmd = &cobra.Command{
resp, r, err := vpcListRequest.Execute()

if err != nil {
logrus.Errorf("Error when calling `NetworkApi.ListSingleTenantVpcs`: %v", err)
logrus.Errorf("Error when calling `NetworkApi.ListSingleTenantVpcs`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down Expand Up @@ -108,7 +108,7 @@ var createVpcCmd = &cobra.Command{
authApi.GetInfo("", "")
resp, r, err := authApi.CreateVpc().SingleTenantVpcRequest(vpcRequest).Execute()
if err != nil {
logrus.Errorf("Error when calling `NetworkApi.CreateVpc`: %v", err)
logrus.Errorf("Error when calling `NetworkApi.CreateVpc`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down Expand Up @@ -147,7 +147,7 @@ var deleteVpcCmd = &cobra.Command{
}
r, err := authApi.DeleteVpc(vpcId).Execute()
if err != nil {
logrus.Errorf("Error when calling `NetworkApi.DeleteVpc``: %v", err)
logrus.Errorf("Error when calling `NetworkApi.DeleteVpc`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Debugf("Full HTTP response: %v", r)
return
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/vpc_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var getVpcPeeringCmd = &cobra.Command{
resp, r, err := authApi.ListVpcPeerings().Execute()

if err != nil {
logrus.Errorf("Error when calling `NetworkApi.ListVpcPeerings``: %v", err)
logrus.Errorf("Error when calling `NetworkApi.ListVpcPeerings`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Errorf("Full HTTP response: %v", r)
return
}
Expand Down Expand Up @@ -144,7 +144,7 @@ var createVpcPeeringCmd = &cobra.Command{
vpcPeeringSpec := *ybmclient.NewVpcPeeringSpec(ybVpcId, vpcPeeringName, *applicationVPCSpec)
vpcPeeringResp, response, err := authApi.CreateVpcPeering().VpcPeeringSpec(vpcPeeringSpec).Execute()
if err != nil {
logrus.Errorf("Error when calling `NetworkApi.CreateVpcPeering``: %v", err)
logrus.Errorf("Error when calling `NetworkApi.CreateVpcPeering`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Errorf("Full HTTP response: %v", response)
return
}
Expand Down Expand Up @@ -176,7 +176,7 @@ var deleteVpcPeeringCmd = &cobra.Command{
resp, r, err := authApi.ListVpcPeerings().Execute()

if err != nil {
logrus.Errorf("Error when calling `NetworkApi.ListVpcPeerings``: %v", err)
logrus.Errorf("Error when calling `NetworkApi.ListVpcPeerings`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Errorf("Full HTTP response: %v", r)
return
}
Expand All @@ -191,7 +191,7 @@ var deleteVpcPeeringCmd = &cobra.Command{

response, err := authApi.DeleteVpcPeering(vpcPeeringId).Execute()
if err != nil {
logrus.Errorf("Error when calling `NetworkApi.ListVpcPeerings``: %v", err)
logrus.Errorf("Error when calling `NetworkApi.ListVpcPeerings`: %s", ybmAuthClient.GetApiErrorDetails(err))
logrus.Errorf("Full HTTP response: %v", response)
return
}
Expand Down
27 changes: 27 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -574,6 +575,32 @@ func getFromNodeConfig(resource string, numCores int32, nodeConfigList []ybmclie
return 0, fmt.Errorf("node with the given number of CPU cores doesn't exist in the given region")
}

// Utils functions

// GetApiErrorDetails will return the api Error message if present
// If not present will return the original err.Error()
func GetApiErrorDetails(err error) string {
switch castedError := err.(type) {
case ybmclient.GenericOpenAPIError:
if v := getAPIError(castedError.Body()); v != nil {
if d, ok := v.GetErrorOk(); ok {
return fmt.Sprintf("%s-%s", err.Error(), d.GetDetail())
}
}
}
return err.Error()

}

func getAPIError(b []byte) *ybmclient.ApiError {
apiError := ybmclient.NewApiErrorWithDefaults()
err := json.Unmarshal(b, &apiError)
if err != nil {
return nil
}
return apiError
}

func parseURL(host string) (*url.URL, error) {
endpoint, err := url.Parse(host)
if err != nil {
Expand Down