blob: 96bf1fdadeac8cf309a4059f42c5e2a99f927140 [file] [log] [blame]
Jamie Hannafordc21ffb92014-10-30 11:46:31 +01001// +build acceptance identity
Ash Wilsonf4aee1e2014-10-03 15:25:13 -04002
3package v2
4
5import (
6 "testing"
7
8 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/openstack"
Ash Wilsonf4aee1e2014-10-03 15:25:13 -040010 th "github.com/rackspace/gophercloud/testhelper"
11)
12
13func v2AuthOptions(t *testing.T) gophercloud.AuthOptions {
14 // Obtain credentials from the environment.
Jamie Hannaford390555a2014-10-22 17:04:03 +020015 ao, err := openstack.AuthOptionsFromEnv()
Ash Wilsonf4aee1e2014-10-03 15:25:13 -040016 th.AssertNoErr(t, err)
17
18 // Trim out unused fields. Prefer authentication by API key to password.
19 ao.UserID, ao.DomainID, ao.DomainName = "", "", ""
20 if ao.APIKey != "" {
21 ao.Password = ""
22 }
23
24 return ao
25}
26
27func createClient(t *testing.T, auth bool) *gophercloud.ServiceClient {
28 ao := v2AuthOptions(t)
29
30 provider, err := openstack.NewClient(ao.IdentityEndpoint)
31 th.AssertNoErr(t, err)
32
33 if auth {
34 err = openstack.AuthenticateV2(provider, ao)
35 th.AssertNoErr(t, err)
36 }
37
38 return openstack.NewIdentityV2(provider)
39}
40
41func unauthenticatedClient(t *testing.T) *gophercloud.ServiceClient {
42 return createClient(t, false)
43}
44
45func authenticatedClient(t *testing.T) *gophercloud.ServiceClient {
46 return createClient(t, true)
47}