blob: a4402b6f06d95cc608b2c2acc963c4f2efe83ce8 [file] [log] [blame]
Jamie Hannaford6adb1252014-10-22 15:25:44 +02001package openstack
2
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 required OS_AUTH_URL, OS_USERNAME, or OS_PASSWORD
13// environment variables, respectively, remain undefined. See the AuthOptions() function for more details.
14var (
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 Hannafordee049ec2014-10-22 17:02:55 +020024func AuthOptionsFromEnv() (gophercloud.AuthOptions, error) {
Jamie Hannaford6adb1252014-10-22 15:25:44 +020025 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}