Skip to content

Commit a22dd0f

Browse files
feat: Support custom AWS region for accounts (#581)
1 parent 7eb348a commit a22dd0f

5 files changed

Lines changed: 29 additions & 4 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/AlecAivazis/survey/v2 v2.3.7
77
github.com/MakeNowJust/heredoc/v2 v2.0.1
88
github.com/OctopusDeploy/go-octodiff v1.0.0
9-
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.99.0
9+
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.103.0
1010
github.com/bmatcuk/doublestar/v4 v4.4.0
1111
github.com/briandowns/spinner v1.19.0
1212
github.com/google/uuid v1.3.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ github.com/OctopusDeploy/go-octodiff v1.0.0 h1:U+ORg6azniwwYo+O44giOw6TiD5USk8S4
4848
github.com/OctopusDeploy/go-octodiff v1.0.0/go.mod h1:Mze0+EkOWTgTmi8++fyUc6r0aLZT7qD9gX+31t8MmIU=
4949
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.99.0 h1:0HzgNBPiOGY7ekP+uoRbX1DeMs0Y2JpJ3ecmUxFtC1o=
5050
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.99.0/go.mod h1:VkTXDoIPbwGFi5+goo1VSwFNdMVo784cVtJdKIEvfus=
51+
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.103.0 h1:yGQxxqm3lXgEFITtdhXC7FQPufTwdPgy0+aXMKw4uOw=
52+
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.103.0/go.mod h1:VkTXDoIPbwGFi5+goo1VSwFNdMVo784cVtJdKIEvfus=
5153
github.com/bmatcuk/doublestar/v4 v4.4.0 h1:LmAwNwhjEbYtyVLzjcP/XeVw4nhuScHGkF/XWXnvIic=
5254
github.com/bmatcuk/doublestar/v4 v4.4.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
5355
github.com/briandowns/spinner v1.19.0 h1:s8aq38H+Qju89yhp89b4iIiMzMm8YN3p6vGpwyh/a8E=

pkg/cmd/account/aws/create/create.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type CreateFlags struct {
2828
Description *flag.Flag[string]
2929
AccessKey *flag.Flag[string]
3030
SecretKey *flag.Flag[string]
31+
Region *flag.Flag[string]
3132
Environments *flag.Flag[[]string]
3233
}
3334

@@ -43,6 +44,7 @@ func NewCreateFlags() *CreateFlags {
4344
Description: flag.New[string]("description", false),
4445
AccessKey: flag.New[string]("access-key", false),
4546
SecretKey: flag.New[string]("secret-key", true),
47+
Region: flag.New[string]("region", false),
4648
Environments: flag.New[[]string]("environment", false),
4749
}
4850
}
@@ -95,6 +97,7 @@ func NewCmdCreate(f factory.Factory) *cobra.Command {
9597
flags.StringVarP(&createFlags.Description.Value, createFlags.Description.Name, "d", "", "A summary explaining the use of the account to other users.")
9698
flags.StringVar(&createFlags.AccessKey.Value, createFlags.AccessKey.Name, "", "The AWS access key to use when authenticating against Amazon Web Services.")
9799
flags.StringVar(&createFlags.SecretKey.Value, createFlags.SecretKey.Name, "", "The AWS secret key to use when authenticating against Amazon Web Services.")
100+
flags.StringVar(&createFlags.Region.Value, createFlags.Region.Name, "", "The AWS region to use for this account.")
98101
flags.StringArrayVarP(&createFlags.Environments.Value, createFlags.Environments.Name, "e", nil, "The environments that are allowed to use this account")
99102
flags.StringVarP(&descriptionFilePath, "description-file", "D", "", "Read the description from `file`")
100103

@@ -112,6 +115,7 @@ func CreateRun(opts *CreateOptions) error {
112115
return err
113116
}
114117
awsAccount.Description = opts.Description.Value
118+
awsAccount.Region = opts.Region.Value
115119
awsAccount.EnvironmentIDs = opts.Environments.Value
116120

117121
createdAccount, err := opts.Client.Accounts.Add(awsAccount)
@@ -126,7 +130,7 @@ func CreateRun(opts *CreateOptions) error {
126130
link := output.Bluef("%s/app#/%s/infrastructure/accounts/%s", opts.Host, opts.Space.GetID(), createdAccount.GetID())
127131
fmt.Fprintf(opts.Out, "\nView this account on Octopus Deploy: %s\n", link)
128132
if !opts.NoPrompt {
129-
autoCmd := flag.GenerateAutomationCmd(opts.CmdPath, opts.Name, opts.AccessKey, opts.SecretKey, opts.Description, opts.Environments)
133+
autoCmd := flag.GenerateAutomationCmd(opts.CmdPath, opts.Name, opts.AccessKey, opts.SecretKey, opts.Region, opts.Description, opts.Environments)
130134
fmt.Fprintf(opts.Out, "\nAutomation Command: %s\n", autoCmd)
131135
}
132136

@@ -182,6 +186,15 @@ func PromptMissing(opts *CreateOptions) error {
182186
}
183187
}
184188

189+
if opts.Region.Value == "" {
190+
if err := opts.Ask(&survey.Input{
191+
Message: "Region",
192+
Help: "The AWS region to use for this account.",
193+
}, &opts.Region.Value); err != nil {
194+
return err
195+
}
196+
}
197+
185198
if opts.Environments.Value == nil {
186199
envs, err := selectors.EnvironmentsMultiSelect(opts.Ask, opts.GetAllEnvironmentsCallback,
187200
"Choose the environments that are allowed to use this account.\n"+

pkg/cmd/account/aws/create/create_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ func TestAWSAccountCreatePromptMissing(t *testing.T) {
7878
Help: "The AWS secret key to use when authenticating against Amazon Web Services.",
7979
}).AnswerWith("testpassword124")
8080

81+
_ = qa.ExpectQuestion(t, &survey.Input{
82+
Message: "Region",
83+
Help: "The AWS region to use for this account.",
84+
}).AnswerWith("us-east-1")
85+
8186
_ = qa.ExpectQuestion(t, &survey.MultiSelect{
8287
Message: "Choose the environments that are allowed to use this account.\nIf nothing is selected, the account can be used for deployments to any environment.",
8388
Options: []string{"testenv"},
@@ -89,6 +94,7 @@ func TestAWSAccountCreatePromptMissing(t *testing.T) {
8994
assert.Equal(t, []string{envID}, opts.Environments.Value)
9095
assert.Equal(t, "testpassword124", opts.SecretKey.Value)
9196
assert.Equal(t, "testaccesskey123", opts.AccessKey.Value)
97+
assert.Equal(t, "us-east-1", opts.Region.Value)
9298
assert.Equal(t, "test 123", opts.Description.Value)
9399
assert.Equal(t, "TestAccount", opts.Name.Value)
94100
}
@@ -108,6 +114,7 @@ func TestAWSAccountCreateNoPrompt(t *testing.T) {
108114
opts.Name.Value = "testaccount"
109115
opts.AccessKey.Value = "testaccesskey123"
110116
opts.SecretKey.Value = "testsecretkey123"
117+
opts.Region.Value = "us-west-2"
111118

112119
errReceiver := testutil.GoBegin(func() error {
113120
defer testutil.Close(api, qa)
@@ -128,6 +135,7 @@ func TestAWSAccountCreateNoPrompt(t *testing.T) {
128135
testAccount.ID = "Account-1"
129136
testAccount.Slug = "testaccount"
130137
testAccount.SpaceID = spaceID
138+
testAccount.Region = opts.Region.Value
131139

132140
api.ExpectRequest(t, "GET", "/api/").RespondWith(rootResource)
133141
api.ExpectRequest(t, "GET", "/api/spaces").RespondWith(rootResource)

pkg/cmd/account/aws/list/list.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,20 @@ func listAwsAccounts(client *client.Client, cmd *cobra.Command) error {
5050
Slug string
5151
Name string
5252
AccessKey string
53+
Region string
5354
}{
5455
Id: acc.GetID(),
5556
Slug: acc.GetSlug(),
5657
Name: acc.GetName(),
5758
AccessKey: acc.AccessKey,
59+
Region: acc.Region,
5860
}
5961
},
6062
Table: output.TableDefinition[accounts.IAccount]{
61-
Header: []string{"NAME", "SLUG", "ACCESS KEY"},
63+
Header: []string{"NAME", "SLUG", "ACCESS KEY", "REGION"},
6264
Row: func(item accounts.IAccount) []string {
6365
acc := item.(*accounts.AmazonWebServicesAccount)
64-
return []string{output.Bold(acc.GetName()), acc.GetSlug(), acc.AccessKey}
66+
return []string{output.Bold(acc.GetName()), acc.GetSlug(), acc.AccessKey, acc.Region}
6567
}},
6668
Basic: func(item accounts.IAccount) string {
6769
return item.GetName()

0 commit comments

Comments
 (0)