blob: 2144a2ba933785bff3eae16d65306aba475f6d82 [file] [log] [blame]
Ash Wilsona1920082014-08-28 14:24:17 -04001package tokens
2
Eugene Yakubovich8e3f2502016-10-06 07:15:46 -07003import "errors"
jrperrittc8834c12016-08-03 16:06:16 -05004import "github.com/gophercloud/gophercloud"
Ash Wilson46d913f2014-08-29 11:00:11 -04005
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +02006// 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.
10type Endpoint struct {
Jon Perritt3c166472016-02-25 03:07:41 -060011 ID string `json:"id"`
12 Region string `json:"region"`
13 Interface string `json:"interface"`
14 URL string `json:"url"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020015}
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.
23type CatalogEntry struct {
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020024 // Service ID
Jon Perritt3c166472016-02-25 03:07:41 -060025 ID string `json:"id"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020026 // Name will contain the provider-specified name for the service.
Jon Perritt3c166472016-02-25 03:07:41 -060027 Name string `json:"name"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020028 // 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 Perritt3c166472016-02-25 03:07:41 -060030 Type string `json:"type"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020031 // Endpoints will let the caller iterate over all the different endpoints that may exist for
32 // the service.
Jon Perritt3c166472016-02-25 03:07:41 -060033 Endpoints []Endpoint `json:"endpoints"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020034}
35
36// ServiceCatalog provides a view into the service catalog from a previous, successful authentication.
37type ServiceCatalog struct {
38 Entries []CatalogEntry
39}
40
Ash Wilsonf8d546a2014-09-30 17:43:25 -040041// commonResult is the deferred result of a Create or a Get call.
42type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040043 gophercloud.Result
Ash Wilson4a52e2a2014-08-29 09:28:00 -040044}
Ash Wilsona1920082014-08-28 14:24:17 -040045
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020046// Extract is a shortcut for ExtractToken.
47// This function is deprecated and still present for backward compatibility.
Ash Wilsonf8d546a2014-09-30 17:43:25 -040048func (r commonResult) Extract() (*Token, error) {
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020049 return r.ExtractToken()
50}
51
52// ExtractToken interprets a commonResult as a Token.
53func (r commonResult) ExtractToken() (*Token, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060054 var s struct {
jrperrittc8834c12016-08-03 16:06:16 -050055 Token *Token `json:"token"`
Ash Wilsone058e342014-08-29 10:31:41 -040056 }
57
Jon Perritt3c166472016-02-25 03:07:41 -060058 err := r.ExtractInto(&s)
Ash Wilsone058e342014-08-29 10:31:41 -040059 if err != nil {
Ash Wilsonf8d546a2014-09-30 17:43:25 -040060 return nil, err
Ash Wilsone058e342014-08-29 10:31:41 -040061 }
62
Eugene Yakubovich8e3f2502016-10-06 07:15:46 -070063 if s.Token == nil {
64 return nil, errors.New("'token' missing in JSON response")
65 }
66
jrperrittc8834c12016-08-03 16:06:16 -050067 // Parse the token itself from the stored headers.
68 s.Token.ID = r.Header.Get("X-Subject-Token")
Ash Wilsone058e342014-08-29 10:31:41 -040069
jrperrittc8834c12016-08-03 16:06:16 -050070 return s.Token, err
Ash Wilsonf8d546a2014-09-30 17:43:25 -040071}
72
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020073// ExtractServiceCatalog returns the ServiceCatalog that was generated along with the user's Token.
Jon Perritt3c166472016-02-25 03:07:41 -060074func (r CreateResult) ExtractServiceCatalog() (*ServiceCatalog, error) {
75 var s struct {
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020076 Token struct {
Jon Perritt3c166472016-02-25 03:07:41 -060077 Entries []CatalogEntry `json:"catalog"`
78 } `json:"token"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020079 }
Jon Perritt3c166472016-02-25 03:07:41 -060080 err := r.ExtractInto(&s)
81 return &ServiceCatalog{Entries: s.Token.Entries}, err
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020082}
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 Wilsonf8d546a2014-09-30 17:43:25 -040086type CreateResult struct {
87 commonResult
88}
89
Ash Wilsonf8d546a2014-09-30 17:43:25 -040090// GetResult is the deferred response from a Get call.
91type GetResult struct {
92 commonResult
93}
94
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +010095// RevokeResult is the deferred response from a Revoke call.
96type RevokeResult struct {
97 commonResult
98}
99
Ash Wilsonf8d546a2014-09-30 17:43:25 -0400100// 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.
102type Token struct {
103 // ID is the issued token.
jrperrittc8834c12016-08-03 16:06:16 -0500104 ID string `json:"id"`
Ash Wilsonf8d546a2014-09-30 17:43:25 -0400105 // ExpiresAt is the timestamp at which this token will no longer be accepted.
jrperrittc8834c12016-08-03 16:06:16 -0500106 ExpiresAt gophercloud.JSONRFC3339Milli `json:"expires_at"`
Ash Wilsone058e342014-08-29 10:31:41 -0400107}