blob: 3f37a275d611fff6b61cdcb3bf41ba343d463c55 [file] [log] [blame]
Ash Wilson8018c392014-10-03 14:35:56 -04001// +build acceptance
2
3package v2
4
5import (
6 "testing"
7
8 "github.com/rackspace/gophercloud/openstack"
9 tokens2 "github.com/rackspace/gophercloud/openstack/identity/v2/tokens"
10 "github.com/rackspace/gophercloud/openstack/utils"
11 th "github.com/rackspace/gophercloud/testhelper"
12)
13
14func TestAuthenticate(t *testing.T) {
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 // Create an unauthenticated client.
26 provider, err := openstack.NewClient(ao.IdentityEndpoint)
27 th.AssertNoErr(t, err)
28
29 // Create a service client.
30 service := openstack.NewIdentityV2(provider)
31
32 // Authenticated!
33 result := tokens2.Create(service, ao)
34
35 // Extract and print the token.
36 token, err := result.ExtractToken()
37 th.AssertNoErr(t, err)
38
39 t.Logf("Acquired token: [%s]", token.ID)
40 t.Logf("The token will expire at: [%s]", token.ExpiresAt.String())
41 t.Logf("The token is valid for tenant: [%#v]", token.Tenant)
42
43 // Extract and print the service catalog.
44 catalog, err := result.ExtractServiceCatalog()
45 th.AssertNoErr(t, err)
46
47 t.Logf("Acquired service catalog listing [%d] services", len(catalog.Entries))
48 for i, entry := range catalog.Entries {
49 t.Logf("[%d]: name=[%s], type=[%s]", i, entry.Name, entry.Type)
50 for _, endpoint := range entry.Endpoints {
51 t.Logf(" - region=[%s] publicURL=[%s]", endpoint.Region, endpoint.PublicURL)
52 }
53 }
54}