Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for account, externalId and aws credentials related API support with example. #261

Merged
merged 14 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions examples/service/account/create/main.go
Original file line number Diff line number Diff line change
@@ -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{
Account: &aws.Account{
Name: spotinst.String("testAcct_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))
}

}
24 changes: 24 additions & 0 deletions examples/service/account/delete/main.go
Original file line number Diff line number Diff line change
@@ -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{
AccountID: spotinst.String("act-123456"),
})

if err != nil {
log.Fatalf("spotinst: failed to delete account: %v", err)
}

}
31 changes: 31 additions & 0 deletions examples/service/account/read/main.go
Original file line number Diff line number Diff line change
@@ -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-123456"),
})

if err != nil {
log.Fatalf("spotinst: faccount not found: %v", err)
}

if out.Account != nil {
log.Printf("Account %q: %s",
spotinst.StringValue(out.Account.ID),
stringutil.Stringify(out.Account))
}

}
28 changes: 28 additions & 0 deletions examples/service/awscredential/create/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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/session"
)

func main() {
sess := session.New()
svc := account.New(sess)
ctx := context.Background()
_, err := svc.CloudProviderAWS().SetCredential(ctx, &aws.SetCredentialInput{
Credential: &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 set credential: %v", err)
}

}
31 changes: 31 additions & 0 deletions examples/service/awscredential/read/main.go
Original file line number Diff line number Diff line change
@@ -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))
}

}
37 changes: 37 additions & 0 deletions service/account/account.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
182 changes: 182 additions & 0 deletions service/account/providers/aws/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package aws
sharadkesarwani marked this conversation as resolved.
Show resolved Hide resolved

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"`
sharadkesarwani marked this conversation as resolved.
Show resolved Hide resolved

// 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) 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)
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.DoOrg(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) {
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.DoOrg(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.DoOrg(ctx, r))
if err != nil {
return nil, err
}
defer resp.Body.Close()

return &DeleteAccountOutput{}, nil
}
Loading
Loading