Provide API for building AuthOptions from env vars.
diff --git a/acceptance/18-osutil-authentication.go b/acceptance/18-osutil-authentication.go
new file mode 100644
index 0000000..936f376
--- /dev/null
+++ b/acceptance/18-osutil-authentication.go
@@ -0,0 +1,17 @@
+package main
+
+import (
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/osutil"
+)
+
+func main() {
+ provider, authOptions, err := osutil.AuthOptions()
+ if err != nil {
+ panic(err)
+ }
+ _, err = gophercloud.Authenticate(provider, authOptions)
+ if err != nil {
+ panic(err)
+ }
+}
diff --git a/osutil/auth.go b/osutil/auth.go
new file mode 100644
index 0000000..4a05db3
--- /dev/null
+++ b/osutil/auth.go
@@ -0,0 +1,59 @@
+package osutil
+
+import (
+ "fmt"
+ "github.com/rackspace/gophercloud"
+ "os"
+)
+
+var (
+ nilOptions = gophercloud.AuthOptions{}
+
+ // ErrNoAuthUrl errors occur when the value of the OS_AUTH_URL environment variable cannot be determined.
+ ErrNoAuthUrl = fmt.Errorf("Environment variable OS_AUTH_URL needs to be set.")
+
+ // ErrNoUsername errors occur when the value of the OS_USERNAME environment variable cannot be determined.
+ ErrNoUsername = fmt.Errorf("Environment variable OS_USERNAME needs to be set.")
+
+ // ErrNoPassword errors occur when the value of the OS_PASSWORD environment variable cannot be determined.
+ ErrNoPassword = fmt.Errorf("Environment variable OS_PASSWORD or OS_API_KEY needs to be set.")
+)
+
+// AuthOptions fills out a gophercloud.AuthOptions structure with the settings found on the various OpenStack
+// OS_* environment variables. The following variables provide sources of truth: OS_AUTH_URL, OS_USERNAME,
+// OS_PASSWORD, OS_TENANT_ID, and OS_TENANT_NAME. Of these, OS_USERNAME, OS_PASSWORD, and OS_AUTH_URL must
+// have settings, or an error will result. OS_TENANT_ID and OS_TENANT_NAME are optional.
+//
+// The value of OS_AUTH_URL will be returned directly to the caller, for subsequent use in
+// gophercloud.Authenticate()'s Provider parameter. This function will not interpret the value of OS_AUTH_URL,
+// so as a convenient extention, you may set OS_AUTH_URL to, e.g., "rackspace-uk", or any other Gophercloud-recognized
+// provider shortcuts. For broad compatibility, especially with local installations, you should probably
+// avoid the temptation to do this.
+func AuthOptions() (string, gophercloud.AuthOptions, error) {
+ provider := os.Getenv("OS_AUTH_URL")
+ username := os.Getenv("OS_USERNAME")
+ password := os.Getenv("OS_PASSWORD")
+ tenantId := os.Getenv("OS_TENANT_ID")
+ tenantName := os.Getenv("OS_TENANT_NAME")
+
+ if provider == "" {
+ return "", nilOptions, ErrNoAuthUrl
+ }
+
+ if username == "" {
+ return "", nilOptions, ErrNoUsername
+ }
+
+ if password == "" && apiKey == "" {
+ return "", nilOptions, ErrNoPassword
+ }
+
+ ao := gophercloud.AuthOptions{
+ Username: username,
+ Password: password,
+ TenantId: tenantId,
+ TenantName: tenantName,
+ }
+
+ return provider, ao, nil
+}
diff --git a/osutil/region.go b/osutil/region.go
new file mode 100644
index 0000000..31288df
--- /dev/null
+++ b/osutil/region.go
@@ -0,0 +1,10 @@
+package osutil
+
+import "os"
+
+// Region provides a means of querying the OS_REGION_NAME environment variable.
+// At present, you may also use os.Getenv("OS_REGION_NAME") as well.
+func Region() string {
+ return os.Getenv("OS_REGION_NAME")
+}
+