requests.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package snapshots
  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. ToSnapshotCreateMap() (map[string]interface{}, error)
  10. }
  11. // CreateOpts contains options for creating a Snapshot. This object is passed to
  12. // the snapshots.Create function. For more information about these parameters,
  13. // see the Snapshot object.
  14. type CreateOpts struct {
  15. VolumeID string `json:"volume_id" required:"true"`
  16. Force bool `json:"force,omitempty"`
  17. Name string `json:"name,omitempty"`
  18. Description string `json:"description,omitempty"`
  19. Metadata map[string]string `json:"metadata,omitempty"`
  20. }
  21. // ToSnapshotCreateMap assembles a request body based on the contents of a
  22. // CreateOpts.
  23. func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) {
  24. return gophercloud.BuildRequestBody(opts, "snapshot")
  25. }
  26. // Create will create a new Snapshot based on the values in CreateOpts. To
  27. // extract the Snapshot object from the response, call the Extract method on the
  28. // CreateResult.
  29. func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
  30. b, err := opts.ToSnapshotCreateMap()
  31. if err != nil {
  32. r.Err = err
  33. return
  34. }
  35. _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
  36. OkCodes: []int{202},
  37. })
  38. return
  39. }
  40. // Delete will delete the existing Snapshot with the provided ID.
  41. func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
  42. _, r.Err = client.Delete(deleteURL(client, id), nil)
  43. return
  44. }
  45. // Get retrieves the Snapshot with the provided ID. To extract the Snapshot
  46. // object from the response, call the Extract method on the GetResult.
  47. func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
  48. _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
  49. return
  50. }
  51. // ListOptsBuilder allows extensions to add additional parameters to the List
  52. // request.
  53. type ListOptsBuilder interface {
  54. ToSnapshotListQuery() (string, error)
  55. }
  56. // ListOpts hold options for listing Snapshots. It is passed to the
  57. // snapshots.List function.
  58. type ListOpts struct {
  59. // AllTenants will retrieve snapshots of all tenants/projects.
  60. AllTenants bool `q:"all_tenants"`
  61. // Name will filter by the specified snapshot name.
  62. Name string `q:"name"`
  63. // Status will filter by the specified status.
  64. Status string `q:"status"`
  65. // TenantID will filter by a specific tenant/project ID.
  66. // Setting AllTenants is required to use this.
  67. TenantID string `q:"project_id"`
  68. // VolumeID will filter by a specified volume ID.
  69. VolumeID string `q:"volume_id"`
  70. }
  71. // ToSnapshotListQuery formats a ListOpts into a query string.
  72. func (opts ListOpts) ToSnapshotListQuery() (string, error) {
  73. q, err := gophercloud.BuildQueryString(opts)
  74. return q.String(), err
  75. }
  76. // List returns Snapshots optionally limited by the conditions provided in
  77. // ListOpts.
  78. func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
  79. url := listURL(client)
  80. if opts != nil {
  81. query, err := opts.ToSnapshotListQuery()
  82. if err != nil {
  83. return pagination.Pager{Err: err}
  84. }
  85. url += query
  86. }
  87. return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
  88. return SnapshotPage{pagination.SinglePageBase(r)}
  89. })
  90. }
  91. // UpdateMetadataOptsBuilder allows extensions to add additional parameters to
  92. // the Update request.
  93. type UpdateMetadataOptsBuilder interface {
  94. ToSnapshotUpdateMetadataMap() (map[string]interface{}, error)
  95. }
  96. // UpdateMetadataOpts contain options for updating an existing Snapshot. This
  97. // object is passed to the snapshots.Update function. For more information
  98. // about the parameters, see the Snapshot object.
  99. type UpdateMetadataOpts struct {
  100. Metadata map[string]interface{} `json:"metadata,omitempty"`
  101. }
  102. // ToSnapshotUpdateMetadataMap assembles a request body based on the contents of
  103. // an UpdateMetadataOpts.
  104. func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) {
  105. return gophercloud.BuildRequestBody(opts, "")
  106. }
  107. // UpdateMetadata will update the Snapshot with provided information. To
  108. // extract the updated Snapshot from the response, call the ExtractMetadata
  109. // method on the UpdateMetadataResult.
  110. func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) {
  111. b, err := opts.ToSnapshotUpdateMetadataMap()
  112. if err != nil {
  113. r.Err = err
  114. return
  115. }
  116. _, r.Err = client.Put(updateMetadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
  117. OkCodes: []int{200},
  118. })
  119. return
  120. }
  121. // IDFromName is a convienience function that returns a snapshot's ID given its name.
  122. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
  123. count := 0
  124. id := ""
  125. pages, err := List(client, nil).AllPages()
  126. if err != nil {
  127. return "", err
  128. }
  129. all, err := ExtractSnapshots(pages)
  130. if err != nil {
  131. return "", err
  132. }
  133. for _, s := range all {
  134. if s.Name == name {
  135. count++
  136. id = s.ID
  137. }
  138. }
  139. switch count {
  140. case 0:
  141. return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "snapshot"}
  142. case 1:
  143. return id, nil
  144. default:
  145. return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "snapshot"}
  146. }
  147. }