Skip to content

Commit 38721e9

Browse files
authored
chore(cli): env initValidate() -- validate flag-set subnet quantities (#2398)
This applies the subnet quantity validations (0 or 2+ for public, 2 for private) introduced in `Ask()` in #2356 to `Validate()` for subnet IDs passed via flags. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 385dc66 commit 38721e9

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

internal/pkg/cli/env_init.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,16 @@ func (o *initEnvOpts) validateCustomizedResources() error {
328328
if (o.importVPC.isSet() || o.adjustVPC.isSet()) && o.defaultConfig {
329329
return fmt.Errorf("cannot import or configure vpc if --%s is set", defaultConfigFlag)
330330
}
331+
if o.importVPC.isSet() {
332+
// We allow 0 or 2+ public subnets.
333+
if len(o.importVPC.PublicSubnetIDs) == 1 {
334+
return fmt.Errorf("at least two public subnets must be imported to enable Load Balancing")
335+
}
336+
// We require 2+ private subnets.
337+
if len(o.importVPC.PrivateSubnetIDs) < 2 {
338+
return fmt.Errorf("at least two private subnets must be imported")
339+
}
340+
}
331341
return nil
332342
}
333343

internal/pkg/cli/env_init_test.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func TestInitEnvOpts_Validate(t *testing.T) {
4141
inDefault bool
4242
inVPCID string
4343
inPublicIDs []string
44+
inPrivateIDs []string
4445
inVPCCIDR net.IPNet
4546
inPublicCIDRs []string
4647

@@ -61,11 +62,11 @@ func TestInitEnvOpts_Validate(t *testing.T) {
6162

6263
wantedErrMsg: fmt.Sprintf("environment name 123env is invalid: %s", errValueBadFormat),
6364
},
64-
"cannot specify both vpc resources importing flags and configuing flags": {
65+
"cannot specify both vpc resources importing flags and configuring flags": {
6566
inEnvName: "test-pdx",
6667
inAppName: "phonetool",
6768
inPublicCIDRs: []string{"mockCIDR"},
68-
inPublicIDs: []string{"mockID"},
69+
inPublicIDs: []string{"mockID", "anotherMockID"},
6970
inVPCCIDR: net.IPNet{
7071
IP: net.IP{10, 1, 232, 0},
7172
Mask: net.IPMask{255, 255, 255, 0},
@@ -106,6 +107,29 @@ func TestInitEnvOpts_Validate(t *testing.T) {
106107

107108
wantedErrMsg: "cannot specify both --profile and --aws-session-token",
108109
},
110+
"should err if only one public subnet is set": {
111+
inVPCID: "mockID",
112+
inPublicIDs: []string{"mockID"},
113+
114+
wantedErrMsg: "at least two public subnets must be imported to enable Load Balancing",
115+
},
116+
"should err if fewer than two private subnets are set:": {
117+
inVPCID: "mockID",
118+
inPublicIDs: []string{"mockID", "anotherMockID"},
119+
inPrivateIDs: []string{"mockID"},
120+
121+
wantedErrMsg: "at least two private subnets must be imported",
122+
},
123+
"valid VPC resource import (0 public, 3 private)": {
124+
inVPCID: "mockID",
125+
inPublicIDs: []string{},
126+
inPrivateIDs: []string{"mockID", "anotherMockID", "yetAnotherMockID"},
127+
},
128+
"valid VPC resource import (3 public, 2 private)": {
129+
inVPCID: "mockID",
130+
inPublicIDs: []string{"mockID", "anotherMockID", "yetAnotherMockID"},
131+
inPrivateIDs: []string{"mockID", "anotherMockID"},
132+
},
109133
}
110134

111135
for name, tc := range testCases {
@@ -120,8 +144,9 @@ func TestInitEnvOpts_Validate(t *testing.T) {
120144
CIDR: tc.inVPCCIDR,
121145
},
122146
importVPC: importVPCVars{
123-
PublicSubnetIDs: tc.inPublicIDs,
124-
ID: tc.inVPCID,
147+
PublicSubnetIDs: tc.inPublicIDs,
148+
PrivateSubnetIDs: tc.inPrivateIDs,
149+
ID: tc.inVPCID,
125150
},
126151
appName: tc.inAppName,
127152
profile: tc.inProfileName,

0 commit comments

Comments
 (0)