blob: 0f1e8c2ba765a44945f850383adacd05612bdbe6 [file] [log] [blame]
Ash Wilsona1920082014-08-28 14:24:17 -04001package tokens
2
jrperritt98d01622017-01-12 14:24:42 -06003import (
4 "time"
5
6 "github.com/gophercloud/gophercloud"
7)
Ash Wilson46d913f2014-08-29 11:00:11 -04008
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +02009// 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.
13type Endpoint struct {
Jon Perritt3c166472016-02-25 03:07:41 -060014 ID string `json:"id"`
15 Region string `json:"region"`
16 Interface string `json:"interface"`
17 URL string `json:"url"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020018}
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.
26type CatalogEntry struct {
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020027 // Service ID
Jon Perritt3c166472016-02-25 03:07:41 -060028 ID string `json:"id"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020029 // Name will contain the provider-specified name for the service.
Jon Perritt3c166472016-02-25 03:07:41 -060030 Name string `json:"name"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020031 // 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 Perritt3c166472016-02-25 03:07:41 -060033 Type string `json:"type"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020034 // Endpoints will let the caller iterate over all the different endpoints that may exist for
35 // the service.
Jon Perritt3c166472016-02-25 03:07:41 -060036 Endpoints []Endpoint `json:"endpoints"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020037}
38
39// ServiceCatalog provides a view into the service catalog from a previous, successful authentication.
40type ServiceCatalog struct {
jrperritt98d01622017-01-12 14:24:42 -060041 Entries []CatalogEntry `json:"catalog"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020042}
43
Ash Wilsonf8d546a2014-09-30 17:43:25 -040044// commonResult is the deferred result of a Create or a Get call.
45type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040046 gophercloud.Result
Ash Wilson4a52e2a2014-08-29 09:28:00 -040047}
Ash Wilsona1920082014-08-28 14:24:17 -040048
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020049// Extract is a shortcut for ExtractToken.
50// This function is deprecated and still present for backward compatibility.
Ash Wilsonf8d546a2014-09-30 17:43:25 -040051func (r commonResult) Extract() (*Token, error) {
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020052 return r.ExtractToken()
53}
54
55// ExtractToken interprets a commonResult as a Token.
56func (r commonResult) ExtractToken() (*Token, error) {
jrperritt98d01622017-01-12 14:24:42 -060057 var s Token
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
jrperrittc8834c12016-08-03 16:06:16 -050063 // Parse the token itself from the stored headers.
jrperritt98d01622017-01-12 14:24:42 -060064 s.ID = r.Header.Get("X-Subject-Token")
Ash Wilsone058e342014-08-29 10:31:41 -040065
jrperritt98d01622017-01-12 14:24:42 -060066 return &s, err
Ash Wilsonf8d546a2014-09-30 17:43:25 -040067}
68
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020069// ExtractServiceCatalog returns the ServiceCatalog that was generated along with the user's Token.
Jon Perritt3c166472016-02-25 03:07:41 -060070func (r CreateResult) ExtractServiceCatalog() (*ServiceCatalog, error) {
jrperritt98d01622017-01-12 14:24:42 -060071 var s ServiceCatalog
Jon Perritt3c166472016-02-25 03:07:41 -060072 err := r.ExtractInto(&s)
jrperritt98d01622017-01-12 14:24:42 -060073 return &s, err
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020074}
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 Wilsonf8d546a2014-09-30 17:43:25 -040078type CreateResult struct {
79 commonResult
80}
81
Ash Wilsonf8d546a2014-09-30 17:43:25 -040082// GetResult is the deferred response from a Get call.
83type GetResult struct {
84 commonResult
85}
86
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +010087// RevokeResult is the deferred response from a Revoke call.
88type RevokeResult struct {
89 commonResult
90}
91
Ash Wilsonf8d546a2014-09-30 17:43:25 -040092// 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.
94type Token struct {
95 // ID is the issued token.
jrperrittc8834c12016-08-03 16:06:16 -050096 ID string `json:"id"`
Ash Wilsonf8d546a2014-09-30 17:43:25 -040097 // ExpiresAt is the timestamp at which this token will no longer be accepted.
jrperritt98d01622017-01-12 14:24:42 -060098 ExpiresAt time.Time `json:"expires_at"`
99}
100
101func (r commonResult) ExtractInto(v interface{}) error {
102 return r.ExtractIntoStructPtr(v, "token")
Ash Wilsone058e342014-08-29 10:31:41 -0400103}