blob: 4b362cc42a590fd910a7d1a09697d80ab3d182cc [file] [log] [blame]
Ash Wilsona1920082014-08-28 14:24:17 -04001package tokens
2
jrperrittc8834c12016-08-03 16:06:16 -05003import "github.com/gophercloud/gophercloud"
Ash Wilson46d913f2014-08-29 11:00:11 -04004
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +02005// Endpoint represents a single API endpoint offered by a service.
6// It matches either a public, internal or admin URL.
7// If supported, it contains a region specifier, again if provided.
8// The significance of the Region field will depend upon your provider.
9type Endpoint struct {
Jon Perritt3c166472016-02-25 03:07:41 -060010 ID string `json:"id"`
11 Region string `json:"region"`
12 Interface string `json:"interface"`
13 URL string `json:"url"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020014}
15
16// CatalogEntry provides a type-safe interface to an Identity API V3 service catalog listing.
17// Each class of service, such as cloud DNS or block storage services, could have multiple
18// CatalogEntry representing it (one by interface type, e.g public, admin or internal).
19//
20// Note: when looking for the desired service, try, whenever possible, to key off the type field.
21// Otherwise, you'll tie the representation of the service to a specific provider.
22type CatalogEntry struct {
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020023 // Service ID
Jon Perritt3c166472016-02-25 03:07:41 -060024 ID string `json:"id"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020025 // Name will contain the provider-specified name for the service.
Jon Perritt3c166472016-02-25 03:07:41 -060026 Name string `json:"name"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020027 // Type will contain a type string if OpenStack defines a type for the service.
28 // Otherwise, for provider-specific services, the provider may assign their own type strings.
Jon Perritt3c166472016-02-25 03:07:41 -060029 Type string `json:"type"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020030 // Endpoints will let the caller iterate over all the different endpoints that may exist for
31 // the service.
Jon Perritt3c166472016-02-25 03:07:41 -060032 Endpoints []Endpoint `json:"endpoints"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020033}
34
35// ServiceCatalog provides a view into the service catalog from a previous, successful authentication.
36type ServiceCatalog struct {
37 Entries []CatalogEntry
38}
39
Ash Wilsonf8d546a2014-09-30 17:43:25 -040040// commonResult is the deferred result of a Create or a Get call.
41type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040042 gophercloud.Result
Ash Wilson4a52e2a2014-08-29 09:28:00 -040043}
Ash Wilsona1920082014-08-28 14:24:17 -040044
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020045// Extract is a shortcut for ExtractToken.
46// This function is deprecated and still present for backward compatibility.
Ash Wilsonf8d546a2014-09-30 17:43:25 -040047func (r commonResult) Extract() (*Token, error) {
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020048 return r.ExtractToken()
49}
50
51// ExtractToken interprets a commonResult as a Token.
52func (r commonResult) ExtractToken() (*Token, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060053 var s struct {
jrperrittc8834c12016-08-03 16:06:16 -050054 Token *Token `json:"token"`
Ash Wilsone058e342014-08-29 10:31:41 -040055 }
56
Jon Perritt3c166472016-02-25 03:07:41 -060057 err := r.ExtractInto(&s)
Ash Wilsone058e342014-08-29 10:31:41 -040058 if err != nil {
Ash Wilsonf8d546a2014-09-30 17:43:25 -040059 return nil, err
Ash Wilsone058e342014-08-29 10:31:41 -040060 }
61
jrperrittc8834c12016-08-03 16:06:16 -050062 // Parse the token itself from the stored headers.
63 s.Token.ID = r.Header.Get("X-Subject-Token")
Ash Wilsone058e342014-08-29 10:31:41 -040064
jrperrittc8834c12016-08-03 16:06:16 -050065 return s.Token, err
Ash Wilsonf8d546a2014-09-30 17:43:25 -040066}
67
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020068// ExtractServiceCatalog returns the ServiceCatalog that was generated along with the user's Token.
Jon Perritt3c166472016-02-25 03:07:41 -060069func (r CreateResult) ExtractServiceCatalog() (*ServiceCatalog, error) {
70 var s struct {
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020071 Token struct {
Jon Perritt3c166472016-02-25 03:07:41 -060072 Entries []CatalogEntry `json:"catalog"`
73 } `json:"token"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020074 }
Jon Perritt3c166472016-02-25 03:07:41 -060075 err := r.ExtractInto(&s)
76 return &ServiceCatalog{Entries: s.Token.Entries}, err
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020077}
78
79// CreateResult defers the interpretation of a created token.
80// Use ExtractToken() to interpret it as a Token, or ExtractServiceCatalog() to interpret it as a service catalog.
Ash Wilsonf8d546a2014-09-30 17:43:25 -040081type CreateResult struct {
82 commonResult
83}
84
85// createErr quickly creates a CreateResult that reports an error.
86func createErr(err error) CreateResult {
87 return CreateResult{
Ash Wilsonf548aad2014-10-20 08:35:34 -040088 commonResult: commonResult{Result: gophercloud.Result{Err: err}},
Ash Wilsonf8d546a2014-09-30 17:43:25 -040089 }
90}
91
92// GetResult is the deferred response from a Get call.
93type GetResult struct {
94 commonResult
95}
96
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +010097// RevokeResult is the deferred response from a Revoke call.
98type RevokeResult struct {
99 commonResult
100}
101
Ash Wilsonf8d546a2014-09-30 17:43:25 -0400102// Token is a string that grants a user access to a controlled set of services in an OpenStack provider.
103// Each Token is valid for a set length of time.
104type Token struct {
105 // ID is the issued token.
jrperrittc8834c12016-08-03 16:06:16 -0500106 ID string `json:"id"`
Ash Wilsonf8d546a2014-09-30 17:43:25 -0400107 // ExpiresAt is the timestamp at which this token will no longer be accepted.
jrperrittc8834c12016-08-03 16:06:16 -0500108 ExpiresAt gophercloud.JSONRFC3339Milli `json:"expires_at"`
Ash Wilsone058e342014-08-29 10:31:41 -0400109}