Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 3 | import ( |
| 4 | "time" |
| 5 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 6 | "github.com/gophercloud/gophercloud" |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 7 | ) |
| 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 { |
| 27 | |
| 28 | // Service ID |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 29 | ID string `json:"id"` |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 30 | |
| 31 | // Name will contain the provider-specified name for the service. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 32 | Name string `json:"name"` |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 33 | |
| 34 | // Type will contain a type string if OpenStack defines a type for the service. |
| 35 | // 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^] | 36 | Type string `json:"type"` |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 37 | |
| 38 | // Endpoints will let the caller iterate over all the different endpoints that may exist for |
| 39 | // the service. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 40 | Endpoints []Endpoint `json:"endpoints"` |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // ServiceCatalog provides a view into the service catalog from a previous, successful authentication. |
| 44 | type ServiceCatalog struct { |
| 45 | Entries []CatalogEntry |
| 46 | } |
| 47 | |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 48 | // commonResult is the deferred result of a Create or a Get call. |
| 49 | type commonResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 50 | gophercloud.Result |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 51 | } |
Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 52 | |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 53 | // Extract is a shortcut for ExtractToken. |
| 54 | // This function is deprecated and still present for backward compatibility. |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 55 | func (r commonResult) Extract() (*Token, error) { |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 56 | return r.ExtractToken() |
| 57 | } |
| 58 | |
| 59 | // ExtractToken interprets a commonResult as a Token. |
| 60 | func (r commonResult) ExtractToken() (*Token, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 61 | var s struct { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 62 | Token struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 63 | ExpiresAt string `json:"expires_at"` |
| 64 | } `json:"token"` |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 67 | var token Token |
| 68 | |
| 69 | // Parse the token itself from the stored headers. |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 70 | token.ID = r.Header.Get("X-Subject-Token") |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 71 | |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 72 | err := r.ExtractInto(&s) |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 73 | if err != nil { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 74 | return nil, err |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // Attempt to parse the timestamp. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 78 | token.ExpiresAt, err = time.Parse(gophercloud.RFC3339Milli, s.Token.ExpiresAt) |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 79 | |
Jamie Hannaford | a253adf | 2014-10-08 17:14:24 +0200 | [diff] [blame] | 80 | return &token, err |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 83 | // 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^] | 84 | func (r CreateResult) ExtractServiceCatalog() (*ServiceCatalog, error) { |
| 85 | var s struct { |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 86 | Token struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 87 | Entries []CatalogEntry `json:"catalog"` |
| 88 | } `json:"token"` |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 89 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 90 | err := r.ExtractInto(&s) |
| 91 | return &ServiceCatalog{Entries: s.Token.Entries}, err |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // CreateResult defers the interpretation of a created token. |
| 95 | // 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] | 96 | type CreateResult struct { |
| 97 | commonResult |
| 98 | } |
| 99 | |
| 100 | // createErr quickly creates a CreateResult that reports an error. |
| 101 | func createErr(err error) CreateResult { |
| 102 | return CreateResult{ |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 103 | commonResult: commonResult{Result: gophercloud.Result{Err: err}}, |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | // GetResult is the deferred response from a Get call. |
| 108 | type GetResult struct { |
| 109 | commonResult |
| 110 | } |
| 111 | |
Jamie Hannaford | f38dd2e | 2014-10-27 11:36:54 +0100 | [diff] [blame] | 112 | // RevokeResult is the deferred response from a Revoke call. |
| 113 | type RevokeResult struct { |
| 114 | commonResult |
| 115 | } |
| 116 | |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 117 | // Token is a string that grants a user access to a controlled set of services in an OpenStack provider. |
| 118 | // Each Token is valid for a set length of time. |
| 119 | type Token struct { |
| 120 | // ID is the issued token. |
| 121 | ID string |
| 122 | |
| 123 | // ExpiresAt is the timestamp at which this token will no longer be accepted. |
| 124 | ExpiresAt time.Time |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 125 | } |