Skip to content

Commit

Permalink
Support AWS profile switch via config key. Closes #33.
Browse files Browse the repository at this point in the history
  • Loading branch information
fxaguessy committed Feb 17, 2017
1 parent d535130 commit 999c979
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions cloud/aws/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions commands/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 5 additions & 1 deletion config/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}
Expand Down Expand Up @@ -96,6 +99,7 @@ func resolveAndSetDefaults() (string, error) {
database.RegionKey: region,
database.InstanceTypeKey: "t2.micro",
database.InstanceCountKey: 1,
database.ProfileKey: "default",
}

if hasAMI {
Expand Down
1 change: 1 addition & 0 deletions database/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
InstanceTypeKey = "instance.type"
InstanceImageKey = "instance.image"
InstanceCountKey = "instance.count"
ProfileKey = "aws.profile"
)

type defaults map[string]interface{}
Expand Down

0 comments on commit 999c979

Please sign in to comment.