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 |
| 13 | // required RAX_AUTH_URL, RAX_USERNAME, or RAX_PASSWORD environment variables, |
| 14 | // respectively, remain undefined. See the AuthOptions() function for more details. |
| 15 | var ( |
| 16 | ErrNoAuthURL = fmt.Errorf("Environment variable RAX_AUTH_URL needs to be set.") |
| 17 | ErrNoUsername = fmt.Errorf("Environment variable RAX_USERNAME needs to be set.") |
| 18 | ErrNoPassword = fmt.Errorf("Environment variable RAX_API_KEY or RAX_PASSWORD needs to be set.") |
| 19 | ) |
| 20 | |
| 21 | // AuthOptionsFromEnv fills out an identity.AuthOptions structure with the |
| 22 | // settings found on the various Rackspace RAX_* environment variables. |
| 23 | func AuthOptionsFromEnv() (gophercloud.AuthOptions, error) { |
| 24 | authURL := os.Getenv("RAX_AUTH_URL") |
| 25 | username := os.Getenv("RAX_USERNAME") |
| 26 | password := os.Getenv("RAX_PASSWORD") |
| 27 | apiKey := os.Getenv("RAX_API_KEY") |
| 28 | |
| 29 | if authURL == "" { |
| 30 | return nilOptions, ErrNoAuthURL |
| 31 | } |
| 32 | |
| 33 | if username == "" { |
| 34 | return nilOptions, ErrNoUsername |
| 35 | } |
| 36 | |
| 37 | if password == "" && apiKey == "" { |
| 38 | return nilOptions, ErrNoPassword |
| 39 | } |
| 40 | |
| 41 | ao := gophercloud.AuthOptions{ |
| 42 | IdentityEndpoint: authURL, |
| 43 | Username: username, |
| 44 | Password: password, |
| 45 | APIKey: apiKey, |
| 46 | } |
| 47 | |
| 48 | return ao, nil |
| 49 | } |