blob: 2309783a328aac6a8c22f3976e50b80a17b51091 [file] [log] [blame]
Jamie Hannaford6adb1252014-10-22 15:25:44 +02001package openstack
2
3import (
Jamie Hannaford6adb1252014-10-22 15:25:44 +02004 "os"
5
Jon Perritt27249f42016-02-18 10:35:59 -06006 "github.com/gophercloud/gophercloud"
Jamie Hannaford6adb1252014-10-22 15:25:44 +02007)
8
9var nilOptions = gophercloud.AuthOptions{}
10
Jon Perritteb015632016-02-21 19:56:53 -060011// AuthOptionsFromEnv fills out an identity.AuthOptions structure with the settings found on the various OpenStack
Jamie Hannaford6adb1252014-10-22 15:25:44 +020012// OS_* environment variables. The following variables provide sources of truth: OS_AUTH_URL, OS_USERNAME,
13// OS_PASSWORD, OS_TENANT_ID, and OS_TENANT_NAME. Of these, OS_USERNAME, OS_PASSWORD, and OS_AUTH_URL must
14// have settings, or an error will result. OS_TENANT_ID and OS_TENANT_NAME are optional.
Jamie Hannafordee049ec2014-10-22 17:02:55 +020015func AuthOptionsFromEnv() (gophercloud.AuthOptions, error) {
Jamie Hannaford6adb1252014-10-22 15:25:44 +020016 authURL := os.Getenv("OS_AUTH_URL")
17 username := os.Getenv("OS_USERNAME")
18 userID := os.Getenv("OS_USERID")
19 password := os.Getenv("OS_PASSWORD")
20 tenantID := os.Getenv("OS_TENANT_ID")
21 tenantName := os.Getenv("OS_TENANT_NAME")
22 domainID := os.Getenv("OS_DOMAIN_ID")
23 domainName := os.Getenv("OS_DOMAIN_NAME")
24
25 if authURL == "" {
Jon Perritt376dfce2016-02-28 23:39:09 -060026 err := &ErrNoAuthURL{}
27 err.Function = "openstack.AuthOptionsFromEnv"
28 err.Argument = "authURL"
29 return nilOptions, err
Jamie Hannaford6adb1252014-10-22 15:25:44 +020030 }
31
32 if username == "" && userID == "" {
Jon Perritt376dfce2016-02-28 23:39:09 -060033 err := &ErrNoUsername{}
34 err.Function = "openstack.AuthOptionsFromEnv"
35 err.Argument = "username"
36 return nilOptions, err
Jamie Hannaford6adb1252014-10-22 15:25:44 +020037 }
38
39 if password == "" {
Jon Perritt376dfce2016-02-28 23:39:09 -060040 err := &ErrNoPassword{}
41 err.Function = "openstack.AuthOptionsFromEnv"
42 err.Argument = "password"
43 return nilOptions, err
Jamie Hannaford6adb1252014-10-22 15:25:44 +020044 }
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}