github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/tokens/results.go (about) 1 package tokens 2 3 import ( 4 "time" 5 6 "github.com/gophercloud/gophercloud" 7 ) 8 9 // 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. 13 type Endpoint struct { 14 ID string `json:"id"` 15 Region string `json:"region"` 16 RegionID string `json:"region_id"` 17 Interface string `json:"interface"` 18 URL string `json:"url"` 19 } 20 21 // CatalogEntry provides a type-safe interface to an Identity API V3 service 22 // catalog listing. Each class of service, such as cloud DNS or block storage 23 // services, could have multiple CatalogEntry representing it (one by interface 24 // type, e.g public, admin or internal). 25 // 26 // Note: when looking for the desired service, try, whenever possible, to key 27 // off the type field. Otherwise, you'll tie the representation of the service 28 // to a specific provider. 29 type CatalogEntry struct { 30 // Service ID 31 ID string `json:"id"` 32 33 // Name will contain the provider-specified name for the service. 34 Name string `json:"name"` 35 36 // Type will contain a type string if OpenStack defines a type for the 37 // service. Otherwise, for provider-specific services, the provider may 38 // assign their own type strings. 39 Type string `json:"type"` 40 41 // Endpoints will let the caller iterate over all the different endpoints that 42 // may exist for the service. 43 Endpoints []Endpoint `json:"endpoints"` 44 } 45 46 // ServiceCatalog provides a view into the service catalog from a previous, 47 // successful authentication. 48 type ServiceCatalog struct { 49 Entries []CatalogEntry `json:"catalog"` 50 } 51 52 // Domain provides information about the domain to which this token grants 53 // access. 54 type Domain struct { 55 ID string `json:"id"` 56 Name string `json:"name"` 57 } 58 59 // User represents a user resource that exists in the Identity Service. 60 type User struct { 61 Domain Domain `json:"domain"` 62 ID string `json:"id"` 63 Name string `json:"name"` 64 } 65 66 // Role provides information about roles to which User is authorized. 67 type Role struct { 68 ID string `json:"id"` 69 Name string `json:"name"` 70 } 71 72 // Project provides information about project to which User is authorized. 73 type Project struct { 74 Domain Domain `json:"domain"` 75 ID string `json:"id"` 76 Name string `json:"name"` 77 } 78 79 // commonResult is the response from a request. A commonResult has various 80 // methods which can be used to extract different details about the result. 81 type commonResult struct { 82 gophercloud.Result 83 } 84 85 // Extract is a shortcut for ExtractToken. 86 // This function is deprecated and still present for backward compatibility. 87 func (r commonResult) Extract() (*Token, error) { 88 return r.ExtractToken() 89 } 90 91 // ExtractToken interprets a commonResult as a Token. 92 func (r commonResult) ExtractToken() (*Token, error) { 93 var s Token 94 err := r.ExtractInto(&s) 95 if err != nil { 96 return nil, err 97 } 98 99 // Parse the token itself from the stored headers. 100 s.ID = r.Header.Get("X-Subject-Token") 101 102 return &s, err 103 } 104 105 // ExtractTokenID implements the gophercloud.AuthResult interface. The returned 106 // string is the same as the ID field of the Token struct returned from 107 // ExtractToken(). 108 func (r CreateResult) ExtractTokenID() (string, error) { 109 return r.Header.Get("X-Subject-Token"), r.Err 110 } 111 112 // ExtractTokenID implements the gophercloud.AuthResult interface. The returned 113 // string is the same as the ID field of the Token struct returned from 114 // ExtractToken(). 115 func (r GetResult) ExtractTokenID() (string, error) { 116 return r.Header.Get("X-Subject-Token"), r.Err 117 } 118 119 // ExtractServiceCatalog returns the ServiceCatalog that was generated along 120 // with the user's Token. 121 func (r commonResult) ExtractServiceCatalog() (*ServiceCatalog, error) { 122 var s ServiceCatalog 123 err := r.ExtractInto(&s) 124 return &s, err 125 } 126 127 // ExtractUser returns the User that is the owner of the Token. 128 func (r commonResult) ExtractUser() (*User, error) { 129 var s struct { 130 User *User `json:"user"` 131 } 132 err := r.ExtractInto(&s) 133 return s.User, err 134 } 135 136 // ExtractRoles returns Roles to which User is authorized. 137 func (r commonResult) ExtractRoles() ([]Role, error) { 138 var s struct { 139 Roles []Role `json:"roles"` 140 } 141 err := r.ExtractInto(&s) 142 return s.Roles, err 143 } 144 145 // ExtractProject returns Project to which User is authorized. 146 func (r commonResult) ExtractProject() (*Project, error) { 147 var s struct { 148 Project *Project `json:"project"` 149 } 150 err := r.ExtractInto(&s) 151 return s.Project, err 152 } 153 154 // ExtractDomain returns Domain to which User is authorized. 155 func (r commonResult) ExtractDomain() (*Domain, error) { 156 var s struct { 157 Domain *Domain `json:"domain"` 158 } 159 err := r.ExtractInto(&s) 160 return s.Domain, err 161 } 162 163 // CreateResult is the response from a Create request. Use ExtractToken() 164 // to interpret it as a Token, or ExtractServiceCatalog() to interpret it 165 // as a service catalog. 166 type CreateResult struct { 167 commonResult 168 } 169 170 // GetResult is the response from a Get request. Use ExtractToken() 171 // to interpret it as a Token, or ExtractServiceCatalog() to interpret it 172 // as a service catalog. 173 type GetResult struct { 174 commonResult 175 } 176 177 // RevokeResult is response from a Revoke request. 178 type RevokeResult struct { 179 commonResult 180 } 181 182 // Token is a string that grants a user access to a controlled set of services 183 // in an OpenStack provider. Each Token is valid for a set length of time. 184 type Token struct { 185 // ID is the issued token. 186 ID string `json:"id"` 187 188 // ExpiresAt is the timestamp at which this token will no longer be accepted. 189 ExpiresAt time.Time `json:"expires_at"` 190 } 191 192 func (r commonResult) ExtractInto(v interface{}) error { 193 return r.ExtractIntoStructPtr(v, "token") 194 }