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