Skip to content

Commit afce78e

Browse files
authored
Merge pull request #39 from gophercloud/clientconfig-default-domain
clientconfig: Add support for default domain
2 parents 799f10b + 5205fa8 commit afce78e

File tree

6 files changed

+120
-39
lines changed

6 files changed

+120
-39
lines changed

openstack/clientconfig/requests.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ func v3auth(cloud *Cloud, opts *ClientOpts) (*gophercloud.AuthOptions, error) {
338338
cloud.AuthInfo.DomainName = v
339339
}
340340

341+
if v := os.Getenv(envPrefix + "DEFAULT_DOMAIN"); v != "" {
342+
cloud.AuthInfo.DefaultDomain = v
343+
}
344+
341345
if v := os.Getenv(envPrefix + "PROJECT_DOMAIN_ID"); v != "" {
342346
cloud.AuthInfo.ProjectDomainID = v
343347
}
@@ -538,3 +542,55 @@ func NewServiceClient(service string, opts *ClientOpts) (*gophercloud.ServiceCli
538542

539543
return nil, fmt.Errorf("unable to create a service client for %s", service)
540544
}
545+
546+
// isProjectScoped determines if an auth struct is project scoped.
547+
func isProjectScoped(authInfo *AuthInfo) bool {
548+
if authInfo.ProjectID == "" && authInfo.ProjectName == "" {
549+
return false
550+
}
551+
552+
return true
553+
}
554+
555+
// setDomainIfNeeded will set a DomainID and DomainName
556+
// to ProjectDomain* and UserDomain* if not already set.
557+
func setDomainIfNeeded(cloud *Cloud) *Cloud {
558+
if cloud.AuthInfo.DomainID != "" {
559+
if cloud.AuthInfo.UserDomainID == "" {
560+
cloud.AuthInfo.UserDomainID = cloud.AuthInfo.DomainID
561+
}
562+
563+
if cloud.AuthInfo.ProjectDomainID == "" {
564+
cloud.AuthInfo.ProjectDomainID = cloud.AuthInfo.DomainID
565+
}
566+
567+
cloud.AuthInfo.DomainID = ""
568+
}
569+
570+
if cloud.AuthInfo.DomainName != "" {
571+
if cloud.AuthInfo.UserDomainName == "" {
572+
cloud.AuthInfo.UserDomainName = cloud.AuthInfo.DomainName
573+
}
574+
575+
if cloud.AuthInfo.ProjectDomainName == "" {
576+
cloud.AuthInfo.ProjectDomainName = cloud.AuthInfo.DomainName
577+
}
578+
579+
cloud.AuthInfo.DomainName = ""
580+
}
581+
582+
// If Domain fields are still not set, and if DefaultDomain has a value,
583+
// set UserDomainID and ProjectDomainID to DefaultDomain.
584+
// https://github.com/openstack/osc-lib/blob/86129e6f88289ef14bfaa3f7c9cdfbea8d9fc944/osc_lib/cli/client_config.py#L117-L146
585+
if cloud.AuthInfo.DefaultDomain != "" {
586+
if cloud.AuthInfo.UserDomainName == "" && cloud.AuthInfo.UserDomainID == "" {
587+
cloud.AuthInfo.UserDomainID = cloud.AuthInfo.DefaultDomain
588+
}
589+
590+
if cloud.AuthInfo.ProjectDomainName == "" && cloud.AuthInfo.ProjectDomainID == "" {
591+
cloud.AuthInfo.ProjectDomainID = cloud.AuthInfo.DefaultDomain
592+
}
593+
}
594+
595+
return cloud
596+
}

openstack/clientconfig/results.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,8 @@ type AuthInfo struct {
8181
// used as a default choice.
8282
// It can also be used be used to specify a domain-only scope.
8383
DomainID string `yaml:"domain_id"`
84+
85+
// DefaultDomain is the domain ID to fall back on if no other domain has
86+
// been specified and a domain is required for scope.
87+
DefaultDomain string `yaml:"default_domain"`
8488
}

openstack/clientconfig/testing/clouds.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ clouds:
6060
project_name: "Some Project"
6161
project_domain_name: "Some Domain"
6262
region_name: "LAS"
63+
texas:
64+
profile: "Some profile"
65+
auth:
66+
auth_url: "https://tx.example.com:5000/v3"
67+
username: "jdoe"
68+
password: "password"
69+
project_name: "Some Project"
70+
user_domain_name: "Some Domain"
71+
default_domain: "default"
72+
region_name: "AUS"
6373
alberta:
6474
profile: "Some profile"
6575
auth_type: "password"

openstack/clientconfig/testing/fixtures.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,50 @@ var NevadaAuthOpts = &gophercloud.AuthOptions{
258258
TenantName: "Some Project",
259259
}
260260

261+
var TexasCloudYAML = clientconfig.Cloud{
262+
RegionName: "AUS",
263+
AuthInfo: &clientconfig.AuthInfo{
264+
AuthURL: "https://tx.example.com:5000/v3",
265+
Username: "jdoe",
266+
Password: "password",
267+
ProjectName: "Some Project",
268+
UserDomainName: "Some Domain",
269+
DefaultDomain: "default",
270+
},
271+
}
272+
273+
var TexasClientOpts = &clientconfig.ClientOpts{
274+
AuthInfo: &clientconfig.AuthInfo{
275+
AuthURL: "https://tx.example.com:5000/v3",
276+
Username: "jdoe",
277+
Password: "password",
278+
ProjectName: "Some Project",
279+
UserDomainName: "Some Domain",
280+
DefaultDomain: "default",
281+
},
282+
}
283+
284+
var TexasEnvAuth = map[string]string{
285+
"OS_AUTH_URL": "https://tx.example.com:5000/v3",
286+
"OS_USERNAME": "jdoe",
287+
"OS_PASSWORD": "password",
288+
"OS_PROJECT_NAME": "Some Project",
289+
"OS_USER_DOMAIN_NAME": "Some Domain",
290+
"OS_DEFAULT_DOMAIN": "default",
291+
}
292+
293+
var TexasAuthOpts = &gophercloud.AuthOptions{
294+
Scope: &gophercloud.AuthScope{
295+
ProjectName: "Some Project",
296+
DomainID: "default",
297+
},
298+
IdentityEndpoint: "https://tx.example.com:5000/v3",
299+
Username: "jdoe",
300+
Password: "password",
301+
TenantName: "Some Project",
302+
DomainName: "Some Domain",
303+
}
304+
261305
var CloudYAML = clientconfig.Clouds{
262306
Clouds: map[string]clientconfig.Cloud{
263307
"hawaii": HawaiiCloudYAML,
@@ -266,6 +310,7 @@ var CloudYAML = clientconfig.Clouds{
266310
"arizona": ArizonaCloudYAML,
267311
"newmexico": NewMexicoCloudYAML,
268312
"nevada": NevadaCloudYAML,
313+
"texas": TexasCloudYAML,
269314
},
270315
}
271316

openstack/clientconfig/testing/requests_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func TestAuthOptionsCreationFromCloudsYAML(t *testing.T) {
7272
"arizona": ArizonaAuthOpts,
7373
"newmexico": NewMexicoAuthOpts,
7474
"nevada": NevadaAuthOpts,
75+
"texas": TexasAuthOpts,
7576
}
7677

7778
for cloud, expected := range allClouds {
@@ -123,6 +124,7 @@ func TestAuthOptionsCreationFromClientConfig(t *testing.T) {
123124
"arizona": ArizonaAuthOpts,
124125
"newmexico": NewMexicoAuthOpts,
125126
"nevada": NevadaAuthOpts,
127+
"texas": TexasAuthOpts,
126128
}
127129

128130
allClientOpts := map[string]*clientconfig.ClientOpts{
@@ -132,6 +134,7 @@ func TestAuthOptionsCreationFromClientConfig(t *testing.T) {
132134
"arizona": ArizonaClientOpts,
133135
"newmexico": NewMexicoClientOpts,
134136
"nevada": NevadaClientOpts,
137+
"texas": TexasClientOpts,
135138
}
136139

137140
for cloud, clientOpts := range allClientOpts {
@@ -171,6 +174,7 @@ func TestAuthOptionsCreationFromEnv(t *testing.T) {
171174
"arizona": ArizonaEnvAuth,
172175
"newmexico": NewMexicoEnvAuth,
173176
"nevada": NevadaEnvAuth,
177+
"texas": TexasEnvAuth,
174178
}
175179

176180
expectedAuthOpts := map[string]*gophercloud.AuthOptions{
@@ -180,6 +184,7 @@ func TestAuthOptionsCreationFromEnv(t *testing.T) {
180184
"arizona": ArizonaAuthOpts,
181185
"newmexico": NewMexicoAuthOpts,
182186
"nevada": NevadaAuthOpts,
187+
"texas": TexasAuthOpts,
183188
}
184189

185190
for cloud, envVars := range allEnvVars {

openstack/clientconfig/utils.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -65,42 +65,3 @@ func fileExists(filename string) bool {
6565
}
6666
return false
6767
}
68-
69-
// isProjectScoped determines if an auth struct is project scoped.
70-
func isProjectScoped(authInfo *AuthInfo) bool {
71-
if authInfo.ProjectID == "" && authInfo.ProjectName == "" {
72-
return false
73-
}
74-
75-
return true
76-
}
77-
78-
// setDomainIfNeeded will set a DomainID and DomainName
79-
// to ProjectDomain* and UserDomain* if not already set.
80-
func setDomainIfNeeded(cloud *Cloud) *Cloud {
81-
if cloud.AuthInfo.DomainID != "" {
82-
if cloud.AuthInfo.UserDomainID == "" {
83-
cloud.AuthInfo.UserDomainID = cloud.AuthInfo.DomainID
84-
}
85-
86-
if cloud.AuthInfo.ProjectDomainID == "" {
87-
cloud.AuthInfo.ProjectDomainID = cloud.AuthInfo.DomainID
88-
}
89-
90-
cloud.AuthInfo.DomainID = ""
91-
}
92-
93-
if cloud.AuthInfo.DomainName != "" {
94-
if cloud.AuthInfo.UserDomainName == "" {
95-
cloud.AuthInfo.UserDomainName = cloud.AuthInfo.DomainName
96-
}
97-
98-
if cloud.AuthInfo.ProjectDomainName == "" {
99-
cloud.AuthInfo.ProjectDomainName = cloud.AuthInfo.DomainName
100-
}
101-
102-
cloud.AuthInfo.DomainName = ""
103-
}
104-
105-
return cloud
106-
}

0 commit comments

Comments
 (0)