blob: a814c7e7b7799ec64c7e5433e7eac13c4e6e4c34 [file] [log] [blame]
Samuel A. Falvo IIb8caba52014-02-09 10:56:26 -08001// This package contains utilities which eases working with Gophercloud's OpenStack APIs.
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -08002package utils
3
4import (
5 "fmt"
6 "github.com/rackspace/gophercloud/openstack/identity"
7 "os"
8)
9
Samuel A. Falvo IIb8caba52014-02-09 10:56:26 -080010var 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 II8a549ef2014-01-24 15:20:54 -080014var (
Samuel A. Falvo IIb8caba52014-02-09 10:56:26 -080015 ErrNoAuthUrl = fmt.Errorf("Environment variable OS_AUTH_URL needs to be set.")
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080016 ErrNoUsername = fmt.Errorf("Environment variable OS_USERNAME needs to be set.")
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080017 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 II8a549ef2014-01-24 15:20:54 -080024func 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 Perritt816d2a02014-03-11 20:49:46 -050053
54func 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}