blob: 7dd2c0b15ae637864eabfa080dc01ecc93ec4bfc [file] [log] [blame]
Ash Wilsona1920082014-08-28 14:24:17 -04001package tokens
2
Ash Wilson46d913f2014-08-29 11:00:11 -04003import (
4 "time"
5
Jon Perritt27249f42016-02-18 10:35:59 -06006 "github.com/gophercloud/gophercloud"
Ash Wilson46d913f2014-08-29 11:00:11 -04007)
8
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 {
27
28 // Service ID
Jon Perritt3c166472016-02-25 03:07:41 -060029 ID string `json:"id"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020030
31 // Name will contain the provider-specified name for the service.
Jon Perritt3c166472016-02-25 03:07:41 -060032 Name string `json:"name"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020033
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 Perritt3c166472016-02-25 03:07:41 -060036 Type string `json:"type"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020037
38 // Endpoints will let the caller iterate over all the different endpoints that may exist for
39 // the service.
Jon Perritt3c166472016-02-25 03:07:41 -060040 Endpoints []Endpoint `json:"endpoints"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020041}
42
43// ServiceCatalog provides a view into the service catalog from a previous, successful authentication.
44type ServiceCatalog struct {
45 Entries []CatalogEntry
46}
47
Ash Wilsonf8d546a2014-09-30 17:43:25 -040048// commonResult is the deferred result of a Create or a Get call.
49type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040050 gophercloud.Result
Ash Wilson4a52e2a2014-08-29 09:28:00 -040051}
Ash Wilsona1920082014-08-28 14:24:17 -040052
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020053// Extract is a shortcut for ExtractToken.
54// This function is deprecated and still present for backward compatibility.
Ash Wilsonf8d546a2014-09-30 17:43:25 -040055func (r commonResult) Extract() (*Token, error) {
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020056 return r.ExtractToken()
57}
58
59// ExtractToken interprets a commonResult as a Token.
60func (r commonResult) ExtractToken() (*Token, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060061 var s struct {
Ash Wilsonf8d546a2014-09-30 17:43:25 -040062 Token struct {
Jon Perritt3c166472016-02-25 03:07:41 -060063 ExpiresAt string `json:"expires_at"`
64 } `json:"token"`
Ash Wilsone058e342014-08-29 10:31:41 -040065 }
66
Ash Wilsonf8d546a2014-09-30 17:43:25 -040067 var token Token
68
69 // Parse the token itself from the stored headers.
Ash Wilson72e4d2c2014-10-20 10:27:30 -040070 token.ID = r.Header.Get("X-Subject-Token")
Ash Wilsonf8d546a2014-09-30 17:43:25 -040071
Jon Perritt3c166472016-02-25 03:07:41 -060072 err := r.ExtractInto(&s)
Ash Wilsone058e342014-08-29 10:31:41 -040073 if err != nil {
Ash Wilsonf8d546a2014-09-30 17:43:25 -040074 return nil, err
Ash Wilsone058e342014-08-29 10:31:41 -040075 }
76
77 // Attempt to parse the timestamp.
Jon Perritt3c166472016-02-25 03:07:41 -060078 token.ExpiresAt, err = time.Parse(gophercloud.RFC3339Milli, s.Token.ExpiresAt)
Ash Wilsone058e342014-08-29 10:31:41 -040079
Jamie Hannaforda253adf2014-10-08 17:14:24 +020080 return &token, err
Ash Wilsonf8d546a2014-09-30 17:43:25 -040081}
82
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020083// ExtractServiceCatalog returns the ServiceCatalog that was generated along with the user's Token.
Jon Perritt3c166472016-02-25 03:07:41 -060084func (r CreateResult) ExtractServiceCatalog() (*ServiceCatalog, error) {
85 var s struct {
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020086 Token struct {
Jon Perritt3c166472016-02-25 03:07:41 -060087 Entries []CatalogEntry `json:"catalog"`
88 } `json:"token"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020089 }
Jon Perritt3c166472016-02-25 03:07:41 -060090 err := r.ExtractInto(&s)
91 return &ServiceCatalog{Entries: s.Token.Entries}, err
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020092}
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 Wilsonf8d546a2014-09-30 17:43:25 -040096type CreateResult struct {
97 commonResult
98}
99
100// createErr quickly creates a CreateResult that reports an error.
101func createErr(err error) CreateResult {
102 return CreateResult{
Ash Wilsonf548aad2014-10-20 08:35:34 -0400103 commonResult: commonResult{Result: gophercloud.Result{Err: err}},
Ash Wilsonf8d546a2014-09-30 17:43:25 -0400104 }
105}
106
107// GetResult is the deferred response from a Get call.
108type GetResult struct {
109 commonResult
110}
111
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +0100112// RevokeResult is the deferred response from a Revoke call.
113type RevokeResult struct {
114 commonResult
115}
116
Ash Wilsonf8d546a2014-09-30 17:43:25 -0400117// 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.
119type 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 Wilsone058e342014-08-29 10:31:41 -0400125}