blob: 409846c63e5a2e04c5a8a4d05e484778bd7c6153 [file] [log] [blame]
Samuel A. Falvo II8280cb72014-01-06 15:06:53 -08001package osutil
2
3import (
4 "fmt"
5 "github.com/rackspace/gophercloud"
6 "os"
7)
8
9var (
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 IId289d752014-01-10 14:28:11 -080013 ErrNoAuthUrl = fmt.Errorf("Environment variable OS_AUTH_URL needs to be set.")
Samuel A. Falvo II8280cb72014-01-06 15:06:53 -080014
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.
32func 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 II3de36c92014-02-01 15:16:37 -080047 if password == "" {
Samuel A. Falvo II8280cb72014-01-06 15:06:53 -080048 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}