Skip to content

Commit 25816bf

Browse files
fix(config): merge selected profile on top of default profile (#243)
* fix(config): merge default profile with asked profile * fix: add a test to make sure this will remain
1 parent e250f2b commit 25816bf

File tree

2 files changed

+171
-139
lines changed

2 files changed

+171
-139
lines changed

scw/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ func (c *Config) GetProfile(profileName string) (*Profile, error) {
124124
return nil, errors.New("given profile %s does not exist", profileName)
125125
}
126126

127-
return p, nil
127+
// Merge selected profile on top of default profile
128+
return MergeProfiles(&c.Profile, p), nil
128129
}
129130

130131
// GetActiveProfile returns the active profile of the config based on the following order:

scw/config_test.go

Lines changed: 169 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,111 @@ import (
1111
"github.com/scaleway/scaleway-sdk-go/logger"
1212
)
1313

14-
func s(value string) *string {
15-
return &value
16-
}
14+
const emptyFile = ""
1715

18-
func r(value Region) *Region {
19-
return &value
20-
}
16+
// v2 config
17+
var (
18+
v2ValidAccessKey2 = "SCW234567890ABCDEFGH"
19+
v2ValidSecretKey2 = "6f6e6574-6f72-756c-6c74-68656d616c6c" // hint: | xxd -ps -r
20+
v2ValidAPIURL2 = "api-fr-par.scaleway.com"
21+
v2ValidInsecure2 = "true"
22+
v2ValidDefaultOrganizationID2 = "6d6f7264-6f72-6772-6561-74616761696e" // hint: | xxd -ps -r
23+
v2ValidDefaultRegion2 = string(RegionFrPar)
24+
v2ValidDefaultZone2 = string(ZoneFrPar2)
2125

22-
func z(value Zone) *Zone {
23-
return &value
24-
}
26+
v2ValidAccessKey = "SCW1234567890ABCDEFG"
27+
v2ValidSecretKey = "7363616c-6577-6573-6862-6f7579616161" // hint: | xxd -ps -r
28+
v2ValidAPIURL = "api.scaleway.com"
29+
v2ValidInsecure = "false"
30+
v2ValidDefaultOrganizationID = "6170692e-7363-616c-6577-61792e636f6d" // hint: | xxd -ps -r
31+
v2ValidDefaultRegion = string(RegionNlAms)
32+
v2ValidDefaultZone = string(ZoneNlAms1)
33+
v2ValidProfile = "flantier"
2534

26-
func b(value bool) *bool {
27-
return &value
28-
}
35+
v2InvalidAccessKey = "invalid"
36+
v2InvalidSecretKey = "invalid"
37+
v2InvalidDefaultOrganizationID = "invalid"
38+
v2InvalidDefaultRegion = "invalid"
39+
v2InvalidDefaultZone = "invalid"
2940

30-
func initEnv(t *testing.T) string {
31-
dir, err := ioutil.TempDir("", "home")
32-
if err != nil {
33-
t.Fatal(err)
41+
v2SimpleValidConfig = &Config{
42+
Profile: Profile{
43+
AccessKey: &v2ValidAccessKey,
44+
SecretKey: &v2ValidSecretKey,
45+
DefaultOrganizationID: &v2ValidDefaultOrganizationID,
46+
DefaultRegion: &v2ValidDefaultRegion,
47+
},
3448
}
35-
return dir
36-
}
49+
v2PartialValidConfigFile = `
50+
access_key: ` + v2ValidAccessKey + `
51+
secret_key: ` + v2ValidSecretKey + `
52+
api_url: ` + v2ValidAPIURL + `
53+
insecure: ` + v2ValidInsecure + `
54+
default_organization_id: ` + v2ValidDefaultOrganizationID + `
55+
default_region: ` + v2ValidDefaultRegion + `
56+
default_zone: ` + v2ValidDefaultZone
3757

38-
func cleanEnv(t *testing.T, files map[string]string, homeDir string) {
39-
for path := range files {
40-
testhelpers.AssertNoError(t, os.RemoveAll(filepath.Join(homeDir, path)))
41-
}
42-
}
58+
v2CompleteValidConfigFile = v2PartialValidConfigFile + `
59+
profiles:
60+
` + v2ValidProfile + `:
61+
access_key: ` + v2ValidAccessKey2 + `
62+
secret_key: ` + v2ValidSecretKey2 + `
63+
api_url: ` + v2ValidAPIURL2 + `
64+
insecure: ` + v2ValidInsecure2 + `
65+
default_organization_id: ` + v2ValidDefaultOrganizationID2 + `
66+
default_region: ` + v2ValidDefaultRegion2 + `
67+
default_zone: ` + v2ValidDefaultZone2 + `
68+
`
4369

44-
func setEnv(t *testing.T, env, files map[string]string, homeDir string) {
45-
os.Clearenv()
46-
for key, value := range env {
47-
value = strings.Replace(value, "{HOME}", homeDir, -1)
48-
testhelpers.AssertNoError(t, os.Setenv(key, value))
49-
}
70+
v2CompleteValidConfigWithActiveProfileFile = `
71+
access_key: ` + v2ValidAccessKey + `
72+
secret_key: ` + v2ValidSecretKey + `
73+
api_url: ` + v2ValidAPIURL + `
74+
insecure: ` + v2ValidInsecure + `
75+
default_organization_id: ` + v2ValidDefaultOrganizationID + `
76+
default_region: ` + v2ValidDefaultRegion + `
77+
default_zone: ` + v2ValidDefaultZone + `
78+
active_profile: ` + v2ValidProfile + `
79+
profiles:
80+
` + v2ValidProfile + `:
81+
access_key: ` + v2ValidAccessKey2 + `
82+
secret_key: ` + v2ValidSecretKey2 + `
83+
api_url: ` + v2ValidAPIURL2 + `
84+
insecure: ` + v2ValidInsecure2 + `
85+
default_organization_id: ` + v2ValidDefaultOrganizationID2 + `
86+
default_region: ` + v2ValidDefaultRegion2 + `
87+
default_zone: ` + v2ValidDefaultZone2 + `
88+
`
5089

51-
for path, content := range files {
52-
targetPath := filepath.Join(homeDir, path)
53-
testhelpers.AssertNoError(t, os.MkdirAll(filepath.Dir(targetPath), 0700))
54-
testhelpers.AssertNoError(t, ioutil.WriteFile(targetPath, []byte(content), defaultConfigPermission))
55-
}
56-
}
90+
v2MixedValidConfigWithActiveProfileFile = `
91+
access_key: ` + v2ValidAccessKey + `
92+
secret_key: ` + v2ValidSecretKey + `
93+
api_url: ` + v2ValidAPIURL + `
94+
insecure: ` + v2ValidInsecure + `
95+
default_organization_id: ` + v2ValidDefaultOrganizationID + `
96+
default_region: ` + v2ValidDefaultRegion + `
97+
default_zone: ` + v2ValidDefaultZone + `
98+
active_profile: ` + v2ValidProfile + `
99+
profiles:
100+
` + v2ValidProfile + `:
101+
access_key: ` + v2ValidAccessKey2 + `
102+
secret_key: ` + v2ValidSecretKey2 + `
103+
`
57104

58-
// function taken from https://golang.org/src/os/env_test.go
59-
func resetEnv(t *testing.T, origEnv []string, homeDir string) {
60-
testhelpers.AssertNoError(t, os.RemoveAll(homeDir))
61-
for _, pair := range origEnv {
62-
// Environment variables on Windows can begin with =
63-
// https://blogs.msdn.com/b/oldnewthing/archive/2010/05/06/10008132.aspx
64-
i := strings.Index(pair[1:], "=") + 1
65-
if err := os.Setenv(pair[:i], pair[i+1:]); err != nil {
66-
t.Errorf("Setenv(%q, %q) failed during reset: %v", pair[:i], pair[i+1:], err)
67-
}
68-
}
69-
}
105+
v2SimpleValidConfigFile = `
106+
access_key: ` + v2ValidAccessKey + `
107+
secret_key: ` + v2ValidSecretKey + `
108+
default_organization_id: ` + v2ValidDefaultOrganizationID + `
109+
default_region: ` + v2ValidDefaultRegion + `
110+
`
111+
112+
v2SimpleInvalidConfigFile = `insecure: "bool""`
113+
v2SimpleConfigFileWithInvalidProfile = `active_profile: flantier`
114+
115+
v2FromV1ConfigFile = `secret_key: ` + v1ValidToken + `
116+
default_organization_id: ` + v1ValidOrganizationID + `
117+
`
118+
)
70119

71120
// TestSaveConfig tests config write the correct values in the config file
72121
func TestSaveConfig(t *testing.T) {
@@ -312,6 +361,22 @@ func TestLoadProfileAndActiveProfile(t *testing.T) {
312361
expectedDefaultRegion: s(v2ValidDefaultRegion2),
313362
expectedDefaultZone: s(v2ValidDefaultZone2),
314363
},
364+
{
365+
name: "Mixed config with active profile",
366+
env: map[string]string{
367+
"HOME": "{HOME}",
368+
},
369+
files: map[string]string{
370+
".config/scw/config.yaml": v2MixedValidConfigWithActiveProfileFile,
371+
},
372+
expectedAccessKey: s(v2ValidAccessKey2),
373+
expectedSecretKey: s(v2ValidSecretKey2),
374+
expectedAPIURL: s(v2ValidAPIURL),
375+
expectedInsecure: b(false),
376+
expectedDefaultOrganizationID: s(v2ValidDefaultOrganizationID),
377+
expectedDefaultRegion: s(v2ValidDefaultRegion),
378+
expectedDefaultZone: s(v2ValidDefaultZone),
379+
},
315380
{
316381
name: "Complete config with active profile env variable",
317382
env: map[string]string{
@@ -400,97 +465,6 @@ func TestLoadProfileAndActiveProfile(t *testing.T) {
400465
}
401466
}
402467

403-
const emptyFile = ""
404-
405-
// v2 config
406-
var (
407-
v2ValidAccessKey2 = "SCW234567890ABCDEFGH"
408-
v2ValidSecretKey2 = "6f6e6574-6f72-756c-6c74-68656d616c6c" // hint: | xxd -ps -r
409-
v2ValidAPIURL2 = "api-fr-par.scaleway.com"
410-
v2ValidInsecure2 = "true"
411-
v2ValidDefaultOrganizationID2 = "6d6f7264-6f72-6772-6561-74616761696e" // hint: | xxd -ps -r
412-
v2ValidDefaultRegion2 = string(RegionFrPar)
413-
v2ValidDefaultZone2 = string(ZoneFrPar2)
414-
415-
v2ValidAccessKey = "SCW1234567890ABCDEFG"
416-
v2ValidSecretKey = "7363616c-6577-6573-6862-6f7579616161" // hint: | xxd -ps -r
417-
v2ValidAPIURL = "api.scaleway.com"
418-
v2ValidInsecure = "false"
419-
v2ValidDefaultOrganizationID = "6170692e-7363-616c-6577-61792e636f6d" // hint: | xxd -ps -r
420-
v2ValidDefaultRegion = string(RegionNlAms)
421-
v2ValidDefaultZone = string(ZoneNlAms1)
422-
v2ValidProfile = "flantier"
423-
424-
v2InvalidAccessKey = "invalid"
425-
v2InvalidSecretKey = "invalid"
426-
v2InvalidDefaultOrganizationID = "invalid"
427-
v2InvalidDefaultRegion = "invalid"
428-
v2InvalidDefaultZone = "invalid"
429-
430-
v2SimpleValidConfig = &Config{
431-
Profile: Profile{
432-
AccessKey: &v2ValidAccessKey,
433-
SecretKey: &v2ValidSecretKey,
434-
DefaultOrganizationID: &v2ValidDefaultOrganizationID,
435-
DefaultRegion: &v2ValidDefaultRegion,
436-
},
437-
}
438-
v2PartialValidConfigFile = `
439-
access_key: ` + v2ValidAccessKey + `
440-
secret_key: ` + v2ValidSecretKey + `
441-
api_url: ` + v2ValidAPIURL + `
442-
insecure: ` + v2ValidInsecure + `
443-
default_organization_id: ` + v2ValidDefaultOrganizationID + `
444-
default_region: ` + v2ValidDefaultRegion + `
445-
default_zone: ` + v2ValidDefaultZone
446-
447-
v2CompleteValidConfigFile = v2PartialValidConfigFile + `
448-
profiles:
449-
` + v2ValidProfile + `:
450-
access_key: ` + v2ValidAccessKey2 + `
451-
secret_key: ` + v2ValidSecretKey2 + `
452-
api_url: ` + v2ValidAPIURL2 + `
453-
insecure: ` + v2ValidInsecure2 + `
454-
default_organization_id: ` + v2ValidDefaultOrganizationID2 + `
455-
default_region: ` + v2ValidDefaultRegion2 + `
456-
default_zone: ` + v2ValidDefaultZone2 + `
457-
`
458-
459-
v2CompleteValidConfigWithActiveProfileFile = `
460-
access_key: ` + v2ValidAccessKey + `
461-
secret_key: ` + v2ValidSecretKey + `
462-
api_url: ` + v2ValidAPIURL + `
463-
insecure: ` + v2ValidInsecure + `
464-
default_organization_id: ` + v2ValidDefaultOrganizationID + `
465-
default_region: ` + v2ValidDefaultRegion + `
466-
default_zone: ` + v2ValidDefaultZone + `
467-
active_profile: ` + v2ValidProfile + `
468-
profiles:
469-
` + v2ValidProfile + `:
470-
access_key: ` + v2ValidAccessKey2 + `
471-
secret_key: ` + v2ValidSecretKey2 + `
472-
api_url: ` + v2ValidAPIURL2 + `
473-
insecure: ` + v2ValidInsecure2 + `
474-
default_organization_id: ` + v2ValidDefaultOrganizationID2 + `
475-
default_region: ` + v2ValidDefaultRegion2 + `
476-
default_zone: ` + v2ValidDefaultZone2 + `
477-
`
478-
479-
v2SimpleValidConfigFile = `
480-
access_key: ` + v2ValidAccessKey + `
481-
secret_key: ` + v2ValidSecretKey + `
482-
default_organization_id: ` + v2ValidDefaultOrganizationID + `
483-
default_region: ` + v2ValidDefaultRegion + `
484-
`
485-
486-
v2SimpleInvalidConfigFile = `insecure: "bool""`
487-
v2SimpleConfigFileWithInvalidProfile = `active_profile: flantier`
488-
489-
v2FromV1ConfigFile = `secret_key: ` + v1ValidToken + `
490-
default_organization_id: ` + v1ValidOrganizationID + `
491-
`
492-
)
493-
494468
func TestConfigString(t *testing.T) {
495469
var c = &Config{
496470
Profile: Profile{
@@ -549,3 +523,60 @@ func TestMergeProfiles(t *testing.T) {
549523

550524
testhelpers.Equals(t, exp, act)
551525
}
526+
527+
func initEnv(t *testing.T) string {
528+
dir, err := ioutil.TempDir("", "home")
529+
if err != nil {
530+
t.Fatal(err)
531+
}
532+
return dir
533+
}
534+
535+
func cleanEnv(t *testing.T, files map[string]string, homeDir string) {
536+
for path := range files {
537+
testhelpers.AssertNoError(t, os.RemoveAll(filepath.Join(homeDir, path)))
538+
}
539+
}
540+
541+
func setEnv(t *testing.T, env, files map[string]string, homeDir string) {
542+
os.Clearenv()
543+
for key, value := range env {
544+
value = strings.Replace(value, "{HOME}", homeDir, -1)
545+
testhelpers.AssertNoError(t, os.Setenv(key, value))
546+
}
547+
548+
for path, content := range files {
549+
targetPath := filepath.Join(homeDir, path)
550+
testhelpers.AssertNoError(t, os.MkdirAll(filepath.Dir(targetPath), 0700))
551+
testhelpers.AssertNoError(t, ioutil.WriteFile(targetPath, []byte(content), defaultConfigPermission))
552+
}
553+
}
554+
555+
// function taken from https://golang.org/src/os/env_test.go
556+
func resetEnv(t *testing.T, origEnv []string, homeDir string) {
557+
testhelpers.AssertNoError(t, os.RemoveAll(homeDir))
558+
for _, pair := range origEnv {
559+
// Environment variables on Windows can begin with =
560+
// https://blogs.msdn.com/b/oldnewthing/archive/2010/05/06/10008132.aspx
561+
i := strings.Index(pair[1:], "=") + 1
562+
if err := os.Setenv(pair[:i], pair[i+1:]); err != nil {
563+
t.Errorf("Setenv(%q, %q) failed during reset: %v", pair[:i], pair[i+1:], err)
564+
}
565+
}
566+
}
567+
568+
func s(value string) *string {
569+
return &value
570+
}
571+
572+
func r(value Region) *Region {
573+
return &value
574+
}
575+
576+
func z(value Zone) *Zone {
577+
return &value
578+
}
579+
580+
func b(value bool) *bool {
581+
return &value
582+
}

0 commit comments

Comments
 (0)