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