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