Samuel A. Falvo II | 8280cb7 | 2014-01-06 15:06:53 -0800 | [diff] [blame] | 1 | package osutil |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | "os" |
| 7 | ) |
| 8 | |
| 9 | var ( |
| 10 | nilOptions = gophercloud.AuthOptions{} |
| 11 | |
| 12 | // ErrNoAuthUrl errors occur when the value of the OS_AUTH_URL environment variable cannot be determined. |
Samuel A. Falvo II | d289d75 | 2014-01-10 14:28:11 -0800 | [diff] [blame] | 13 | ErrNoAuthUrl = fmt.Errorf("Environment variable OS_AUTH_URL needs to be set.") |
Samuel A. Falvo II | 8280cb7 | 2014-01-06 15:06:53 -0800 | [diff] [blame] | 14 | |
| 15 | // ErrNoUsername errors occur when the value of the OS_USERNAME environment variable cannot be determined. |
| 16 | ErrNoUsername = fmt.Errorf("Environment variable OS_USERNAME needs to be set.") |
| 17 | |
| 18 | // ErrNoPassword errors occur when the value of the OS_PASSWORD environment variable cannot be determined. |
| 19 | ErrNoPassword = fmt.Errorf("Environment variable OS_PASSWORD or OS_API_KEY needs to be set.") |
| 20 | ) |
| 21 | |
| 22 | // AuthOptions fills out a gophercloud.AuthOptions structure with the settings found on the various OpenStack |
| 23 | // OS_* environment variables. The following variables provide sources of truth: OS_AUTH_URL, OS_USERNAME, |
| 24 | // OS_PASSWORD, OS_TENANT_ID, and OS_TENANT_NAME. Of these, OS_USERNAME, OS_PASSWORD, and OS_AUTH_URL must |
| 25 | // have settings, or an error will result. OS_TENANT_ID and OS_TENANT_NAME are optional. |
| 26 | // |
| 27 | // The value of OS_AUTH_URL will be returned directly to the caller, for subsequent use in |
| 28 | // gophercloud.Authenticate()'s Provider parameter. This function will not interpret the value of OS_AUTH_URL, |
| 29 | // so as a convenient extention, you may set OS_AUTH_URL to, e.g., "rackspace-uk", or any other Gophercloud-recognized |
| 30 | // provider shortcuts. For broad compatibility, especially with local installations, you should probably |
| 31 | // avoid the temptation to do this. |
| 32 | func AuthOptions() (string, gophercloud.AuthOptions, error) { |
| 33 | provider := os.Getenv("OS_AUTH_URL") |
| 34 | username := os.Getenv("OS_USERNAME") |
| 35 | password := os.Getenv("OS_PASSWORD") |
| 36 | tenantId := os.Getenv("OS_TENANT_ID") |
| 37 | tenantName := os.Getenv("OS_TENANT_NAME") |
| 38 | |
| 39 | if provider == "" { |
| 40 | return "", nilOptions, ErrNoAuthUrl |
| 41 | } |
| 42 | |
| 43 | if username == "" { |
| 44 | return "", nilOptions, ErrNoUsername |
| 45 | } |
| 46 | |
Samuel A. Falvo II | 3de36c9 | 2014-02-01 15:16:37 -0800 | [diff] [blame] | 47 | if password == "" { |
Samuel A. Falvo II | 8280cb7 | 2014-01-06 15:06:53 -0800 | [diff] [blame] | 48 | return "", nilOptions, ErrNoPassword |
| 49 | } |
| 50 | |
| 51 | ao := gophercloud.AuthOptions{ |
| 52 | Username: username, |
| 53 | Password: password, |
| 54 | TenantId: tenantId, |
| 55 | TenantName: tenantName, |
| 56 | } |
| 57 | |
| 58 | return provider, ao, nil |
| 59 | } |