Jamie Hannaford | c21ffb9 | 2014-10-30 11:46:31 +0100 | [diff] [blame] | 1 | // +build acceptance identity |
Ash Wilson | f4aee1e | 2014-10-03 15:25:13 -0400 | [diff] [blame] | 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/openstack" |
Ash Wilson | f4aee1e | 2014-10-03 15:25:13 -0400 | [diff] [blame] | 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | ) |
| 12 | |
| 13 | func v2AuthOptions(t *testing.T) gophercloud.AuthOptions { |
| 14 | // Obtain credentials from the environment. |
Jamie Hannaford | 390555a | 2014-10-22 17:04:03 +0200 | [diff] [blame] | 15 | ao, err := openstack.AuthOptionsFromEnv() |
Ash Wilson | f4aee1e | 2014-10-03 15:25:13 -0400 | [diff] [blame] | 16 | 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 | |
| 27 | func 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 | |
| 41 | func unauthenticatedClient(t *testing.T) *gophercloud.ServiceClient { |
| 42 | return createClient(t, false) |
| 43 | } |
| 44 | |
| 45 | func authenticatedClient(t *testing.T) *gophercloud.ServiceClient { |
| 46 | return createClient(t, true) |
| 47 | } |