From 999c979c4559f04371c35dfca6625f56b1f16c44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois-Xavier=20Aguessy?= Date: Fri, 17 Feb 2017 14:21:22 +0100 Subject: [PATCH] Support AWS profile switch via config key. Closes #33. --- CHANGELOG.md | 5 +++++ cloud/aws/init.go | 8 ++++---- commands/hooks.go | 5 +++-- config/conf.go | 6 +++++- database/defaults.go | 1 + 5 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 000000000..3e9e46d27 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +## 0.0.14 [unreleased] + +### Features + +- [#33](https://github.com/wallix/awless/issues/33): Ability to set AWS profile using `aws.profile` config key \ No newline at end of file diff --git a/cloud/aws/init.go b/cloud/aws/init.go index 0488f6182..ead883a53 100644 --- a/cloud/aws/init.go +++ b/cloud/aws/init.go @@ -31,13 +31,13 @@ var ( SecuAPI Security ) -func InitSession(region string) (*session.Session, error) { +func InitSession(region, profile string) (*session.Session, error) { session, err := session.NewSession( &awssdk.Config{ Region: awssdk.String(region), Credentials: credentials.NewChainCredentials([]credentials.Provider{ &credentials.EnvProvider{}, - &credentials.SharedCredentialsProvider{Filename: "", Profile: ""}, + &credentials.SharedCredentialsProvider{Filename: "", Profile: profile}, })}) if err != nil { return nil, err @@ -52,8 +52,8 @@ Installation documentation is at https://github.com/wallix/awless/wiki/Installat return session, nil } -func InitServices(region string) error { - sess, err := InitSession(region) +func InitServices(region, profile string) error { + sess, err := InitSession(region, profile) if err != nil { return err } diff --git a/commands/hooks.go b/commands/hooks.go index c9c2adabb..31e4df57b 100644 --- a/commands/hooks.go +++ b/commands/hooks.go @@ -50,12 +50,13 @@ func initAwlessEnvHook(cmd *cobra.Command, args []string) error { } func initCloudServicesHook(cmd *cobra.Command, args []string) error { - region := os.Getenv("__AWLESS_REGION") + region := os.Getenv("__AWLESS_CLOUD_REGION") if region == "" { return errors.New("region should be in env") } + profile := os.Getenv("__AWLESS_CLOUD_PROFILE") - if err := aws.InitServices(region); err != nil { + if err := aws.InitServices(region, profile); err != nil { return err } diff --git a/config/conf.go b/config/conf.go index c0588201f..4913c8f45 100644 --- a/config/conf.go +++ b/config/conf.go @@ -51,6 +51,7 @@ func InitAwlessEnv() error { os.MkdirAll(KeysDir, 0700) var region string + var profile string if AwlessFirstInstall { fmt.Println("First install. Welcome!\n") @@ -64,10 +65,12 @@ func InitAwlessEnv() error { return fmt.Errorf("init env: database error: %s", err) } defer close() + profile, _ = db.GetDefaultString(database.ProfileKey) region = db.MustGetDefaultRegion() } - os.Setenv("__AWLESS_REGION", region) + os.Setenv("__AWLESS_CLOUD_REGION", region) + os.Setenv("__AWLESS_CLOUD_PROFILE", profile) return nil } @@ -96,6 +99,7 @@ func resolveAndSetDefaults() (string, error) { database.RegionKey: region, database.InstanceTypeKey: "t2.micro", database.InstanceCountKey: 1, + database.ProfileKey: "default", } if hasAMI { diff --git a/database/defaults.go b/database/defaults.go index ecd8e326d..5c8e77f41 100644 --- a/database/defaults.go +++ b/database/defaults.go @@ -29,6 +29,7 @@ const ( InstanceTypeKey = "instance.type" InstanceImageKey = "instance.image" InstanceCountKey = "instance.count" + ProfileKey = "aws.profile" ) type defaults map[string]interface{}