blob: fe7c3a0bc1a12d55806cfb921c4a700911af7726 [file] [log] [blame]
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -08001package utils
2
3import (
4 "fmt"
5 "github.com/rackspace/gophercloud/openstack/identity"
6 "os"
7)
8
9var (
10 nilOptions = identity.AuthOptions{}
11
12 // ErrNoAuthUrl errors occur when the value of the OS_AUTH_URL environment variable cannot be determined.
13 ErrNoAuthUrl = fmt.Errorf("Environment variable OS_AUTH_URL needs to be set.")
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 needs to be set.")
20)
21
22// AuthOptions fills out an identity.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() (identity.AuthOptions, error) {
33 authUrl := 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 authUrl == "" {
40 return nilOptions, ErrNoAuthUrl
41 }
42
43 if username == "" {
44 return nilOptions, ErrNoUsername
45 }
46
47 if password == "" {
48 return nilOptions, ErrNoPassword
49 }
50
51 ao := identity.AuthOptions{
52 Endpoint: authUrl,
53 Username: username,
54 Password: password,
55 TenantId: tenantId,
56 TenantName: tenantName,
57 }
58
59 return ao, nil
60}