github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/blockstorage/v3/snapshots/requests.go (about) 1 package snapshots 2 3 import ( 4 "context" 5 6 "github.com/vnpaycloud-console/gophercloud/v2" 7 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 8 ) 9 10 // CreateOptsBuilder allows extensions to add additional parameters to the 11 // Create request. 12 type CreateOptsBuilder interface { 13 ToSnapshotCreateMap() (map[string]any, error) 14 } 15 16 // CreateOpts contains options for creating a Snapshot. This object is passed to 17 // the snapshots.Create function. For more information about these parameters, 18 // see the Snapshot object. 19 type CreateOpts struct { 20 VolumeID string `json:"volume_id" required:"true"` 21 Force bool `json:"force,omitempty"` 22 Name string `json:"name,omitempty"` 23 Description string `json:"description,omitempty"` 24 Metadata map[string]string `json:"metadata,omitempty"` 25 } 26 27 // ToSnapshotCreateMap assembles a request body based on the contents of a 28 // CreateOpts. 29 func (opts CreateOpts) ToSnapshotCreateMap() (map[string]any, error) { 30 return gophercloud.BuildRequestBody(opts, "snapshot") 31 } 32 33 // Create will create a new Snapshot based on the values in CreateOpts. To 34 // extract the Snapshot object from the response, call the Extract method on the 35 // CreateResult. 36 func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 37 b, err := opts.ToSnapshotCreateMap() 38 if err != nil { 39 r.Err = err 40 return 41 } 42 resp, err := client.Post(ctx, createURL(client), b, &r.Body, &gophercloud.RequestOpts{ 43 OkCodes: []int{202}, 44 }) 45 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 46 return 47 } 48 49 // Delete will delete the existing Snapshot with the provided ID. 50 func Delete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r DeleteResult) { 51 resp, err := client.Delete(ctx, deleteURL(client, id), nil) 52 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 53 return 54 } 55 56 // Get retrieves the Snapshot with the provided ID. To extract the Snapshot 57 // object from the response, call the Extract method on the GetResult. 58 func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) { 59 resp, err := client.Get(ctx, getURL(client, id), &r.Body, nil) 60 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 61 return 62 } 63 64 // ListOptsBuilder allows extensions to add additional parameters to the List 65 // request. 66 type ListOptsBuilder interface { 67 ToSnapshotListQuery() (string, error) 68 } 69 70 // ListOpts holds options for listing Snapshots. It is passed to the snapshots.List 71 // function. 72 type ListOpts struct { 73 // AllTenants will retrieve snapshots of all tenants/projects. 74 AllTenants bool `q:"all_tenants"` 75 76 // Name will filter by the specified snapshot name. 77 Name string `q:"name"` 78 79 // Status will filter by the specified status. 80 Status string `q:"status"` 81 82 // TenantID will filter by a specific tenant/project ID. 83 // Setting AllTenants is required to use this. 84 TenantID string `q:"project_id"` 85 86 // VolumeID will filter by a specified volume ID. 87 VolumeID string `q:"volume_id"` 88 89 // Comma-separated list of sort keys and optional sort directions in the 90 // form of <key>[:<direction>]. 91 Sort string `q:"sort"` 92 93 // Requests a page size of items. 94 Limit int `q:"limit"` 95 96 // Used in conjunction with limit to return a slice of items. 97 Offset int `q:"offset"` 98 99 // The ID of the last-seen item. 100 Marker string `q:"marker"` 101 } 102 103 // ToSnapshotListQuery formats a ListOpts into a query string. 104 func (opts ListOpts) ToSnapshotListQuery() (string, error) { 105 q, err := gophercloud.BuildQueryString(opts) 106 return q.String(), err 107 } 108 109 // List returns Snapshots optionally limited by the conditions provided in 110 // ListOpts. 111 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 112 url := listURL(client) 113 if opts != nil { 114 query, err := opts.ToSnapshotListQuery() 115 if err != nil { 116 return pagination.Pager{Err: err} 117 } 118 url += query 119 } 120 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 121 return SnapshotPage{pagination.LinkedPageBase{PageResult: r}} 122 }) 123 } 124 125 // UpdateOptsBuilder allows extensions to add additional parameters to the 126 // Update request. 127 type UpdateOptsBuilder interface { 128 ToSnapshotUpdateMap() (map[string]any, error) 129 } 130 131 // UpdateOpts contain options for updating an existing Snapshot. This object is passed 132 // to the snapshots.Update function. For more information about the parameters, see 133 // the Snapshot object. 134 type UpdateOpts struct { 135 Name *string `json:"name,omitempty"` 136 Description *string `json:"description,omitempty"` 137 } 138 139 // ToSnapshotUpdateMap assembles a request body based on the contents of an 140 // UpdateOpts. 141 func (opts UpdateOpts) ToSnapshotUpdateMap() (map[string]any, error) { 142 return gophercloud.BuildRequestBody(opts, "snapshot") 143 } 144 145 // Update will update the Snapshot with provided information. To extract the updated 146 // Snapshot from the response, call the Extract method on the UpdateResult. 147 func Update(ctx context.Context, client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 148 b, err := opts.ToSnapshotUpdateMap() 149 if err != nil { 150 r.Err = err 151 return 152 } 153 resp, err := client.Put(ctx, updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 154 OkCodes: []int{200}, 155 }) 156 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 157 return 158 } 159 160 // UpdateMetadataOptsBuilder allows extensions to add additional parameters to 161 // the Update request. 162 type UpdateMetadataOptsBuilder interface { 163 ToSnapshotUpdateMetadataMap() (map[string]any, error) 164 } 165 166 // UpdateMetadataOpts contain options for updating an existing Snapshot. This 167 // object is passed to the snapshots.Update function. For more information 168 // about the parameters, see the Snapshot object. 169 type UpdateMetadataOpts struct { 170 Metadata map[string]any `json:"metadata,omitempty"` 171 } 172 173 // ToSnapshotUpdateMetadataMap assembles a request body based on the contents of 174 // an UpdateMetadataOpts. 175 func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]any, error) { 176 return gophercloud.BuildRequestBody(opts, "") 177 } 178 179 // UpdateMetadata will update the Snapshot with provided information. To 180 // extract the updated Snapshot from the response, call the ExtractMetadata 181 // method on the UpdateMetadataResult. 182 func UpdateMetadata(ctx context.Context, client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) { 183 b, err := opts.ToSnapshotUpdateMetadataMap() 184 if err != nil { 185 r.Err = err 186 return 187 } 188 resp, err := client.Put(ctx, updateMetadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 189 OkCodes: []int{200}, 190 }) 191 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 192 return 193 } 194 195 // ResetStatusOptsBuilder allows extensions to add additional parameters to the 196 // ResetStatus request. 197 type ResetStatusOptsBuilder interface { 198 ToSnapshotResetStatusMap() (map[string]any, error) 199 } 200 201 // ResetStatusOpts contains options for resetting a Snapshot status. 202 // For more information about these parameters, please, refer to the Block Storage API V3, 203 // Snapshot Actions, ResetStatus snapshot documentation. 204 type ResetStatusOpts struct { 205 // Status is a snapshot status to reset to. 206 Status string `json:"status"` 207 } 208 209 // ToSnapshotResetStatusMap assembles a request body based on the contents of a 210 // ResetStatusOpts. 211 func (opts ResetStatusOpts) ToSnapshotResetStatusMap() (map[string]any, error) { 212 return gophercloud.BuildRequestBody(opts, "os-reset_status") 213 } 214 215 // ResetStatus will reset the existing snapshot status. ResetStatusResult contains only the error. 216 // To extract it, call the ExtractErr method on the ResetStatusResult. 217 func ResetStatus(ctx context.Context, client *gophercloud.ServiceClient, id string, opts ResetStatusOptsBuilder) (r ResetStatusResult) { 218 b, err := opts.ToSnapshotResetStatusMap() 219 if err != nil { 220 r.Err = err 221 return 222 } 223 224 resp, err := client.Post(ctx, resetStatusURL(client, id), b, nil, &gophercloud.RequestOpts{ 225 OkCodes: []int{202}, 226 }) 227 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 228 return 229 } 230 231 // UpdateStatusOptsBuilder allows extensions to add additional parameters to the 232 // UpdateStatus request. 233 type UpdateStatusOptsBuilder interface { 234 ToSnapshotUpdateStatusMap() (map[string]any, error) 235 } 236 237 // UpdateStatusOpts contains options for resetting a Snapshot status. 238 // For more information about these parameters, please, refer to the Block Storage API V3, 239 // Snapshot Actions, UpdateStatus snapshot documentation. 240 type UpdateStatusOpts struct { 241 // Status is a snapshot status to update to. 242 Status string `json:"status"` 243 // A progress percentage value for snapshot build progress. 244 Progress string `json:"progress,omitempty"` 245 } 246 247 // ToSnapshotUpdateStatusMap assembles a request body based on the contents of a 248 // UpdateStatusOpts. 249 func (opts UpdateStatusOpts) ToSnapshotUpdateStatusMap() (map[string]any, error) { 250 return gophercloud.BuildRequestBody(opts, "os-update_snapshot_status") 251 } 252 253 // UpdateStatus will update the existing snapshot status. UpdateStatusResult contains only the error. 254 // To extract it, call the ExtractErr method on the UpdateStatusResult. 255 func UpdateStatus(ctx context.Context, client *gophercloud.ServiceClient, id string, opts UpdateStatusOptsBuilder) (r UpdateStatusResult) { 256 b, err := opts.ToSnapshotUpdateStatusMap() 257 if err != nil { 258 r.Err = err 259 return 260 } 261 262 resp, err := client.Post(ctx, updateStatusURL(client, id), b, nil, &gophercloud.RequestOpts{ 263 OkCodes: []int{202}, 264 }) 265 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 266 return 267 } 268 269 // ForceDelete will delete the existing snapshot in any state. ForceDeleteResult contains only the error. 270 // To extract it, call the ExtractErr method on the ForceDeleteResult. 271 func ForceDelete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r ForceDeleteResult) { 272 b := map[string]any{ 273 "os-force_delete": struct{}{}, 274 } 275 resp, err := client.Post(ctx, forceDeleteURL(client, id), b, nil, &gophercloud.RequestOpts{ 276 OkCodes: []int{202}, 277 }) 278 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 279 return 280 }