blob: d134f7d4d074b5031856c33bf88d4b0b439ad509 [file] [log] [blame]
Ash Wilsona1920082014-08-28 14:24:17 -04001package tokens
2
Ash Wilson46d913f2014-08-29 11:00:11 -04003import (
4 "time"
5
6 "github.com/mitchellh/mapstructure"
Ash Wilsona6b08312014-10-02 15:27:45 -04007 "github.com/rackspace/gophercloud"
Ash Wilson46d913f2014-08-29 11:00:11 -04008)
9
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020010// Endpoint represents a single API endpoint offered by a service.
11// It matches either a public, internal or admin URL.
12// If supported, it contains a region specifier, again if provided.
13// The significance of the Region field will depend upon your provider.
14type Endpoint struct {
15 ID string `mapstructure:"id"`
16 Region string `mapstructure:"region"`
17 Interface string `mapstructure:"interface"`
18 URL string `mapstructure:"url"`
19}
20
21// CatalogEntry provides a type-safe interface to an Identity API V3 service catalog listing.
22// Each class of service, such as cloud DNS or block storage services, could have multiple
23// CatalogEntry representing it (one by interface type, e.g public, admin or internal).
24//
25// Note: when looking for the desired service, try, whenever possible, to key off the type field.
26// Otherwise, you'll tie the representation of the service to a specific provider.
27type CatalogEntry struct {
28
29 // Service ID
30 ID string `mapstructure:"id"`
31
32 // Name will contain the provider-specified name for the service.
33 Name string `mapstructure:"name"`
34
35 // Type will contain a type string if OpenStack defines a type for the service.
36 // Otherwise, for provider-specific services, the provider may assign their own type strings.
37 Type string `mapstructure:"type"`
38
39 // Endpoints will let the caller iterate over all the different endpoints that may exist for
40 // the service.
41 Endpoints []Endpoint `mapstructure:"endpoints"`
42}
43
44// ServiceCatalog provides a view into the service catalog from a previous, successful authentication.
45type ServiceCatalog struct {
46 Entries []CatalogEntry
47}
48
Ash Wilsonf8d546a2014-09-30 17:43:25 -040049// commonResult is the deferred result of a Create or a Get call.
50type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040051 gophercloud.Result
Ash Wilson4a52e2a2014-08-29 09:28:00 -040052}
Ash Wilsona1920082014-08-28 14:24:17 -040053
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020054// Extract is a shortcut for ExtractToken.
55// This function is deprecated and still present for backward compatibility.
Ash Wilsonf8d546a2014-09-30 17:43:25 -040056func (r commonResult) Extract() (*Token, error) {
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020057 return r.ExtractToken()
58}
59
60// ExtractToken interprets a commonResult as a Token.
61func (r commonResult) ExtractToken() (*Token, error) {
Ash Wilsonf8d546a2014-09-30 17:43:25 -040062 if r.Err != nil {
63 return nil, r.Err
Ash Wilsone058e342014-08-29 10:31:41 -040064 }
65
Ash Wilsonf8d546a2014-09-30 17:43:25 -040066 var response struct {
67 Token struct {
68 ExpiresAt string `mapstructure:"expires_at"`
69 } `mapstructure:"token"`
Ash Wilsone058e342014-08-29 10:31:41 -040070 }
71
Ash Wilsonf8d546a2014-09-30 17:43:25 -040072 var token Token
73
74 // Parse the token itself from the stored headers.
Ash Wilson72e4d2c2014-10-20 10:27:30 -040075 token.ID = r.Header.Get("X-Subject-Token")
Ash Wilsonf8d546a2014-09-30 17:43:25 -040076
Ash Wilsond3dc2542014-10-20 10:10:48 -040077 err := mapstructure.Decode(r.Body, &response)
Ash Wilsone058e342014-08-29 10:31:41 -040078 if err != nil {
Ash Wilsonf8d546a2014-09-30 17:43:25 -040079 return nil, err
Ash Wilsone058e342014-08-29 10:31:41 -040080 }
81
82 // Attempt to parse the timestamp.
Ash Wilsonf25ae372014-10-06 14:40:29 -040083 token.ExpiresAt, err = time.Parse(gophercloud.RFC3339Milli, response.Token.ExpiresAt)
Ash Wilsone058e342014-08-29 10:31:41 -040084
Jamie Hannaforda253adf2014-10-08 17:14:24 +020085 return &token, err
Ash Wilsonf8d546a2014-09-30 17:43:25 -040086}
87
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020088// ExtractServiceCatalog returns the ServiceCatalog that was generated along with the user's Token.
89func (result CreateResult) ExtractServiceCatalog() (*ServiceCatalog, error) {
90 if result.Err != nil {
91 return nil, result.Err
92 }
93
94 var response struct {
95 Token struct {
96 Entries []CatalogEntry `mapstructure:"catalog"`
97 } `mapstructure:"token"`
98 }
99
100 err := mapstructure.Decode(result.Body, &response)
101 if err != nil {
102 return nil, err
103 }
104
105 return &ServiceCatalog{Entries: response.Token.Entries}, nil
106}
107
108// CreateResult defers the interpretation of a created token.
109// Use ExtractToken() to interpret it as a Token, or ExtractServiceCatalog() to interpret it as a service catalog.
Ash Wilsonf8d546a2014-09-30 17:43:25 -0400110type CreateResult struct {
111 commonResult
112}
113
114// createErr quickly creates a CreateResult that reports an error.
115func createErr(err error) CreateResult {
116 return CreateResult{
Ash Wilsonf548aad2014-10-20 08:35:34 -0400117 commonResult: commonResult{Result: gophercloud.Result{Err: err}},
Ash Wilsonf8d546a2014-09-30 17:43:25 -0400118 }
119}
120
121// GetResult is the deferred response from a Get call.
122type GetResult struct {
123 commonResult
124}
125
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +0100126// RevokeResult is the deferred response from a Revoke call.
127type RevokeResult struct {
128 commonResult
129}
130
Ash Wilsonf8d546a2014-09-30 17:43:25 -0400131// Token is a string that grants a user access to a controlled set of services in an OpenStack provider.
132// Each Token is valid for a set length of time.
133type Token struct {
134 // ID is the issued token.
135 ID string
136
137 // ExpiresAt is the timestamp at which this token will no longer be accepted.
138 ExpiresAt time.Time
Ash Wilsone058e342014-08-29 10:31:41 -0400139}