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 }