results.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package tokens
  2. import (
  3. "time"
  4. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  5. )
  6. // Endpoint represents a single API endpoint offered by a service.
  7. // It matches either a public, internal or admin URL.
  8. // If supported, it contains a region specifier, again if provided.
  9. // The significance of the Region field will depend upon your provider.
  10. type Endpoint struct {
  11. ID string `json:"id"`
  12. Region string `json:"region"`
  13. RegionID string `json:"region_id"`
  14. Interface string `json:"interface"`
  15. URL string `json:"url"`
  16. }
  17. // CatalogEntry provides a type-safe interface to an Identity API V3 service
  18. // catalog listing. Each class of service, such as cloud DNS or block storage
  19. // services, could have multiple CatalogEntry representing it (one by interface
  20. // type, e.g public, admin or internal).
  21. //
  22. // Note: when looking for the desired service, try, whenever possible, to key
  23. // off the type field. Otherwise, you'll tie the representation of the service
  24. // to a specific provider.
  25. type CatalogEntry struct {
  26. // Service ID
  27. ID string `json:"id"`
  28. // Name will contain the provider-specified name for the service.
  29. Name string `json:"name"`
  30. // Type will contain a type string if OpenStack defines a type for the
  31. // service. Otherwise, for provider-specific services, the provider may
  32. // assign their own type strings.
  33. Type string `json:"type"`
  34. // Endpoints will let the caller iterate over all the different endpoints that
  35. // may exist for the service.
  36. Endpoints []Endpoint `json:"endpoints"`
  37. }
  38. // ServiceCatalog provides a view into the service catalog from a previous,
  39. // successful authentication.
  40. type ServiceCatalog struct {
  41. Entries []CatalogEntry `json:"catalog"`
  42. }
  43. // Domain provides information about the domain to which this token grants
  44. // access.
  45. type Domain struct {
  46. ID string `json:"id"`
  47. Name string `json:"name"`
  48. }
  49. // User represents a user resource that exists in the Identity Service.
  50. type User struct {
  51. Domain Domain `json:"domain"`
  52. ID string `json:"id"`
  53. Name string `json:"name"`
  54. }
  55. // Role provides information about roles to which User is authorized.
  56. type Role struct {
  57. ID string `json:"id"`
  58. Name string `json:"name"`
  59. }
  60. // Project provides information about project to which User is authorized.
  61. type Project struct {
  62. Domain Domain `json:"domain"`
  63. ID string `json:"id"`
  64. Name string `json:"name"`
  65. }
  66. // commonResult is the response from a request. A commonResult has various
  67. // methods which can be used to extract different details about the result.
  68. type commonResult struct {
  69. gophercloud.Result
  70. }
  71. // Extract is a shortcut for ExtractToken.
  72. // This function is deprecated and still present for backward compatibility.
  73. func (r commonResult) Extract() (*Token, error) {
  74. return r.ExtractToken()
  75. }
  76. // ExtractToken interprets a commonResult as a Token.
  77. func (r commonResult) ExtractToken() (*Token, error) {
  78. var s Token
  79. err := r.ExtractInto(&s)
  80. if err != nil {
  81. return nil, err
  82. }
  83. // Parse the token itself from the stored headers.
  84. s.ID = r.Header.Get("X-Subject-Token")
  85. return &s, err
  86. }
  87. // ExtractServiceCatalog returns the ServiceCatalog that was generated along
  88. // with the user's Token.
  89. func (r commonResult) ExtractServiceCatalog() (*ServiceCatalog, error) {
  90. var s ServiceCatalog
  91. err := r.ExtractInto(&s)
  92. return &s, err
  93. }
  94. // ExtractUser returns the User that is the owner of the Token.
  95. func (r commonResult) ExtractUser() (*User, error) {
  96. var s struct {
  97. User *User `json:"user"`
  98. }
  99. err := r.ExtractInto(&s)
  100. return s.User, err
  101. }
  102. // ExtractRoles returns Roles to which User is authorized.
  103. func (r commonResult) ExtractRoles() ([]Role, error) {
  104. var s struct {
  105. Roles []Role `json:"roles"`
  106. }
  107. err := r.ExtractInto(&s)
  108. return s.Roles, err
  109. }
  110. // ExtractProject returns Project to which User is authorized.
  111. func (r commonResult) ExtractProject() (*Project, error) {
  112. var s struct {
  113. Project *Project `json:"project"`
  114. }
  115. err := r.ExtractInto(&s)
  116. return s.Project, err
  117. }
  118. // CreateResult is the response from a Create request. Use ExtractToken()
  119. // to interpret it as a Token, or ExtractServiceCatalog() to interpret it
  120. // as a service catalog.
  121. type CreateResult struct {
  122. commonResult
  123. }
  124. // GetResult is the response from a Get request. Use ExtractToken()
  125. // to interpret it as a Token, or ExtractServiceCatalog() to interpret it
  126. // as a service catalog.
  127. type GetResult struct {
  128. commonResult
  129. }
  130. // RevokeResult is response from a Revoke request.
  131. type RevokeResult struct {
  132. commonResult
  133. }
  134. // Token is a string that grants a user access to a controlled set of services
  135. // in an OpenStack provider. Each Token is valid for a set length of time.
  136. type Token struct {
  137. // ID is the issued token.
  138. ID string `json:"id"`
  139. // ExpiresAt is the timestamp at which this token will no longer be accepted.
  140. ExpiresAt time.Time `json:"expires_at"`
  141. }
  142. func (r commonResult) ExtractInto(v interface{}) error {
  143. return r.ExtractIntoStructPtr(v, "token")
  144. }