Skip to content

Commit

Permalink
remove dependency on the cli package from aws
Browse files Browse the repository at this point in the history
  • Loading branch information
ianic committed Dec 26, 2021
1 parent 828035b commit 8bc5961
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
23 changes: 14 additions & 9 deletions aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/aws/smithy-go"
"github.com/aws/smithy-go/transport/http"
"github.com/mantil-io/mantil/cli/log"
)

type AWS struct {
Expand All @@ -38,6 +37,12 @@ type AWS struct {
accountID string
}

var errAwsRegionNotSet = fmt.Errorf("AWS region not set")

func errLoadDefaultConfig(err error) error {
return fmt.Errorf("unable to load SDK configuration - %w", err)
}

func NewWithCredentials(accessKeyID, secretAccessKey, sessionToken, region string) (*AWS, error) {
config, err := config.LoadDefaultConfig(context.Background(),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
Expand All @@ -47,10 +52,10 @@ func NewWithCredentials(accessKeyID, secretAccessKey, sessionToken, region strin
config.WithRegion(region),
)
if err != nil {
return nil, fmt.Errorf("unable to load SDK configuration - %w", err)
return nil, errLoadDefaultConfig(err)
}
if config.Region == "" {
return nil, fmt.Errorf("aws region not set")
return nil, errAwsRegionNotSet
}
return clientFromConfig(config)
}
Expand All @@ -64,21 +69,21 @@ func NewWithEndpointCredentials(endpoint, region string, token func() string) (*
config.WithRegion(region),
)
if err != nil {
return nil, log.Wrap(err)
return nil, errLoadDefaultConfig(err)
}
if config.Region == "" {
return nil, log.Wrapf("aws region not set")
return nil, errAwsRegionNotSet
}
return clientFromConfig(config)
}

func New() (*AWS, error) {
config, err := config.LoadDefaultConfig(context.Background())
if err != nil {
return nil, fmt.Errorf("unable to load SDK configuration - %w", err)
return nil, errLoadDefaultConfig(err)
}
if config.Region == "" {
return nil, fmt.Errorf("aws region not set")
return nil, errAwsRegionNotSet
}
return clientFromConfig(config)
}
Expand All @@ -89,10 +94,10 @@ func NewFromProfile(profile string) (*AWS, error) {
config.WithSharedConfigProfile(profile),
)
if err != nil {
return nil, fmt.Errorf("unable to load SDK configuration - %w", err)
return nil, errLoadDefaultConfig(err)
}
if config.Region == "" {
return nil, fmt.Errorf("aws region not set")
return nil, errAwsRegionNotSet
}
return clientFromConfig(config)
}
Expand Down
22 changes: 11 additions & 11 deletions aws/cloudformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package aws
import (
"context"
"fmt"
"log"
"strings"
"sync"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
"github.com/mantil-io/mantil/cli/log"
)

type CloudFormation struct {
Expand Down Expand Up @@ -47,7 +47,7 @@ func (f *CloudFormation) CreateStack(name, templateBody string, resourceTags map
go func() {
cso, err := f.cli.CreateStack(context.Background(), csi)
if err != nil {
sw.close(log.Wrap(err))
sw.close(err)
return
}
w := cloudformation.NewStackCreateCompleteWaiter(f.cli, func(opts *cloudformation.StackCreateCompleteWaiterOptions) {
Expand All @@ -60,10 +60,10 @@ func (f *CloudFormation) CreateStack(name, templateBody string, resourceTags map
if err := w.Wait(context.Background(), dsi, 5*time.Minute); err != nil {
reason := f.stackActionFailedReason(aws.ToString(cso.StackId))
if reason != "" {
sw.close(log.Wrapf("could not create stack %s - %s", name, reason))
sw.close(fmt.Errorf("could not create stack %s - %s", name, reason))
return
}
sw.close(log.Wrapf("could not create stack %s", name))
sw.close(fmt.Errorf("could not create stack %s", name))
return
}
sw.close(nil)
Expand Down Expand Up @@ -92,7 +92,7 @@ func (f *CloudFormation) UpdateStack(name, templateBody string, resourceTags map
go func() {
cso, err := f.cli.UpdateStack(context.Background(), usi)
if err != nil {
sw.close(log.Wrap(err))
sw.close(err)
return
}
w := cloudformation.NewStackUpdateCompleteWaiter(f.cli, func(opts *cloudformation.StackUpdateCompleteWaiterOptions) {
Expand All @@ -105,10 +105,10 @@ func (f *CloudFormation) UpdateStack(name, templateBody string, resourceTags map
if err := w.Wait(context.Background(), dsi, 5*time.Minute); err != nil {
reason := f.stackActionFailedReason(aws.ToString(cso.StackId))
if reason != "" {
sw.close(log.Wrapf("could not update stack %s - %s", name, reason))
sw.close(fmt.Errorf("could not update stack %s - %s", name, reason))
return
}
sw.close(log.Wrapf("could not update stack %s", name))
sw.close(fmt.Errorf("could not update stack %s", name))
return
}
sw.close(nil)
Expand All @@ -124,7 +124,7 @@ func (f *CloudFormation) DeleteStack(name string) *StackWaiter {
}
_, err := f.cli.DeleteStack(context.Background(), dsi)
if err != nil {
sw.close(log.Wrap(err, fmt.Sprintf("could not delete stack %s", name)))
sw.close(fmt.Errorf("could not delete stack %s, error: %w", name, err))
return
}
w := cloudformation.NewStackDeleteCompleteWaiter(f.cli, func(opts *cloudformation.StackDeleteCompleteWaiterOptions) {
Expand All @@ -137,10 +137,10 @@ func (f *CloudFormation) DeleteStack(name string) *StackWaiter {
if err := w.Wait(context.Background(), descsi, 5*time.Minute); err != nil {
reason := f.stackActionFailedReason(name)
if reason != "" {
sw.close(log.Wrapf("could not delete stack %s - %s", name, reason))
sw.close(fmt.Errorf("could not delete stack %s - %s", name, reason))
return
}
sw.close(log.Wrapf("could not delete stack %s", name))
sw.close(fmt.Errorf("could not delete stack %s", name))
return
}
sw.close(nil)
Expand Down Expand Up @@ -181,7 +181,7 @@ func (f *CloudFormation) stackEvents(stack string, from *time.Time) ([]types.Sta
}
dseo, err := f.cli.DescribeStackEvents(context.Background(), dsei)
if err != nil {
return nil, log.Wrap(err)
return nil, err
}
events = append(events, dseo.StackEvents...)
if dseo.NextToken == nil {
Expand Down

0 comments on commit 8bc5961

Please sign in to comment.