results.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package stackresources
  2. import (
  3. "encoding/json"
  4. "time"
  5. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  6. "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
  7. )
  8. // Resource represents a stack resource.
  9. type Resource struct {
  10. Attributes map[string]interface{} `json:"attributes"`
  11. CreationTime time.Time `json:"-"`
  12. Description string `json:"description"`
  13. Links []gophercloud.Link `json:"links"`
  14. LogicalID string `json:"logical_resource_id"`
  15. Name string `json:"resource_name"`
  16. PhysicalID string `json:"physical_resource_id"`
  17. RequiredBy []interface{} `json:"required_by"`
  18. Status string `json:"resource_status"`
  19. StatusReason string `json:"resource_status_reason"`
  20. Type string `json:"resource_type"`
  21. UpdatedTime time.Time `json:"-"`
  22. }
  23. func (r *Resource) UnmarshalJSON(b []byte) error {
  24. type tmp Resource
  25. var s struct {
  26. tmp
  27. CreationTime gophercloud.JSONRFC3339NoZ `json:"creation_time"`
  28. UpdatedTime gophercloud.JSONRFC3339NoZ `json:"updated_time"`
  29. }
  30. err := json.Unmarshal(b, &s)
  31. if err != nil {
  32. return err
  33. }
  34. *r = Resource(s.tmp)
  35. r.CreationTime = time.Time(s.CreationTime)
  36. r.UpdatedTime = time.Time(s.UpdatedTime)
  37. return nil
  38. }
  39. // FindResult represents the result of a Find operation.
  40. type FindResult struct {
  41. gophercloud.Result
  42. }
  43. // Extract returns a slice of Resource objects and is called after a
  44. // Find operation.
  45. func (r FindResult) Extract() ([]Resource, error) {
  46. var s struct {
  47. Resources []Resource `json:"resources"`
  48. }
  49. err := r.ExtractInto(&s)
  50. return s.Resources, err
  51. }
  52. // ResourcePage abstracts the raw results of making a List() request against the API.
  53. // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the
  54. // data provided through the ExtractResources call.
  55. type ResourcePage struct {
  56. pagination.SinglePageBase
  57. }
  58. // IsEmpty returns true if a page contains no Server results.
  59. func (r ResourcePage) IsEmpty() (bool, error) {
  60. resources, err := ExtractResources(r)
  61. return len(resources) == 0, err
  62. }
  63. // ExtractResources interprets the results of a single page from a List() call, producing a slice of Resource entities.
  64. func ExtractResources(r pagination.Page) ([]Resource, error) {
  65. var s struct {
  66. Resources []Resource `json:"resources"`
  67. }
  68. err := (r.(ResourcePage)).ExtractInto(&s)
  69. return s.Resources, err
  70. }
  71. // GetResult represents the result of a Get operation.
  72. type GetResult struct {
  73. gophercloud.Result
  74. }
  75. // Extract returns a pointer to a Resource object and is called after a
  76. // Get operation.
  77. func (r GetResult) Extract() (*Resource, error) {
  78. var s struct {
  79. Resource *Resource `json:"resource"`
  80. }
  81. err := r.ExtractInto(&s)
  82. return s.Resource, err
  83. }
  84. // MetadataResult represents the result of a Metadata operation.
  85. type MetadataResult struct {
  86. gophercloud.Result
  87. }
  88. // Extract returns a map object and is called after a
  89. // Metadata operation.
  90. func (r MetadataResult) Extract() (map[string]string, error) {
  91. var s struct {
  92. Meta map[string]string `json:"metadata"`
  93. }
  94. err := r.ExtractInto(&s)
  95. return s.Meta, err
  96. }
  97. // ResourceTypePage abstracts the raw results of making a ListTypes() request against the API.
  98. // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the
  99. // data provided through the ExtractResourceTypes call.
  100. type ResourceTypePage struct {
  101. pagination.SinglePageBase
  102. }
  103. // IsEmpty returns true if a ResourceTypePage contains no resource types.
  104. func (r ResourceTypePage) IsEmpty() (bool, error) {
  105. rts, err := ExtractResourceTypes(r)
  106. return len(rts) == 0, err
  107. }
  108. // ResourceTypes represents the type that holds the result of ExtractResourceTypes.
  109. // We define methods on this type to sort it before output
  110. type ResourceTypes []string
  111. func (r ResourceTypes) Len() int {
  112. return len(r)
  113. }
  114. func (r ResourceTypes) Swap(i, j int) {
  115. r[i], r[j] = r[j], r[i]
  116. }
  117. func (r ResourceTypes) Less(i, j int) bool {
  118. return r[i] < r[j]
  119. }
  120. // ExtractResourceTypes extracts and returns resource types.
  121. func ExtractResourceTypes(r pagination.Page) (ResourceTypes, error) {
  122. var s struct {
  123. ResourceTypes ResourceTypes `json:"resource_types"`
  124. }
  125. err := (r.(ResourceTypePage)).ExtractInto(&s)
  126. return s.ResourceTypes, err
  127. }
  128. // TypeSchema represents a stack resource schema.
  129. type TypeSchema struct {
  130. Attributes map[string]interface{} `json:"attributes"`
  131. Properties map[string]interface{} `json:"properties"`
  132. ResourceType string `json:"resource_type"`
  133. SupportStatus map[string]interface{} `json:"support_status"`
  134. }
  135. // SchemaResult represents the result of a Schema operation.
  136. type SchemaResult struct {
  137. gophercloud.Result
  138. }
  139. // Extract returns a pointer to a TypeSchema object and is called after a
  140. // Schema operation.
  141. func (r SchemaResult) Extract() (*TypeSchema, error) {
  142. var s *TypeSchema
  143. err := r.ExtractInto(&s)
  144. return s, err
  145. }
  146. // TemplateResult represents the result of a Template operation.
  147. type TemplateResult struct {
  148. gophercloud.Result
  149. }
  150. // Extract returns the template and is called after a
  151. // Template operation.
  152. func (r TemplateResult) Extract() ([]byte, error) {
  153. if r.Err != nil {
  154. return nil, r.Err
  155. }
  156. template, err := json.MarshalIndent(r.Body, "", " ")
  157. return template, err
  158. }