From 5c952fbac96ebb4a7d328b30d94d11349d4c8b92 Mon Sep 17 00:00:00 2001 From: Sharad Kesarwani <108344822+sharadkesarwani@users.noreply.github.com> Date: Tue, 14 Jan 2025 16:58:24 +0530 Subject: [PATCH 1/5] Supported setCredentials for azure. --- .../providers/azure/create/main.go | 31 ++++ .../providers/azure/read/main.go | 25 +++ service/account/account.go | 8 + .../providers/azure/azurecredentials.go | 159 ++++++++++++++++++ service/account/providers/azure/service.go | 35 ++++ 5 files changed, 258 insertions(+) create mode 100644 examples/service/setcredentails/providers/azure/create/main.go create mode 100644 examples/service/setcredentails/providers/azure/read/main.go create mode 100644 service/account/providers/azure/azurecredentials.go create mode 100644 service/account/providers/azure/service.go diff --git a/examples/service/setcredentails/providers/azure/create/main.go b/examples/service/setcredentails/providers/azure/create/main.go new file mode 100644 index 00000000..92425291 --- /dev/null +++ b/examples/service/setcredentails/providers/azure/create/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "context" + "github.com/spotinst/spotinst-sdk-go/service/account/providers/azure" + "github.com/spotinst/spotinst-sdk-go/spotinst" + "log" + + "github.com/spotinst/spotinst-sdk-go/service/account" + "github.com/spotinst/spotinst-sdk-go/spotinst/session" +) + +func main() { + sess := session.New() + svc := account.New(sess) + ctx := context.Background() + _, err := svc.CloudProviderAzure().SetCredentials(ctx, &azure.SetCredentialsInput{ + Credentials: &azure.Credentials{ + AccountId: spotinst.String("accountId"), + ClientId: spotinst.String("clientId"), + ClientSecret: spotinst.String("clientSecret"), + TenantId: spotinst.String("tenantId"), + SubscriptionId: spotinst.String("subscriptionId"), + }, + }) + + if err != nil { + log.Fatalf("spotinst: failed to set credential: %v", err) + } + +} diff --git a/examples/service/setcredentails/providers/azure/read/main.go b/examples/service/setcredentails/providers/azure/read/main.go new file mode 100644 index 00000000..4ff2a17b --- /dev/null +++ b/examples/service/setcredentails/providers/azure/read/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "context" + "github.com/spotinst/spotinst-sdk-go/service/account/providers/azure" + "log" + + "github.com/spotinst/spotinst-sdk-go/service/account" + "github.com/spotinst/spotinst-sdk-go/spotinst" + "github.com/spotinst/spotinst-sdk-go/spotinst/session" +) + +func main() { + sess := session.New() + svc := account.New(sess) + ctx := context.Background() + _, err := svc.CloudProviderAzure().ReadCredentials(ctx, &azure.ReadCredentialsInput{ + AccountId: spotinst.String("act-123456"), + }) + + if err != nil { + log.Fatalf("spotinst: failed to fetch credential: %v", err) + } + +} diff --git a/service/account/account.go b/service/account/account.go index e77f3b50..208eddfa 100644 --- a/service/account/account.go +++ b/service/account/account.go @@ -2,6 +2,7 @@ package account import ( "github.com/spotinst/spotinst-sdk-go/service/account/providers/aws" + "github.com/spotinst/spotinst-sdk-go/service/account/providers/azure" "github.com/spotinst/spotinst-sdk-go/service/account/providers/common" "github.com/spotinst/spotinst-sdk-go/service/account/providers/gcp" "github.com/spotinst/spotinst-sdk-go/spotinst" @@ -15,6 +16,7 @@ import ( type Service interface { CloudProviderAWS() aws.Service CloudProviderGCP() gcp.Service + CloudProviderAzure() azure.Service CloudProviderCommon() common.Service } @@ -46,6 +48,12 @@ func (s *ServiceOp) CloudProviderGCP() gcp.Service { } } +func (s *ServiceOp) CloudProviderAzure() azure.Service { + return &azure.ServiceOp{ + Client: s.Client, + } +} + func (s *ServiceOp) CloudProviderCommon() common.Service { return &common.ServiceOp{ Client: s.Client, diff --git a/service/account/providers/azure/azurecredentials.go b/service/account/providers/azure/azurecredentials.go new file mode 100644 index 00000000..149d51b9 --- /dev/null +++ b/service/account/providers/azure/azurecredentials.go @@ -0,0 +1,159 @@ +package azure + +import ( + "context" + "encoding/json" + "github.com/spotinst/spotinst-sdk-go/spotinst" + "github.com/spotinst/spotinst-sdk-go/spotinst/client" + "github.com/spotinst/spotinst-sdk-go/spotinst/util/jsonutil" + "io/ioutil" + "net/http" +) + +type Credentials struct { + AccountId *string `json:"accountId,omitempty"` + ClientId *string `json:"clientId,omitempty"` + ClientSecret *string `json:"clientSecret,omitempty"` + TenantId *string `json:"tenantId,omitempty"` + SubscriptionId *string `json:"subscriptionId,omitempty"` + + forceSendFields []string + + nullFields []string +} + +func (o Credentials) MarshalJSON() ([]byte, error) { + type noMethod Credentials + raw := noMethod(o) + return jsonutil.MarshalJSON(raw, o.forceSendFields, o.nullFields) +} + +func (o *Credentials) SetClientId(v *string) *Credentials { + if o.ClientId = v; o.ClientId == nil { + o.nullFields = append(o.nullFields, "ClientId") + } + return o +} + +func (o *Credentials) SetClientSecret(v *string) *Credentials { + if o.ClientSecret = v; o.ClientSecret == nil { + o.nullFields = append(o.nullFields, "ClientSecret") + } + return o +} + +func (o *Credentials) SetTenantId(v *string) *Credentials { + if o.TenantId = v; o.TenantId == nil { + o.nullFields = append(o.nullFields, "TenantId") + } + return o +} + +func (o *Credentials) SetSubscriptionId(v *string) *Credentials { + if o.SubscriptionId = v; o.SubscriptionId == nil { + o.nullFields = append(o.nullFields, "SubscriptionId") + } + return o +} + +type SetCredentialsInput struct { + Credentials *Credentials `json:"credentials,omitempty"` +} +type SetCredentialsOutput struct { + Credentials *Credentials `json:"Credentials,omitempty"` +} + +func (s *ServiceOp) SetCredentials(ctx context.Context, input *SetCredentialsInput) (*SetCredentialsOutput, error) { + r := client.NewRequest(http.MethodPost, "/azure/setup/credentials") + + if input != nil { + r.Params.Set("accountId", spotinst.StringValue(input.Credentials.AccountId)) + } + input.Credentials.AccountId = nil + r.Obj = input.Credentials + + resp, err := client.RequireOK(s.Client.DoOrg(ctx, r)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + gs, err := credentialsFromHttpResponse(resp) + if err != nil { + return nil, err + } + + output := new(SetCredentialsOutput) + if len(gs) > 0 { + output.Credentials = gs[0] + } + + return output, nil +} + +type ReadCredentialsInput struct { + AccountId *string `json:"accountId,omitempty"` +} +type ReadCredentialsOutput struct { + Credentials *Credentials `json:"Credentials,omitempty"` +} + +func (s *ServiceOp) ReadCredentials(ctx context.Context, input *ReadCredentialsInput) (*ReadCredentialsOutput, error) { + r := client.NewRequest(http.MethodGet, "/azure/setup/credentials") + if input != nil { + r.Params.Set("accountId", spotinst.StringValue(input.AccountId)) + } + + resp, err := client.RequireOK(s.Client.DoOrg(ctx, r)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + gs, err := credentialsFromHttpResponse(resp) + if err != nil { + return nil, err + } + + output := new(ReadCredentialsOutput) + if len(gs) > 0 { + output.Credentials = gs[0] + } + + return output, nil +} + +func credentialsFromHttpResponse(resp *http.Response) ([]*Credentials, error) { + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + return credentialsFromJSON(body) +} + +func credentialsFromJSON(in []byte) ([]*Credentials, error) { + var rw client.Response + if err := json.Unmarshal(in, &rw); err != nil { + return nil, err + } + out := make([]*Credentials, len(rw.Response.Items)) + if len(out) == 0 { + return out, nil + } + for i, rb := range rw.Response.Items { + b, err := credentialFromJSON(rb) + if err != nil { + return nil, err + } + out[i] = b + } + return out, nil +} + +func credentialFromJSON(in []byte) (*Credentials, error) { + b := new(Credentials) + if err := json.Unmarshal(in, b); err != nil { + return nil, err + } + return b, nil +} diff --git a/service/account/providers/azure/service.go b/service/account/providers/azure/service.go new file mode 100644 index 00000000..08a2bbd9 --- /dev/null +++ b/service/account/providers/azure/service.go @@ -0,0 +1,35 @@ +package azure + +import ( + "context" + + "github.com/spotinst/spotinst-sdk-go/spotinst" + "github.com/spotinst/spotinst-sdk-go/spotinst/client" + "github.com/spotinst/spotinst-sdk-go/spotinst/session" +) + +// Service provides the API operation methods for making requests to endpoints +// of the Spotinst API. See this package's package overview docs for details on +// the service. +type Service interface { + serviceCredentials +} + +type serviceCredentials interface { + SetCredentials(context.Context, *SetCredentialsInput) (*SetCredentialsOutput, error) + ReadCredentials(context.Context, *ReadCredentialsInput) (*ReadCredentialsOutput, error) +} + +type ServiceOp struct { + Client *client.Client +} + +func New(sess *session.Session, cfgs ...*spotinst.Config) *ServiceOp { + cfg := &spotinst.Config{} + cfg.Merge(sess.Config) + cfg.Merge(cfgs...) + + return &ServiceOp{ + Client: client.New(sess.Config), + } +} From da3efd4c4b9cb4bfbd2b1cdf948affb1cd357767 Mon Sep 17 00:00:00 2001 From: Sharad Kesarwani <108344822+sharadkesarwani@users.noreply.github.com> Date: Thu, 16 Jan 2025 00:45:13 +0530 Subject: [PATCH 2/5] added func setAccount for azurecredentials --- service/account/providers/azure/azurecredentials.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/service/account/providers/azure/azurecredentials.go b/service/account/providers/azure/azurecredentials.go index 149d51b9..466ada81 100644 --- a/service/account/providers/azure/azurecredentials.go +++ b/service/account/providers/azure/azurecredentials.go @@ -56,6 +56,13 @@ func (o *Credentials) SetSubscriptionId(v *string) *Credentials { return o } +func (o *Credentials) SetAccountId(v *string) *Credentials { + if o.AccountId = v; o.AccountId == nil { + o.nullFields = append(o.nullFields, "AccountId") + } + return o +} + type SetCredentialsInput struct { Credentials *Credentials `json:"credentials,omitempty"` } From 27d6675107489dd08423e3f60029cb10b8e2645f Mon Sep 17 00:00:00 2001 From: Sharad Kesarwani <108344822+sharadkesarwani@users.noreply.github.com> Date: Wed, 22 Jan 2025 12:35:37 +0530 Subject: [PATCH 3/5] removed extra line spaces --- .../account/providers/azure/azurecredentials.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/service/account/providers/azure/azurecredentials.go b/service/account/providers/azure/azurecredentials.go index 466ada81..842e553a 100644 --- a/service/account/providers/azure/azurecredentials.go +++ b/service/account/providers/azure/azurecredentials.go @@ -11,15 +11,13 @@ import ( ) type Credentials struct { - AccountId *string `json:"accountId,omitempty"` - ClientId *string `json:"clientId,omitempty"` - ClientSecret *string `json:"clientSecret,omitempty"` - TenantId *string `json:"tenantId,omitempty"` - SubscriptionId *string `json:"subscriptionId,omitempty"` - + AccountId *string `json:"accountId,omitempty"` + ClientId *string `json:"clientId,omitempty"` + ClientSecret *string `json:"clientSecret,omitempty"` + TenantId *string `json:"tenantId,omitempty"` + SubscriptionId *string `json:"subscriptionId,omitempty"` forceSendFields []string - - nullFields []string + nullFields []string } func (o Credentials) MarshalJSON() ([]byte, error) { From 0a028caba3a2877d526619989cf48b7b1c79c121 Mon Sep 17 00:00:00 2001 From: chandra1-n <86221454+chandra1-n@users.noreply.github.com> Date: Thu, 23 Jan 2025 13:52:28 +0530 Subject: [PATCH 4/5] Update azurecredentials.go --- service/account/providers/azure/azurecredentials.go | 1 + 1 file changed, 1 insertion(+) diff --git a/service/account/providers/azure/azurecredentials.go b/service/account/providers/azure/azurecredentials.go index 842e553a..b7a5f3cc 100644 --- a/service/account/providers/azure/azurecredentials.go +++ b/service/account/providers/azure/azurecredentials.go @@ -16,6 +16,7 @@ type Credentials struct { ClientSecret *string `json:"clientSecret,omitempty"` TenantId *string `json:"tenantId,omitempty"` SubscriptionId *string `json:"subscriptionId,omitempty"` + forceSendFields []string nullFields []string } From 22e466a59b73beece7db8e76566e98d277bf9391 Mon Sep 17 00:00:00 2001 From: Sharad Kesarwani <108344822+sharadkesarwani@users.noreply.github.com> Date: Thu, 23 Jan 2025 14:06:31 +0530 Subject: [PATCH 5/5] file formatted --- service/account/providers/azure/azurecredentials.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/service/account/providers/azure/azurecredentials.go b/service/account/providers/azure/azurecredentials.go index b7a5f3cc..8760e74d 100644 --- a/service/account/providers/azure/azurecredentials.go +++ b/service/account/providers/azure/azurecredentials.go @@ -11,12 +11,12 @@ import ( ) type Credentials struct { - AccountId *string `json:"accountId,omitempty"` - ClientId *string `json:"clientId,omitempty"` - ClientSecret *string `json:"clientSecret,omitempty"` - TenantId *string `json:"tenantId,omitempty"` - SubscriptionId *string `json:"subscriptionId,omitempty"` - + AccountId *string `json:"accountId,omitempty"` + ClientId *string `json:"clientId,omitempty"` + ClientSecret *string `json:"clientSecret,omitempty"` + TenantId *string `json:"tenantId,omitempty"` + SubscriptionId *string `json:"subscriptionId,omitempty"` + forceSendFields []string nullFields []string }