blob: d6917f9adeb22774970d2c0d846d5ea282f9b60d [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 {
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 {
41 Entries []CatalogEntry
42}
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) {
Jon Perritt3c166472016-02-25 03:07:41 -060057 var s struct {
Ash Wilsonf8d546a2014-09-30 17:43:25 -040058 Token struct {
jrperritt93b4a3c2016-07-20 20:29:30 -050059 ExpiresAt gophercloud.JSONRFC3339Milli `json:"expires_at"`
Jon Perritt3c166472016-02-25 03:07:41 -060060 } `json:"token"`
Ash Wilsone058e342014-08-29 10:31:41 -040061 }
62
Ash Wilsonf8d546a2014-09-30 17:43:25 -040063 var token Token
64
65 // Parse the token itself from the stored headers.
Ash Wilson72e4d2c2014-10-20 10:27:30 -040066 token.ID = r.Header.Get("X-Subject-Token")
Ash Wilsonf8d546a2014-09-30 17:43:25 -040067
Jon Perritt3c166472016-02-25 03:07:41 -060068 err := r.ExtractInto(&s)
Ash Wilsone058e342014-08-29 10:31:41 -040069 if err != nil {
Ash Wilsonf8d546a2014-09-30 17:43:25 -040070 return nil, err
Ash Wilsone058e342014-08-29 10:31:41 -040071 }
72
jrperritt93b4a3c2016-07-20 20:29:30 -050073 token.ExpiresAt = time.Time(s.Token.ExpiresAt)
Ash Wilsone058e342014-08-29 10:31:41 -040074
Jamie Hannaforda253adf2014-10-08 17:14:24 +020075 return &token, err
Ash Wilsonf8d546a2014-09-30 17:43:25 -040076}
77
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020078// ExtractServiceCatalog returns the ServiceCatalog that was generated along with the user's Token.
Jon Perritt3c166472016-02-25 03:07:41 -060079func (r CreateResult) ExtractServiceCatalog() (*ServiceCatalog, error) {
80 var s struct {
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020081 Token struct {
Jon Perritt3c166472016-02-25 03:07:41 -060082 Entries []CatalogEntry `json:"catalog"`
83 } `json:"token"`
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020084 }
Jon Perritt3c166472016-02-25 03:07:41 -060085 err := r.ExtractInto(&s)
86 return &ServiceCatalog{Entries: s.Token.Entries}, err
Guillaume Giamarchic043a3d2015-04-01 01:19:55 +020087}
88
89// CreateResult defers the interpretation of a created token.
90// Use ExtractToken() to interpret it as a Token, or ExtractServiceCatalog() to interpret it as a service catalog.
Ash Wilsonf8d546a2014-09-30 17:43:25 -040091type CreateResult struct {
92 commonResult
93}
94
95// createErr quickly creates a CreateResult that reports an error.
96func createErr(err error) CreateResult {
97 return CreateResult{
Ash Wilsonf548aad2014-10-20 08:35:34 -040098 commonResult: commonResult{Result: gophercloud.Result{Err: err}},
Ash Wilsonf8d546a2014-09-30 17:43:25 -040099 }
100}
101
102// GetResult is the deferred response from a Get call.
103type GetResult struct {
104 commonResult
105}
106
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +0100107// RevokeResult is the deferred response from a Revoke call.
108type RevokeResult struct {
109 commonResult
110}
111
Ash Wilsonf8d546a2014-09-30 17:43:25 -0400112// Token is a string that grants a user access to a controlled set of services in an OpenStack provider.
113// Each Token is valid for a set length of time.
114type Token struct {
115 // ID is the issued token.
116 ID string
117
118 // ExpiresAt is the timestamp at which this token will no longer be accepted.
119 ExpiresAt time.Time
Ash Wilsone058e342014-08-29 10:31:41 -0400120}