results.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package clusters
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "reflect"
  6. "time"
  7. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  8. "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
  9. )
  10. // commonResult is the response of a base result.
  11. type commonResult struct {
  12. gophercloud.Result
  13. }
  14. // CreateResult is the response of a Create operations.
  15. type CreateResult struct {
  16. commonResult
  17. }
  18. // GetResult is the response of a Get operations.
  19. type GetResult struct {
  20. commonResult
  21. }
  22. // UpdateResult is the response of a Update operations.
  23. type UpdateResult struct {
  24. commonResult
  25. }
  26. type Cluster struct {
  27. Config map[string]interface{} `json:"config"`
  28. CreatedAt time.Time `json:"-"`
  29. Data map[string]interface{} `json:"data"`
  30. Dependents map[string]interface{} `json:"dependents"`
  31. DesiredCapacity int `json:"desired_capacity"`
  32. Domain string `json:"domain"`
  33. ID string `json:"id"`
  34. InitAt time.Time `json:"-"`
  35. MaxSize int `json:"max_size"`
  36. Metadata map[string]interface{} `json:"metadata"`
  37. MinSize int `json:"min_size"`
  38. Name string `json:"name"`
  39. Nodes []string `json:"nodes"`
  40. Policies []string `json:"policies"`
  41. ProfileID string `json:"profile_id"`
  42. ProfileName string `json:"profile_name"`
  43. Project string `json:"project"`
  44. Status string `json:"status"`
  45. StatusReason string `json:"status_reason"`
  46. Timeout int `json:"timeout"`
  47. UpdatedAt time.Time `json:"-"`
  48. User string `json:"user"`
  49. }
  50. func (r commonResult) Extract() (*Cluster, error) {
  51. var s struct {
  52. Cluster *Cluster `json:"cluster"`
  53. }
  54. err := r.ExtractInto(&s)
  55. if err != nil {
  56. return s.Cluster, err
  57. }
  58. return s.Cluster, nil
  59. }
  60. // ClusterPage contains a single page of all clusters from a List call.
  61. type ClusterPage struct {
  62. pagination.LinkedPageBase
  63. }
  64. func (page ClusterPage) IsEmpty() (bool, error) {
  65. clusters, err := ExtractClusters(page)
  66. return len(clusters) == 0, err
  67. }
  68. // ExtractCluster provides access to the list of clusters in a page acquired from the List operation.
  69. func ExtractClusters(r pagination.Page) ([]Cluster, error) {
  70. var s struct {
  71. Clusters []Cluster `json:"clusters"`
  72. }
  73. err := (r.(ClusterPage)).ExtractInto(&s)
  74. return s.Clusters, err
  75. }
  76. func (r *Cluster) UnmarshalJSON(b []byte) error {
  77. type tmp Cluster
  78. var s struct {
  79. tmp
  80. CreatedAt interface{} `json:"created_at"`
  81. InitAt interface{} `json:"init_at"`
  82. UpdatedAt interface{} `json:"updated_at"`
  83. }
  84. err := json.Unmarshal(b, &s)
  85. if err != nil {
  86. return err
  87. }
  88. *r = Cluster(s.tmp)
  89. switch t := s.CreatedAt.(type) {
  90. case string:
  91. if t != "" {
  92. r.CreatedAt, err = time.Parse(gophercloud.RFC3339Milli, t)
  93. if err != nil {
  94. return err
  95. }
  96. }
  97. case nil:
  98. r.CreatedAt = time.Time{}
  99. default:
  100. return fmt.Errorf("Invalid type for time. type=%v", reflect.TypeOf(s.CreatedAt))
  101. }
  102. switch t := s.InitAt.(type) {
  103. case string:
  104. if t != "" {
  105. r.InitAt, err = time.Parse(gophercloud.RFC3339Milli, t)
  106. if err != nil {
  107. return err
  108. }
  109. }
  110. case nil:
  111. r.InitAt = time.Time{}
  112. default:
  113. return fmt.Errorf("Invalid type for time. type=%v", reflect.TypeOf(s.InitAt))
  114. }
  115. switch t := s.UpdatedAt.(type) {
  116. case string:
  117. if t != "" {
  118. r.UpdatedAt, err = time.Parse(gophercloud.RFC3339Milli, t)
  119. if err != nil {
  120. return err
  121. }
  122. }
  123. case nil:
  124. r.UpdatedAt = time.Time{}
  125. default:
  126. return fmt.Errorf("Invalid type for time. type=%v", reflect.TypeOf(s.UpdatedAt))
  127. }
  128. return nil
  129. }
  130. // DeleteResult is the result from a Delete operation. Call its ExtractErr
  131. // method to determine if the call succeeded or failed.
  132. type DeleteResult struct {
  133. gophercloud.ErrResult
  134. }