Skip to content

Commit 1dbdd0b

Browse files
authored
cmd: init gives you a choice to select default region when ESS is selected (#180)
Whenever a user opts to use ESS in the init flow, they will be prompted to select a default region. ``` Select which type of Elastic Cloud offering you will be working with: [1] Elasticsearch Service (default). [2] Elastic Cloud Enterprise (ECE). [3] Elasticsearch Service Private (ESSP). Please enter your choice: 1 Using "https://api.elastic-cloud.com" as the API endpoint. Select a region you would like to have as default: GCP [1] us-central1 (Iowa) [2] us-east4 (N. Virginia) [3] us-west1 (Oregon) [4] northamerica-northeast1 (Montreal) [5] australia-southeast1 (Sydney) [6] europe-west1 (Belgium) [7] europe-west2 (London) [8] europe-west3 (Frankfurt) [9] asia-northeast1 (Tokyo) [10] asia-south1 (Mumbai) AWS [11] us-east-1 (N. Virginia) [12] us-west-1 (N. California) [13] us-west-2 (Oregon) [14] eu-central-1 (Frankfurt) [15] eu-west-2 (London) [16] eu-west-1 (Ireland) [17] ap-northeast-1 (Tokyo) [18] ap-southeast-1 (Singapore) [19] ap-southeast-2 (Sydney) [20] sa-east-1 (São Paulo) Azure [21] eastus2 (Virginia) [22] westus2 (Washington) [23] westeurope (Netherlands) [24] japaneast (Tokyo) [25] southeastasia (Singapore) Please enter your choice: ``` Things to note: - ESSP defaults to us-west-2 as it's the only available region for the time being - In order to have some consistency in the list, for the region names, I've decided to go with this format instead of the display format when available. This is because GCP does not have a display format like the other two. - The desired thing would be to query an API endpoint and print a list of available regions, said endpoint is not available today so in the meantime maintaining a static list of providers is low effort and a quick win, any time a new release of ecctl is shipped, new regions can be included which makes it a valid distribution mechanism.
1 parent edeabee commit 1dbdd0b

File tree

2 files changed

+124
-3
lines changed

2 files changed

+124
-3
lines changed

pkg/ecctl/init.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,35 @@ const (
5555
esspInfraChoice
5656
)
5757

58+
const (
59+
_ = iota
60+
gcpUsCentral1Choice
61+
gcpUsEast4Choice
62+
gcpUsWest1Choice
63+
gcpNorthamericaNortheast1Choice
64+
gcpAustraliaSoutheast1Choice
65+
gcpEuropeWest1Choice
66+
gcpEuropeWest2Choice
67+
gcpEuropeWest3Choice
68+
gcpAsiaNortheast1Choice
69+
gcpAsiaSouth1Choice
70+
awsUsEast1Choice
71+
awsUsWest1Choice
72+
awsUsWest2Choice
73+
awsEuCentral1Choice
74+
awsEuWest2Choice
75+
awsEuWest1Choice
76+
awsApNortheast1Choice
77+
awsApSoutheast1Choice
78+
awsApSoutheast2Choice
79+
awsSaEast1Choice
80+
azureEastUs2Choice
81+
azureWestUs2Choice
82+
azureWestEuropeChoice
83+
azureJapanEastChoice
84+
azureSouthEastAsiaChoice
85+
)
86+
5887
const (
5988
disclaimer = "Welcome to Elastic Cloud Control (ecctl)! This command will guide you through authenticating and setting some default values.\n\n"
6089
redacted = "[REDACTED]"
@@ -83,6 +112,42 @@ Select which type of Elastic Cloud offering you will be working with:
83112
[2] Elastic Cloud Enterprise (ECE).
84113
[3] Elasticsearch Service Private (ESSP).
85114
115+
Please enter your choice: `
116+
117+
regionChoiceMsg = `
118+
Select a region you would like to have as default:
119+
120+
GCP
121+
[1] us-central1 (Iowa)
122+
[2] us-east4 (N. Virginia)
123+
[3] us-west1 (Oregon)
124+
[4] northamerica-northeast1 (Montreal)
125+
[5] australia-southeast1 (Sydney)
126+
[6] europe-west1 (Belgium)
127+
[7] europe-west2 (London)
128+
[8] europe-west3 (Frankfurt)
129+
[9] asia-northeast1 (Tokyo)
130+
[10] asia-south1 (Mumbai)
131+
132+
AWS
133+
[11] us-east-1 (N. Virginia)
134+
[12] us-west-1 (N. California)
135+
[13] us-west-2 (Oregon)
136+
[14] eu-central-1 (Frankfurt)
137+
[15] eu-west-2 (London)
138+
[16] eu-west-1 (Ireland)
139+
[17] ap-northeast-1 (Tokyo)
140+
[18] ap-southeast-1 (Singapore)
141+
[19] ap-southeast-2 (Sydney)
142+
[20] sa-east-1 (São Paulo)
143+
144+
Azure
145+
[21] eastus2 (Virginia)
146+
[22] westus2 (Washington)
147+
[23] westeurope (Netherlands)
148+
[24] japaneast (Tokyo)
149+
[25] southeastasia (Singapore)
150+
86151
Please enter your choice: `
87152

88153
authChoiceMsg = `
@@ -103,6 +168,35 @@ Please enter a choice: `
103168
You're all set! Here are some commands to try:
104169
$ ecctl auth user key list
105170
$ ecctl deployment elasticsearch list`[1:]
171+
172+
// Remove once we have an endpoint available to list regions.
173+
essRegions = map[int]string{
174+
gcpUsCentral1Choice: "gcp-us-central1",
175+
gcpUsEast4Choice: "gcp-us-east4",
176+
gcpUsWest1Choice: "gcp-us-west1",
177+
gcpNorthamericaNortheast1Choice: "gcp-northamerica-northeast1",
178+
gcpAustraliaSoutheast1Choice: "gcp-australia-southeast1",
179+
gcpEuropeWest1Choice: "gcp-europe-west1",
180+
gcpEuropeWest2Choice: "gcp-europe-west2",
181+
gcpEuropeWest3Choice: "gcp-europe-west3",
182+
gcpAsiaNortheast1Choice: "gcp-asia-northeast1",
183+
gcpAsiaSouth1Choice: "gcp-asia-south1",
184+
awsUsEast1Choice: "us-east-1",
185+
awsUsWest1Choice: "us-west-1",
186+
awsUsWest2Choice: "us-west-2",
187+
awsEuCentral1Choice: "aws-eu-central-1",
188+
awsEuWest2Choice: "aws-eu-west-2",
189+
awsEuWest1Choice: "eu-west-1",
190+
awsApNortheast1Choice: "ap-northeast-1",
191+
awsApSoutheast1Choice: "ap-southeast-1",
192+
awsApSoutheast2Choice: "ap-southeast-2",
193+
awsSaEast1Choice: "sa-east-1",
194+
azureEastUs2Choice: "azure-eastus2",
195+
azureWestUs2Choice: "azure-westus2",
196+
azureWestEuropeChoice: "azure-westeurope",
197+
azureJapanEastChoice: "azure-japaneast",
198+
azureSouthEastAsiaChoice: "azure-southeastasia",
199+
}
106200
)
107201

108202
// PassFunc represents the function used to consume a password.
@@ -276,17 +370,41 @@ func askInfraSelection(cfg *Config, scanner *input.Scanner, writer, errWriter io
276370
switch infraChoice {
277371
case essInfraChoice:
278372
fmt.Fprintf(writer, essChoiceMsg, essHostAddress)
373+
if err := askRegionSelection(cfg, scanner, writer, essRegions); err != nil {
374+
return err
375+
}
279376
case eceInfraChoice:
280377
cfg.Host = scanner.Scan(eceHostMsg)
281378
case esspInfraChoice:
282379
cfg.Host = scanner.Scan(esspHostMsg)
380+
// For the time being the only available region for ESSP is us-west-2. Once more
381+
// regions have been added, this should be set in a similar way to essInfraChoice
382+
cfg.Region = "us-west-2"
283383
default:
284384
fmt.Fprintf(errWriter, "invalid choice, defaulting to %s", essHostAddress)
285385
}
286386

287387
return nil
288388
}
289389

390+
func askRegionSelection(cfg *Config, scanner *input.Scanner, writer io.Writer, regions map[int]string) error {
391+
regionChoiceRaw := scanner.Scan(regionChoiceMsg)
392+
fmt.Fprintln(writer)
393+
regionChoice, err := strconv.Atoi(regionChoiceRaw)
394+
if err != nil {
395+
return err
396+
}
397+
398+
region, ok := regions[regionChoice]
399+
if !ok {
400+
return errors.New("invalid region choice")
401+
}
402+
403+
cfg.Region = region
404+
405+
return nil
406+
}
407+
290408
func askOutputFormat(cfg *Config, scanner *input.Scanner, writer, errWriter io.Writer) error {
291409
formatChoiceRaw := scanner.Scan(formatChoiceMsg)
292410
fmt.Fprintln(writer)

pkg/ecctl/init_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ func TestInitConfig(t *testing.T) {
244244
strings.NewReader("1\n"),
245245
strings.NewReader("1\n"),
246246
strings.NewReader("1\n"),
247+
strings.NewReader("1\n"),
247248
strings.NewReader("anapikey\n"),
248249
),
249250
Writer: new(bytes.Buffer),
@@ -260,11 +261,12 @@ func TestInitConfig(t *testing.T) {
260261
"host": "https://api.elastic-cloud.com",
261262
"insecure": true,
262263
"output": "text",
264+
"region": "gcp-us-central1",
263265
},
264266
wantOutput: disclaimer + missingConfigMsg + hostChoiceMsg + "\n" +
265-
fmt.Sprintf(essChoiceMsg, essHostAddress) + formatChoiceMsg +
266-
"\n" + authChoiceMsg + "\n" + apiKeyMsg + "\n" + "\n" +
267-
fmt.Sprintf(validCredentialsMsg, "anacleto") + finalMsg + "\n",
267+
fmt.Sprintf(essChoiceMsg, essHostAddress) + regionChoiceMsg + "\n" +
268+
formatChoiceMsg + "\n" + authChoiceMsg + "\n" + apiKeyMsg + "\n" +
269+
"\n" + fmt.Sprintf(validCredentialsMsg, "anacleto") + finalMsg + "\n",
268270
},
269271
{
270272
name: "doesn't find a config file and user creates a new one with user/pass",
@@ -331,6 +333,7 @@ func TestInitConfig(t *testing.T) {
331333
"host": "https://ahost",
332334
"insecure": true,
333335
"output": "text",
336+
"region": "us-west-2",
334337
},
335338
wantOutput: disclaimer +
336339
fmt.Sprintf(settingsPathMsg, "test_files/userpassmodif.yaml") +

0 commit comments

Comments
 (0)