Skip to content

Commit

Permalink
Update aws config mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
stilvoid committed Mar 1, 2021
1 parent 2c3c720 commit 97582f0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
9 changes: 0 additions & 9 deletions internal/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,9 @@ func MFAProvider() (string, error) {
var awsCfg *aws.Config
var creds aws.Credentials

// For debug resolver
type uaResolver string

var defaultSessionName = fmt.Sprintf("%s-%s", config.NAME, config.VERSION)
var lastSessionName = defaultSessionName

func (u uaResolver) ResolveEndpoint(service string, region string) (aws.Endpoint, error) {
return aws.Endpoint{
URL: string(u),
}, nil
}

func loadConfig(ctx context.Context, sessionName string) *aws.Config {
// Credential configs
var configs = make([]func(*awsconfig.LoadOptions) error, 0)
Expand Down
2 changes: 1 addition & 1 deletion internal/aws/cfn/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func GetChangeSet(stackName, changeSetName string) (*cloudformation.DescribeChan
Details: []types.ResourceChangeDetail{
{
CausingEntity: ptr.String("mock entity"),
ChangeSource: types.ChangeSourceResourceattribute,
ChangeSource: types.ChangeSourceResourceAttribute,
Evaluation: types.EvaluationTypeDynamic,
Target: &types.ResourceTargetDefinition{
Attribute: types.ResourceAttributeProperties,
Expand Down
27 changes: 21 additions & 6 deletions internal/aws/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package aws

import (
"context"
"fmt"
"os"

"github.com/aws-cloudformation/rain/internal/config"
Expand All @@ -14,7 +15,10 @@ import (

var awsCfg *aws.Config

func loadConfig(ctx context.Context) aws.Config {
var defaultSessionName = fmt.Sprintf("%s-%s", config.NAME, config.VERSION)
var lastSessionName = defaultSessionName

func loadConfig(ctx context.Context, sessionName string) *aws.Config {
cfg := aws.Config{}

if config.Region != "" {
Expand All @@ -23,18 +27,29 @@ func loadConfig(ctx context.Context) aws.Config {
cfg.Region = r
}

return cfg
lastSessionName = sessionName

return &cfg
}

// Config loads an aws.Config based on current settings
func Config() aws.Config {
if awsCfg == nil {
spinner.Push("Loading AWS config")
return NamedConfig(defaultSessionName)
}

cfg := loadConfig(context.Background())
// NamedConfig loads an aws.Config based on current settings
// with configurable session name
func NamedConfig(sessionName string) aws.Config {
message := "Loading AWS config"

awsCfg = &cfg
if sessionName != lastSessionName {
message = "Reloading AWS credentials"
awsCfg = nil
}

if awsCfg == nil {
spinner.Push(message)
awsCfg = loadConfig(context.Background(), sessionName)
spinner.Pop()
}

Expand Down

0 comments on commit 97582f0

Please sign in to comment.