From a5d9a3b6241948e35599c6e97dd886ad93ced61c Mon Sep 17 00:00:00 2001 From: nikolay-spectro Date: Tue, 21 Nov 2023 21:21:38 -0800 Subject: [PATCH] PLT-827: Support for cloud account import API. --- client/account.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 2 ++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 client/account.go diff --git a/client/account.go b/client/account.go new file mode 100644 index 00000000..7a76a447 --- /dev/null +++ b/client/account.go @@ -0,0 +1,53 @@ +package client + +import ( + "errors" + "fmt" + + "github.com/spectrocloud/hapi/apiutil/transport" + "github.com/spectrocloud/hapi/models" + clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1" +) + +func (h *V1Client) ListCloudAccounts(scope string) ([]*models.V1CloudAccountSummary, error) { + client, err := h.GetClusterClient() + if err != nil { + return nil, err + } + + var params *clusterC.V1CloudAccountsListSummaryParams + switch scope { + case "project": + params = clusterC.NewV1CloudAccountsListSummaryParams().WithContext(h.Ctx) + case "tenant": + params = clusterC.NewV1CloudAccountsListSummaryParams() + + } + var limit int64 = 0 + params.Limit = &limit + resp, err := client.V1CloudAccountsListSummary(params) + + 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.Items, nil +} + +func (h *V1Client) GetCloudAccount(scope, id string) (*models.V1CloudAccountSummary, error) { + accounts, err := h.ListCloudAccounts(scope) + if err != nil { + return nil, err + } + + for _, account := range accounts { + if account.Metadata.UID == id { + return account, nil + } + } + + return nil, fmt.Errorf("account not found with id %s", id) +} diff --git a/go.mod b/go.mod index bfaf926e..744e5d93 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/go-openapi/runtime v0.19.24 github.com/go-openapi/strfmt v0.20.0 github.com/pkg/errors v0.9.1 - github.com/spectrocloud/hapi v1.14.1-0.20231107061715-d18e1e78daf8 + github.com/spectrocloud/hapi v1.14.1-0.20231120161727-f0d8bd27c6ef github.com/stretchr/testify v1.7.0 ) diff --git a/go.sum b/go.sum index 67ab3ed6..32fddbd5 100644 --- a/go.sum +++ b/go.sum @@ -210,6 +210,8 @@ github.com/spectrocloud/gomi v1.14.1-0.20230412095143-b0595c6c6f08 h1:AnOC0U+Exl github.com/spectrocloud/gomi v1.14.1-0.20230412095143-b0595c6c6f08/go.mod h1:UnhUDpFEvtYh6m384r3xzj8/+Z6/hMp2O8whEMYVHec= github.com/spectrocloud/hapi v1.14.1-0.20231107061715-d18e1e78daf8 h1:vSU7q/fj0vmQIh5R/+m3o5a3ppcCbCCbB34JYveM5n4= github.com/spectrocloud/hapi v1.14.1-0.20231107061715-d18e1e78daf8/go.mod h1:Krk9mzbO1MnUTLvvzztBYpOWCFunIlSDWSGlsLh1vkk= +github.com/spectrocloud/hapi v1.14.1-0.20231120161727-f0d8bd27c6ef h1:ROS17WffaRM8yVPVYLub1MqLeeN9Jt5f/zXNwCSypjo= +github.com/spectrocloud/hapi v1.14.1-0.20231120161727-f0d8bd27c6ef/go.mod h1:Krk9mzbO1MnUTLvvzztBYpOWCFunIlSDWSGlsLh1vkk= github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=