Jamie Hannaford | a49b374 | 2014-10-22 17:03:48 +0200 | [diff] [blame] | 1 | package rackspace |
Jamie Hannaford | 9272104 | 2014-10-22 15:21:29 +0200 | [diff] [blame] | 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 |
Ash Wilson | e447449 | 2014-10-22 15:17:25 -0400 | [diff] [blame] | 13 | // required RS_AUTH_URL, RS_USERNAME, or RS_PASSWORD environment variables, |
Jamie Hannaford | 9272104 | 2014-10-22 15:21:29 +0200 | [diff] [blame] | 14 | // respectively, remain undefined. See the AuthOptions() function for more details. |
| 15 | var ( |
Ash Wilson | e447449 | 2014-10-22 15:17:25 -0400 | [diff] [blame] | 16 | ErrNoAuthURL = fmt.Errorf("Environment variable RS_AUTH_URL or OS_AUTH_URL need to be set.") |
| 17 | ErrNoUsername = fmt.Errorf("Environment variable RS_USERNAME or OS_USERNAME need to be set.") |
| 18 | ErrNoPassword = fmt.Errorf("Environment variable RS_API_KEY or RS_PASSWORD needs to be set.") |
Jamie Hannaford | 9272104 | 2014-10-22 15:21:29 +0200 | [diff] [blame] | 19 | ) |
| 20 | |
Ash Wilson | e447449 | 2014-10-22 15:17:25 -0400 | [diff] [blame] | 21 | func prefixedEnv(base string) string { |
| 22 | value := os.Getenv("RS_" + base) |
| 23 | if value == "" { |
| 24 | value = os.Getenv("OS_" + base) |
| 25 | } |
| 26 | return value |
| 27 | } |
| 28 | |
Jamie Hannaford | 9272104 | 2014-10-22 15:21:29 +0200 | [diff] [blame] | 29 | // AuthOptionsFromEnv fills out an identity.AuthOptions structure with the |
Ash Wilson | e447449 | 2014-10-22 15:17:25 -0400 | [diff] [blame] | 30 | // settings found on the various Rackspace RS_* environment variables. |
Jamie Hannaford | 9272104 | 2014-10-22 15:21:29 +0200 | [diff] [blame] | 31 | func AuthOptionsFromEnv() (gophercloud.AuthOptions, error) { |
Ash Wilson | e447449 | 2014-10-22 15:17:25 -0400 | [diff] [blame] | 32 | authURL := prefixedEnv("AUTH_URL") |
| 33 | username := prefixedEnv("USERNAME") |
| 34 | password := prefixedEnv("PASSWORD") |
| 35 | apiKey := prefixedEnv("API_KEY") |
Jamie Hannaford | 9272104 | 2014-10-22 15:21:29 +0200 | [diff] [blame] | 36 | |
| 37 | if authURL == "" { |
| 38 | return nilOptions, ErrNoAuthURL |
| 39 | } |
| 40 | |
| 41 | if username == "" { |
| 42 | return nilOptions, ErrNoUsername |
| 43 | } |
| 44 | |
| 45 | if password == "" && apiKey == "" { |
| 46 | return nilOptions, ErrNoPassword |
| 47 | } |
| 48 | |
| 49 | ao := gophercloud.AuthOptions{ |
| 50 | IdentityEndpoint: authURL, |
| 51 | Username: username, |
| 52 | Password: password, |
| 53 | APIKey: apiKey, |
| 54 | } |
| 55 | |
| 56 | return ao, nil |
| 57 | } |