Jamie Hannaford | 6adb125 | 2014-10-22 15:25:44 +0200 | [diff] [blame] | 1 | package openstack |
| 2 | |
| 3 | import ( |
Jamie Hannaford | 6adb125 | 2014-10-22 15:25:44 +0200 | [diff] [blame] | 4 | "os" |
| 5 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 6 | "github.com/gophercloud/gophercloud" |
Jamie Hannaford | 6adb125 | 2014-10-22 15:25:44 +0200 | [diff] [blame] | 7 | ) |
| 8 | |
| 9 | var nilOptions = gophercloud.AuthOptions{} |
| 10 | |
Jon Perritt | eb01563 | 2016-02-21 19:56:53 -0600 | [diff] [blame] | 11 | // AuthOptionsFromEnv fills out an identity.AuthOptions structure with the settings found on the various OpenStack |
Jamie Hannaford | 6adb125 | 2014-10-22 15:25:44 +0200 | [diff] [blame] | 12 | // 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 Hannaford | ee049ec | 2014-10-22 17:02:55 +0200 | [diff] [blame] | 15 | func AuthOptionsFromEnv() (gophercloud.AuthOptions, error) { |
Jamie Hannaford | 6adb125 | 2014-10-22 15:25:44 +0200 | [diff] [blame] | 16 | 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 == "" { |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 26 | err := gophercloud.ErrMissingInput{Argument: "authURL"} |
Jon Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame] | 27 | return nilOptions, err |
Jamie Hannaford | 6adb125 | 2014-10-22 15:25:44 +0200 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | if username == "" && userID == "" { |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 31 | err := gophercloud.ErrMissingInput{Argument: "username"} |
Jon Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame] | 32 | return nilOptions, err |
Jamie Hannaford | 6adb125 | 2014-10-22 15:25:44 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | if password == "" { |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 36 | err := gophercloud.ErrMissingInput{Argument: "password"} |
Jon Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame] | 37 | return nilOptions, err |
Jamie Hannaford | 6adb125 | 2014-10-22 15:25:44 +0200 | [diff] [blame] | 38 | } |
| 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 | } |