requests.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package clusters
  2. import (
  3. "net/http"
  4. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  5. "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
  6. )
  7. // CreateOptsBuilder Builder.
  8. type CreateOptsBuilder interface {
  9. ToClusterCreateMap() (map[string]interface{}, error)
  10. }
  11. // CreateOpts params
  12. type CreateOpts struct {
  13. Name string `json:"name" required:"true"`
  14. DesiredCapacity int `json:"desired_capacity"`
  15. ProfileID string `json:"profile_id" required:"true"`
  16. MinSize *int `json:"min_size,omitempty"`
  17. Timeout int `json:"timeout,omitempty"`
  18. MaxSize int `json:"max_size,omitempty"`
  19. Metadata map[string]interface{} `json:"metadata,omitempty"`
  20. Config map[string]interface{} `json:"config,omitempty"`
  21. }
  22. // ToClusterCreateMap constructs a request body from CreateOpts.
  23. func (opts CreateOpts) ToClusterCreateMap() (map[string]interface{}, error) {
  24. return gophercloud.BuildRequestBody(opts, "cluster")
  25. }
  26. // Create requests the creation of a new cluster.
  27. func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
  28. b, err := opts.ToClusterCreateMap()
  29. if err != nil {
  30. r.Err = err
  31. return
  32. }
  33. var result *http.Response
  34. result, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
  35. OkCodes: []int{200, 201, 202},
  36. })
  37. if r.Err == nil {
  38. r.Header = result.Header
  39. }
  40. return
  41. }
  42. // Get retrieves details of a single cluster. Use Extract to convert its
  43. // result into a Cluster.
  44. func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
  45. var result *http.Response
  46. result, r.Err = client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{OkCodes: []int{200}})
  47. if r.Err == nil {
  48. r.Header = result.Header
  49. }
  50. return
  51. }
  52. // ListOptsBuilder Builder.
  53. type ListOptsBuilder interface {
  54. ToClusterListQuery() (string, error)
  55. }
  56. // ListOpts params
  57. type ListOpts struct {
  58. Limit int `q:"limit"`
  59. Marker string `q:"marker"`
  60. Sort string `q:"sort"`
  61. GlobalProject *bool `q:"global_project"`
  62. Name string `q:"name,omitempty"`
  63. Status string `q:"status,omitempty"`
  64. }
  65. // ToClusterListQuery formats a ListOpts into a query string.
  66. func (opts ListOpts) ToClusterListQuery() (string, error) {
  67. q, err := gophercloud.BuildQueryString(opts)
  68. return q.String(), err
  69. }
  70. // List instructs OpenStack to provide a list of clusters.
  71. func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
  72. url := listURL(client)
  73. if opts != nil {
  74. query, err := opts.ToClusterListQuery()
  75. if err != nil {
  76. return pagination.Pager{Err: err}
  77. }
  78. url += query
  79. }
  80. return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
  81. return ClusterPage{pagination.LinkedPageBase{PageResult: r}}
  82. })
  83. }
  84. // UpdateOpts implements UpdateOpts
  85. type UpdateOpts struct {
  86. Config string `json:"config,omitempty"`
  87. Name string `json:"name,omitempty"`
  88. ProfileID string `json:"profile_id,omitempty"`
  89. Timeout *int `json:"timeout,omitempty"`
  90. Metadata map[string]interface{} `json:"metadata,omitempty"`
  91. ProfileOnly *bool `json:"profile_only,omitempty"`
  92. }
  93. // UpdateOptsBuilder allows extensions to add additional parameters to the
  94. // Update request.
  95. type UpdateOptsBuilder interface {
  96. ToClusterUpdateMap() (map[string]interface{}, error)
  97. }
  98. // ToClusterUpdateMap assembles a request body based on the contents of
  99. // UpdateOpts.
  100. func (opts UpdateOpts) ToClusterUpdateMap() (map[string]interface{}, error) {
  101. b, err := gophercloud.BuildRequestBody(opts, "cluster")
  102. if err != nil {
  103. return nil, err
  104. }
  105. return b, nil
  106. }
  107. // Update implements profile updated request.
  108. func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
  109. b, err := opts.ToClusterUpdateMap()
  110. if err != nil {
  111. r.Err = err
  112. return r
  113. }
  114. var result *http.Response
  115. result, r.Err = client.Patch(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
  116. OkCodes: []int{200, 202},
  117. })
  118. if r.Err == nil {
  119. r.Header = result.Header
  120. }
  121. return
  122. }
  123. // Delete deletes the specified cluster ID.
  124. func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
  125. var result *http.Response
  126. result, r.Err = client.Delete(deleteURL(client, id), nil)
  127. if r.Err == nil {
  128. r.Header = result.Header
  129. }
  130. return
  131. }