blob: f6d2eb194bf1d915c867cf74594cf5bba41de89a [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 == "" {
jrperritt29ae6b32016-04-13 12:59:37 -050026 err := gophercloud.ErrMissingInput{Argument: "authURL"}
Jon Perritt376dfce2016-02-28 23:39:09 -060027 return nilOptions, err
Jamie Hannaford6adb1252014-10-22 15:25:44 +020028 }
29
30 if username == "" && userID == "" {
jrperritt29ae6b32016-04-13 12:59:37 -050031 err := gophercloud.ErrMissingInput{Argument: "username"}
Jon Perritt376dfce2016-02-28 23:39:09 -060032 return nilOptions, err
Jamie Hannaford6adb1252014-10-22 15:25:44 +020033 }
34
35 if password == "" {
jrperritt29ae6b32016-04-13 12:59:37 -050036 err := gophercloud.ErrMissingInput{Argument: "password"}
Jon Perritt376dfce2016-02-28 23:39:09 -060037 return nilOptions, err
Jamie Hannaford6adb1252014-10-22 15:25:44 +020038 }
39
40 ao := gophercloud.AuthOptions{
41 IdentityEndpoint: authURL,
42 UserID: userID,
43 Username: username,
44 Password: password,
45 TenantID: tenantID,
46 TenantName: tenantName,
47 DomainID: domainID,
48 DomainName: domainName,
49 }
50
51 return ao, nil
52}