Samuel A. Falvo II | b8caba5 | 2014-02-09 10:56:26 -0800 | [diff] [blame] | 1 | // This package contains utilities which eases working with Gophercloud's OpenStack APIs. |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 2 | package utils |
| 3 | |
| 4 | import ( |
| 5 | "fmt" |
| 6 | "github.com/rackspace/gophercloud/openstack/identity" |
| 7 | "os" |
| 8 | ) |
| 9 | |
Samuel A. Falvo II | b8caba5 | 2014-02-09 10:56:26 -0800 | [diff] [blame] | 10 | var nilOptions = identity.AuthOptions{} |
| 11 | |
| 12 | // ErrNoAuthUrl, ErrNoUsername, and ErrNoPassword errors indicate of the required OS_AUTH_URL, OS_USERNAME, or OS_PASSWORD |
| 13 | // environment variables, respectively, remain undefined. See the AuthOptions() function for more details. |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 14 | var ( |
Samuel A. Falvo II | b8caba5 | 2014-02-09 10:56:26 -0800 | [diff] [blame] | 15 | ErrNoAuthUrl = fmt.Errorf("Environment variable OS_AUTH_URL needs to be set.") |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 16 | ErrNoUsername = fmt.Errorf("Environment variable OS_USERNAME needs to be set.") |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 17 | ErrNoPassword = fmt.Errorf("Environment variable OS_PASSWORD needs to be set.") |
| 18 | ) |
| 19 | |
| 20 | // AuthOptions fills out an identity.AuthOptions structure with the settings found on the various OpenStack |
| 21 | // OS_* environment variables. The following variables provide sources of truth: OS_AUTH_URL, OS_USERNAME, |
| 22 | // OS_PASSWORD, OS_TENANT_ID, and OS_TENANT_NAME. Of these, OS_USERNAME, OS_PASSWORD, and OS_AUTH_URL must |
| 23 | // have settings, or an error will result. OS_TENANT_ID and OS_TENANT_NAME are optional. |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 24 | func AuthOptions() (identity.AuthOptions, error) { |
| 25 | authUrl := os.Getenv("OS_AUTH_URL") |
| 26 | username := os.Getenv("OS_USERNAME") |
| 27 | password := os.Getenv("OS_PASSWORD") |
| 28 | tenantId := os.Getenv("OS_TENANT_ID") |
| 29 | tenantName := os.Getenv("OS_TENANT_NAME") |
| 30 | |
| 31 | if authUrl == "" { |
| 32 | return nilOptions, ErrNoAuthUrl |
| 33 | } |
| 34 | |
| 35 | if username == "" { |
| 36 | return nilOptions, ErrNoUsername |
| 37 | } |
| 38 | |
| 39 | if password == "" { |
| 40 | return nilOptions, ErrNoPassword |
| 41 | } |
| 42 | |
| 43 | ao := identity.AuthOptions{ |
| 44 | Endpoint: authUrl, |
| 45 | Username: username, |
| 46 | Password: password, |
| 47 | TenantId: tenantId, |
| 48 | TenantName: tenantName, |
| 49 | } |
| 50 | |
| 51 | return ao, nil |
| 52 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame^] | 53 | |
| 54 | func BuildQuery(params map[string]string) string { |
| 55 | query := "?" |
| 56 | for k, v := range params { |
| 57 | query += k + "=" + v + "&" |
| 58 | } |
| 59 | query = query[:len(query)-1] |
| 60 | return query |
| 61 | } |