Ash Wilson | e646c74 | 2014-09-02 14:06:15 -0400 | [diff] [blame] | 1 | // Package utils 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" |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 6 | "os" |
Ash Wilson | e646c74 | 2014-09-02 14:06:15 -0400 | [diff] [blame] | 7 | |
| 8 | "github.com/rackspace/gophercloud" |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 9 | ) |
| 10 | |
Ash Wilson | e646c74 | 2014-09-02 14:06:15 -0400 | [diff] [blame] | 11 | var nilOptions = gophercloud.AuthOptions{} |
Samuel A. Falvo II | b8caba5 | 2014-02-09 10:56:26 -0800 | [diff] [blame] | 12 | |
| 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 II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 15 | var ( |
Ash Wilson | e646c74 | 2014-09-02 14:06:15 -0400 | [diff] [blame] | 16 | 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] | 17 | 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] | 18 | 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 Wilson | e646c74 | 2014-09-02 14:06:15 -0400 | [diff] [blame] | 25 | func AuthOptions() (gophercloud.AuthOptions, error) { |
| 26 | authURL := os.Getenv("OS_AUTH_URL") |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 27 | username := os.Getenv("OS_USERNAME") |
Ash Wilson | e646c74 | 2014-09-02 14:06:15 -0400 | [diff] [blame] | 28 | userID := os.Getenv("OS_USERID") |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 29 | password := os.Getenv("OS_PASSWORD") |
Ash Wilson | e646c74 | 2014-09-02 14:06:15 -0400 | [diff] [blame] | 30 | tenantID := os.Getenv("OS_TENANT_ID") |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 31 | tenantName := os.Getenv("OS_TENANT_NAME") |
Ash Wilson | e646c74 | 2014-09-02 14:06:15 -0400 | [diff] [blame] | 32 | domainID := os.Getenv("OS_DOMAIN_ID") |
| 33 | domainName := os.Getenv("OS_DOMAIN_NAME") |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 34 | |
Ash Wilson | e646c74 | 2014-09-02 14:06:15 -0400 | [diff] [blame] | 35 | if authURL == "" { |
| 36 | return nilOptions, ErrNoAuthURL |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Ash Wilson | e646c74 | 2014-09-02 14:06:15 -0400 | [diff] [blame] | 39 | if username == "" && userID == "" { |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 40 | return nilOptions, ErrNoUsername |
| 41 | } |
| 42 | |
| 43 | if password == "" { |
| 44 | return nilOptions, ErrNoPassword |
| 45 | } |
| 46 | |
Ash Wilson | e646c74 | 2014-09-02 14:06:15 -0400 | [diff] [blame] | 47 | 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 II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | return ao, nil |
| 59 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 60 | |
Ash Wilson | e646c74 | 2014-09-02 14:06:15 -0400 | [diff] [blame] | 61 | // BuildQuery constructs the query section of a URI from a map. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 62 | func BuildQuery(params map[string]string) string { |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 63 | if len(params) == 0 { |
| 64 | return "" |
| 65 | } |
| 66 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 67 | query := "?" |
| 68 | for k, v := range params { |
| 69 | query += k + "=" + v + "&" |
| 70 | } |
| 71 | query = query[:len(query)-1] |
| 72 | return query |
| 73 | } |