blob: a411b6330b287a21c258dab925a43ad9be2d774d [file] [log] [blame]
Samuel A. Falvo II8280cb72014-01-06 15:06:53 -08001package osutil
2
3import (
4 "fmt"
5 "github.com/rackspace/gophercloud"
6 "os"
Marc Abramowitz68e7ed52014-08-12 13:00:47 -07007 "strings"
Samuel A. Falvo II8280cb72014-01-06 15:06:53 -08008)
9
10var (
11 nilOptions = gophercloud.AuthOptions{}
12
13 // ErrNoAuthUrl errors occur when the value of the OS_AUTH_URL environment variable cannot be determined.
Samuel A. Falvo IId289d752014-01-10 14:28:11 -080014 ErrNoAuthUrl = fmt.Errorf("Environment variable OS_AUTH_URL needs to be set.")
Samuel A. Falvo II8280cb72014-01-06 15:06:53 -080015
16 // ErrNoUsername errors occur when the value of the OS_USERNAME environment variable cannot be determined.
17 ErrNoUsername = fmt.Errorf("Environment variable OS_USERNAME needs to be set.")
18
19 // ErrNoPassword errors occur when the value of the OS_PASSWORD environment variable cannot be determined.
20 ErrNoPassword = fmt.Errorf("Environment variable OS_PASSWORD or OS_API_KEY needs to be set.")
21)
22
23// AuthOptions fills out a gophercloud.AuthOptions structure with the settings found on the various OpenStack
24// OS_* environment variables. The following variables provide sources of truth: OS_AUTH_URL, OS_USERNAME,
25// OS_PASSWORD, OS_TENANT_ID, and OS_TENANT_NAME. Of these, OS_USERNAME, OS_PASSWORD, and OS_AUTH_URL must
26// have settings, or an error will result. OS_TENANT_ID and OS_TENANT_NAME are optional.
27//
28// The value of OS_AUTH_URL will be returned directly to the caller, for subsequent use in
29// gophercloud.Authenticate()'s Provider parameter. This function will not interpret the value of OS_AUTH_URL,
30// so as a convenient extention, you may set OS_AUTH_URL to, e.g., "rackspace-uk", or any other Gophercloud-recognized
31// provider shortcuts. For broad compatibility, especially with local installations, you should probably
32// avoid the temptation to do this.
33func AuthOptions() (string, gophercloud.AuthOptions, error) {
34 provider := os.Getenv("OS_AUTH_URL")
35 username := os.Getenv("OS_USERNAME")
36 password := os.Getenv("OS_PASSWORD")
37 tenantId := os.Getenv("OS_TENANT_ID")
38 tenantName := os.Getenv("OS_TENANT_NAME")
39
40 if provider == "" {
41 return "", nilOptions, ErrNoAuthUrl
42 }
43
44 if username == "" {
45 return "", nilOptions, ErrNoUsername
46 }
47
Samuel A. Falvo II3de36c92014-02-01 15:16:37 -080048 if password == "" {
Samuel A. Falvo II8280cb72014-01-06 15:06:53 -080049 return "", nilOptions, ErrNoPassword
50 }
51
52 ao := gophercloud.AuthOptions{
53 Username: username,
54 Password: password,
55 TenantId: tenantId,
56 TenantName: tenantName,
57 }
58
Marc Abramowitz68e7ed52014-08-12 13:00:47 -070059 if !strings.HasSuffix(provider, "/tokens") {
60 provider += "/tokens"
61 }
62
Samuel A. Falvo II8280cb72014-01-06 15:06:53 -080063 return provider, ao, nil
64}