Skip to content

Commit

Permalink
Merge pull request #39 from gophercloud/clientconfig-default-domain
Browse files Browse the repository at this point in the history
clientconfig: Add support for default domain
  • Loading branch information
jtopjian authored May 22, 2018
2 parents 799f10b + 5205fa8 commit afce78e
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 39 deletions.
56 changes: 56 additions & 0 deletions openstack/clientconfig/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ func v3auth(cloud *Cloud, opts *ClientOpts) (*gophercloud.AuthOptions, error) {
cloud.AuthInfo.DomainName = v
}

if v := os.Getenv(envPrefix + "DEFAULT_DOMAIN"); v != "" {
cloud.AuthInfo.DefaultDomain = v
}

if v := os.Getenv(envPrefix + "PROJECT_DOMAIN_ID"); v != "" {
cloud.AuthInfo.ProjectDomainID = v
}
Expand Down Expand Up @@ -538,3 +542,55 @@ func NewServiceClient(service string, opts *ClientOpts) (*gophercloud.ServiceCli

return nil, fmt.Errorf("unable to create a service client for %s", service)
}

// isProjectScoped determines if an auth struct is project scoped.
func isProjectScoped(authInfo *AuthInfo) bool {
if authInfo.ProjectID == "" && authInfo.ProjectName == "" {
return false
}

return true
}

// setDomainIfNeeded will set a DomainID and DomainName
// to ProjectDomain* and UserDomain* if not already set.
func setDomainIfNeeded(cloud *Cloud) *Cloud {
if cloud.AuthInfo.DomainID != "" {
if cloud.AuthInfo.UserDomainID == "" {
cloud.AuthInfo.UserDomainID = cloud.AuthInfo.DomainID
}

if cloud.AuthInfo.ProjectDomainID == "" {
cloud.AuthInfo.ProjectDomainID = cloud.AuthInfo.DomainID
}

cloud.AuthInfo.DomainID = ""
}

if cloud.AuthInfo.DomainName != "" {
if cloud.AuthInfo.UserDomainName == "" {
cloud.AuthInfo.UserDomainName = cloud.AuthInfo.DomainName
}

if cloud.AuthInfo.ProjectDomainName == "" {
cloud.AuthInfo.ProjectDomainName = cloud.AuthInfo.DomainName
}

cloud.AuthInfo.DomainName = ""
}

// If Domain fields are still not set, and if DefaultDomain has a value,
// set UserDomainID and ProjectDomainID to DefaultDomain.
// https://github.com/openstack/osc-lib/blob/86129e6f88289ef14bfaa3f7c9cdfbea8d9fc944/osc_lib/cli/client_config.py#L117-L146
if cloud.AuthInfo.DefaultDomain != "" {
if cloud.AuthInfo.UserDomainName == "" && cloud.AuthInfo.UserDomainID == "" {
cloud.AuthInfo.UserDomainID = cloud.AuthInfo.DefaultDomain
}

if cloud.AuthInfo.ProjectDomainName == "" && cloud.AuthInfo.ProjectDomainID == "" {
cloud.AuthInfo.ProjectDomainID = cloud.AuthInfo.DefaultDomain
}
}

return cloud
}
4 changes: 4 additions & 0 deletions openstack/clientconfig/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,8 @@ type AuthInfo struct {
// used as a default choice.
// It can also be used be used to specify a domain-only scope.
DomainID string `yaml:"domain_id"`

// DefaultDomain is the domain ID to fall back on if no other domain has
// been specified and a domain is required for scope.
DefaultDomain string `yaml:"default_domain"`
}
10 changes: 10 additions & 0 deletions openstack/clientconfig/testing/clouds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ clouds:
project_name: "Some Project"
project_domain_name: "Some Domain"
region_name: "LAS"
texas:
profile: "Some profile"
auth:
auth_url: "https://tx.example.com:5000/v3"
username: "jdoe"
password: "password"
project_name: "Some Project"
user_domain_name: "Some Domain"
default_domain: "default"
region_name: "AUS"
alberta:
profile: "Some profile"
auth_type: "password"
Expand Down
45 changes: 45 additions & 0 deletions openstack/clientconfig/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,50 @@ var NevadaAuthOpts = &gophercloud.AuthOptions{
TenantName: "Some Project",
}

var TexasCloudYAML = clientconfig.Cloud{
RegionName: "AUS",
AuthInfo: &clientconfig.AuthInfo{
AuthURL: "https://tx.example.com:5000/v3",
Username: "jdoe",
Password: "password",
ProjectName: "Some Project",
UserDomainName: "Some Domain",
DefaultDomain: "default",
},
}

var TexasClientOpts = &clientconfig.ClientOpts{
AuthInfo: &clientconfig.AuthInfo{
AuthURL: "https://tx.example.com:5000/v3",
Username: "jdoe",
Password: "password",
ProjectName: "Some Project",
UserDomainName: "Some Domain",
DefaultDomain: "default",
},
}

var TexasEnvAuth = map[string]string{
"OS_AUTH_URL": "https://tx.example.com:5000/v3",
"OS_USERNAME": "jdoe",
"OS_PASSWORD": "password",
"OS_PROJECT_NAME": "Some Project",
"OS_USER_DOMAIN_NAME": "Some Domain",
"OS_DEFAULT_DOMAIN": "default",
}

var TexasAuthOpts = &gophercloud.AuthOptions{
Scope: &gophercloud.AuthScope{
ProjectName: "Some Project",
DomainID: "default",
},
IdentityEndpoint: "https://tx.example.com:5000/v3",
Username: "jdoe",
Password: "password",
TenantName: "Some Project",
DomainName: "Some Domain",
}

var CloudYAML = clientconfig.Clouds{
Clouds: map[string]clientconfig.Cloud{
"hawaii": HawaiiCloudYAML,
Expand All @@ -266,6 +310,7 @@ var CloudYAML = clientconfig.Clouds{
"arizona": ArizonaCloudYAML,
"newmexico": NewMexicoCloudYAML,
"nevada": NevadaCloudYAML,
"texas": TexasCloudYAML,
},
}

Expand Down
5 changes: 5 additions & 0 deletions openstack/clientconfig/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func TestAuthOptionsCreationFromCloudsYAML(t *testing.T) {
"arizona": ArizonaAuthOpts,
"newmexico": NewMexicoAuthOpts,
"nevada": NevadaAuthOpts,
"texas": TexasAuthOpts,
}

for cloud, expected := range allClouds {
Expand Down Expand Up @@ -123,6 +124,7 @@ func TestAuthOptionsCreationFromClientConfig(t *testing.T) {
"arizona": ArizonaAuthOpts,
"newmexico": NewMexicoAuthOpts,
"nevada": NevadaAuthOpts,
"texas": TexasAuthOpts,
}

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

for cloud, clientOpts := range allClientOpts {
Expand Down Expand Up @@ -171,6 +174,7 @@ func TestAuthOptionsCreationFromEnv(t *testing.T) {
"arizona": ArizonaEnvAuth,
"newmexico": NewMexicoEnvAuth,
"nevada": NevadaEnvAuth,
"texas": TexasEnvAuth,
}

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

for cloud, envVars := range allEnvVars {
Expand Down
39 changes: 0 additions & 39 deletions openstack/clientconfig/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,42 +65,3 @@ func fileExists(filename string) bool {
}
return false
}

// isProjectScoped determines if an auth struct is project scoped.
func isProjectScoped(authInfo *AuthInfo) bool {
if authInfo.ProjectID == "" && authInfo.ProjectName == "" {
return false
}

return true
}

// setDomainIfNeeded will set a DomainID and DomainName
// to ProjectDomain* and UserDomain* if not already set.
func setDomainIfNeeded(cloud *Cloud) *Cloud {
if cloud.AuthInfo.DomainID != "" {
if cloud.AuthInfo.UserDomainID == "" {
cloud.AuthInfo.UserDomainID = cloud.AuthInfo.DomainID
}

if cloud.AuthInfo.ProjectDomainID == "" {
cloud.AuthInfo.ProjectDomainID = cloud.AuthInfo.DomainID
}

cloud.AuthInfo.DomainID = ""
}

if cloud.AuthInfo.DomainName != "" {
if cloud.AuthInfo.UserDomainName == "" {
cloud.AuthInfo.UserDomainName = cloud.AuthInfo.DomainName
}

if cloud.AuthInfo.ProjectDomainName == "" {
cloud.AuthInfo.ProjectDomainName = cloud.AuthInfo.DomainName
}

cloud.AuthInfo.DomainName = ""
}

return cloud
}

0 comments on commit afce78e

Please sign in to comment.