blob: 1706cc4c5936e65a645a9eb4075092901a89f59b [file] [log] [blame]
Jamie Hannaforda49b3742014-10-22 17:03:48 +02001package rackspace
Jamie Hannaford92721042014-10-22 15:21:29 +02002
3import (
4 "fmt"
5 "os"
6
7 "github.com/rackspace/gophercloud"
8)
9
10var 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.
15var (
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.
23func 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}