blob: f97f3d1f7d6cb4f15113c96fbe7a46d0599cc0df [file] [log] [blame]
Ash Wilsone646c742014-09-02 14:06:15 -04001// Package utils 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"
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -08006 "os"
Ash Wilsone646c742014-09-02 14:06:15 -04007
8 "github.com/rackspace/gophercloud"
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -08009)
10
Ash Wilsone646c742014-09-02 14:06:15 -040011var nilOptions = gophercloud.AuthOptions{}
Samuel A. Falvo IIb8caba52014-02-09 10:56:26 -080012
13// ErrNoAuthUrl, ErrNoUsername, and ErrNoPassword errors indicate of the required OS_AUTH_URL, OS_USERNAME, or OS_PASSWORD
14// environment variables, respectively, remain undefined. See the AuthOptions() function for more details.
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080015var (
Ash Wilsone646c742014-09-02 14:06:15 -040016 ErrNoAuthURL = fmt.Errorf("Environment variable OS_AUTH_URL needs to be set.")
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080017 ErrNoUsername = fmt.Errorf("Environment variable OS_USERNAME needs to be set.")
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080018 ErrNoPassword = fmt.Errorf("Environment variable OS_PASSWORD needs to be set.")
19)
20
21// AuthOptions fills out an identity.AuthOptions structure with the settings found on the various OpenStack
22// OS_* environment variables. The following variables provide sources of truth: OS_AUTH_URL, OS_USERNAME,
23// OS_PASSWORD, OS_TENANT_ID, and OS_TENANT_NAME. Of these, OS_USERNAME, OS_PASSWORD, and OS_AUTH_URL must
24// have settings, or an error will result. OS_TENANT_ID and OS_TENANT_NAME are optional.
Ash Wilsone646c742014-09-02 14:06:15 -040025func AuthOptions() (gophercloud.AuthOptions, error) {
26 authURL := os.Getenv("OS_AUTH_URL")
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080027 username := os.Getenv("OS_USERNAME")
Ash Wilsone646c742014-09-02 14:06:15 -040028 userID := os.Getenv("OS_USERID")
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080029 password := os.Getenv("OS_PASSWORD")
Ash Wilsone646c742014-09-02 14:06:15 -040030 tenantID := os.Getenv("OS_TENANT_ID")
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080031 tenantName := os.Getenv("OS_TENANT_NAME")
Ash Wilsone646c742014-09-02 14:06:15 -040032 domainID := os.Getenv("OS_DOMAIN_ID")
33 domainName := os.Getenv("OS_DOMAIN_NAME")
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080034
Ash Wilsone646c742014-09-02 14:06:15 -040035 if authURL == "" {
36 return nilOptions, ErrNoAuthURL
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080037 }
38
Ash Wilsone646c742014-09-02 14:06:15 -040039 if username == "" && userID == "" {
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080040 return nilOptions, ErrNoUsername
41 }
42
43 if password == "" {
44 return nilOptions, ErrNoPassword
45 }
46
Ash Wilsone646c742014-09-02 14:06:15 -040047 ao := gophercloud.AuthOptions{
48 IdentityEndpoint: authURL,
49 UserID: userID,
50 Username: username,
51 Password: password,
52 TenantID: tenantID,
53 TenantName: tenantName,
54 DomainID: domainID,
55 DomainName: domainName,
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080056 }
57
58 return ao, nil
59}
Jon Perritt816d2a02014-03-11 20:49:46 -050060
Ash Wilsone646c742014-09-02 14:06:15 -040061// BuildQuery constructs the query section of a URI from a map.
Jon Perritt816d2a02014-03-11 20:49:46 -050062func BuildQuery(params map[string]string) string {
63 query := "?"
64 for k, v := range params {
65 query += k + "=" + v + "&"
66 }
67 query = query[:len(query)-1]
68 return query
69}