blob: 5852c3ce738486d81fc34a15f1087097ce7fa9ca [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
Ash Wilsone4474492014-10-22 15:17:25 -040013// required RS_AUTH_URL, RS_USERNAME, or RS_PASSWORD environment variables,
Jamie Hannaford92721042014-10-22 15:21:29 +020014// respectively, remain undefined. See the AuthOptions() function for more details.
15var (
Ash Wilsone4474492014-10-22 15:17:25 -040016 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 Hannaford92721042014-10-22 15:21:29 +020019)
20
Ash Wilsone4474492014-10-22 15:17:25 -040021func 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 Hannaford92721042014-10-22 15:21:29 +020029// AuthOptionsFromEnv fills out an identity.AuthOptions structure with the
Ash Wilsone4474492014-10-22 15:17:25 -040030// settings found on the various Rackspace RS_* environment variables.
Jamie Hannaford92721042014-10-22 15:21:29 +020031func AuthOptionsFromEnv() (gophercloud.AuthOptions, error) {
Ash Wilsone4474492014-10-22 15:17:25 -040032 authURL := prefixedEnv("AUTH_URL")
33 username := prefixedEnv("USERNAME")
34 password := prefixedEnv("PASSWORD")
35 apiKey := prefixedEnv("API_KEY")
Jamie Hannaford92721042014-10-22 15:21:29 +020036
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}