Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
jrperritt | c8834c1 | 2016-08-03 16:06:16 -0500 | [diff] [blame^] | 3 | import "github.com/gophercloud/gophercloud" |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 4 | |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 5 | // Endpoint represents a single API endpoint offered by a service. |
| 6 | // It matches either a public, internal or admin URL. |
| 7 | // If supported, it contains a region specifier, again if provided. |
| 8 | // The significance of the Region field will depend upon your provider. |
| 9 | type Endpoint struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 10 | ID string `json:"id"` |
| 11 | Region string `json:"region"` |
| 12 | Interface string `json:"interface"` |
| 13 | URL string `json:"url"` |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | // CatalogEntry provides a type-safe interface to an Identity API V3 service catalog listing. |
| 17 | // Each class of service, such as cloud DNS or block storage services, could have multiple |
| 18 | // CatalogEntry representing it (one by interface type, e.g public, admin or internal). |
| 19 | // |
| 20 | // Note: when looking for the desired service, try, whenever possible, to key off the type field. |
| 21 | // Otherwise, you'll tie the representation of the service to a specific provider. |
| 22 | type CatalogEntry struct { |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 23 | // Service ID |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 24 | ID string `json:"id"` |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 25 | // Name will contain the provider-specified name for the service. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 26 | Name string `json:"name"` |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 27 | // Type will contain a type string if OpenStack defines a type for the service. |
| 28 | // Otherwise, for provider-specific services, the provider may assign their own type strings. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 29 | Type string `json:"type"` |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 30 | // Endpoints will let the caller iterate over all the different endpoints that may exist for |
| 31 | // the service. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 32 | Endpoints []Endpoint `json:"endpoints"` |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // ServiceCatalog provides a view into the service catalog from a previous, successful authentication. |
| 36 | type ServiceCatalog struct { |
| 37 | Entries []CatalogEntry |
| 38 | } |
| 39 | |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 40 | // commonResult is the deferred result of a Create or a Get call. |
| 41 | type commonResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 42 | gophercloud.Result |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 43 | } |
Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 44 | |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 45 | // Extract is a shortcut for ExtractToken. |
| 46 | // This function is deprecated and still present for backward compatibility. |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 47 | func (r commonResult) Extract() (*Token, error) { |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 48 | return r.ExtractToken() |
| 49 | } |
| 50 | |
| 51 | // ExtractToken interprets a commonResult as a Token. |
| 52 | func (r commonResult) ExtractToken() (*Token, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 53 | var s struct { |
jrperritt | c8834c1 | 2016-08-03 16:06:16 -0500 | [diff] [blame^] | 54 | Token *Token `json:"token"` |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 55 | } |
| 56 | |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 57 | err := r.ExtractInto(&s) |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 58 | if err != nil { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 59 | return nil, err |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 60 | } |
| 61 | |
jrperritt | c8834c1 | 2016-08-03 16:06:16 -0500 | [diff] [blame^] | 62 | // Parse the token itself from the stored headers. |
| 63 | s.Token.ID = r.Header.Get("X-Subject-Token") |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 64 | |
jrperritt | c8834c1 | 2016-08-03 16:06:16 -0500 | [diff] [blame^] | 65 | return s.Token, err |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 68 | // ExtractServiceCatalog returns the ServiceCatalog that was generated along with the user's Token. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 69 | func (r CreateResult) ExtractServiceCatalog() (*ServiceCatalog, error) { |
| 70 | var s struct { |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 71 | Token struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 72 | Entries []CatalogEntry `json:"catalog"` |
| 73 | } `json:"token"` |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 74 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 75 | err := r.ExtractInto(&s) |
| 76 | return &ServiceCatalog{Entries: s.Token.Entries}, err |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // CreateResult defers the interpretation of a created token. |
| 80 | // Use ExtractToken() to interpret it as a Token, or ExtractServiceCatalog() to interpret it as a service catalog. |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 81 | type CreateResult struct { |
| 82 | commonResult |
| 83 | } |
| 84 | |
| 85 | // createErr quickly creates a CreateResult that reports an error. |
| 86 | func createErr(err error) CreateResult { |
| 87 | return CreateResult{ |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 88 | commonResult: commonResult{Result: gophercloud.Result{Err: err}}, |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | // GetResult is the deferred response from a Get call. |
| 93 | type GetResult struct { |
| 94 | commonResult |
| 95 | } |
| 96 | |
Jamie Hannaford | f38dd2e | 2014-10-27 11:36:54 +0100 | [diff] [blame] | 97 | // RevokeResult is the deferred response from a Revoke call. |
| 98 | type RevokeResult struct { |
| 99 | commonResult |
| 100 | } |
| 101 | |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 102 | // Token is a string that grants a user access to a controlled set of services in an OpenStack provider. |
| 103 | // Each Token is valid for a set length of time. |
| 104 | type Token struct { |
| 105 | // ID is the issued token. |
jrperritt | c8834c1 | 2016-08-03 16:06:16 -0500 | [diff] [blame^] | 106 | ID string `json:"id"` |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 107 | // ExpiresAt is the timestamp at which this token will no longer be accepted. |
jrperritt | c8834c1 | 2016-08-03 16:06:16 -0500 | [diff] [blame^] | 108 | ExpiresAt gophercloud.JSONRFC3339Milli `json:"expires_at"` |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 109 | } |