Skip to content

Commit 9ed4d5f

Browse files
authored
Add single configuration loader (#5)
* Add single configuration loader Refactor AuthOptionsFromEnv Fix miss-prints in related files
1 parent 28fd654 commit 9ed4d5f

File tree

6 files changed

+676
-55
lines changed

6 files changed

+676
-55
lines changed

auth_aksk_options.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ type AKSKAuthOptions struct {
2727
BssDomain string
2828
BssDomainID string
2929

30-
AccessKey string //Access Key
31-
SecretKey string //Secret key
30+
AccessKey string // Access Key
31+
SecretKey string // Secret key
3232

33-
// AgencyNmae is the name of agnecy
33+
// AgencyName is the name of agency
3434
AgencyName string
3535

3636
// AgencyDomainName is the name of domain who created the agency

auth_options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ type AuthOptions struct {
8282
// authentication token ID.
8383
TokenID string `json:"-"`
8484

85-
// AgencyNmae is the name of agnecy
85+
// AgencyName is the name of agency
8686
AgencyName string `json:"-"`
8787

8888
// AgencyDomainName is the name of domain who created the agency

openstack/auth_env.go

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package openstack
22

33
import (
4-
"os"
5-
6-
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
4+
"github.com/opentelekomcloud/gophertelekomcloud"
75
)
86

9-
var nilOptions = golangsdk.AuthOptions{}
10-
117
/*
128
AuthOptionsFromEnv fills out an identity.AuthOptions structure with the
139
settings found on the various OpenStack OS_* environment variables.
@@ -29,51 +25,25 @@ by sourcing an `openrc` file), then:
2925
opts, err := openstack.AuthOptionsFromEnv()
3026
provider, err := openstack.AuthenticatedClient(opts)
3127
*/
32-
func AuthOptionsFromEnv() (golangsdk.AuthOptions, error) {
33-
authURL := os.Getenv("OS_AUTH_URL")
34-
username := os.Getenv("OS_USERNAME")
35-
userID := os.Getenv("OS_USERID")
36-
password := os.Getenv("OS_PASSWORD")
37-
tenantID := os.Getenv("OS_TENANT_ID")
38-
tenantName := os.Getenv("OS_TENANT_NAME")
39-
domainID := os.Getenv("OS_DOMAIN_ID")
40-
domainName := os.Getenv("OS_DOMAIN_NAME")
41-
42-
// If OS_PROJECT_ID is set, overwrite tenantID with the value.
43-
if v := os.Getenv("OS_PROJECT_ID"); v != "" {
44-
tenantID = v
45-
}
46-
47-
// If OS_PROJECT_NAME is set, overwrite tenantName with the value.
48-
if v := os.Getenv("OS_PROJECT_NAME"); v != "" {
49-
tenantName = v
50-
}
51-
52-
if authURL == "" {
53-
err := golangsdk.ErrMissingInput{Argument: "authURL"}
54-
return nilOptions, err
55-
}
56-
57-
if username == "" && userID == "" {
58-
err := golangsdk.ErrMissingInput{Argument: "username"}
59-
return nilOptions, err
60-
}
61-
62-
if password == "" {
63-
err := golangsdk.ErrMissingInput{Argument: "password"}
64-
return nilOptions, err
28+
func AuthOptionsFromEnv(envs ...*env) (golangsdk.AuthOptions, error) {
29+
e := NewEnv(defaultPrefix)
30+
if len(envs) > 0 {
31+
e = envs[0]
6532
}
6633

6734
ao := golangsdk.AuthOptions{
68-
IdentityEndpoint: authURL,
69-
UserID: userID,
70-
Username: username,
71-
Password: password,
72-
TenantID: tenantID,
73-
TenantName: tenantName,
74-
DomainID: domainID,
75-
DomainName: domainName,
35+
IdentityEndpoint: e.GetEnv("AUTH_URL"),
36+
Username: e.GetEnv("USERNAME"),
37+
UserID: e.GetEnv("USERID", "USER_ID"),
38+
Password: e.GetEnv("PASSWORD"),
39+
DomainID: e.GetEnv("DOMAIN_ID"),
40+
DomainName: e.GetEnv("DOMAIN_NAME"),
41+
TenantID: e.GetEnv("PROJECT_ID", "TENANT_ID"),
42+
TenantName: e.GetEnv("PROJECT_NAME", "TENANT_NAME"),
43+
TokenID: e.GetEnv("TOKEN", "TOKEN_ID"),
44+
AgencyName: e.GetEnv("AGENCY_NAME", "TARGET_AGENCY_NAME"),
45+
AgencyDomainName: e.GetEnv("AGENCY_DOMAIN_NAME", "TARGET_DOMAIN_NAME"),
46+
DelegatedProject: e.GetEnv("DELEGATED_PROJECT", "TARGET_DOMAIN_NAME"),
7647
}
77-
7848
return ao, nil
7949
}

openstack/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ Example:
9191
Region: os.Getenv("OS_REGION_NAME"),
9292
})
9393
*/
94-
func AuthenticatedClient(options golangsdk.AuthOptions) (*golangsdk.ProviderClient, error) {
95-
client, err := NewClient(options.IdentityEndpoint)
94+
func AuthenticatedClient(options golangsdk.AuthOptionsProvider) (*golangsdk.ProviderClient, error) {
95+
client, err := NewClient(options.GetIdentityEndpoint())
9696
if err != nil {
9797
return nil, err
9898
}

0 commit comments

Comments
 (0)