blob: c0e82bd306d412e788114da0e1000ff53f9c66b3 [file] [log] [blame] [view]
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +02001---
2layout: page
3title: Getting Started with Identity v2
4---
5
6## Tokens
7
8A token is an arbitrary bit of text that is used to access resources. Each
9token has a scope that describes which resources are accessible with it. A
10token may be revoked at anytime and is valid for a finite duration.
11
12### Generate a token
13
14The nature of required and optional auth options will depend on your provider,
15but generally the `Username` and `IdentityEndpoint` fields are always
16required. Some providers will insist on a `Password` instead of an `APIKey`,
17others will prefer `TenantID` over `TenantName` - so it is always worth
18checking before writing your implementation in Go.
19
20{% highlight go %}
21import "github.com/rackspace/gophercloud/openstack/identity/v2/tokens"
22
23opts := tokens.AuthOptions{
24 IdentityEndpoint: "{identityURL}",
25 Username: "{username}",
26 APIKey: "{apiKey}",
27}
28
29token, err := tokens.Create(client, opts).Extract()
30{% endhighlight %}
31
32## Tenants
33
34A tenant is a container used to group or isolate API resources. Depending on
35the provider, a tenant can map to a customer, account, organization, or project.
36
37### List tenants
38
39{% highlight go %}
40import (
41 "github.com/rackspace/gophercloud/pagination"
42 "github.com/rackspace/gophercloud/openstack/identity/v2/tenants"
43)
44
45// We have the option of filtering the tenant list. If we want the full
46// collection, leave it as an empty struct
47opts := tenants.ListOpts{Limit: 10}
48
49// Retrieve a pager (i.e. a paginated collection)
50pager := tenants.List(client, opts)
51
52// Define an anonymous function to be executed on each page's iteration
53err := pager.EachPage(func(page pagination.Page) (bool, error) {
54 tenantList, err := tenants.ExtractTenants(page)
55
56 for _, t := range tenantList {
57 // "t" will be a tenants.Tenant
58 }
59})
60{% endhighlight %}