| Ash Wilson | 8018c39 | 2014-10-03 14:35:56 -0400 | [diff] [blame] | 1 | // +build acceptance | 
|  | 2 |  | 
|  | 3 | package v2 | 
|  | 4 |  | 
|  | 5 | import ( | 
|  | 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 |  | 
|  | 14 | func 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 | } |