Skip to content

Commit

Permalink
Allow reading creds as env var for account mgmt commands (#604)
Browse files Browse the repository at this point in the history
* Update account mgmt cmds

* Move const to cmd.go
  • Loading branch information
sam-nguyen7 authored Aug 26, 2024
1 parent 6f3c3c7 commit 2f187f3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
8 changes: 3 additions & 5 deletions cmd/account/mgmt/account-assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mgmt
import (
"fmt"
"math/rand"
"os"
"time"

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

o.output = o.GlobalOptions.Output

Expand All @@ -97,10 +95,10 @@ func (o *accountAssignOptions) run() error {
rootID string
)

if o.payerAccount == "osd-staging-1" {
if o.payerAccount == osdStaging1 || os.Getenv(envKeyAWSAccountName) == osdStaging1 {
rootID = OSDStaging1RootID
destinationOU = OSDStaging1OuID
} else if o.payerAccount == "osd-staging-2" {
} else if o.payerAccount == osdStaging2 || os.Getenv(envKeyAWSAccountName) == osdStaging2 {
rootID = OSDStaging2RootID
destinationOU = OSDStaging2OuID
} else {
Expand Down
9 changes: 4 additions & 5 deletions cmd/account/mgmt/account-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mgmt

import (
"fmt"
"os"

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

func (o *accountListOptions) complete(cmd *cobra.Command, _ []string) error {
if o.payerAccount == "" {
return cmdutil.UsageErrorf(cmd, "Payer account was not provided")
}
if o.username != "" && o.accountID != "" {
return cmdutil.UsageErrorf(cmd, "Cannot provide both username and account ID")
}
Expand All @@ -84,15 +82,16 @@ func (o *accountListOptions) run() error {
var (
OuID string
)

// Instantiate Aws client
awsClient, err := awsprovider.NewAwsClient(o.payerAccount, "us-east-1", "")
if err != nil {
return err
}

if o.payerAccount == "osd-staging-2" {
if o.payerAccount == osdStaging2 || os.Getenv(envKeyAWSAccountName) == osdStaging2 {
OuID = "ou-rs3h-ry0hn2l9"
} else if o.payerAccount == "osd-staging-1" {
} else if o.payerAccount == osdStaging1 || os.Getenv(envKeyAWSAccountName) == osdStaging1 {
OuID = "ou-0wd6-z6tzkjek"
}

Expand Down
10 changes: 4 additions & 6 deletions cmd/account/mgmt/account-unassign.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ func newAccountUnassignOptions(streams genericclioptions.IOStreams) *accountUnas
}
}
func (o *accountUnassignOptions) complete(cmd *cobra.Command, _ []string) error {
if o.payerAccount == "" {
return cmdutil.UsageErrorf(cmd, "Payer account was not provided")
}
if o.username == "" && o.accountID == "" {
return cmdutil.UsageErrorf(cmd, "Please provide either an username or account ID")
}
Expand All @@ -71,24 +68,25 @@ func (o *accountUnassignOptions) run() error {
destinationOU string
rootID string
assumedRoleAwsClient awsprovider.Client
allUsers []string
)

// Instantiate Aws client
awsClient, err := awsprovider.NewAwsClient(o.payerAccount, "us-east-1", "")
if err != nil {
return err
}
if o.payerAccount == "osd-staging-1" {
if o.payerAccount == osdStaging1 || os.Getenv(envKeyAWSAccountName) == osdStaging1 {
rootID = OSDStaging1RootID
destinationOU = OSDStaging1OuID
} else if o.payerAccount == "osd-staging-2" {
} else if o.payerAccount == osdStaging2 || os.Getenv(envKeyAWSAccountName) == osdStaging2 {
rootID = OSDStaging2RootID
destinationOU = OSDStaging2OuID
} else {
return fmt.Errorf("invalid payer account provided")
}

o.awsClient = awsClient
var allUsers []string

if o.accountID != "" {
// Check aws tag to see if it's a ccs acct, if it's not return name of owner
Expand Down
6 changes: 6 additions & 0 deletions cmd/account/mgmt/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
)

const (
osdStaging1 = "osd-staging-1"
osdStaging2 = "osd-staging-2"
envKeyAWSAccountName = "AWS_ACCOUNT_NAME"
)

// NewCmdMgmt implements the mgmt command to get AWS Account resources
func NewCmdMgmt(streams genericclioptions.IOStreams, globalOpts *globalflags.GlobalOptions) *cobra.Command {
mgmtCmd := &cobra.Command{
Expand Down

0 comments on commit 2f187f3

Please sign in to comment.