| Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame] | 1 | --- |
| 2 | layout: page |
| 3 | title: Getting Started with Identity v2 |
| 4 | --- |
| 5 | |
| 6 | ## Tokens |
| 7 | |
| 8 | A token is an arbitrary bit of text that is used to access resources. Each |
| 9 | token has a scope that describes which resources are accessible with it. A |
| 10 | token may be revoked at anytime and is valid for a finite duration. |
| 11 | |
| 12 | ### Generate a token |
| 13 | |
| 14 | The nature of required and optional auth options will depend on your provider, |
| 15 | but generally the `Username` and `IdentityEndpoint` fields are always |
| 16 | required. Some providers will insist on a `Password` instead of an `APIKey`, |
| 17 | others will prefer `TenantID` over `TenantName` - so it is always worth |
| 18 | checking before writing your implementation in Go. |
| 19 | |
| 20 | {% highlight go %} |
| 21 | import "github.com/rackspace/gophercloud/openstack/identity/v2/tokens" |
| 22 | |
| 23 | opts := tokens.AuthOptions{ |
| 24 | IdentityEndpoint: "{identityURL}", |
| 25 | Username: "{username}", |
| 26 | APIKey: "{apiKey}", |
| 27 | } |
| 28 | |
| 29 | token, err := tokens.Create(client, opts).Extract() |
| 30 | {% endhighlight %} |
| 31 | |
| 32 | ## Tenants |
| 33 | |
| 34 | A tenant is a container used to group or isolate API resources. Depending on |
| 35 | the provider, a tenant can map to a customer, account, organization, or project. |
| 36 | |
| 37 | ### List tenants |
| 38 | |
| 39 | {% highlight go %} |
| 40 | import ( |
| 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 |
| 47 | opts := tenants.ListOpts{Limit: 10} |
| 48 | |
| 49 | // Retrieve a pager (i.e. a paginated collection) |
| 50 | pager := tenants.List(client, opts) |
| 51 | |
| 52 | // Define an anonymous function to be executed on each page's iteration |
| 53 | err := 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 %} |