From 8a142de2f18ee627ad8c7c9ab5886f6bdc15f6e2 Mon Sep 17 00:00:00 2001 From: sharadkesarwani Date: Wed, 13 Sep 2023 01:30:55 +0530 Subject: [PATCH 01/14] added support for create, read and delete account added support for create, read and delete account --- examples/service/account/create/main.go | 34 +++++ examples/service/account/delete/main.go | 24 ++++ examples/service/account/read/main.go | 31 +++++ service/account/account.go | 37 +++++ service/account/providers/aws/account.go | 169 +++++++++++++++++++++++ service/account/providers/aws/service.go | 36 +++++ 6 files changed, 331 insertions(+) create mode 100644 examples/service/account/create/main.go create mode 100644 examples/service/account/delete/main.go create mode 100644 examples/service/account/read/main.go create mode 100644 service/account/account.go create mode 100644 service/account/providers/aws/account.go create mode 100644 service/account/providers/aws/service.go diff --git a/examples/service/account/create/main.go b/examples/service/account/create/main.go new file mode 100644 index 00000000..81874ff8 --- /dev/null +++ b/examples/service/account/create/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "context" + "github.com/spotinst/spotinst-sdk-go/service/account" + "github.com/spotinst/spotinst-sdk-go/service/account/providers/aws" + "github.com/spotinst/spotinst-sdk-go/spotinst" + "github.com/spotinst/spotinst-sdk-go/spotinst/session" + "github.com/spotinst/spotinst-sdk-go/spotinst/util/stringutil" + "log" +) + +func main() { + sess := session.New() + svc := account.New(sess) + ctx := context.Background() + out, err := svc.CloudProviderAWS().CreateAccount(ctx, &aws.CreateAccountInput{ + &aws.Account{ + Name: spotinst.String("testTerraformAcct_123"), + }, + }) + + if err != nil { + log.Fatalf("spotinst: failed to create account: %v", err) + } + + // Output. + if out.Account != nil { + log.Printf("Account %q: %s", + spotinst.StringValue(out.Account.ID), + stringutil.Stringify(out.Account)) + } + +} diff --git a/examples/service/account/delete/main.go b/examples/service/account/delete/main.go new file mode 100644 index 00000000..ed006a5f --- /dev/null +++ b/examples/service/account/delete/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "context" + "github.com/spotinst/spotinst-sdk-go/service/account" + "github.com/spotinst/spotinst-sdk-go/service/account/providers/aws" + "github.com/spotinst/spotinst-sdk-go/spotinst" + "github.com/spotinst/spotinst-sdk-go/spotinst/session" + "log" +) + +func main() { + sess := session.New() + svc := account.New(sess) + ctx := context.Background() + _, err := svc.CloudProviderAWS().DeleteAccount(ctx, &aws.DeleteAccountInput{ + spotinst.String("act-7926f067"), + }) + + if err != nil { + log.Fatalf("spotinst: failed to delete account: %v", err) + } + +} diff --git a/examples/service/account/read/main.go b/examples/service/account/read/main.go new file mode 100644 index 00000000..6885ddb8 --- /dev/null +++ b/examples/service/account/read/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "context" + "github.com/spotinst/spotinst-sdk-go/service/account" + "github.com/spotinst/spotinst-sdk-go/service/account/providers/aws" + "github.com/spotinst/spotinst-sdk-go/spotinst" + "github.com/spotinst/spotinst-sdk-go/spotinst/session" + "github.com/spotinst/spotinst-sdk-go/spotinst/util/stringutil" + "log" +) + +func main() { + sess := session.New() + svc := account.New(sess) + ctx := context.Background() + out, err := svc.CloudProviderAWS().ReadAccount(ctx, &aws.ReadAccountInput{ + AccountID: spotinst.String("act-19c96d38"), + }) + + if err != nil { + log.Fatalf("spotinst: failed to fetch account: %v", err) + } + + if out.Account != nil { + log.Printf("Account %q: %s", + spotinst.StringValue(out.Account.ID), + stringutil.Stringify(out.Account)) + } + +} diff --git a/service/account/account.go b/service/account/account.go new file mode 100644 index 00000000..08badd6e --- /dev/null +++ b/service/account/account.go @@ -0,0 +1,37 @@ +package account + +import ( + "github.com/spotinst/spotinst-sdk-go/service/account/providers/aws" + "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 { + CloudProviderAWS() aws.Service +} + +type ServiceOp struct { + Client *client.Client +} + +var _ Service = &ServiceOp{} + +func New(sess *session.Session, cfgs ...*spotinst.Config) *ServiceOp { + cfg := &spotinst.Config{} + cfg.Merge(sess.Config) + cfg.Merge(cfgs...) + + return &ServiceOp{ + Client: client.New(cfg), + } +} + +func (s *ServiceOp) CloudProviderAWS() aws.Service { + return &aws.ServiceOp{ + Client: s.Client, + } +} diff --git a/service/account/providers/aws/account.go b/service/account/providers/aws/account.go new file mode 100644 index 00000000..f1dc7802 --- /dev/null +++ b/service/account/providers/aws/account.go @@ -0,0 +1,169 @@ +package aws + +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" + "github.com/spotinst/spotinst-sdk-go/spotinst/util/uritemplates" + "io/ioutil" + "net/http" + "time" +) + +type Account struct { + ID *string `json:"id,omitempty"` + Name *string `json:"name,omitempty"` + OrganizationId *string `json:"organizationId,omitempty"` + AccountId *string `json:"accountId,omitempty"` + CloudProvider *string `json:"cloudProvider,omitempty"` + ProviderExternalId *string `json:"providerExternalId,omitempty"` + + // Read-only fields. + CreatedAt *time.Time `json:"createdAt,omitempty"` + UpdatedAt *time.Time `json:"updatedAt,omitempty"` + + // forceSendFields is a list of field names (e.g. "Keys") to + // unconditionally include in API requests. By default, fields with + // empty values are omitted from API requests. However, any non-pointer, + // non-interface field appearing in ForceSendFields will be sent to the + // server regardless of whether the field is empty or not. This may be + // used to include empty fields in Patch requests. + forceSendFields []string + + // nullFields is a list of field names (e.g. "Keys") to include in API + // requests with the JSON null value. By default, fields with empty + // values are omitted from API requests. However, any field with an + // empty value appearing in NullFields will be sent to the server as + // null. It is an error if a field in this list has a non-empty value. + // This may be used to include null fields in Patch requests. + nullFields []string +} + +func (o Account) MarshalJSON() ([]byte, error) { + type noMethod Account + raw := noMethod(o) + return jsonutil.MarshalJSON(raw, o.forceSendFields, o.nullFields) +} + +type CreateAccountInput struct { + Account *Account `json:"account,omitempty"` +} +type CreateAccountOutput struct { + Account *Account `json:"account,omitempty"` +} + +func (s *ServiceOp) CreateAccount(ctx context.Context, input *CreateAccountInput) (*CreateAccountOutput, error) { + r := client.NewRequest(http.MethodPost, "/setup/account") + r.Obj = input + + resp, err := client.RequireOK(s.Client.Do(ctx, r)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + gs, err := accountsFromHttpResponse(resp) + if err != nil { + return nil, err + } + + output := new(CreateAccountOutput) + if len(gs) > 0 { + output.Account = gs[0] + } + + return output, nil +} + +type ReadAccountInput struct { + AccountID *string `json:"account,omitempty"` +} +type ReadAccountOutput struct { + Account *Account `json:"account,omitempty"` +} + +func (s *ServiceOp) ReadAccount(ctx context.Context, input *ReadAccountInput) (*ReadAccountOutput, error) { + acctid := spotinst.StringValue(input.AccountID) + r := client.NewRequest(http.MethodGet, "/setup/account/"+acctid) + r.Obj = input + + resp, err := client.RequireOK(s.Client.Do(ctx, r)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + gs, err := accountsFromHttpResponse(resp) + if err != nil { + return nil, err + } + output := new(ReadAccountOutput) + if len(gs) > 0 { + output.Account = gs[0] + } + + return output, nil + +} + +func accountsFromHttpResponse(resp *http.Response) ([]*Account, error) { + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + return accountsFromJSON(body) +} + +func accountsFromJSON(in []byte) ([]*Account, error) { + var rw client.Response + if err := json.Unmarshal(in, &rw); err != nil { + return nil, err + } + out := make([]*Account, len(rw.Response.Items)) + if len(out) == 0 { + return out, nil + } + for i, rb := range rw.Response.Items { + b, err := accountFromJSON(rb) + if err != nil { + return nil, err + } + out[i] = b + } + return out, nil +} + +func accountFromJSON(in []byte) (*Account, error) { + b := new(Account) + if err := json.Unmarshal(in, b); err != nil { + return nil, err + } + return b, nil +} + +type DeleteAccountInput struct { + AccountID *string `json:"accountId,omitempty"` +} + +type DeleteAccountOutput struct{} + +func (s *ServiceOp) DeleteAccount(ctx context.Context, input *DeleteAccountInput) (*DeleteAccountOutput, error) { + path, err := uritemplates.Expand("/setup/account/{accountId}", uritemplates.Values{ + "accountId": spotinst.StringValue(input.AccountID), + }) + if err != nil { + return nil, err + } + + r := client.NewRequest(http.MethodDelete, path) + + resp, err := client.RequireOK(s.Client.Do(ctx, r)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + return &DeleteAccountOutput{}, nil +} diff --git a/service/account/providers/aws/service.go b/service/account/providers/aws/service.go new file mode 100644 index 00000000..0aafdf12 --- /dev/null +++ b/service/account/providers/aws/service.go @@ -0,0 +1,36 @@ +package aws + +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 { + serviceAccount +} + +type serviceAccount interface { + CreateAccount(context.Context, *CreateAccountInput) (*CreateAccountOutput, error) + DeleteAccount(context.Context, *DeleteAccountInput) (*DeleteAccountOutput, error) + ReadAccount(context.Context, *ReadAccountInput) (*ReadAccountOutput, 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 5cb838167bede1e98b5f1eb34cd2af4f90e62080 Mon Sep 17 00:00:00 2001 From: sharadkesarwani Date: Thu, 14 Sep 2023 03:22:28 +0530 Subject: [PATCH 02/14] Added support for externalId, setCredential, modified client.go and request.go ,fixed acct support Added support for externalId, modified client.go and request.go to avoid credentials fetching from local, fixed acct support, added support for setCredential also however we need to avoid sending AcctId in payload somehow. --- examples/service/account/create/main.go | 2 +- examples/service/account/delete/main.go | 2 +- examples/service/account/read/main.go | 4 +- examples/service/awscredential/create/main.go | 34 ++++ examples/service/awscredential/read/main.go | 31 ++++ examples/service/externalid/create/main.go | 29 +++ examples/service/externalid/read/main.go | 29 +++ service/account/providers/aws/account.go | 23 ++- .../account/providers/aws/awscredential.go | 166 ++++++++++++++++++ .../account/providers/aws/awsexternalId.go | 161 +++++++++++++++++ service/account/providers/aws/service.go | 11 +- spotinst/client/client.go | 12 ++ spotinst/client/request.go | 43 +++++ 13 files changed, 537 insertions(+), 10 deletions(-) create mode 100644 examples/service/awscredential/create/main.go create mode 100644 examples/service/awscredential/read/main.go create mode 100644 examples/service/externalid/create/main.go create mode 100644 examples/service/externalid/read/main.go create mode 100644 service/account/providers/aws/awscredential.go create mode 100644 service/account/providers/aws/awsexternalId.go diff --git a/examples/service/account/create/main.go b/examples/service/account/create/main.go index 81874ff8..611ac194 100644 --- a/examples/service/account/create/main.go +++ b/examples/service/account/create/main.go @@ -16,7 +16,7 @@ func main() { ctx := context.Background() out, err := svc.CloudProviderAWS().CreateAccount(ctx, &aws.CreateAccountInput{ &aws.Account{ - Name: spotinst.String("testTerraformAcct_123"), + Name: spotinst.String("testAcct_123"), }, }) diff --git a/examples/service/account/delete/main.go b/examples/service/account/delete/main.go index ed006a5f..c4933bed 100644 --- a/examples/service/account/delete/main.go +++ b/examples/service/account/delete/main.go @@ -14,7 +14,7 @@ func main() { svc := account.New(sess) ctx := context.Background() _, err := svc.CloudProviderAWS().DeleteAccount(ctx, &aws.DeleteAccountInput{ - spotinst.String("act-7926f067"), + spotinst.String("act-123456"), }) if err != nil { diff --git a/examples/service/account/read/main.go b/examples/service/account/read/main.go index 6885ddb8..9e660a81 100644 --- a/examples/service/account/read/main.go +++ b/examples/service/account/read/main.go @@ -15,11 +15,11 @@ func main() { svc := account.New(sess) ctx := context.Background() out, err := svc.CloudProviderAWS().ReadAccount(ctx, &aws.ReadAccountInput{ - AccountID: spotinst.String("act-19c96d38"), + AccountID: spotinst.String("act-123456"), }) if err != nil { - log.Fatalf("spotinst: failed to fetch account: %v", err) + log.Fatalf("spotinst: faccount not found: %v", err) } if out.Account != nil { diff --git a/examples/service/awscredential/create/main.go b/examples/service/awscredential/create/main.go new file mode 100644 index 00000000..ea844de6 --- /dev/null +++ b/examples/service/awscredential/create/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "context" + "github.com/spotinst/spotinst-sdk-go/spotinst/util/stringutil" + "log" + + "github.com/spotinst/spotinst-sdk-go/service/account" + "github.com/spotinst/spotinst-sdk-go/service/account/providers/aws" + "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() + out, err := svc.CloudProviderAWS().SetCredential(ctx, &aws.CreateCredentialInput{ + &aws.Credential{ + AccountId: spotinst.String("act-12345"), + IamRole: spotinst.String("arn:aws:iam::12345:role/test-role"), + }, + }) + + if err != nil { + log.Fatalf("spotinst: failed to fetch credential: %v", err) + } + if out != nil { + log.Printf("credential %q: %s", + spotinst.StringValue(out.Credential.AccountId), + stringutil.Stringify(out.Credential.IamRole)) + } + +} diff --git a/examples/service/awscredential/read/main.go b/examples/service/awscredential/read/main.go new file mode 100644 index 00000000..0264112d --- /dev/null +++ b/examples/service/awscredential/read/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "context" + "github.com/spotinst/spotinst-sdk-go/spotinst/util/stringutil" + "log" + + "github.com/spotinst/spotinst-sdk-go/service/account" + "github.com/spotinst/spotinst-sdk-go/service/account/providers/aws" + "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() + out, err := svc.CloudProviderAWS().ReadCredential(ctx, &aws.ReadCredentialInput{ + AccountId: spotinst.String("act-12345"), + }) + + if err != nil { + log.Fatalf("spotinst: failed to fetch credential: %v", err) + } + if out != nil { + log.Printf("credential %q: %s", + spotinst.StringValue(out.Credential.AccountId), + stringutil.Stringify(out.Credential.IamRole)) + } + +} diff --git a/examples/service/externalid/create/main.go b/examples/service/externalid/create/main.go new file mode 100644 index 00000000..50cad3f1 --- /dev/null +++ b/examples/service/externalid/create/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "context" + "github.com/spotinst/spotinst-sdk-go/service/account" + "github.com/spotinst/spotinst-sdk-go/service/account/providers/aws" + "github.com/spotinst/spotinst-sdk-go/spotinst" + "github.com/spotinst/spotinst-sdk-go/spotinst/session" + "log" +) + +func main() { + sess := session.New() + svc := account.New(sess) + ctx := context.Background() + out, err := svc.CloudProviderAWS().CreateAWSAccountExternalId(ctx, &aws.CreateAWSAccountExternalIdInput{ + AccountID: spotinst.String("act-123456"), + }) + + if err != nil { + log.Fatalf("spotinst: failed to genrate externalId %v", err) + } + + if out != nil { + log.Printf("externalId: %s", + spotinst.StringValue(out.AWSAccountExternalId.ExternalId)) + } + +} diff --git a/examples/service/externalid/read/main.go b/examples/service/externalid/read/main.go new file mode 100644 index 00000000..06850e72 --- /dev/null +++ b/examples/service/externalid/read/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "context" + "github.com/spotinst/spotinst-sdk-go/service/account" + "github.com/spotinst/spotinst-sdk-go/service/account/providers/aws" + "github.com/spotinst/spotinst-sdk-go/spotinst" + "github.com/spotinst/spotinst-sdk-go/spotinst/session" + "log" +) + +func main() { + sess := session.New() + svc := account.New(sess) + ctx := context.Background() + out, err := svc.CloudProviderAWS().ReadAWSAccountExternalId(ctx, &aws.ReadAWSAccountExternalIdInput{ + AccountID: spotinst.String("act-123456"), + }) + + if err != nil { + log.Fatalf("spotinst: failed to fetch account: %v", err) + } + + if out != nil { + log.Printf("externalId: %s", + spotinst.StringValue(out.AwsAccountExternalId.ExternalId)) + } + +} diff --git a/service/account/providers/aws/account.go b/service/account/providers/aws/account.go index f1dc7802..4aedc2c8 100644 --- a/service/account/providers/aws/account.go +++ b/service/account/providers/aws/account.go @@ -41,6 +41,19 @@ type Account struct { nullFields []string } +func (o *Account) SetId(v *string) *Account { + if o.ID = v; o.ID == nil { + o.nullFields = append(o.nullFields, "ID") + } + return o +} +func (o *Account) SetName(v *string) *Account { + if o.Name = v; o.Name == nil { + o.nullFields = append(o.nullFields, "Name") + } + return o +} + func (o Account) MarshalJSON() ([]byte, error) { type noMethod Account raw := noMethod(o) @@ -58,7 +71,7 @@ func (s *ServiceOp) CreateAccount(ctx context.Context, input *CreateAccountInput r := client.NewRequest(http.MethodPost, "/setup/account") r.Obj = input - resp, err := client.RequireOK(s.Client.Do(ctx, r)) + resp, err := client.RequireOK(s.Client.DoOrg(ctx, r)) if err != nil { return nil, err } @@ -85,11 +98,11 @@ type ReadAccountOutput struct { } func (s *ServiceOp) ReadAccount(ctx context.Context, input *ReadAccountInput) (*ReadAccountOutput, error) { - acctid := spotinst.StringValue(input.AccountID) - r := client.NewRequest(http.MethodGet, "/setup/account/"+acctid) + path, err := uritemplates.Expand("/setup/account/{acctId}", uritemplates.Values{"acctId": spotinst.StringValue(input.AccountID)}) + r := client.NewRequest(http.MethodGet, path) r.Obj = input - resp, err := client.RequireOK(s.Client.Do(ctx, r)) + resp, err := client.RequireOK(s.Client.DoOrg(ctx, r)) if err != nil { return nil, err } @@ -159,7 +172,7 @@ func (s *ServiceOp) DeleteAccount(ctx context.Context, input *DeleteAccountInput r := client.NewRequest(http.MethodDelete, path) - resp, err := client.RequireOK(s.Client.Do(ctx, r)) + resp, err := client.RequireOK(s.Client.DoOrg(ctx, r)) if err != nil { return nil, err } diff --git a/service/account/providers/aws/awscredential.go b/service/account/providers/aws/awscredential.go new file mode 100644 index 00000000..be38839a --- /dev/null +++ b/service/account/providers/aws/awscredential.go @@ -0,0 +1,166 @@ +package aws + +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" + "time" +) + +type Credential struct { + IamRole *string `json:"iamRole,omitempty"` + AccountId *string `json:"accountId,omitempty"` + + // Read-only fields. + CreatedAt *time.Time `json:"createdAt,omitempty"` + UpdatedAt *time.Time `json:"updatedAt,omitempty"` + + // forceSendFields is a list of field names (e.g. "Keys") to + // unconditionally include in API requests. By default, fields with + // empty values are omitted from API requests. However, any non-pointer, + // non-interface field appearing in ForceSendFields will be sent to the + // server regardless of whether the field is empty or not. This may be + // used to include empty fields in Patch requests. + forceSendFields []string + + // nullFields is a list of field names (e.g. "Keys") to include in API + // requests with the JSON null value. By default, fields with empty + // values are omitted from API requests. However, any field with an + // empty value appearing in NullFields will be sent to the server as + // null. It is an error if a field in this list has a non-empty value. + // This may be used to include null fields in Patch requests. + nullFields []string +} + +func (o Credential) MarshalJSON() ([]byte, error) { + type noMethod Credential + raw := noMethod(o) + return jsonutil.MarshalJSON(raw, o.forceSendFields, o.nullFields) +} + +type CreateCredentialInput struct { + Credential *Credential `json:"credentials,omitempty"` +} +type CreateCredentialOutput struct { + Credential *Credential `json:"Credential,omitempty"` +} + +func (s *ServiceOp) SetCredential(ctx context.Context, input *CreateCredentialInput) (*CreateCredentialOutput, error) { + r := client.NewRequest(http.MethodPost, "/setup/credentials/aws") + /*r.Obj = input + if input != nil { + r.Params.Set("accountId", spotinst.StringValue(input.Credential.AccountId)) + }*/ + + if input != nil { + r.Params.Set("accountId", spotinst.StringValue(input.Credential.AccountId)) + } + //input.Credential.AccountId = nil + r.Obj = input + + 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(CreateCredentialOutput) + if len(gs) > 0 { + output.Credential = gs[0] + } + + return output, nil +} + +type ReadCredentialInput struct { + AccountId *string `json:"accountId,omitempty"` +} +type ReadCredentialOutput struct { + Credential *Credential `json:"Credential,omitempty"` +} + +func (s *ServiceOp) ReadCredential(ctx context.Context, input *ReadCredentialInput) (*ReadCredentialOutput, error) { + r := client.NewRequest(http.MethodPost, "/setup/credentials/aws") + r.Obj = input + + resp, err := client.RequireOK(s.Client.Do(ctx, r)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + gs, err := credentialsFromHttpResponse(resp) + if err != nil { + return nil, err + } + + output := new(ReadCredentialOutput) + if len(gs) > 0 { + output.Credential = gs[0] + } + + return output, nil +} + +func credentialsFromHttpResponse(resp *http.Response) ([]*Credential, error) { + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + return credentialsFromJSON(body) +} + +func credentialsFromJSON(in []byte) ([]*Credential, error) { + var rw client.Response + if err := json.Unmarshal(in, &rw); err != nil { + return nil, err + } + out := make([]*Credential, 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) (*Credential, error) { + b := new(Credential) + if err := json.Unmarshal(in, b); err != nil { + return nil, err + } + return b, nil +} + +/* +type IamRole struct { + IamRole *string `json:"iamRole,omitempty"` + + forceSendFields []string + nullFields []string +} +func (o *Credential) SetIamRole(v *string) *Credential { + if o.IamRole = v; o.IamRole == nil { + o.nullFields = append(o.nullFields, "IamRole") + } + return o +} +func (o IamRole) MarshalJSON() ([]byte, error) { + type noMethod IamRole + raw := noMethod(o) + return jsonutil.MarshalJSON(raw, o.forceSendFields, o.nullFields) +}*/ diff --git a/service/account/providers/aws/awsexternalId.go b/service/account/providers/aws/awsexternalId.go new file mode 100644 index 00000000..234526d9 --- /dev/null +++ b/service/account/providers/aws/awsexternalId.go @@ -0,0 +1,161 @@ +package aws + +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" + "github.com/spotinst/spotinst-sdk-go/spotinst/util/uritemplates" + "io/ioutil" + "net/http" + "time" +) + +type AwsAccountExternalId struct { + ID *string `json:"accountId,omitempty"` + ExternalId *string `json:"externalId,omitempty"` + // Read-only fields. + CreatedAt *time.Time `json:"createdAt,omitempty"` + UpdatedAt *time.Time `json:"updatedAt,omitempty"` + + // forceSendFields is a list of field names (e.g. "Keys") to + // unconditionally include in API requests. By default, fields with + // empty values are omitted from API requests. However, any non-pointer, + // non-interface field appearing in ForceSendFields will be sent to the + // server regardless of whether the field is empty or not. This may be + // used to include empty fields in Patch requests. + forceSendFields []string + + // nullFields is a list of field names (e.g. "Keys") to include in API + // requests with the JSON null value. By default, fields with empty + // values are omitted from API requests. However, any field with an + // empty value appearing in NullFields will be sent to the server as + // null. It is an error if a field in this list has a non-empty value. + // This may be used to include null fields in Patch requests. + nullFields []string +} + +func (o AwsAccountExternalId) MarshalJSON() ([]byte, error) { + type noMethod AwsAccountExternalId + raw := noMethod(o) + return jsonutil.MarshalJSON(raw, o.forceSendFields, o.nullFields) +} + +type CreateAWSAccountExternalIdInput struct { + //AWSAccountExternalId *AwsAccountExternalId `json:"AWSAccountExternalId,omitempty"` + //AWSAccountExternalId map[string]string `json:"AWSAccountExternalId,omitempty"` + AccountID *string `json:"accountId,omitempty"` +} +type CreateAWSAccountExternalIdOutput struct { + AWSAccountExternalId *AwsAccountExternalId `json:"AWSAccountExternalId,omitempty"` +} + +func (s *ServiceOp) CreateAWSAccountExternalId(ctx context.Context, input *CreateAWSAccountExternalIdInput) (*CreateAWSAccountExternalIdOutput, error) { + //path, err := uritemplates.Expand("/setup/credentials/aws/externalId/{acctId}", uritemplates.Values{"acctId": spotinst.StringValue(input.AccountID)}) + r := client.NewRequest(http.MethodPost, "/setup/credentials/aws/externalId") + //r := client.NewRequest(http.MethodPost, path) + /*if input.AccountID != nil { + r.Params.Set("accountId", spotinst.StringValue(input.AccountID)) + }*/ + //r.Obj = input + 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 := awsAccountExternalIdFromHttpResponse(resp) + if err != nil { + return nil, err + } + + output := new(CreateAWSAccountExternalIdOutput) + if len(gs) > 0 { + output.AWSAccountExternalId = gs[0] + } + + return output, nil +} + +type ReadAWSAccountExternalIdInput struct { + AccountID *string `json:"account,omitempty"` +} +type ReadAWSAccountExternalIdOutput struct { + AwsAccountExternalId *AwsAccountExternalId `json:"externalId,omitempty"` +} + +func (s *ServiceOp) ReadAWSAccountExternalId(ctx context.Context, input *ReadAWSAccountExternalIdInput) (*ReadAWSAccountExternalIdOutput, error) { + // acctid := spotinst.StringValue(input.AccountID) + path, err := uritemplates.Expand("/setup/credentials/aws/externalId/{acctId}", uritemplates.Values{"acctId": spotinst.StringValue(input.AccountID)}) + r := client.NewRequest(http.MethodGet, path) + r.Obj = input + resp, err := client.RequireOK(s.Client.DoOrg(ctx, r)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + gs, err := awsAccountExternalIdFromHttpResponse(resp) + if err != nil { + return nil, err + } + + output := new(ReadAWSAccountExternalIdOutput) + if len(gs) > 0 { + output.AwsAccountExternalId = gs[0] + } + + return output, nil + + /* gs, err := accountsFromHttpResponse(resp) + if err != nil { + return nil, err + } + output := new(ReadAccountOutput) + if len(gs) > 0 { + output.Account = gs[0] + } + + return output, nil*/ + +} + +func awsAccountExternalIdFromHttpResponse(resp *http.Response) ([]*AwsAccountExternalId, error) { + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + return awsAccountExternalIdsFromJSON(body) +} + +func awsAccountExternalIdsFromJSON(in []byte) ([]*AwsAccountExternalId, error) { + var rw client.Response + if err := json.Unmarshal(in, &rw); err != nil { + return nil, err + } + out := make([]*AwsAccountExternalId, len(rw.Response.Items)) + if len(out) == 0 { + return out, nil + } + for i, rb := range rw.Response.Items { + b, err := awsAccountExternalIdFromJSON(rb) + if err != nil { + return nil, err + } + out[i] = b + } + return out, nil +} + +func awsAccountExternalIdFromJSON(in []byte) (*AwsAccountExternalId, error) { + b := new(AwsAccountExternalId) + if err := json.Unmarshal(in, b); err != nil { + return nil, err + } + return b, nil +} diff --git a/service/account/providers/aws/service.go b/service/account/providers/aws/service.go index 0aafdf12..badeca4f 100644 --- a/service/account/providers/aws/service.go +++ b/service/account/providers/aws/service.go @@ -13,6 +13,8 @@ import ( // the service. type Service interface { serviceAccount + serviceAwsAccountExternalId + serviceCredential } type serviceAccount interface { @@ -20,7 +22,14 @@ type serviceAccount interface { DeleteAccount(context.Context, *DeleteAccountInput) (*DeleteAccountOutput, error) ReadAccount(context.Context, *ReadAccountInput) (*ReadAccountOutput, error) } - +type serviceAwsAccountExternalId interface { + CreateAWSAccountExternalId(context.Context, *CreateAWSAccountExternalIdInput) (*CreateAWSAccountExternalIdOutput, error) + ReadAWSAccountExternalId(context.Context, *ReadAWSAccountExternalIdInput) (*ReadAWSAccountExternalIdOutput, error) +} +type serviceCredential interface { + SetCredential(context.Context, *CreateCredentialInput) (*CreateCredentialOutput, error) + ReadCredential(context.Context, *ReadCredentialInput) (*ReadCredentialOutput, error) +} type ServiceOp struct { Client *client.Client } diff --git a/spotinst/client/client.go b/spotinst/client/client.go index 0c448d96..4e2db7c1 100644 --- a/spotinst/client/client.go +++ b/spotinst/client/client.go @@ -79,3 +79,15 @@ func (c *Client) logResponse(resp *http.Response) { } } } + +// Do runs a request with our client. +func (c *Client) DoOrg(ctx context.Context, r *Request) (*http.Response, error) { + req, err := r.toHTTPOrg(ctx, c.config) + if err != nil { + return nil, err + } + c.logRequest(req) + resp, err := c.config.HTTPClient.Do(req) + c.logResponse(resp) + return resp, err +} diff --git a/spotinst/client/request.go b/spotinst/client/request.go index efbe32f9..f00a68fc 100644 --- a/spotinst/client/request.go +++ b/spotinst/client/request.go @@ -74,3 +74,46 @@ func EncodeBody(obj interface{}) (io.Reader, error) { } return buf, nil } + +// toHTTP converts the request to an HTTP request. +func (r *Request) toHTTPOrg(ctx context.Context, cfg *spotinst.Config) (*http.Request, error) { + // Set the user credentials. + creds, err := cfg.Credentials.Get() + if err != nil { + return nil, err + } + if creds.Token != "" { + r.header.Set("Authorization", "Bearer "+creds.Token) + } + + // Encode the query parameters. + r.url.RawQuery = r.Params.Encode() + + // Check if we should encode the body. + if r.body == nil && r.Obj != nil { + if b, err := EncodeBody(r.Obj); err != nil { + return nil, err + } else { + r.body = b + } + } + + // Create the HTTP request. + req, err := http.NewRequest(r.method, r.url.RequestURI(), r.body) + if err != nil { + return nil, err + } + + // Set request base URL. + req.URL.Host = cfg.BaseURL.Host + req.URL.Scheme = cfg.BaseURL.Scheme + + // Set request headers. + req.Host = cfg.BaseURL.Host + req.Header = r.header + req.Header.Set("Content-Type", cfg.ContentType) + req.Header.Add("Accept", cfg.ContentType) + req.Header.Add("User-Agent", cfg.UserAgent) + + return req.WithContext(ctx), nil +} From 5b12e469e043a9502f2af1d1f20be06572bc2fc2 Mon Sep 17 00:00:00 2001 From: sharadkesarwani Date: Thu, 14 Sep 2023 16:32:15 +0530 Subject: [PATCH 03/14] fixed set credential API support fixed set credential API support --- examples/service/awscredential/create/main.go | 10 ++-------- service/account/providers/aws/awscredential.go | 14 ++++++-------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/examples/service/awscredential/create/main.go b/examples/service/awscredential/create/main.go index ea844de6..1f79d2b4 100644 --- a/examples/service/awscredential/create/main.go +++ b/examples/service/awscredential/create/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "github.com/spotinst/spotinst-sdk-go/spotinst/util/stringutil" "log" "github.com/spotinst/spotinst-sdk-go/service/account" @@ -15,7 +14,7 @@ func main() { sess := session.New() svc := account.New(sess) ctx := context.Background() - out, err := svc.CloudProviderAWS().SetCredential(ctx, &aws.CreateCredentialInput{ + _, err := svc.CloudProviderAWS().SetCredential(ctx, &aws.CreateCredentialInput{ &aws.Credential{ AccountId: spotinst.String("act-12345"), IamRole: spotinst.String("arn:aws:iam::12345:role/test-role"), @@ -23,12 +22,7 @@ func main() { }) if err != nil { - log.Fatalf("spotinst: failed to fetch credential: %v", err) - } - if out != nil { - log.Printf("credential %q: %s", - spotinst.StringValue(out.Credential.AccountId), - stringutil.Stringify(out.Credential.IamRole)) + log.Fatalf("spotinst: failed to set credential: %v", err) } } diff --git a/service/account/providers/aws/awscredential.go b/service/account/providers/aws/awscredential.go index be38839a..4d7593ca 100644 --- a/service/account/providers/aws/awscredential.go +++ b/service/account/providers/aws/awscredential.go @@ -51,15 +51,11 @@ type CreateCredentialOutput struct { func (s *ServiceOp) SetCredential(ctx context.Context, input *CreateCredentialInput) (*CreateCredentialOutput, error) { r := client.NewRequest(http.MethodPost, "/setup/credentials/aws") - /*r.Obj = input - if input != nil { - r.Params.Set("accountId", spotinst.StringValue(input.Credential.AccountId)) - }*/ if input != nil { r.Params.Set("accountId", spotinst.StringValue(input.Credential.AccountId)) } - //input.Credential.AccountId = nil + input.Credential.AccountId = nil r.Obj = input resp, err := client.RequireOK(s.Client.DoOrg(ctx, r)) @@ -89,10 +85,12 @@ type ReadCredentialOutput struct { } func (s *ServiceOp) ReadCredential(ctx context.Context, input *ReadCredentialInput) (*ReadCredentialOutput, error) { - r := client.NewRequest(http.MethodPost, "/setup/credentials/aws") - r.Obj = input + r := client.NewRequest(http.MethodGet, "/setup/credentials/aws") + if input != nil { + r.Params.Set("accountId", spotinst.StringValue(input.AccountId)) + } - resp, err := client.RequireOK(s.Client.Do(ctx, r)) + resp, err := client.RequireOK(s.Client.DoOrg(ctx, r)) if err != nil { return nil, err } From 6f3b1610af2c52712594e3b4664bc22789c1e57a Mon Sep 17 00:00:00 2001 From: sharadkesarwani Date: Thu, 14 Sep 2023 18:13:27 +0530 Subject: [PATCH 04/14] removed commented code from awscredentials.go removed commented code from awscredentials.go --- .../account/providers/aws/awscredential.go | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/service/account/providers/aws/awscredential.go b/service/account/providers/aws/awscredential.go index 4d7593ca..e7b430fb 100644 --- a/service/account/providers/aws/awscredential.go +++ b/service/account/providers/aws/awscredential.go @@ -143,22 +143,3 @@ func credentialFromJSON(in []byte) (*Credential, error) { } return b, nil } - -/* -type IamRole struct { - IamRole *string `json:"iamRole,omitempty"` - - forceSendFields []string - nullFields []string -} -func (o *Credential) SetIamRole(v *string) *Credential { - if o.IamRole = v; o.IamRole == nil { - o.nullFields = append(o.nullFields, "IamRole") - } - return o -} -func (o IamRole) MarshalJSON() ([]byte, error) { - type noMethod IamRole - raw := noMethod(o) - return jsonutil.MarshalJSON(raw, o.forceSendFields, o.nullFields) -}*/ From 355f6584d80047790cc0540016fb6f19666546bf Mon Sep 17 00:00:00 2001 From: sharadkesarwani Date: Thu, 14 Sep 2023 20:45:39 +0530 Subject: [PATCH 05/14] Added setters for credential attributes and renamed CreateCredentialInput to SetCredentialInput Added setters for credential attributes and renamed CreateCredentialInput to SetCredentialInput and CreateCredentialOutput to SetCredentialOutput --- examples/service/awscredential/create/main.go | 2 +- .../account/providers/aws/awscredential.go | 21 +++++++++++++++---- service/account/providers/aws/service.go | 2 +- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/examples/service/awscredential/create/main.go b/examples/service/awscredential/create/main.go index 1f79d2b4..c4ed358f 100644 --- a/examples/service/awscredential/create/main.go +++ b/examples/service/awscredential/create/main.go @@ -14,7 +14,7 @@ func main() { sess := session.New() svc := account.New(sess) ctx := context.Background() - _, err := svc.CloudProviderAWS().SetCredential(ctx, &aws.CreateCredentialInput{ + _, err := svc.CloudProviderAWS().SetCredential(ctx, &aws.SetCredentialInput{ &aws.Credential{ AccountId: spotinst.String("act-12345"), IamRole: spotinst.String("arn:aws:iam::12345:role/test-role"), diff --git a/service/account/providers/aws/awscredential.go b/service/account/providers/aws/awscredential.go index e7b430fb..3994cab4 100644 --- a/service/account/providers/aws/awscredential.go +++ b/service/account/providers/aws/awscredential.go @@ -42,14 +42,27 @@ func (o Credential) MarshalJSON() ([]byte, error) { return jsonutil.MarshalJSON(raw, o.forceSendFields, o.nullFields) } -type CreateCredentialInput struct { +func (o *Credential) SetIamRole(v *string) *Credential { + if o.IamRole = v; o.IamRole == nil { + o.nullFields = append(o.nullFields, "IamRole") + } + return o +} +func (o *Credential) SetAccountId(v *string) *Credential { + if o.AccountId = v; o.AccountId == nil { + o.nullFields = append(o.nullFields, "AccountId") + } + return o +} + +type SetCredentialInput struct { Credential *Credential `json:"credentials,omitempty"` } -type CreateCredentialOutput struct { +type SetCredentialOutput struct { Credential *Credential `json:"Credential,omitempty"` } -func (s *ServiceOp) SetCredential(ctx context.Context, input *CreateCredentialInput) (*CreateCredentialOutput, error) { +func (s *ServiceOp) SetCredential(ctx context.Context, input *SetCredentialInput) (*SetCredentialOutput, error) { r := client.NewRequest(http.MethodPost, "/setup/credentials/aws") if input != nil { @@ -69,7 +82,7 @@ func (s *ServiceOp) SetCredential(ctx context.Context, input *CreateCredentialIn return nil, err } - output := new(CreateCredentialOutput) + output := new(SetCredentialOutput) if len(gs) > 0 { output.Credential = gs[0] } diff --git a/service/account/providers/aws/service.go b/service/account/providers/aws/service.go index badeca4f..2a1e2da1 100644 --- a/service/account/providers/aws/service.go +++ b/service/account/providers/aws/service.go @@ -27,7 +27,7 @@ type serviceAwsAccountExternalId interface { ReadAWSAccountExternalId(context.Context, *ReadAWSAccountExternalIdInput) (*ReadAWSAccountExternalIdOutput, error) } type serviceCredential interface { - SetCredential(context.Context, *CreateCredentialInput) (*CreateCredentialOutput, error) + SetCredential(context.Context, *SetCredentialInput) (*SetCredentialOutput, error) ReadCredential(context.Context, *ReadCredentialInput) (*ReadCredentialOutput, error) } type ServiceOp struct { From be57b06a917f91205004cf6c7dc935eba3541e9e Mon Sep 17 00:00:00 2001 From: sharadkesarwani Date: Mon, 25 Sep 2023 02:50:31 +0530 Subject: [PATCH 06/14] Removed support for externalId creation. Removed support for externalId creation. --- examples/service/externalid/create/main.go | 29 ---- examples/service/externalid/read/main.go | 29 ---- .../account/providers/aws/awsexternalId.go | 161 ------------------ service/account/providers/aws/service.go | 5 - 4 files changed, 224 deletions(-) delete mode 100644 examples/service/externalid/create/main.go delete mode 100644 examples/service/externalid/read/main.go delete mode 100644 service/account/providers/aws/awsexternalId.go diff --git a/examples/service/externalid/create/main.go b/examples/service/externalid/create/main.go deleted file mode 100644 index 50cad3f1..00000000 --- a/examples/service/externalid/create/main.go +++ /dev/null @@ -1,29 +0,0 @@ -package main - -import ( - "context" - "github.com/spotinst/spotinst-sdk-go/service/account" - "github.com/spotinst/spotinst-sdk-go/service/account/providers/aws" - "github.com/spotinst/spotinst-sdk-go/spotinst" - "github.com/spotinst/spotinst-sdk-go/spotinst/session" - "log" -) - -func main() { - sess := session.New() - svc := account.New(sess) - ctx := context.Background() - out, err := svc.CloudProviderAWS().CreateAWSAccountExternalId(ctx, &aws.CreateAWSAccountExternalIdInput{ - AccountID: spotinst.String("act-123456"), - }) - - if err != nil { - log.Fatalf("spotinst: failed to genrate externalId %v", err) - } - - if out != nil { - log.Printf("externalId: %s", - spotinst.StringValue(out.AWSAccountExternalId.ExternalId)) - } - -} diff --git a/examples/service/externalid/read/main.go b/examples/service/externalid/read/main.go deleted file mode 100644 index 06850e72..00000000 --- a/examples/service/externalid/read/main.go +++ /dev/null @@ -1,29 +0,0 @@ -package main - -import ( - "context" - "github.com/spotinst/spotinst-sdk-go/service/account" - "github.com/spotinst/spotinst-sdk-go/service/account/providers/aws" - "github.com/spotinst/spotinst-sdk-go/spotinst" - "github.com/spotinst/spotinst-sdk-go/spotinst/session" - "log" -) - -func main() { - sess := session.New() - svc := account.New(sess) - ctx := context.Background() - out, err := svc.CloudProviderAWS().ReadAWSAccountExternalId(ctx, &aws.ReadAWSAccountExternalIdInput{ - AccountID: spotinst.String("act-123456"), - }) - - if err != nil { - log.Fatalf("spotinst: failed to fetch account: %v", err) - } - - if out != nil { - log.Printf("externalId: %s", - spotinst.StringValue(out.AwsAccountExternalId.ExternalId)) - } - -} diff --git a/service/account/providers/aws/awsexternalId.go b/service/account/providers/aws/awsexternalId.go deleted file mode 100644 index 234526d9..00000000 --- a/service/account/providers/aws/awsexternalId.go +++ /dev/null @@ -1,161 +0,0 @@ -package aws - -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" - "github.com/spotinst/spotinst-sdk-go/spotinst/util/uritemplates" - "io/ioutil" - "net/http" - "time" -) - -type AwsAccountExternalId struct { - ID *string `json:"accountId,omitempty"` - ExternalId *string `json:"externalId,omitempty"` - // Read-only fields. - CreatedAt *time.Time `json:"createdAt,omitempty"` - UpdatedAt *time.Time `json:"updatedAt,omitempty"` - - // forceSendFields is a list of field names (e.g. "Keys") to - // unconditionally include in API requests. By default, fields with - // empty values are omitted from API requests. However, any non-pointer, - // non-interface field appearing in ForceSendFields will be sent to the - // server regardless of whether the field is empty or not. This may be - // used to include empty fields in Patch requests. - forceSendFields []string - - // nullFields is a list of field names (e.g. "Keys") to include in API - // requests with the JSON null value. By default, fields with empty - // values are omitted from API requests. However, any field with an - // empty value appearing in NullFields will be sent to the server as - // null. It is an error if a field in this list has a non-empty value. - // This may be used to include null fields in Patch requests. - nullFields []string -} - -func (o AwsAccountExternalId) MarshalJSON() ([]byte, error) { - type noMethod AwsAccountExternalId - raw := noMethod(o) - return jsonutil.MarshalJSON(raw, o.forceSendFields, o.nullFields) -} - -type CreateAWSAccountExternalIdInput struct { - //AWSAccountExternalId *AwsAccountExternalId `json:"AWSAccountExternalId,omitempty"` - //AWSAccountExternalId map[string]string `json:"AWSAccountExternalId,omitempty"` - AccountID *string `json:"accountId,omitempty"` -} -type CreateAWSAccountExternalIdOutput struct { - AWSAccountExternalId *AwsAccountExternalId `json:"AWSAccountExternalId,omitempty"` -} - -func (s *ServiceOp) CreateAWSAccountExternalId(ctx context.Context, input *CreateAWSAccountExternalIdInput) (*CreateAWSAccountExternalIdOutput, error) { - //path, err := uritemplates.Expand("/setup/credentials/aws/externalId/{acctId}", uritemplates.Values{"acctId": spotinst.StringValue(input.AccountID)}) - r := client.NewRequest(http.MethodPost, "/setup/credentials/aws/externalId") - //r := client.NewRequest(http.MethodPost, path) - /*if input.AccountID != nil { - r.Params.Set("accountId", spotinst.StringValue(input.AccountID)) - }*/ - //r.Obj = input - 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 := awsAccountExternalIdFromHttpResponse(resp) - if err != nil { - return nil, err - } - - output := new(CreateAWSAccountExternalIdOutput) - if len(gs) > 0 { - output.AWSAccountExternalId = gs[0] - } - - return output, nil -} - -type ReadAWSAccountExternalIdInput struct { - AccountID *string `json:"account,omitempty"` -} -type ReadAWSAccountExternalIdOutput struct { - AwsAccountExternalId *AwsAccountExternalId `json:"externalId,omitempty"` -} - -func (s *ServiceOp) ReadAWSAccountExternalId(ctx context.Context, input *ReadAWSAccountExternalIdInput) (*ReadAWSAccountExternalIdOutput, error) { - // acctid := spotinst.StringValue(input.AccountID) - path, err := uritemplates.Expand("/setup/credentials/aws/externalId/{acctId}", uritemplates.Values{"acctId": spotinst.StringValue(input.AccountID)}) - r := client.NewRequest(http.MethodGet, path) - r.Obj = input - resp, err := client.RequireOK(s.Client.DoOrg(ctx, r)) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - gs, err := awsAccountExternalIdFromHttpResponse(resp) - if err != nil { - return nil, err - } - - output := new(ReadAWSAccountExternalIdOutput) - if len(gs) > 0 { - output.AwsAccountExternalId = gs[0] - } - - return output, nil - - /* gs, err := accountsFromHttpResponse(resp) - if err != nil { - return nil, err - } - output := new(ReadAccountOutput) - if len(gs) > 0 { - output.Account = gs[0] - } - - return output, nil*/ - -} - -func awsAccountExternalIdFromHttpResponse(resp *http.Response) ([]*AwsAccountExternalId, error) { - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - return awsAccountExternalIdsFromJSON(body) -} - -func awsAccountExternalIdsFromJSON(in []byte) ([]*AwsAccountExternalId, error) { - var rw client.Response - if err := json.Unmarshal(in, &rw); err != nil { - return nil, err - } - out := make([]*AwsAccountExternalId, len(rw.Response.Items)) - if len(out) == 0 { - return out, nil - } - for i, rb := range rw.Response.Items { - b, err := awsAccountExternalIdFromJSON(rb) - if err != nil { - return nil, err - } - out[i] = b - } - return out, nil -} - -func awsAccountExternalIdFromJSON(in []byte) (*AwsAccountExternalId, error) { - b := new(AwsAccountExternalId) - if err := json.Unmarshal(in, b); err != nil { - return nil, err - } - return b, nil -} diff --git a/service/account/providers/aws/service.go b/service/account/providers/aws/service.go index 2a1e2da1..126d324f 100644 --- a/service/account/providers/aws/service.go +++ b/service/account/providers/aws/service.go @@ -13,7 +13,6 @@ import ( // the service. type Service interface { serviceAccount - serviceAwsAccountExternalId serviceCredential } @@ -22,10 +21,6 @@ type serviceAccount interface { DeleteAccount(context.Context, *DeleteAccountInput) (*DeleteAccountOutput, error) ReadAccount(context.Context, *ReadAccountInput) (*ReadAccountOutput, error) } -type serviceAwsAccountExternalId interface { - CreateAWSAccountExternalId(context.Context, *CreateAWSAccountExternalIdInput) (*CreateAWSAccountExternalIdOutput, error) - ReadAWSAccountExternalId(context.Context, *ReadAWSAccountExternalIdInput) (*ReadAWSAccountExternalIdOutput, error) -} type serviceCredential interface { SetCredential(context.Context, *SetCredentialInput) (*SetCredentialOutput, error) ReadCredential(context.Context, *ReadCredentialInput) (*ReadCredentialOutput, error) From 88e484989cdaa132a9209e609c18fbd33bb86988 Mon Sep 17 00:00:00 2001 From: sharadkesarwani Date: Mon, 25 Sep 2023 16:45:15 +0530 Subject: [PATCH 07/14] Fixed input variable names in example file of create account and set credential Fixed input variable names in example file of create account and set credential --- examples/service/account/create/main.go | 2 +- examples/service/account/delete/main.go | 2 +- examples/service/awscredential/create/main.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/service/account/create/main.go b/examples/service/account/create/main.go index 611ac194..5c5f80a2 100644 --- a/examples/service/account/create/main.go +++ b/examples/service/account/create/main.go @@ -15,7 +15,7 @@ func main() { svc := account.New(sess) ctx := context.Background() out, err := svc.CloudProviderAWS().CreateAccount(ctx, &aws.CreateAccountInput{ - &aws.Account{ + Account: &aws.Account{ Name: spotinst.String("testAcct_123"), }, }) diff --git a/examples/service/account/delete/main.go b/examples/service/account/delete/main.go index c4933bed..bd983d1a 100644 --- a/examples/service/account/delete/main.go +++ b/examples/service/account/delete/main.go @@ -14,7 +14,7 @@ func main() { svc := account.New(sess) ctx := context.Background() _, err := svc.CloudProviderAWS().DeleteAccount(ctx, &aws.DeleteAccountInput{ - spotinst.String("act-123456"), + AccountID: spotinst.String("act-123456"), }) if err != nil { diff --git a/examples/service/awscredential/create/main.go b/examples/service/awscredential/create/main.go index c4ed358f..9c67c27d 100644 --- a/examples/service/awscredential/create/main.go +++ b/examples/service/awscredential/create/main.go @@ -2,11 +2,11 @@ package main import ( "context" + "github.com/spotinst/spotinst-sdk-go/spotinst" "log" "github.com/spotinst/spotinst-sdk-go/service/account" "github.com/spotinst/spotinst-sdk-go/service/account/providers/aws" - "github.com/spotinst/spotinst-sdk-go/spotinst" "github.com/spotinst/spotinst-sdk-go/spotinst/session" ) @@ -15,7 +15,7 @@ func main() { svc := account.New(sess) ctx := context.Background() _, err := svc.CloudProviderAWS().SetCredential(ctx, &aws.SetCredentialInput{ - &aws.Credential{ + Credential: &aws.Credential{ AccountId: spotinst.String("act-12345"), IamRole: spotinst.String("arn:aws:iam::12345:role/test-role"), }, From d5030e482f3ae6ed28ae20c6ee12806639dad2ee Mon Sep 17 00:00:00 2001 From: sharadkesarwani Date: Mon, 25 Sep 2023 20:37:15 +0530 Subject: [PATCH 08/14] Removed updatedAt field from account and updatedAt, createdAt from set credential struct Removed updatedAt field from account and updatedAt, createdAt from set credential struct --- service/account/providers/aws/account.go | 2 +- service/account/providers/aws/awscredential.go | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/service/account/providers/aws/account.go b/service/account/providers/aws/account.go index 4aedc2c8..3c349918 100644 --- a/service/account/providers/aws/account.go +++ b/service/account/providers/aws/account.go @@ -22,7 +22,7 @@ type Account struct { // Read-only fields. CreatedAt *time.Time `json:"createdAt,omitempty"` - UpdatedAt *time.Time `json:"updatedAt,omitempty"` + //UpdatedAt *time.Time `json:"updatedAt,omitempty"` // forceSendFields is a list of field names (e.g. "Keys") to // unconditionally include in API requests. By default, fields with diff --git a/service/account/providers/aws/awscredential.go b/service/account/providers/aws/awscredential.go index 3994cab4..bf3eea0e 100644 --- a/service/account/providers/aws/awscredential.go +++ b/service/account/providers/aws/awscredential.go @@ -8,7 +8,6 @@ import ( "github.com/spotinst/spotinst-sdk-go/spotinst/util/jsonutil" "io/ioutil" "net/http" - "time" ) type Credential struct { @@ -16,8 +15,8 @@ type Credential struct { AccountId *string `json:"accountId,omitempty"` // Read-only fields. - CreatedAt *time.Time `json:"createdAt,omitempty"` - UpdatedAt *time.Time `json:"updatedAt,omitempty"` + //CreatedAt *time.Time `json:"createdAt,omitempty"` + //UpdatedAt *time.Time `json:"updatedAt,omitempty"` // forceSendFields is a list of field names (e.g. "Keys") to // unconditionally include in API requests. By default, fields with From 75cd481559ad239a585c30492e4531936f7a2bba Mon Sep 17 00:00:00 2001 From: sharadkesarwani Date: Tue, 26 Sep 2023 01:26:59 +0530 Subject: [PATCH 09/14] removed commented code removed commented code --- service/account/providers/aws/account.go | 1 - service/account/providers/aws/awscredential.go | 4 ---- 2 files changed, 5 deletions(-) diff --git a/service/account/providers/aws/account.go b/service/account/providers/aws/account.go index 3c349918..935c7366 100644 --- a/service/account/providers/aws/account.go +++ b/service/account/providers/aws/account.go @@ -22,7 +22,6 @@ type Account struct { // Read-only fields. CreatedAt *time.Time `json:"createdAt,omitempty"` - //UpdatedAt *time.Time `json:"updatedAt,omitempty"` // forceSendFields is a list of field names (e.g. "Keys") to // unconditionally include in API requests. By default, fields with diff --git a/service/account/providers/aws/awscredential.go b/service/account/providers/aws/awscredential.go index bf3eea0e..1c24a2cb 100644 --- a/service/account/providers/aws/awscredential.go +++ b/service/account/providers/aws/awscredential.go @@ -14,10 +14,6 @@ type Credential struct { IamRole *string `json:"iamRole,omitempty"` AccountId *string `json:"accountId,omitempty"` - // Read-only fields. - //CreatedAt *time.Time `json:"createdAt,omitempty"` - //UpdatedAt *time.Time `json:"updatedAt,omitempty"` - // forceSendFields is a list of field names (e.g. "Keys") to // unconditionally include in API requests. By default, fields with // empty values are omitted from API requests. However, any non-pointer, From 5686571cf868e8be72ebf03989a9166cf10fcf81 Mon Sep 17 00:00:00 2001 From: sharadkesarwani Date: Tue, 26 Sep 2023 15:16:28 +0530 Subject: [PATCH 10/14] Added support for externalId APIs Added support for externalId APIs --- examples/service/externalid/create/main.go | 29 ++++ examples/service/externalid/read/main.go | 29 ++++ .../account/providers/aws/awsexternalId.go | 149 ++++++++++++++++++ service/account/providers/aws/service.go | 5 + 4 files changed, 212 insertions(+) create mode 100644 examples/service/externalid/create/main.go create mode 100644 examples/service/externalid/read/main.go create mode 100644 service/account/providers/aws/awsexternalId.go diff --git a/examples/service/externalid/create/main.go b/examples/service/externalid/create/main.go new file mode 100644 index 00000000..d000e9ba --- /dev/null +++ b/examples/service/externalid/create/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "context" + "github.com/spotinst/spotinst-sdk-go/service/account" + "github.com/spotinst/spotinst-sdk-go/service/account/providers/aws" + "github.com/spotinst/spotinst-sdk-go/spotinst" + "github.com/spotinst/spotinst-sdk-go/spotinst/session" + "log" +) + +func main() { + sess := session.New() + svc := account.New(sess) + ctx := context.Background() + out, err := svc.CloudProviderAWS().CreateAWSAccountExternalId(ctx, &aws.CreateAWSAccountExternalIdInput{ + AccountID: spotinst.String("act-12345678"), + }) + + if err != nil { + log.Fatalf("spotinst: failed to genrate externalId %v", err) + } + + if out != nil { + log.Printf("externalId: %s", + spotinst.StringValue(out.AWSAccountExternalId.ExternalId)) + } + +} diff --git a/examples/service/externalid/read/main.go b/examples/service/externalid/read/main.go new file mode 100644 index 00000000..4d0340f5 --- /dev/null +++ b/examples/service/externalid/read/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "context" + "github.com/spotinst/spotinst-sdk-go/service/account" + "github.com/spotinst/spotinst-sdk-go/service/account/providers/aws" + "github.com/spotinst/spotinst-sdk-go/spotinst" + "github.com/spotinst/spotinst-sdk-go/spotinst/session" + "log" +) + +func main() { + sess := session.New() + svc := account.New(sess) + ctx := context.Background() + out, err := svc.CloudProviderAWS().ReadAWSAccountExternalId(ctx, &aws.ReadAWSAccountExternalIdInput{ + AccountID: spotinst.String("act-12345678"), + }) + + if err != nil { + log.Fatalf("spotinst: failed to fetch account: %v", err) + } + + if out != nil { + log.Printf("externalId: %s", + spotinst.StringValue(out.AwsAccountExternalId.ExternalId)) + } + +} diff --git a/service/account/providers/aws/awsexternalId.go b/service/account/providers/aws/awsexternalId.go new file mode 100644 index 00000000..9d538370 --- /dev/null +++ b/service/account/providers/aws/awsexternalId.go @@ -0,0 +1,149 @@ +package aws + +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" + "github.com/spotinst/spotinst-sdk-go/spotinst/util/uritemplates" + "io/ioutil" + "net/http" +) + +type AwsAccountExternalId struct { + AccountId *string `json:"accountId,omitempty"` + ExternalId *string `json:"externalId,omitempty"` + + // forceSendFields is a list of field names (e.g. "Keys") to + // unconditionally include in API requests. By default, fields with + // empty values are omitted from API requests. However, any non-pointer, + // non-interface field appearing in ForceSendFields will be sent to the + // server regardless of whether the field is empty or not. This may be + // used to include empty fields in Patch requests. + forceSendFields []string + + // nullFields is a list of field names (e.g. "Keys") to include in API + // requests with the JSON null value. By default, fields with empty + // values are omitted from API requests. However, any field with an + // empty value appearing in NullFields will be sent to the server as + // null. It is an error if a field in this list has a non-empty value. + // This may be used to include null fields in Patch requests. + nullFields []string +} + +func (o *AwsAccountExternalId) SetAccountId(v *string) *AwsAccountExternalId { + if o.AccountId = v; o.AccountId == nil { + o.nullFields = append(o.nullFields, "AccountId") + } + return o +} +func (o *AwsAccountExternalId) SetExternalId(v *string) *AwsAccountExternalId { + if o.ExternalId = v; o.ExternalId == nil { + o.nullFields = append(o.nullFields, "ExternalId") + } + return o +} + +func (o AwsAccountExternalId) MarshalJSON() ([]byte, error) { + type noMethod AwsAccountExternalId + raw := noMethod(o) + return jsonutil.MarshalJSON(raw, o.forceSendFields, o.nullFields) +} + +type CreateAWSAccountExternalIdInput struct { + AccountID *string `json:"accountId,omitempty"` +} +type CreateAWSAccountExternalIdOutput struct { + AWSAccountExternalId *AwsAccountExternalId `json:"AWSAccountExternalId,omitempty"` +} + +func (s *ServiceOp) CreateAWSAccountExternalId(ctx context.Context, input *CreateAWSAccountExternalIdInput) (*CreateAWSAccountExternalIdOutput, error) { + r := client.NewRequest(http.MethodPost, "/setup/credentials/aws/externalId") + 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 := awsAccountExternalIdFromHttpResponse(resp) + gs[0].AccountId = input.AccountID + if err != nil { + return nil, err + } + output := new(CreateAWSAccountExternalIdOutput) + if len(gs) > 0 { + output.AWSAccountExternalId = gs[0] + } + + return output, nil +} + +type ReadAWSAccountExternalIdInput struct { + AccountID *string `json:"account,omitempty"` +} +type ReadAWSAccountExternalIdOutput struct { + AwsAccountExternalId *AwsAccountExternalId `json:"externalId,omitempty"` +} + +func (s *ServiceOp) ReadAWSAccountExternalId(ctx context.Context, input *ReadAWSAccountExternalIdInput) (*ReadAWSAccountExternalIdOutput, error) { + path, err := uritemplates.Expand("/setup/credentials/aws/externalId/{acctId}", uritemplates.Values{"acctId": spotinst.StringValue(input.AccountID)}) + r := client.NewRequest(http.MethodGet, path) + r.Obj = input + resp, err := client.RequireOK(s.Client.DoOrg(ctx, r)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + gs, err := awsAccountExternalIdFromHttpResponse(resp) + if err != nil { + return nil, err + } + + output := new(ReadAWSAccountExternalIdOutput) + if len(gs) > 0 { + output.AwsAccountExternalId = gs[0] + } + + return output, nil + +} + +func awsAccountExternalIdFromHttpResponse(resp *http.Response) ([]*AwsAccountExternalId, error) { + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + return awsAccountExternalIdsFromJSON(body) +} + +func awsAccountExternalIdsFromJSON(in []byte) ([]*AwsAccountExternalId, error) { + var rw client.Response + if err := json.Unmarshal(in, &rw); err != nil { + return nil, err + } + out := make([]*AwsAccountExternalId, len(rw.Response.Items)) + if len(out) == 0 { + return out, nil + } + for i, rb := range rw.Response.Items { + b, err := awsAccountExternalIdFromJSON(rb) + if err != nil { + return nil, err + } + out[i] = b + } + return out, nil +} + +func awsAccountExternalIdFromJSON(in []byte) (*AwsAccountExternalId, error) { + b := new(AwsAccountExternalId) + if err := json.Unmarshal(in, b); err != nil { + return nil, err + } + return b, nil +} diff --git a/service/account/providers/aws/service.go b/service/account/providers/aws/service.go index 126d324f..98846a30 100644 --- a/service/account/providers/aws/service.go +++ b/service/account/providers/aws/service.go @@ -14,6 +14,7 @@ import ( type Service interface { serviceAccount serviceCredential + serviceAwsAccountExternalId } type serviceAccount interface { @@ -25,6 +26,10 @@ type serviceCredential interface { SetCredential(context.Context, *SetCredentialInput) (*SetCredentialOutput, error) ReadCredential(context.Context, *ReadCredentialInput) (*ReadCredentialOutput, error) } +type serviceAwsAccountExternalId interface { + CreateAWSAccountExternalId(context.Context, *CreateAWSAccountExternalIdInput) (*CreateAWSAccountExternalIdOutput, error) + ReadAWSAccountExternalId(context.Context, *ReadAWSAccountExternalIdInput) (*ReadAWSAccountExternalIdOutput, error) +} type ServiceOp struct { Client *client.Client } From 47fe200a45639df404e2a435b563f075d054e686 Mon Sep 17 00:00:00 2001 From: sharadkesarwani Date: Wed, 27 Sep 2023 13:39:01 +0530 Subject: [PATCH 11/14] Removed comment description for forceSendFields and nullFields Removed comment description for forceSendFields and nullFields --- service/account/providers/aws/awscredential.go | 12 ------------ service/account/providers/aws/awsexternalId.go | 14 +------------- 2 files changed, 1 insertion(+), 25 deletions(-) diff --git a/service/account/providers/aws/awscredential.go b/service/account/providers/aws/awscredential.go index 1c24a2cb..d6758326 100644 --- a/service/account/providers/aws/awscredential.go +++ b/service/account/providers/aws/awscredential.go @@ -14,20 +14,8 @@ type Credential struct { IamRole *string `json:"iamRole,omitempty"` AccountId *string `json:"accountId,omitempty"` - // forceSendFields is a list of field names (e.g. "Keys") to - // unconditionally include in API requests. By default, fields with - // empty values are omitted from API requests. However, any non-pointer, - // non-interface field appearing in ForceSendFields will be sent to the - // server regardless of whether the field is empty or not. This may be - // used to include empty fields in Patch requests. forceSendFields []string - // nullFields is a list of field names (e.g. "Keys") to include in API - // requests with the JSON null value. By default, fields with empty - // values are omitted from API requests. However, any field with an - // empty value appearing in NullFields will be sent to the server as - // null. It is an error if a field in this list has a non-empty value. - // This may be used to include null fields in Patch requests. nullFields []string } diff --git a/service/account/providers/aws/awsexternalId.go b/service/account/providers/aws/awsexternalId.go index 9d538370..459182a9 100644 --- a/service/account/providers/aws/awsexternalId.go +++ b/service/account/providers/aws/awsexternalId.go @@ -15,20 +15,8 @@ type AwsAccountExternalId struct { AccountId *string `json:"accountId,omitempty"` ExternalId *string `json:"externalId,omitempty"` - // forceSendFields is a list of field names (e.g. "Keys") to - // unconditionally include in API requests. By default, fields with - // empty values are omitted from API requests. However, any non-pointer, - // non-interface field appearing in ForceSendFields will be sent to the - // server regardless of whether the field is empty or not. This may be - // used to include empty fields in Patch requests. forceSendFields []string - - // nullFields is a list of field names (e.g. "Keys") to include in API - // requests with the JSON null value. By default, fields with empty - // values are omitted from API requests. However, any field with an - // empty value appearing in NullFields will be sent to the server as - // null. It is an error if a field in this list has a non-empty value. - // This may be used to include null fields in Patch requests. + nullFields []string } From 2a2e4f797e501a5c16e7775a257191618f2eaea0 Mon Sep 17 00:00:00 2001 From: sharadkesarwani Date: Wed, 27 Sep 2023 13:46:53 +0530 Subject: [PATCH 12/14] Added description for field forceSendFields and nullFields --- service/account/providers/aws/awsexternalId.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/service/account/providers/aws/awsexternalId.go b/service/account/providers/aws/awsexternalId.go index 459182a9..9d538370 100644 --- a/service/account/providers/aws/awsexternalId.go +++ b/service/account/providers/aws/awsexternalId.go @@ -15,8 +15,20 @@ type AwsAccountExternalId struct { AccountId *string `json:"accountId,omitempty"` ExternalId *string `json:"externalId,omitempty"` + // forceSendFields is a list of field names (e.g. "Keys") to + // unconditionally include in API requests. By default, fields with + // empty values are omitted from API requests. However, any non-pointer, + // non-interface field appearing in ForceSendFields will be sent to the + // server regardless of whether the field is empty or not. This may be + // used to include empty fields in Patch requests. forceSendFields []string - + + // nullFields is a list of field names (e.g. "Keys") to include in API + // requests with the JSON null value. By default, fields with empty + // values are omitted from API requests. However, any field with an + // empty value appearing in NullFields will be sent to the server as + // null. It is an error if a field in this list has a non-empty value. + // This may be used to include null fields in Patch requests. nullFields []string } From 5225c99de35f967f7e3b679658fdc4925d87205f Mon Sep 17 00:00:00 2001 From: sharadkesarwani Date: Thu, 28 Sep 2023 17:55:51 +0530 Subject: [PATCH 13/14] Changed function name and updated all credential to credentials including struct Changed function name from SetCredential to Credentials and updated all credential to credentials including struct --- examples/service/awscredential/create/main.go | 4 +- examples/service/awscredential/read/main.go | 6 +-- .../account/providers/aws/awscredential.go | 50 +++++++++---------- service/account/providers/aws/service.go | 8 +-- 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/examples/service/awscredential/create/main.go b/examples/service/awscredential/create/main.go index 9c67c27d..187d1cb9 100644 --- a/examples/service/awscredential/create/main.go +++ b/examples/service/awscredential/create/main.go @@ -14,8 +14,8 @@ func main() { sess := session.New() svc := account.New(sess) ctx := context.Background() - _, err := svc.CloudProviderAWS().SetCredential(ctx, &aws.SetCredentialInput{ - Credential: &aws.Credential{ + _, err := svc.CloudProviderAWS().Credentials(ctx, &aws.SetCredentialsInput{ + Credentials: &aws.Credentials{ AccountId: spotinst.String("act-12345"), IamRole: spotinst.String("arn:aws:iam::12345:role/test-role"), }, diff --git a/examples/service/awscredential/read/main.go b/examples/service/awscredential/read/main.go index 0264112d..488080e6 100644 --- a/examples/service/awscredential/read/main.go +++ b/examples/service/awscredential/read/main.go @@ -15,7 +15,7 @@ func main() { sess := session.New() svc := account.New(sess) ctx := context.Background() - out, err := svc.CloudProviderAWS().ReadCredential(ctx, &aws.ReadCredentialInput{ + out, err := svc.CloudProviderAWS().ReadCredentials(ctx, &aws.ReadCredentialsInput{ AccountId: spotinst.String("act-12345"), }) @@ -24,8 +24,8 @@ func main() { } if out != nil { log.Printf("credential %q: %s", - spotinst.StringValue(out.Credential.AccountId), - stringutil.Stringify(out.Credential.IamRole)) + spotinst.StringValue(out.Credentials.AccountId), + stringutil.Stringify(out.Credentials.IamRole)) } } diff --git a/service/account/providers/aws/awscredential.go b/service/account/providers/aws/awscredential.go index d6758326..6b07b9a4 100644 --- a/service/account/providers/aws/awscredential.go +++ b/service/account/providers/aws/awscredential.go @@ -10,7 +10,7 @@ import ( "net/http" ) -type Credential struct { +type Credentials struct { IamRole *string `json:"iamRole,omitempty"` AccountId *string `json:"accountId,omitempty"` @@ -19,39 +19,39 @@ type Credential struct { nullFields []string } -func (o Credential) MarshalJSON() ([]byte, error) { - type noMethod Credential +func (o Credentials) MarshalJSON() ([]byte, error) { + type noMethod Credentials raw := noMethod(o) return jsonutil.MarshalJSON(raw, o.forceSendFields, o.nullFields) } -func (o *Credential) SetIamRole(v *string) *Credential { +func (o *Credentials) SetIamRole(v *string) *Credentials { if o.IamRole = v; o.IamRole == nil { o.nullFields = append(o.nullFields, "IamRole") } return o } -func (o *Credential) SetAccountId(v *string) *Credential { +func (o *Credentials) SetAccountId(v *string) *Credentials { if o.AccountId = v; o.AccountId == nil { o.nullFields = append(o.nullFields, "AccountId") } return o } -type SetCredentialInput struct { - Credential *Credential `json:"credentials,omitempty"` +type SetCredentialsInput struct { + Credentials *Credentials `json:"credentials,omitempty"` } -type SetCredentialOutput struct { - Credential *Credential `json:"Credential,omitempty"` +type SetCredentialsOutput struct { + Credentials *Credentials `json:"Credentials,omitempty"` } -func (s *ServiceOp) SetCredential(ctx context.Context, input *SetCredentialInput) (*SetCredentialOutput, error) { +func (s *ServiceOp) Credentials(ctx context.Context, input *SetCredentialsInput) (*SetCredentialsOutput, error) { r := client.NewRequest(http.MethodPost, "/setup/credentials/aws") if input != nil { - r.Params.Set("accountId", spotinst.StringValue(input.Credential.AccountId)) + r.Params.Set("accountId", spotinst.StringValue(input.Credentials.AccountId)) } - input.Credential.AccountId = nil + input.Credentials.AccountId = nil r.Obj = input resp, err := client.RequireOK(s.Client.DoOrg(ctx, r)) @@ -65,22 +65,22 @@ func (s *ServiceOp) SetCredential(ctx context.Context, input *SetCredentialInput return nil, err } - output := new(SetCredentialOutput) + output := new(SetCredentialsOutput) if len(gs) > 0 { - output.Credential = gs[0] + output.Credentials = gs[0] } return output, nil } -type ReadCredentialInput struct { +type ReadCredentialsInput struct { AccountId *string `json:"accountId,omitempty"` } -type ReadCredentialOutput struct { - Credential *Credential `json:"Credential,omitempty"` +type ReadCredentialsOutput struct { + Credentials *Credentials `json:"Credentials,omitempty"` } -func (s *ServiceOp) ReadCredential(ctx context.Context, input *ReadCredentialInput) (*ReadCredentialOutput, error) { +func (s *ServiceOp) ReadCredentials(ctx context.Context, input *ReadCredentialsInput) (*ReadCredentialsOutput, error) { r := client.NewRequest(http.MethodGet, "/setup/credentials/aws") if input != nil { r.Params.Set("accountId", spotinst.StringValue(input.AccountId)) @@ -97,15 +97,15 @@ func (s *ServiceOp) ReadCredential(ctx context.Context, input *ReadCredentialInp return nil, err } - output := new(ReadCredentialOutput) + output := new(ReadCredentialsOutput) if len(gs) > 0 { - output.Credential = gs[0] + output.Credentials = gs[0] } return output, nil } -func credentialsFromHttpResponse(resp *http.Response) ([]*Credential, error) { +func credentialsFromHttpResponse(resp *http.Response) ([]*Credentials, error) { body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err @@ -113,12 +113,12 @@ func credentialsFromHttpResponse(resp *http.Response) ([]*Credential, error) { return credentialsFromJSON(body) } -func credentialsFromJSON(in []byte) ([]*Credential, error) { +func credentialsFromJSON(in []byte) ([]*Credentials, error) { var rw client.Response if err := json.Unmarshal(in, &rw); err != nil { return nil, err } - out := make([]*Credential, len(rw.Response.Items)) + out := make([]*Credentials, len(rw.Response.Items)) if len(out) == 0 { return out, nil } @@ -132,8 +132,8 @@ func credentialsFromJSON(in []byte) ([]*Credential, error) { return out, nil } -func credentialFromJSON(in []byte) (*Credential, error) { - b := new(Credential) +func credentialFromJSON(in []byte) (*Credentials, error) { + b := new(Credentials) if err := json.Unmarshal(in, b); err != nil { return nil, err } diff --git a/service/account/providers/aws/service.go b/service/account/providers/aws/service.go index 98846a30..94a3ae0c 100644 --- a/service/account/providers/aws/service.go +++ b/service/account/providers/aws/service.go @@ -13,7 +13,7 @@ import ( // the service. type Service interface { serviceAccount - serviceCredential + serviceCredentials serviceAwsAccountExternalId } @@ -22,9 +22,9 @@ type serviceAccount interface { DeleteAccount(context.Context, *DeleteAccountInput) (*DeleteAccountOutput, error) ReadAccount(context.Context, *ReadAccountInput) (*ReadAccountOutput, error) } -type serviceCredential interface { - SetCredential(context.Context, *SetCredentialInput) (*SetCredentialOutput, error) - ReadCredential(context.Context, *ReadCredentialInput) (*ReadCredentialOutput, error) +type serviceCredentials interface { + Credentials(context.Context, *SetCredentialsInput) (*SetCredentialsOutput, error) + ReadCredentials(context.Context, *ReadCredentialsInput) (*ReadCredentialsOutput, error) } type serviceAwsAccountExternalId interface { CreateAWSAccountExternalId(context.Context, *CreateAWSAccountExternalIdInput) (*CreateAWSAccountExternalIdOutput, error) From 2e58c6e4bf47f93b49e0c80bee0436a89cafb77f Mon Sep 17 00:00:00 2001 From: sharadkesarwani Date: Thu, 28 Sep 2023 20:00:18 +0530 Subject: [PATCH 14/14] modified file and folder name. modified file name awscredential.go to awscredentials.go and folder name awscredential to awscredentials under example folder --- .../service/{awscredential => awscredentials}/create/main.go | 4 ++-- .../service/{awscredential => awscredentials}/read/main.go | 2 +- .../providers/aws/{awscredential.go => awscredentials.go} | 0 3 files changed, 3 insertions(+), 3 deletions(-) rename examples/service/{awscredential => awscredentials}/create/main.go (81%) rename examples/service/{awscredential => awscredentials}/read/main.go (94%) rename service/account/providers/aws/{awscredential.go => awscredentials.go} (100%) diff --git a/examples/service/awscredential/create/main.go b/examples/service/awscredentials/create/main.go similarity index 81% rename from examples/service/awscredential/create/main.go rename to examples/service/awscredentials/create/main.go index 187d1cb9..a175377d 100644 --- a/examples/service/awscredential/create/main.go +++ b/examples/service/awscredentials/create/main.go @@ -16,8 +16,8 @@ func main() { ctx := context.Background() _, err := svc.CloudProviderAWS().Credentials(ctx, &aws.SetCredentialsInput{ Credentials: &aws.Credentials{ - AccountId: spotinst.String("act-12345"), - IamRole: spotinst.String("arn:aws:iam::12345:role/test-role"), + AccountId: spotinst.String("act-c4842ba3"), + IamRole: spotinst.String("arn:aws:iam::253244684816:role/terraform-role-sept"), }, }) diff --git a/examples/service/awscredential/read/main.go b/examples/service/awscredentials/read/main.go similarity index 94% rename from examples/service/awscredential/read/main.go rename to examples/service/awscredentials/read/main.go index 488080e6..02612b8f 100644 --- a/examples/service/awscredential/read/main.go +++ b/examples/service/awscredentials/read/main.go @@ -16,7 +16,7 @@ func main() { svc := account.New(sess) ctx := context.Background() out, err := svc.CloudProviderAWS().ReadCredentials(ctx, &aws.ReadCredentialsInput{ - AccountId: spotinst.String("act-12345"), + AccountId: spotinst.String("act-c4842ba3"), }) if err != nil { diff --git a/service/account/providers/aws/awscredential.go b/service/account/providers/aws/awscredentials.go similarity index 100% rename from service/account/providers/aws/awscredential.go rename to service/account/providers/aws/awscredentials.go