From 61509482aa65ed7ff96d8d5611e7cf41f435b539 Mon Sep 17 00:00:00 2001 From: nikolay-spectro Date: Fri, 17 Nov 2023 22:32:48 -0800 Subject: [PATCH 1/2] PLT-824: AWS cloud account validation feature. --- client/account_aws.go | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/client/account_aws.go b/client/account_aws.go index ce50674c..b7ba03c3 100644 --- a/client/account_aws.go +++ b/client/account_aws.go @@ -2,16 +2,34 @@ package client import ( hapitransport "github.com/spectrocloud/hapi/apiutil/transport" + cloudC "github.com/spectrocloud/hapi/cloud/client/v1" "github.com/spectrocloud/hapi/models" clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" ) +func toV1AwsCloudAccount(account *models.V1AwsAccount) *models.V1AwsCloudAccount { + return &models.V1AwsCloudAccount{ + AccessKey: account.Spec.AccessKey, + CredentialType: account.Spec.CredentialType, + Partition: account.Spec.Partition, + PolicyARNs: account.Spec.PolicyARNs, + SecretKey: account.Spec.SecretKey, + Sts: account.Spec.Sts, + } +} + func (h *V1Client) CreateCloudAccountAws(account *models.V1AwsAccount, AccountContext string) (string, error) { client, err := h.GetClusterClient() if err != nil { return "", err } + // validate account + err = validateCloudAccountAws(account, h) + if err != nil { + return "", err + } + var params *clusterC.V1CloudAccountsAwsCreateParams switch AccountContext { case "project": @@ -28,12 +46,42 @@ func (h *V1Client) CreateCloudAccountAws(account *models.V1AwsAccount, AccountCo return *success.Payload.UID, nil } +func validateCloudAccountAws(account *models.V1AwsAccount, h *V1Client) error { + client, err := h.GetCloudClient() + if err != nil { + return err + } + + PcgId := account.Metadata.Annotations[OverlordUID] + // check PCG + err = h.CheckPCG(PcgId) + if err != nil { + return err + } + + // validate account + paramsValidate := cloudC.NewV1AwsAccountValidateParams() + paramsValidate = paramsValidate.WithAwsCloudAccount(toV1AwsCloudAccount(account)) + _, err = client.V1AwsAccountValidate(paramsValidate) + if err != nil { + return err + } + + return nil +} + func (h *V1Client) UpdateCloudAccountAws(account *models.V1AwsAccount) error { client, err := h.GetClusterClient() if err != nil { return err } + // validate account + err = validateCloudAccountAws(account, h) + if err != nil { + return err + } + uid := account.Metadata.UID params := clusterC.NewV1CloudAccountsAwsUpdateParamsWithContext(h.Ctx).WithUID(uid).WithBody(account) _, err = client.V1CloudAccountsAwsUpdate(params) From 55b4376a7c063259b5bf6994912f9fc44a29ddfd Mon Sep 17 00:00:00 2001 From: nikolay-spectro Date: Sat, 18 Nov 2023 15:04:13 -0800 Subject: [PATCH 2/2] PLT-825: Azure cloud account validation feature. --- client/account_azure.go | 54 +++++++++++++++++++++++++++++++++- client/account_gcp.go | 5 +++- client/account_maas.go | 5 +++- client/account_openstack.go | 5 +++- client/account_tke.go | 5 +++- client/application.go | 9 ++++-- client/application_profile.go | 14 ++++++--- client/cluster.go | 9 ++++-- client/cluster_aks.go | 4 ++- client/cluster_azure.go | 5 +++- client/cluster_coxedge.go | 5 +++- client/cluster_edge.go | 5 +++- client/cluster_edge_native.go | 5 +++- client/cluster_edge_vsphere.go | 5 +++- client/cluster_eks.go | 4 ++- client/cluster_gcp.go | 5 +++- client/cluster_group.go | 9 ++++-- client/cluster_libvirt.go | 5 +++- client/cluster_maas.go | 5 +++- client/cluster_tke.go | 5 +++- client/cluster_virtual.go | 5 +++- client/project.go | 4 ++- client/workspace.go | 9 ++++-- 23 files changed, 153 insertions(+), 33 deletions(-) diff --git a/client/account_azure.go b/client/account_azure.go index d78dc576..39ba77e9 100644 --- a/client/account_azure.go +++ b/client/account_azure.go @@ -1,17 +1,37 @@ package client import ( + "errors" + "github.com/spectrocloud/hapi/apiutil/transport" + cloudC "github.com/spectrocloud/hapi/cloud/client/v1" "github.com/spectrocloud/hapi/models" clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" ) +func toV1AzureCloudAccount(account *models.V1AzureAccount) *models.V1AzureCloudAccount { + return &models.V1AzureCloudAccount{ + AzureEnvironment: account.Spec.AzureEnvironment, + ClientID: account.Spec.ClientID, + ClientSecret: account.Spec.ClientSecret, + Settings: account.Spec.Settings, + TenantID: account.Spec.TenantID, + TenantName: account.Spec.TenantName, + } +} + func (h *V1Client) CreateCloudAccountAzure(account *models.V1AzureAccount, AccountContext string) (string, error) { client, err := h.GetClusterClient() if err != nil { return "", err } + // validate account + err = validateCloudAccountAzure(account, h) + if err != nil { + return "", err + } + var params *clusterC.V1CloudAccountsAzureCreateParams switch AccountContext { case "project": @@ -27,12 +47,42 @@ func (h *V1Client) CreateCloudAccountAzure(account *models.V1AzureAccount, Accou return *success.Payload.UID, nil } +func validateCloudAccountAzure(account *models.V1AzureAccount, h *V1Client) error { + client, err := h.GetCloudClient() + if err != nil { + return err + } + + PcgId := account.Metadata.Annotations[OverlordUID] + // check PCG + err = h.CheckPCG(PcgId) + if err != nil { + return err + } + + // validate account + paramsValidate := cloudC.NewV1AzureAccountValidateParams() + paramsValidate = paramsValidate.WithAzureCloudAccount(toV1AzureCloudAccount(account)) + _, err = client.V1AzureAccountValidate(paramsValidate) + if err != nil { + return err + } + + return nil +} + func (h *V1Client) UpdateCloudAccountAzure(account *models.V1AzureAccount) error { client, err := h.GetClusterClient() if err != nil { return nil } + // validate account + err = validateCloudAccountAzure(account, h) + if err != nil { + return err + } + uid := account.Metadata.UID params := clusterC.NewV1CloudAccountsAzureUpdateParamsWithContext(h.Ctx).WithUID(uid).WithBody(account) _, err = client.V1CloudAccountsAzureUpdate(params) @@ -69,8 +119,10 @@ func (h *V1Client) GetCloudAccountAzure(uid, AccountContext string) (*models.V1A case "tenant": params = clusterC.NewV1CloudAccountsAzureGetParams().WithUID(uid) } + success, err := client.V1CloudAccountsAzureGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/account_gcp.go b/client/account_gcp.go index 21ad2aac..a796e0ab 100644 --- a/client/account_gcp.go +++ b/client/account_gcp.go @@ -1,6 +1,8 @@ package client import ( + "errors" + "github.com/spectrocloud/hapi/apiutil/transport" "github.com/spectrocloud/hapi/models" clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" @@ -73,7 +75,8 @@ func (h *V1Client) GetCloudAccountGcp(uid, AccountContext string) (*models.V1Gcp } success, err := client.V1CloudAccountsGcpGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/account_maas.go b/client/account_maas.go index e70a991e..4a892aaa 100644 --- a/client/account_maas.go +++ b/client/account_maas.go @@ -1,6 +1,8 @@ package client import ( + "errors" + "github.com/spectrocloud/hapi/apiutil/transport" "github.com/spectrocloud/hapi/models" clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" @@ -115,7 +117,8 @@ func (h *V1Client) GetCloudAccountMaas(uid, AccountContext string) (*models.V1Ma } success, err := client.V1CloudAccountsMaasGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/account_openstack.go b/client/account_openstack.go index 13ea3c7e..b0dd509e 100644 --- a/client/account_openstack.go +++ b/client/account_openstack.go @@ -1,6 +1,8 @@ package client import ( + "errors" + "github.com/spectrocloud/hapi/apiutil/transport" "github.com/spectrocloud/hapi/models" clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" @@ -114,7 +116,8 @@ func (h *V1Client) GetCloudAccountOpenStack(uid, AccountContext string) (*models } success, err := client.V1CloudAccountsOpenStackGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/account_tke.go b/client/account_tke.go index e70a3929..9fbd2b68 100644 --- a/client/account_tke.go +++ b/client/account_tke.go @@ -1,6 +1,8 @@ package client import ( + "errors" + "github.com/spectrocloud/hapi/apiutil/transport" "github.com/spectrocloud/hapi/models" clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" @@ -73,7 +75,8 @@ func (h *V1Client) GetCloudAccountTke(uid, AccountContext string) (*models.V1Ten } success, err := client.V1CloudAccountsTencentGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/application.go b/client/application.go index 8cc28b0d..34236083 100644 --- a/client/application.go +++ b/client/application.go @@ -1,6 +1,8 @@ package client import ( + "errors" + "github.com/spectrocloud/hapi/apiutil/transport" hashboardC "github.com/spectrocloud/hapi/hashboard/client/v1" "github.com/spectrocloud/hapi/models" @@ -18,7 +20,8 @@ func (h *V1Client) GetApplication(uid string) (*models.V1AppDeployment, error) { params := v1.NewV1AppDeploymentsUIDGetParamsWithContext(h.Ctx).WithUID(uid) success, err := client.V1AppDeploymentsUIDGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err @@ -48,11 +51,13 @@ func (h *V1Client) SearchAppDeploymentSummaries(scope string, filter *models.V1A } resp, err := client.V1DashboardAppDeployments(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err } + return resp.Payload.AppDeployments, nil } diff --git a/client/application_profile.go b/client/application_profile.go index 77f1c59e..6b8c434d 100644 --- a/client/application_profile.go +++ b/client/application_profile.go @@ -72,7 +72,8 @@ func (h *V1Client) GetApplicationProfileTiers(applicationProfileUID string) ([]* params := clusterC.NewV1AppProfilesUIDTiersGetParamsWithContext(h.Ctx).WithUID(applicationProfileUID) success, err := client.V1AppProfilesUIDTiersGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err @@ -96,12 +97,15 @@ func (h *V1Client) GetApplicationProfileTierManifestContent(applicationProfileUI ManifestUID: manifestUID, Context: h.Ctx, } + success, err := client.V1AppProfilesUIDTiersUIDManifestsUIDGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return "", nil } else if err != nil { return "", err } + return success.Payload.Spec.Published.Content, nil } @@ -129,7 +133,8 @@ func (h *V1Client) SearchAppProfileSummaries(scope string, filter *models.V1AppP params.Offset = &resp.Payload.Listmeta.Offset } resp, err = client.V1DashboardAppProfiles(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err @@ -197,7 +202,8 @@ func (h *V1Client) UpdateApplicationProfileTiers(appProfileUID, tierUID string, } _, err = client.V1AppProfilesUIDTiersUIDUpdate(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil } else if err != nil { return err diff --git a/client/cluster.go b/client/cluster.go index fc4b0dec..e6bff6c7 100644 --- a/client/cluster.go +++ b/client/cluster.go @@ -94,7 +94,8 @@ func (h *V1Client) SearchClusterSummaries(clusterContext string, filter *models. } resp, err := client.V1SpectroClustersSearchFilterSummary(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err @@ -118,7 +119,8 @@ func (h *V1Client) listClusters(clusterContext string) ([]*models.V1SpectroClust var limit int64 = 0 params.Limit = &limit resp, err := client.V1SpectroClustersList(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err @@ -140,7 +142,8 @@ func (h *V1Client) listClustersMetadata(clusterContext string) ([]*models.V1Obje params = hashboardC.NewV1SpectroClustersMetadataParams() } resp, err := client.V1SpectroClustersMetadata(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/cluster_aks.go b/client/cluster_aks.go index 551ab123..0993bb06 100644 --- a/client/cluster_aks.go +++ b/client/cluster_aks.go @@ -1,6 +1,7 @@ package client import ( + "errors" "time" "github.com/spectrocloud/hapi/apiutil/transport" @@ -105,7 +106,8 @@ func (h *V1Client) GetCloudConfigAks(configUID, ClusterContext string) (*models. } success, err := client.V1CloudConfigsAksGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/cluster_azure.go b/client/cluster_azure.go index ad04ec83..f830efde 100644 --- a/client/cluster_azure.go +++ b/client/cluster_azure.go @@ -1,6 +1,8 @@ package client import ( + "errors" + "github.com/spectrocloud/hapi/apiutil/transport" "github.com/spectrocloud/hapi/models" clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" @@ -103,7 +105,8 @@ func (h *V1Client) GetCloudConfigAzure(configUID, ClusterContext string) (*model } success, err := client.V1CloudConfigsAzureGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/cluster_coxedge.go b/client/cluster_coxedge.go index b0679f7b..8af22e66 100644 --- a/client/cluster_coxedge.go +++ b/client/cluster_coxedge.go @@ -1,6 +1,8 @@ package client import ( + "errors" + "github.com/spectrocloud/hapi/apiutil/transport" "github.com/spectrocloud/hapi/models" clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" @@ -103,7 +105,8 @@ func (h *V1Client) GetCloudConfigCoxEdge(configUID, ClusterContext string) (*mod } success, err := client.V1CloudConfigsCoxEdgeGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/cluster_edge.go b/client/cluster_edge.go index e4d3b75b..7b1fc2f3 100644 --- a/client/cluster_edge.go +++ b/client/cluster_edge.go @@ -1,6 +1,8 @@ package client import ( + "errors" + "github.com/spectrocloud/hapi/apiutil/transport" "github.com/spectrocloud/hapi/models" clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" @@ -103,7 +105,8 @@ func (h *V1Client) GetCloudConfigEdge(configUID, ClusterContext string) (*models } success, err := client.V1CloudConfigsEdgeGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/cluster_edge_native.go b/client/cluster_edge_native.go index 288606b6..6ba00512 100644 --- a/client/cluster_edge_native.go +++ b/client/cluster_edge_native.go @@ -1,6 +1,8 @@ package client import ( + "errors" + "github.com/spectrocloud/hapi/apiutil/transport" "github.com/spectrocloud/hapi/models" clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" @@ -106,7 +108,8 @@ func (h *V1Client) GetCloudConfigEdgeNative(configUID, ClusterContext string) (* } success, err := client.V1CloudConfigsEdgeNativeGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/cluster_edge_vsphere.go b/client/cluster_edge_vsphere.go index b20d89fe..e88edeb0 100644 --- a/client/cluster_edge_vsphere.go +++ b/client/cluster_edge_vsphere.go @@ -1,6 +1,8 @@ package client import ( + "errors" + "github.com/spectrocloud/hapi/apiutil/transport" "github.com/spectrocloud/hapi/models" clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" @@ -103,7 +105,8 @@ func (h *V1Client) GetCloudConfigEdgeVsphere(configUID, ClusterContext string) ( } success, err := client.V1CloudConfigsVsphereGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/cluster_eks.go b/client/cluster_eks.go index 22d37ad3..52ef059f 100644 --- a/client/cluster_eks.go +++ b/client/cluster_eks.go @@ -1,6 +1,7 @@ package client import ( + "errors" "time" "github.com/spectrocloud/hapi/apiutil/transport" @@ -127,7 +128,8 @@ func (h *V1Client) GetCloudConfigEks(configUID, ClusterContext string) (*models. } success, err := client.V1CloudConfigsEksGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/cluster_gcp.go b/client/cluster_gcp.go index 8510fc6f..40bfe556 100644 --- a/client/cluster_gcp.go +++ b/client/cluster_gcp.go @@ -1,6 +1,8 @@ package client import ( + "errors" + "github.com/spectrocloud/hapi/apiutil/transport" "github.com/spectrocloud/hapi/models" clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" @@ -103,7 +105,8 @@ func (h *V1Client) GetCloudConfigGcp(configUID, ClusterContext string) (*models. } success, err := client.V1CloudConfigsGcpGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/cluster_group.go b/client/cluster_group.go index 400b41c7..711aa76b 100644 --- a/client/cluster_group.go +++ b/client/cluster_group.go @@ -88,7 +88,8 @@ func (h *V1Client) GetClusterGroupWithoutStatus(uid, scope string) (*models.V1Cl return nil, errors.New("invalid scope " + scope) } success, err := client.V1ClusterGroupsUIDGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err @@ -155,7 +156,8 @@ func (h *V1Client) GetClusterGroupSummaries(clusterGroupContext string) ([]*mode } resp, err := client.V1ClusterGroupsHostClusterSummary(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err @@ -248,7 +250,8 @@ func (h *V1Client) getClusterGroupMetadata(clusterGroupContext string) ([]*model } resp, err := client.V1ClusterGroupsHostClusterMetadata(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/cluster_libvirt.go b/client/cluster_libvirt.go index e86e26f5..47efbe90 100644 --- a/client/cluster_libvirt.go +++ b/client/cluster_libvirt.go @@ -1,6 +1,8 @@ package client import ( + "errors" + "github.com/spectrocloud/hapi/apiutil/transport" "github.com/spectrocloud/hapi/models" clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" @@ -103,7 +105,8 @@ func (h *V1Client) GetCloudConfigLibvirt(configUID, ClusterContext string) (*mod } success, err := client.V1CloudConfigsLibvirtGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/cluster_maas.go b/client/cluster_maas.go index c3a79acb..aec95390 100644 --- a/client/cluster_maas.go +++ b/client/cluster_maas.go @@ -1,6 +1,8 @@ package client import ( + "errors" + "github.com/spectrocloud/hapi/apiutil/transport" "github.com/spectrocloud/hapi/models" clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" @@ -109,7 +111,8 @@ func (h *V1Client) GetCloudConfigMaas(configUID, ClusterContext string) (*models } success, err := client.V1CloudConfigsMaasGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/cluster_tke.go b/client/cluster_tke.go index 42593ff0..6963e5e9 100644 --- a/client/cluster_tke.go +++ b/client/cluster_tke.go @@ -1,6 +1,8 @@ package client import ( + "errors" + "github.com/spectrocloud/hapi/apiutil/transport" "github.com/spectrocloud/hapi/models" clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" @@ -103,7 +105,8 @@ func (h *V1Client) GetCloudConfigTke(configUID, ClusterContext string) (*models. } success, err := client.V1CloudConfigsTkeGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/cluster_virtual.go b/client/cluster_virtual.go index 151a0490..e5ccbdba 100644 --- a/client/cluster_virtual.go +++ b/client/cluster_virtual.go @@ -1,6 +1,8 @@ package client import ( + "errors" + "github.com/spectrocloud/hapi/apiutil/transport" "github.com/spectrocloud/hapi/models" clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" @@ -78,7 +80,8 @@ func (h *V1Client) GetCloudConfigVirtual(configUID string) (*models.V1VirtualClo params := clusterC.NewV1CloudConfigsVirtualGetParamsWithContext(h.Ctx).WithConfigUID(configUID) success, err := client.V1CloudConfigsVirtualGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err diff --git a/client/project.go b/client/project.go index 733632e7..8abde1c8 100644 --- a/client/project.go +++ b/client/project.go @@ -1,6 +1,7 @@ package client import ( + "errors" "fmt" "github.com/spectrocloud/hapi/apiutil/transport" @@ -68,7 +69,8 @@ func (h *V1Client) GetProjects() (*models.V1ProjectsMetadata, error) { projects, err := client.V1ProjectsMetadata(params) if err != nil || projects == nil { // to support 2.6 projects list - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { limit := int64(0) userClient, err := h.GetUserClient() if err != nil { diff --git a/client/workspace.go b/client/workspace.go index 6994eba8..9328232a 100644 --- a/client/workspace.go +++ b/client/workspace.go @@ -32,7 +32,8 @@ func (h *V1Client) GetWorkspace(uid string) (*models.V1Workspace, error) { params := clusterC.NewV1WorkspacesUIDGetParamsWithContext(h.Ctx).WithUID(uid) success, err := client.V1WorkspacesUIDGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err @@ -51,7 +52,8 @@ func (h *V1Client) GetWorkspaceByName(name string) (*models.V1DashboardWorkspace params := hashboardC.NewV1DashboardWorkspacesListParamsWithContext(h.Ctx) success, err := client.V1DashboardWorkspacesList(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err @@ -124,7 +126,8 @@ func (h *V1Client) GetWorkspaceBackup(uid string) (*models.V1WorkspaceBackup, er params := clusterC.NewV1WorkspaceOpsBackupGetParams().WithContext(h.Ctx).WithUID(uid) success, err := client.V1WorkspaceOpsBackupGet(params) - if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 { + var e *transport.TransportError + if errors.As(err, &e) && e.HttpCode == 404 { return nil, nil } else if err != nil { return nil, err