Skip to content

Commit

Permalink
Added support for account, externalId and aws credentials support (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadkesarwani authored Sep 27, 2023
1 parent 7096568 commit 32819e8
Show file tree
Hide file tree
Showing 14 changed files with 814 additions and 0 deletions.
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))
}

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

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

}
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,
}
}
Loading

0 comments on commit 32819e8

Please sign in to comment.