Jamie Hannaford | 6adb125 | 2014-10-22 15:25:44 +0200 | [diff] [blame] | 1 | package openstack |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | |
| 7 | "github.com/rackspace/gophercloud" |
| 8 | ) |
| 9 | |
| 10 | var nilOptions = gophercloud.AuthOptions{} |
| 11 | |
| 12 | // ErrNoAuthUrl, ErrNoUsername, and ErrNoPassword errors indicate of the required OS_AUTH_URL, OS_USERNAME, or OS_PASSWORD |
| 13 | // environment variables, respectively, remain undefined. See the AuthOptions() function for more details. |
| 14 | var ( |
| 15 | ErrNoAuthURL = fmt.Errorf("Environment variable OS_AUTH_URL needs to be set.") |
| 16 | ErrNoUsername = fmt.Errorf("Environment variable OS_USERNAME needs to be set.") |
| 17 | ErrNoPassword = fmt.Errorf("Environment variable OS_PASSWORD needs to be set.") |
| 18 | ) |
| 19 | |
| 20 | // AuthOptions fills out an identity.AuthOptions structure with the settings found on the various OpenStack |
| 21 | // OS_* environment variables. The following variables provide sources of truth: OS_AUTH_URL, OS_USERNAME, |
| 22 | // OS_PASSWORD, OS_TENANT_ID, and OS_TENANT_NAME. Of these, OS_USERNAME, OS_PASSWORD, and OS_AUTH_URL must |
| 23 | // have settings, or an error will result. OS_TENANT_ID and OS_TENANT_NAME are optional. |
Jamie Hannaford | ee049ec | 2014-10-22 17:02:55 +0200 | [diff] [blame] | 24 | func AuthOptionsFromEnv() (gophercloud.AuthOptions, error) { |
Jamie Hannaford | 6adb125 | 2014-10-22 15:25:44 +0200 | [diff] [blame] | 25 | authURL := os.Getenv("OS_AUTH_URL") |
| 26 | username := os.Getenv("OS_USERNAME") |
| 27 | userID := os.Getenv("OS_USERID") |
| 28 | password := os.Getenv("OS_PASSWORD") |
| 29 | tenantID := os.Getenv("OS_TENANT_ID") |
| 30 | tenantName := os.Getenv("OS_TENANT_NAME") |
| 31 | domainID := os.Getenv("OS_DOMAIN_ID") |
| 32 | domainName := os.Getenv("OS_DOMAIN_NAME") |
| 33 | |
| 34 | if authURL == "" { |
| 35 | return nilOptions, ErrNoAuthURL |
| 36 | } |
| 37 | |
| 38 | if username == "" && userID == "" { |
| 39 | return nilOptions, ErrNoUsername |
| 40 | } |
| 41 | |
| 42 | if password == "" { |
| 43 | return nilOptions, ErrNoPassword |
| 44 | } |
| 45 | |
| 46 | ao := gophercloud.AuthOptions{ |
| 47 | IdentityEndpoint: authURL, |
| 48 | UserID: userID, |
| 49 | Username: username, |
| 50 | Password: password, |
| 51 | TenantID: tenantID, |
| 52 | TenantName: tenantName, |
| 53 | DomainID: domainID, |
| 54 | DomainName: domainName, |
| 55 | } |
| 56 | |
| 57 | return ao, nil |
| 58 | } |