requests.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package volumes
  2. import (
  3. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  4. "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
  5. )
  6. // CreateOptsBuilder allows extensions to add additional parameters to the
  7. // Create request.
  8. type CreateOptsBuilder interface {
  9. ToVolumeCreateMap() (map[string]interface{}, error)
  10. }
  11. // CreateOpts contains options for creating a Volume. This object is passed to
  12. // the volumes.Create function. For more information about these parameters,
  13. // see the Volume object.
  14. type CreateOpts struct {
  15. // The size of the volume, in GB
  16. Size int `json:"size" required:"true"`
  17. // The availability zone
  18. AvailabilityZone string `json:"availability_zone,omitempty"`
  19. // ConsistencyGroupID is the ID of a consistency group
  20. ConsistencyGroupID string `json:"consistencygroup_id,omitempty"`
  21. // The volume description
  22. Description string `json:"description,omitempty"`
  23. // One or more metadata key and value pairs to associate with the volume
  24. Metadata map[string]string `json:"metadata,omitempty"`
  25. // The volume name
  26. Name string `json:"name,omitempty"`
  27. // the ID of the existing volume snapshot
  28. SnapshotID string `json:"snapshot_id,omitempty"`
  29. // SourceReplica is a UUID of an existing volume to replicate with
  30. SourceReplica string `json:"source_replica,omitempty"`
  31. // the ID of the existing volume
  32. SourceVolID string `json:"source_volid,omitempty"`
  33. // The ID of the image from which you want to create the volume.
  34. // Required to create a bootable volume.
  35. ImageID string `json:"imageRef,omitempty"`
  36. // The associated volume type
  37. VolumeType string `json:"volume_type,omitempty"`
  38. }
  39. // ToVolumeCreateMap assembles a request body based on the contents of a
  40. // CreateOpts.
  41. func (opts CreateOpts) ToVolumeCreateMap() (map[string]interface{}, error) {
  42. return gophercloud.BuildRequestBody(opts, "volume")
  43. }
  44. // Create will create a new Volume based on the values in CreateOpts. To extract
  45. // the Volume object from the response, call the Extract method on the
  46. // CreateResult.
  47. func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
  48. b, err := opts.ToVolumeCreateMap()
  49. if err != nil {
  50. r.Err = err
  51. return
  52. }
  53. _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
  54. OkCodes: []int{202},
  55. })
  56. return
  57. }
  58. // Delete will delete the existing Volume with the provided ID.
  59. func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
  60. _, r.Err = client.Delete(deleteURL(client, id), nil)
  61. return
  62. }
  63. // Get retrieves the Volume with the provided ID. To extract the Volume object
  64. // from the response, call the Extract method on the GetResult.
  65. func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
  66. _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
  67. return
  68. }
  69. // ListOptsBuilder allows extensions to add additional parameters to the List
  70. // request.
  71. type ListOptsBuilder interface {
  72. ToVolumeListQuery() (string, error)
  73. }
  74. // ListOpts holds options for listing Volumes. It is passed to the volumes.List
  75. // function.
  76. type ListOpts struct {
  77. // AllTenants will retrieve volumes of all tenants/projects.
  78. AllTenants bool `q:"all_tenants"`
  79. // Metadata will filter results based on specified metadata.
  80. Metadata map[string]string `q:"metadata"`
  81. // Name will filter by the specified volume name.
  82. Name string `q:"name"`
  83. // Status will filter by the specified status.
  84. Status string `q:"status"`
  85. // TenantID will filter by a specific tenant/project ID.
  86. // Setting AllTenants is required for this.
  87. TenantID string `q:"project_id"`
  88. // Comma-separated list of sort keys and optional sort directions in the
  89. // form of <key>[:<direction>].
  90. Sort string `q:"sort"`
  91. // Requests a page size of items.
  92. Limit int `q:"limit"`
  93. // Used in conjunction with limit to return a slice of items.
  94. Offset int `q:"offset"`
  95. // The ID of the last-seen item.
  96. Marker string `q:"marker"`
  97. }
  98. // ToVolumeListQuery formats a ListOpts into a query string.
  99. func (opts ListOpts) ToVolumeListQuery() (string, error) {
  100. q, err := gophercloud.BuildQueryString(opts)
  101. return q.String(), err
  102. }
  103. // List returns Volumes optionally limited by the conditions provided in ListOpts.
  104. func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
  105. url := listURL(client)
  106. if opts != nil {
  107. query, err := opts.ToVolumeListQuery()
  108. if err != nil {
  109. return pagination.Pager{Err: err}
  110. }
  111. url += query
  112. }
  113. return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
  114. return VolumePage{pagination.LinkedPageBase{PageResult: r}}
  115. })
  116. }
  117. // UpdateOptsBuilder allows extensions to add additional parameters to the
  118. // Update request.
  119. type UpdateOptsBuilder interface {
  120. ToVolumeUpdateMap() (map[string]interface{}, error)
  121. }
  122. // UpdateOpts contain options for updating an existing Volume. This object is passed
  123. // to the volumes.Update function. For more information about the parameters, see
  124. // the Volume object.
  125. type UpdateOpts struct {
  126. Name string `json:"name,omitempty"`
  127. Description string `json:"description,omitempty"`
  128. Metadata map[string]string `json:"metadata,omitempty"`
  129. }
  130. // ToVolumeUpdateMap assembles a request body based on the contents of an
  131. // UpdateOpts.
  132. func (opts UpdateOpts) ToVolumeUpdateMap() (map[string]interface{}, error) {
  133. return gophercloud.BuildRequestBody(opts, "volume")
  134. }
  135. // Update will update the Volume with provided information. To extract the updated
  136. // Volume from the response, call the Extract method on the UpdateResult.
  137. func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
  138. b, err := opts.ToVolumeUpdateMap()
  139. if err != nil {
  140. r.Err = err
  141. return
  142. }
  143. _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
  144. OkCodes: []int{200},
  145. })
  146. return
  147. }
  148. // IDFromName is a convienience function that returns a server's ID given its name.
  149. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
  150. count := 0
  151. id := ""
  152. pages, err := List(client, nil).AllPages()
  153. if err != nil {
  154. return "", err
  155. }
  156. all, err := ExtractVolumes(pages)
  157. if err != nil {
  158. return "", err
  159. }
  160. for _, s := range all {
  161. if s.Name == name {
  162. count++
  163. id = s.ID
  164. }
  165. }
  166. switch count {
  167. case 0:
  168. return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "volume"}
  169. case 1:
  170. return id, nil
  171. default:
  172. return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "volume"}
  173. }
  174. }