Skip to content

Commit 2f187f3

Browse files
authored
Allow reading creds as env var for account mgmt commands (#604)
* Update account mgmt cmds * Move const to cmd.go
1 parent 6f3c3c7 commit 2f187f3

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

cmd/account/mgmt/account-assign.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mgmt
33
import (
44
"fmt"
55
"math/rand"
6+
"os"
67
"time"
78

89
"github.com/aws/aws-sdk-go-v2/service/organizations"
@@ -80,9 +81,6 @@ func (o *accountAssignOptions) complete(cmd *cobra.Command, _ []string) error {
8081
if o.username == "" {
8182
return cmdutil.UsageErrorf(cmd, "LDAP username was not provided")
8283
}
83-
if o.payerAccount == "" {
84-
return cmdutil.UsageErrorf(cmd, "Payer account was not provided")
85-
}
8684

8785
o.output = o.GlobalOptions.Output
8886

@@ -97,10 +95,10 @@ func (o *accountAssignOptions) run() error {
9795
rootID string
9896
)
9997

100-
if o.payerAccount == "osd-staging-1" {
98+
if o.payerAccount == osdStaging1 || os.Getenv(envKeyAWSAccountName) == osdStaging1 {
10199
rootID = OSDStaging1RootID
102100
destinationOU = OSDStaging1OuID
103-
} else if o.payerAccount == "osd-staging-2" {
101+
} else if o.payerAccount == osdStaging2 || os.Getenv(envKeyAWSAccountName) == osdStaging2 {
104102
rootID = OSDStaging2RootID
105103
destinationOU = OSDStaging2OuID
106104
} else {

cmd/account/mgmt/account-list.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mgmt
22

33
import (
44
"fmt"
5+
"os"
56

67
"github.com/aws/aws-sdk-go-v2/service/organizations"
78
"github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi"
@@ -67,9 +68,6 @@ func newCmdAccountList(streams genericclioptions.IOStreams, globalOpts *globalfl
6768
}
6869

6970
func (o *accountListOptions) complete(cmd *cobra.Command, _ []string) error {
70-
if o.payerAccount == "" {
71-
return cmdutil.UsageErrorf(cmd, "Payer account was not provided")
72-
}
7371
if o.username != "" && o.accountID != "" {
7472
return cmdutil.UsageErrorf(cmd, "Cannot provide both username and account ID")
7573
}
@@ -84,15 +82,16 @@ func (o *accountListOptions) run() error {
8482
var (
8583
OuID string
8684
)
85+
8786
// Instantiate Aws client
8887
awsClient, err := awsprovider.NewAwsClient(o.payerAccount, "us-east-1", "")
8988
if err != nil {
9089
return err
9190
}
9291

93-
if o.payerAccount == "osd-staging-2" {
92+
if o.payerAccount == osdStaging2 || os.Getenv(envKeyAWSAccountName) == osdStaging2 {
9493
OuID = "ou-rs3h-ry0hn2l9"
95-
} else if o.payerAccount == "osd-staging-1" {
94+
} else if o.payerAccount == osdStaging1 || os.Getenv(envKeyAWSAccountName) == osdStaging1 {
9695
OuID = "ou-0wd6-z6tzkjek"
9796
}
9897

cmd/account/mgmt/account-unassign.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ func newAccountUnassignOptions(streams genericclioptions.IOStreams) *accountUnas
5353
}
5454
}
5555
func (o *accountUnassignOptions) complete(cmd *cobra.Command, _ []string) error {
56-
if o.payerAccount == "" {
57-
return cmdutil.UsageErrorf(cmd, "Payer account was not provided")
58-
}
5956
if o.username == "" && o.accountID == "" {
6057
return cmdutil.UsageErrorf(cmd, "Please provide either an username or account ID")
6158
}
@@ -71,24 +68,25 @@ func (o *accountUnassignOptions) run() error {
7168
destinationOU string
7269
rootID string
7370
assumedRoleAwsClient awsprovider.Client
71+
allUsers []string
7472
)
73+
7574
// Instantiate Aws client
7675
awsClient, err := awsprovider.NewAwsClient(o.payerAccount, "us-east-1", "")
7776
if err != nil {
7877
return err
7978
}
80-
if o.payerAccount == "osd-staging-1" {
79+
if o.payerAccount == osdStaging1 || os.Getenv(envKeyAWSAccountName) == osdStaging1 {
8180
rootID = OSDStaging1RootID
8281
destinationOU = OSDStaging1OuID
83-
} else if o.payerAccount == "osd-staging-2" {
82+
} else if o.payerAccount == osdStaging2 || os.Getenv(envKeyAWSAccountName) == osdStaging2 {
8483
rootID = OSDStaging2RootID
8584
destinationOU = OSDStaging2OuID
8685
} else {
8786
return fmt.Errorf("invalid payer account provided")
8887
}
8988

9089
o.awsClient = awsClient
91-
var allUsers []string
9290

9391
if o.accountID != "" {
9492
// Check aws tag to see if it's a ccs acct, if it's not return name of owner

cmd/account/mgmt/cmd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import (
88
"k8s.io/cli-runtime/pkg/genericclioptions"
99
)
1010

11+
const (
12+
osdStaging1 = "osd-staging-1"
13+
osdStaging2 = "osd-staging-2"
14+
envKeyAWSAccountName = "AWS_ACCOUNT_NAME"
15+
)
16+
1117
// NewCmdMgmt implements the mgmt command to get AWS Account resources
1218
func NewCmdMgmt(streams genericclioptions.IOStreams, globalOpts *globalflags.GlobalOptions) *cobra.Command {
1319
mgmtCmd := &cobra.Command{

0 commit comments

Comments
 (0)