diff --git a/.gitignore b/.gitignore index 6d05167..d5cd922 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,6 @@ dist coverage.txt node_modules + +# ignore the executable +aws-mfa-login diff --git a/README.md b/README.md index 2812bfb..5b608f6 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ clusters: accountId: "1234" role: DeveloperAccessRole region: eu-central-1 + destination: superProfile #overrides default destination - name: eks-prod alias: suite-academic accountId: "4321" diff --git a/action/awsconfig.go b/action/awsconfig.go index f874920..9e477c0 100644 --- a/action/awsconfig.go +++ b/action/awsconfig.go @@ -2,11 +2,12 @@ package action import ( "fmt" + "log" + "os" + "github.com/ghodss/yaml" "github.com/go-ini/ini" "github.com/spf13/viper" - "log" - "os" ) type State int @@ -24,11 +25,12 @@ type Clusters struct { } type ClusterConfig struct { - Name string `yaml:"name"` - Alias string `yaml:"alias"` - AccountID string `yaml:"accountId"` - Role string `yaml:"role"` - Region string `yaml:"region"` + Name string `yaml:"name"` + Alias string `yaml:"alias"` + AccountID string `yaml:"accountId"` + Role string `yaml:"role"` + Region string `yaml:"region"` + Destination string `yaml:"destination"` } func (clusters *Clusters) InitConfig() { @@ -87,7 +89,12 @@ func (c *ClusterConfig) Write(filePath string) (State, error) { if err != nil { return Error, err } - _, err = section.NewKey("source_profile", viper.GetString("destination")) + + destinationProfile := viper.GetString("destination") + if c.Destination != "" { + destinationProfile = c.Destination + } + _, err = section.NewKey("source_profile", destinationProfile) if err != nil { return Error, err } diff --git a/action/awsconfig_test.go b/action/awsconfig_test.go index fbc45de..e26eac9 100644 --- a/action/awsconfig_test.go +++ b/action/awsconfig_test.go @@ -2,13 +2,14 @@ package action import ( "fmt" - "github.com/go-ini/ini" - "github.com/spf13/viper" - "github.com/stretchr/testify/assert" "io/ioutil" "log" "os" "testing" + + "github.com/go-ini/ini" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" ) func TestWrite(t *testing.T) { @@ -25,6 +26,13 @@ func TestWrite(t *testing.T) { AccountID: "123456", Role: "admin", }, + { + Name: "testname3", + Alias: "testalias3", + AccountID: "123456", + Role: "admin", + Destination: "altProfile", + }, } viper.Set("clusters", conf) viper.Set("destination", "test-mfa") @@ -74,12 +82,24 @@ func TestWrite(t *testing.T) { } // one section DEFAULT will be added by default - assert.ElementsMatch(t, result.SectionStrings(), []string{"DEFAULT", conf[0].Alias, conf[1].Alias}) + assert.ElementsMatch(t, result.SectionStrings(), []string{"DEFAULT", conf[0].Alias, conf[1].Alias, conf[2].Alias}) assert.Equal(t, foundRole.Value(), getArn(conf[0].AccountID, conf[0].Role)) assert.Equal(t, foundProfile.Value(), viper.GetString("destination")) - assert.Equal(t, clusters.states[Created], 2) - assert.Equal(t, clusters.states[Updated], 0) - assert.Equal(t, clusters.states[Deleted], 0) + assert.Equal(t, 3, clusters.states[Created], "Expected 3 cluster to be created") + assert.Equal(t, 0, clusters.states[Updated], "Expected no cluster to be updated") + assert.Equal(t, 0, clusters.states[Deleted], "Expected no cluster to be deleted") + + foundSectionDestinationModified, err := result.GetSection(conf[2].Alias) + if err != nil { + log.Fatal(err) + } + foundDestination, err := foundSectionDestinationModified.GetKey("source_profile") + if err != nil { + log.Fatal(err) + } + + assert.Equal(t, conf[2].Destination, foundDestination.Value(), "expect that destination has been overwritten") + assert.NotEqual(t, conf[2].Destination, viper.GetString("destination"), "expect that the profile does no return the default") modified := &ClusterConfig{ Name: "changed", @@ -112,7 +132,7 @@ func TestWrite(t *testing.T) { log.Fatal(err) } - assert.Len(t, result.SectionStrings(), 3) + assert.Len(t, result.SectionStrings(), 4) assert.Equal(t, foundRole.Value(), getArn(modified.AccountID, modified.Role)) assert.Equal(t, foundProfile.Value(), viper.GetString("destination")) assert.Equal(t, state, Updated)