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

PLT-825: Azure cloud account validation feature. #69

Merged
merged 2 commits into from
Nov 22, 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
48 changes: 48 additions & 0 deletions client/account_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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)
Expand Down
54 changes: 53 additions & 1 deletion client/account_azure.go
Original file line number Diff line number Diff line change
@@ -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":
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion client/account_gcp.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion client/account_maas.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion client/account_openstack.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion client/account_tke.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions client/application.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
14 changes: 10 additions & 4 deletions client/application_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion client/cluster_aks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"errors"
"time"

"github.com/spectrocloud/hapi/apiutil/transport"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion client/cluster_azure.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
Loading