Skip to content

Commit d0ab9f2

Browse files
authored
Merge pull request #165 from stephenfin/issues/164
clientconfig: Ignore OS_CLOUD if explicit cloud config provided
2 parents d708520 + b2ceaa8 commit d0ab9f2

2 files changed

Lines changed: 80 additions & 29 deletions

File tree

openstack/clientconfig/requests.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ func LoadPublicCloudsYAML() (map[string]Cloud, error) {
183183

184184
// GetCloudFromYAML will return a cloud entry from a clouds.yaml file.
185185
func GetCloudFromYAML(opts *ClientOpts) (*Cloud, error) {
186+
if opts == nil {
187+
opts = new(ClientOpts)
188+
}
189+
186190
if opts.YAMLOpts == nil {
187191
opts.YAMLOpts = new(YAMLOpts)
188192
}
@@ -197,19 +201,18 @@ func GetCloudFromYAML(opts *ClientOpts) (*Cloud, error) {
197201
// Determine which cloud to use.
198202
// First see if a cloud name was explicitly set in opts.
199203
var cloudName string
200-
if opts != nil && opts.Cloud != "" {
204+
if opts.Cloud != "" {
201205
cloudName = opts.Cloud
202-
}
203-
204-
// Next see if a cloud name was specified as an environment variable.
205-
// This is supposed to override an explicit opts setting.
206-
envPrefix := "OS_"
207-
if opts.EnvPrefix != "" {
208-
envPrefix = opts.EnvPrefix
209-
}
206+
} else {
207+
// If not, see if a cloud name was specified as an environment variable.
208+
envPrefix := "OS_"
209+
if opts.EnvPrefix != "" {
210+
envPrefix = opts.EnvPrefix
211+
}
210212

211-
if v := env.Getenv(envPrefix + "CLOUD"); v != "" {
212-
cloudName = v
213+
if v := env.Getenv(envPrefix + "CLOUD"); v != "" {
214+
cloudName = v
215+
}
213216
}
214217

215218
var cloud *Cloud
@@ -351,16 +354,17 @@ func AuthOptions(opts *ClientOpts) (*gophercloud.AuthOptions, error) {
351354
var cloudName string
352355
if opts.Cloud != "" {
353356
cloudName = opts.Cloud
354-
}
355-
356-
// Next see if a cloud name was specified as an environment variable.
357-
envPrefix := "OS_"
358-
if opts.EnvPrefix != "" {
359-
envPrefix = opts.EnvPrefix
360-
}
357+
} else {
358+
// If not, see if a cloud name was specified as an environment
359+
// variable.
360+
envPrefix := "OS_"
361+
if opts.EnvPrefix != "" {
362+
envPrefix = opts.EnvPrefix
363+
}
361364

362-
if v := env.Getenv(envPrefix + "CLOUD"); v != "" {
363-
cloudName = v
365+
if v := env.Getenv(envPrefix + "CLOUD"); v != "" {
366+
cloudName = v
367+
}
364368
}
365369

366370
// If a cloud name was determined, try to look it up in clouds.yaml.

openstack/clientconfig/testing/requests_test.go

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@ func TestGetCloudFromYAML(t *testing.T) {
8484
}
8585
}
8686

87+
func TestGetCloudFromYAMLOSCLOUD(t *testing.T) {
88+
os.Setenv("OS_CLOUD", "california")
89+
defer os.Unsetenv("OS_CLOUD")
90+
91+
clientOpts := &clientconfig.ClientOpts{
92+
Cloud: "hawaii",
93+
}
94+
95+
actual, err := clientconfig.GetCloudFromYAML(clientOpts)
96+
th.AssertNoErr(t, err)
97+
th.AssertDeepEquals(t, &HawaiiCloudYAML, actual)
98+
}
99+
100+
func TestGetCloudFromYAMLMissingClientOpts(t *testing.T) {
101+
os.Setenv("OS_CLOUD", "california")
102+
defer os.Unsetenv("OS_CLOUD")
103+
104+
actual, err := clientconfig.GetCloudFromYAML(nil)
105+
th.AssertNoErr(t, err)
106+
th.AssertDeepEquals(t, &CaliforniaCloudYAML, actual)
107+
}
108+
87109
func TestAuthOptionsExplicitCloud(t *testing.T) {
88110
os.Unsetenv("OS_CLOUD")
89111

@@ -101,6 +123,7 @@ func TestAuthOptionsExplicitCloud(t *testing.T) {
101123

102124
func TestAuthOptionsOSCLOUD(t *testing.T) {
103125
os.Setenv("FOO_CLOUD", "hawaii")
126+
defer os.Unsetenv("FOO_CLOUD")
104127

105128
clientOpts := &clientconfig.ClientOpts{
106129
EnvPrefix: "FOO_",
@@ -112,8 +135,38 @@ func TestAuthOptionsOSCLOUD(t *testing.T) {
112135
}
113136

114137
th.AssertDeepEquals(t, HawaiiAuthOpts, actual)
138+
}
139+
140+
func TestAuthOptionsExplicitCloudAndOSCLOUD(t *testing.T) {
141+
os.Setenv("FOO_CLOUD", "hawaii")
142+
defer os.Unsetenv("FOO_CLOUD")
143+
144+
clientOpts := &clientconfig.ClientOpts{
145+
EnvPrefix: "FOO_",
146+
Cloud: "california",
147+
}
115148

116-
os.Unsetenv("FOO_CLOUD")
149+
actual, err := clientconfig.AuthOptions(clientOpts)
150+
if err != nil {
151+
t.Fatal(err)
152+
}
153+
154+
// We should have ignored the cloud configuration option
155+
th.AssertDeepEquals(t, CaliforniaAuthOpts, actual)
156+
}
157+
158+
func TestAuthOptionsMissingClientOpts(t *testing.T) {
159+
os.Setenv("OS_CLOUD", "hawaii")
160+
defer os.Unsetenv("OS_CLOUD")
161+
162+
actual, err := clientconfig.AuthOptions(nil)
163+
if err != nil {
164+
t.Fatal(err)
165+
}
166+
167+
// We should have handled the missing config opts and fallen back to
168+
// defaults
169+
th.AssertDeepEquals(t, HawaiiAuthOpts, actual)
117170
}
118171

119172
func TestAuthOptionsCreationFromCloudsYAML(t *testing.T) {
@@ -246,15 +299,12 @@ func TestAuthOptionsCreationFromEnv(t *testing.T) {
246299
for cloud, envVars := range allEnvVars {
247300
for k, v := range envVars {
248301
os.Setenv(k, v)
302+
defer os.Unsetenv(k)
249303
}
250304

251305
actualAuthOpts, err := clientconfig.AuthOptions(nil)
252306
th.AssertNoErr(t, err)
253307
th.AssertDeepEquals(t, expectedAuthOpts[cloud], actualAuthOpts)
254-
255-
for k := range envVars {
256-
os.Unsetenv(k)
257-
}
258308
}
259309
}
260310

@@ -274,15 +324,12 @@ func TestAuthOptionsCreationFromLegacyEnv(t *testing.T) {
274324
for cloud, envVars := range allEnvVars {
275325
for k, v := range envVars {
276326
os.Setenv(k, v)
327+
defer os.Unsetenv(k)
277328
}
278329

279330
actualAuthOpts, err := clientconfig.AuthOptions(nil)
280331
th.AssertNoErr(t, err)
281332
th.AssertDeepEquals(t, expectedAuthOpts[cloud], actualAuthOpts)
282-
283-
for k := range envVars {
284-
os.Unsetenv(k)
285-
}
286333
}
287334
}
288335

0 commit comments

Comments
 (0)