Skip to content

Commit d9c72c6

Browse files
authored
cmd: init ask for type of infrastructure and default to ESS config setup (#173)
The init command will now ask for the type of infrastructure to determine whether to ask for a custom host address or to use the default ESS public api address (https://api.elastic-cloud.com) .
1 parent 24fee93 commit d9c72c6

File tree

2 files changed

+64
-16
lines changed

2 files changed

+64
-16
lines changed

pkg/ecctl/init.go

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,24 @@ const (
4949
)
5050

5151
const (
52-
disclaimer = "Welcome to the Elastic Cloud CLI! This command will guide you through authenticating and setting some default values.\n\n"
52+
_ = iota
53+
essInfraChoice
54+
eceInfraChoice
55+
esspInfraChoice
56+
)
57+
58+
const (
59+
disclaimer = "Welcome to Elastic Cloud Control (ecctl)! This command will guide you through authenticating and setting some default values.\n\n"
5360
redacted = "[REDACTED]"
5461
settingsPathMsg = "Found existing settings in %s. Here's a JSON representation of what they look like:\n"
5562

5663
missingConfigMsg = `Missing configuration file, would you like to initialise it? [y/n]: `
5764
existingConfigMsg = `Would you like to change your current settings? [y/n]: `
5865

59-
hostMsg = "Enter the URL of your ECE installation: "
66+
essHostAddress = "https://api.elastic-cloud.com"
67+
eceHostMsg = "Enter the URL of your ECE installation: "
68+
esspHostMsg = "Enter the URL of your ESSP installation: "
69+
essChoiceMsg = "Using \"%s\" as the API endpoint.\n"
6070

6171
apiKeyMsg = "Paste your API Key and press enter: "
6272
userMsg = "Type in your username: "
@@ -67,6 +77,14 @@ const (
6777
)
6878

6979
var (
80+
hostChoiceMsg = `
81+
Select which type of Elastic Cloud offering will you be working with:
82+
[1] Elasticsearch Service (default).
83+
[2] Elastic Cloud Enterprise (ECE).
84+
[3] Elasticsearch Service Private (ESSP).
85+
86+
Please enter your choice: `
87+
7088
authChoiceMsg = `
7189
Which authentication mechanism would you like to use?
7290
[1] API Keys (Recommended).
@@ -186,7 +204,10 @@ func InitConfig(params InitConfigParams) error {
186204
// Insecure is set to true by default to allow API calls against HTTPS
187205
// endpoints with self-signed certificates.
188206
cfg.Insecure = true
189-
cfg.Host = scanner.Scan(hostMsg)
207+
208+
if err := askInfraSelection(&cfg, scanner, params.Writer, params.ErrWriter); err != nil {
209+
return err
210+
}
190211

191212
if err := askOutputFormat(&cfg, scanner, params.Writer, params.ErrWriter); err != nil {
192213
return err
@@ -206,7 +227,7 @@ func InitConfig(params InitConfigParams) error {
206227
return err
207228
}
208229

209-
// It's better to write the config as is since it ommits defaults and
230+
// It's better to write the config as is since it omits defaults and
210231
// empties vs viper's behaviour in `WriteConfig`.
211232
return writeConfig(cfg, params.FilePath, ".json")
212233
}
@@ -243,6 +264,29 @@ func printConfig(writer io.Writer, v *viper.Viper) error {
243264
return enc.Encode(c)
244265
}
245266

267+
func askInfraSelection(cfg *Config, scanner *input.Scanner, writer, errWriter io.Writer) error {
268+
infraChoiceRaw := scanner.Scan(hostChoiceMsg)
269+
fmt.Fprintln(writer)
270+
infraChoice, err := strconv.Atoi(infraChoiceRaw)
271+
if err != nil {
272+
return err
273+
}
274+
275+
cfg.Host = essHostAddress
276+
switch infraChoice {
277+
case essInfraChoice:
278+
fmt.Fprintf(writer, essChoiceMsg, essHostAddress)
279+
case eceInfraChoice:
280+
cfg.Host = scanner.Scan(eceHostMsg)
281+
case esspInfraChoice:
282+
cfg.Host = scanner.Scan(esspHostMsg)
283+
default:
284+
fmt.Fprintf(errWriter, "invalid choice, defaulting to %s", essHostAddress)
285+
}
286+
287+
return nil
288+
}
289+
246290
func askOutputFormat(cfg *Config, scanner *input.Scanner, writer, errWriter io.Writer) error {
247291
formatChoiceRaw := scanner.Scan(formatChoiceMsg)
248292
fmt.Fprintln(writer)

pkg/ecctl/init_test.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func TestInitConfig(t *testing.T) {
241241
FilePath: filepath.Join(testFiles, "newConfig"),
242242
Reader: io.MultiReader(
243243
strings.NewReader("y\n"),
244-
strings.NewReader("https://ahost\n"),
244+
strings.NewReader("1\n"),
245245
strings.NewReader("1\n"),
246246
strings.NewReader("1\n"),
247247
strings.NewReader("anapikey\n"),
@@ -257,13 +257,14 @@ func TestInitConfig(t *testing.T) {
257257
}},
258258
wantSettings: map[string]interface{}{
259259
"apikey": "somekey",
260-
"host": "https://ahost",
260+
"host": "https://api.elastic-cloud.com",
261261
"insecure": true,
262262
"output": "text",
263263
},
264-
wantOutput: disclaimer + missingConfigMsg + hostMsg +
265-
formatChoiceMsg + "\n" + authChoiceMsg + "\n" + apiKeyMsg + "\n" +
266-
"\n" + fmt.Sprintf(validCredentialsMsg, "anacleto") + finalMsg + "\n",
264+
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",
267268
},
268269
{
269270
name: "doesn't find a config file and user creates a new one with user/pass",
@@ -272,6 +273,7 @@ func TestInitConfig(t *testing.T) {
272273
FilePath: filepath.Join(testFiles, "newConfigUserPass"),
273274
Reader: io.MultiReader(
274275
strings.NewReader("y\n"),
276+
strings.NewReader("2\n"),
275277
strings.NewReader("https://ahost\n"),
276278
strings.NewReader("1\n"),
277279
strings.NewReader("2\n"),
@@ -298,7 +300,7 @@ func TestInitConfig(t *testing.T) {
298300
"pass": "apassword",
299301
"user": "auser",
300302
},
301-
wantOutput: disclaimer + missingConfigMsg + hostMsg +
303+
wantOutput: disclaimer + missingConfigMsg + hostChoiceMsg + "\n" + eceHostMsg +
302304
formatChoiceMsg + "\n" + authChoiceMsg + "\n" + userMsg + passMsg +
303305
"\n" + "\n" + fmt.Sprintf(validCredentialsMsg, "auser") + finalMsg + "\n",
304306
},
@@ -309,6 +311,7 @@ func TestInitConfig(t *testing.T) {
309311
FilePath: filepath.Join(testFiles, "doesnt_matter"),
310312
Reader: io.MultiReader(
311313
strings.NewReader("y\n"),
314+
strings.NewReader("3\n"),
312315
strings.NewReader("https://ahost\n"),
313316
strings.NewReader("1\n"),
314317
strings.NewReader("1\n"),
@@ -331,9 +334,9 @@ func TestInitConfig(t *testing.T) {
331334
},
332335
wantOutput: disclaimer +
333336
fmt.Sprintf(settingsPathMsg, "test_files/userpassmodif.yaml") +
334-
userPassConfigToModifyContents + "\n" + existingConfigMsg + hostMsg +
335-
formatChoiceMsg + "\n" + authChoiceMsg + "\n" + apiKeyMsg + "\n" +
336-
"\n" + fmt.Sprintf(validCredentialsMsg, "anacleto") + finalMsg + "\n",
337+
userPassConfigToModifyContents + "\n" + existingConfigMsg + hostChoiceMsg +
338+
"\n" + esspHostMsg + formatChoiceMsg + "\n" + authChoiceMsg + "\n" + apiKeyMsg +
339+
"\n" + "\n" + fmt.Sprintf(validCredentialsMsg, "anacleto") + finalMsg + "\n",
337340
},
338341
{
339342
name: "finds a config file and user changes the values, from apikey to user/pass",
@@ -342,6 +345,7 @@ func TestInitConfig(t *testing.T) {
342345
FilePath: filepath.Join(testFiles, "doesnt_matter"),
343346
Reader: io.MultiReader(
344347
strings.NewReader("y\n"),
348+
strings.NewReader("2\n"),
345349
strings.NewReader("https://ahost\n"),
346350
strings.NewReader("1\n"),
347351
strings.NewReader("2\n"),
@@ -370,9 +374,9 @@ func TestInitConfig(t *testing.T) {
370374
},
371375
wantOutput: disclaimer +
372376
fmt.Sprintf(settingsPathMsg, "test_files/apikeymodif.yaml") +
373-
apiKeyConfigToModifyContents + "\n" + existingConfigMsg + hostMsg +
374-
formatChoiceMsg + "\n" + authChoiceMsg + "\n" + userMsg + passMsg +
375-
"\n" + "\n" + fmt.Sprintf(validCredentialsMsg, "auser") + finalMsg + "\n",
377+
apiKeyConfigToModifyContents + "\n" + existingConfigMsg + hostChoiceMsg +
378+
"\n" + eceHostMsg + formatChoiceMsg + "\n" + authChoiceMsg + "\n" + userMsg +
379+
passMsg + "\n" + "\n" + fmt.Sprintf(validCredentialsMsg, "auser") + finalMsg + "\n",
376380
},
377381
}
378382
for _, tt := range tests {

0 commit comments

Comments
 (0)