Skip to content

Commit

Permalink
chore(cli): change env init selection order and default flag change (#…
Browse files Browse the repository at this point in the history
…1265)

<!-- Provide summary of changes -->
This PR
1. Change the selection order in env init.
2. Update default flag in env init from `--no-customized-env` to `--default-config`

<!-- Issue number, if available. E.g. "Fixes #31", "Addresses #42, 77" -->

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
  • Loading branch information
iamhopaul123 authored Aug 13, 2020
1 parent 13049a1 commit 1851c10
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 45 deletions.
39 changes: 18 additions & 21 deletions internal/pkg/cli/env_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ https://github.com/aws/copilot-cli/wiki/credentials#environment-credentials`
var (
errNamedProfilesNotFound = fmt.Errorf("no named AWS profiles found, run %s first please", color.HighlightCode("aws configure"))

envInitImportEnvResourcesSelectOption = "No, I'd like to import existing resources (VPC, subnets)."
envInitAdjustEnvResourcesSelectOption = "Yes, but I'd like configure the default resources (CIDR ranges)."
envInitWithNoCustomizedResourcesSelectOption = "Yes, use default."
envInitCustomizedEnvTypes = []string{envInitImportEnvResourcesSelectOption, envInitAdjustEnvResourcesSelectOption, envInitWithNoCustomizedResourcesSelectOption}
envInitDefaultConfigSelectOption = "Yes, use default."
envInitAdjustEnvResourcesSelectOption = "Yes, but I'd like configure the default resources (CIDR ranges)."
envInitImportEnvResourcesSelectOption = "No, I'd like to import existing resources (VPC, subnets)."
envInitCustomizedEnvTypes = []string{envInitDefaultConfigSelectOption, envInitAdjustEnvResourcesSelectOption, envInitImportEnvResourcesSelectOption}
)

type importVPCVars struct {
Expand Down Expand Up @@ -116,10 +116,10 @@ func (v tempCredsVars) isSet() bool {

type initEnvVars struct {
*GlobalOpts
Name string // Name for the environment.
Profile string // The named profile to use for credential retrieval. Mutually exclusive with TempCreds.
IsProduction bool // True means retain resources even after deletion.
NoCustomResources bool // True means no importing an existing VPC or adjusting a VPC.
Name string // Name for the environment.
Profile string // The named profile to use for credential retrieval. Mutually exclusive with TempCreds.
IsProduction bool // True means retain resources even after deletion.
DefaultConfig bool // True means using default environment configuration.

ImportVPC importVPCVars // Existing VPC resources to use instead of creating new ones.
AdjustVPC adjustVPCVars // Configure parameters for VPC resources generated while initializing an environment.
Expand Down Expand Up @@ -261,8 +261,8 @@ func (o *initEnvOpts) validateCustomizedResources() error {
if o.ImportVPC.isSet() && o.AdjustVPC.isSet() {
return errors.New("cannot specify both import vpc flags and configure vpc flags")
}
if (o.ImportVPC.isSet() || o.AdjustVPC.isSet()) && o.NoCustomResources {
return fmt.Errorf("cannot import or configure vpc if --%s is set", noCustomResourcesFlag)
if (o.ImportVPC.isSet() || o.AdjustVPC.isSet()) && o.DefaultConfig {
return fmt.Errorf("cannot import or configure vpc if --%s is set", defaultConfigFlag)
}
return nil
}
Expand Down Expand Up @@ -322,7 +322,7 @@ func (o *initEnvOpts) askEnvRegion() error {
}

func (o *initEnvOpts) askCustomizedResources() error {
if o.NoCustomResources {
if o.DefaultConfig {
return nil
}
if o.ImportVPC.isSet() {
Expand All @@ -342,7 +342,7 @@ func (o *initEnvOpts) askCustomizedResources() error {
return o.askImportResources()
case envInitAdjustEnvResourcesSelectOption:
return o.askAdjustResources()
case envInitWithNoCustomizedResourcesSelectOption:
case envInitDefaultConfigSelectOption:
return nil
}
return nil
Expand Down Expand Up @@ -425,7 +425,7 @@ func (o *initEnvOpts) askAdjustResources() error {
}

func (o *initEnvOpts) importVPCConfig() *deploy.ImportVPCConfig {
if o.NoCustomResources || !o.ImportVPC.isSet() {
if o.DefaultConfig || !o.ImportVPC.isSet() {
return nil
}
return &deploy.ImportVPCConfig{
Expand All @@ -436,7 +436,7 @@ func (o *initEnvOpts) importVPCConfig() *deploy.ImportVPCConfig {
}

func (o *initEnvOpts) adjustVPCConfig() *deploy.AdjustVPCConfig {
if o.NoCustomResources || !o.AdjustVPC.isSet() {
if o.DefaultConfig || !o.AdjustVPC.isSet() {
return nil
}
return &deploy.AdjustVPCConfig{
Expand Down Expand Up @@ -585,15 +585,12 @@ func BuildEnvInitCmd() *cobra.Command {
Use: "init",
Short: "Creates a new environment in your application.",
Example: `
Creates a test environment in your "default" AWS profile.
/code $ copilot env init --name test --profile default
Creates a test environment in your "default" AWS profile using default configuration.
/code $ copilot env init --name test --profile default --default-config
Creates a prod-iad environment using your "prod-admin" AWS profile.
/code $ copilot env init --name prod-iad --profile prod-admin --prod
Creates a test environment using default environment configuration.
/code $ copilot env init --name test --no-custom-resources
Creates an environment with imported VPC resources.
/code $ copilot env init --import-vpc-id vpc-099c32d2b98cdcf47 \
/code --import-public-subnets subnet-013e8b691862966cf,subnet -014661ebb7ab8681a \
Expand Down Expand Up @@ -635,7 +632,7 @@ func BuildEnvInitCmd() *cobra.Command {
// TODO: use IPNetSliceVar when it is available (https://github.com/spf13/pflag/issues/273).
cmd.Flags().StringSliceVar(&vars.AdjustVPC.PublicSubnetCIDRs, publicSubnetCIDRsFlag, nil, publicSubnetCIDRsFlagDescription)
cmd.Flags().StringSliceVar(&vars.AdjustVPC.PrivateSubnetCIDRs, privateSubnetCIDRsFlag, nil, privateSubnetCIDRsFlagDescription)
cmd.Flags().BoolVar(&vars.NoCustomResources, noCustomResourcesFlag, false, noCustomResourcesFlagDescription)
cmd.Flags().BoolVar(&vars.DefaultConfig, defaultConfigFlag, false, defaultConfigFlagDescription)

flags := pflag.NewFlagSet("Common", pflag.ContinueOnError)
flags.AddFlag(cmd.Flags().Lookup(nameFlag))
Expand All @@ -644,7 +641,7 @@ func BuildEnvInitCmd() *cobra.Command {
flags.AddFlag(cmd.Flags().Lookup(secretAccessKeyFlag))
flags.AddFlag(cmd.Flags().Lookup(sessionTokenFlag))
flags.AddFlag(cmd.Flags().Lookup(regionFlag))
flags.AddFlag(cmd.Flags().Lookup(noCustomResourcesFlag))
flags.AddFlag(cmd.Flags().Lookup(defaultConfigFlag))
flags.AddFlag(cmd.Flags().Lookup(prodEnvFlag))

resourcesImportFlag := pflag.NewFlagSet("Import Existing Resources", pflag.ContinueOnError)
Expand Down
44 changes: 22 additions & 22 deletions internal/pkg/cli/env_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ type initEnvMocks struct {

func TestInitEnvOpts_Validate(t *testing.T) {
testCases := map[string]struct {
inEnvName string
inAppName string
inNoCustomResources bool
inVPCID string
inPublicIDs []string
inVPCCIDR net.IPNet
inPublicCIDRs []string
inEnvName string
inAppName string
inDefault bool
inVPCID string
inPublicIDs []string
inVPCCIDR net.IPNet
inPublicCIDRs []string

inProfileName string
inAccessKeyID string
Expand Down Expand Up @@ -78,12 +78,12 @@ func TestInitEnvOpts_Validate(t *testing.T) {
wantedErrMsg: "cannot specify both import vpc flags and configure vpc flags",
},
"cannot import or configure resources if use default flag is set": {
inEnvName: "test-pdx",
inAppName: "phonetool",
inNoCustomResources: true,
inVPCID: "mockID",
inEnvName: "test-pdx",
inAppName: "phonetool",
inDefault: true,
inVPCID: "mockID",

wantedErrMsg: fmt.Sprintf("cannot import or configure vpc if --%s is set", noCustomResourcesFlag),
wantedErrMsg: fmt.Sprintf("cannot import or configure vpc if --%s is set", defaultConfigFlag),
},
"should err if both profile and access key id are set": {
inAppName: "phonetool",
Expand Down Expand Up @@ -116,8 +116,8 @@ func TestInitEnvOpts_Validate(t *testing.T) {
// GIVEN
opts := &initEnvOpts{
initEnvVars: initEnvVars{
Name: tc.inEnvName,
NoCustomResources: tc.inNoCustomResources,
Name: tc.inEnvName,
DefaultConfig: tc.inDefault,
AdjustVPC: adjustVPCVars{
PublicSubnetCIDRs: tc.inPublicCIDRs,
CIDR: tc.inVPCCIDR,
Expand Down Expand Up @@ -265,7 +265,7 @@ func TestInitEnvOpts_Ask(t *testing.T) {
setupMocks: func(m initEnvMocks) {
m.sessProvider.EXPECT().FromProfile(gomock.Any()).Return(mockSession, nil)
m.prompt.EXPECT().SelectOne(envInitDefaultEnvConfirmPrompt, "", envInitCustomizedEnvTypes).
Return(envInitWithNoCustomizedResourcesSelectOption, nil)
Return(envInitDefaultConfigSelectOption, nil)
},
},
"fail to select VPC": {
Expand Down Expand Up @@ -426,13 +426,13 @@ func TestInitEnvOpts_Ask(t *testing.T) {
// GIVEN
addEnv := &initEnvOpts{
initEnvVars: initEnvVars{
Name: tc.inEnv,
Profile: tc.inProfile,
TempCreds: tc.inTempCreds,
Region: tc.inRegion,
NoCustomResources: tc.inDefault,
AdjustVPC: tc.inAdjustVPCVars,
ImportVPC: tc.inImportVPCVars,
Name: tc.inEnv,
Profile: tc.inProfile,
TempCreds: tc.inTempCreds,
Region: tc.inRegion,
DefaultConfig: tc.inDefault,
AdjustVPC: tc.inAdjustVPCVars,
ImportVPC: tc.inImportVPCVars,
GlobalOpts: &GlobalOpts{
prompt: mocks.prompt,
},
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/cli/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const (
publicSubnetCIDRsFlag = "override-public-cidrs"
privateSubnetCIDRsFlag = "override-private-cidrs"

noCustomResourcesFlag = "no-custom-resources"
defaultConfigFlag = "default-config"

accessKeyIDFlag = "aws-access-key-id"
secretAccessKeyFlag = "aws-secret-access-key"
Expand Down Expand Up @@ -186,7 +186,7 @@ Tasks with the same group name share the same set of resources.
publicSubnetCIDRsFlagDescription = "Optional. CIDR to use for public subnets (default 10.0.0.0/24,10.0.1.0/24)."
privateSubnetCIDRsFlagDescription = "Optional. CIDR to use for private subnets (default 10.0.2.0/24,10.0.3.0/24)."

noCustomResourcesFlagDescription = "Optional. Skip prompting and use default environment configuration."
defaultConfigFlagDescription = "Optional. Skip prompting and use default environment configuration."

accessKeyIDFlagDescription = "Optional. An AWS access key."
secretAccessKeyFlagDescription = "Optional. An AWS secret access key."
Expand Down

0 comments on commit 1851c10

Please sign in to comment.