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