github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/extensions/volumeactions/requests.go (about) 1 package volumeactions 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 ) 6 7 // AttachOptsBuilder allows extensions to add additional parameters to the 8 // Attach request. 9 type AttachOptsBuilder interface { 10 ToVolumeAttachMap() (map[string]interface{}, error) 11 } 12 13 // AttachMode describes the attachment mode for volumes. 14 type AttachMode string 15 16 // These constants determine how a volume is attached. 17 const ( 18 ReadOnly AttachMode = "ro" 19 ReadWrite AttachMode = "rw" 20 ) 21 22 // AttachOpts contains options for attaching a Volume. 23 type AttachOpts struct { 24 // The mountpoint of this volume. 25 MountPoint string `json:"mountpoint,omitempty"` 26 27 // The nova instance ID, can't set simultaneously with HostName. 28 InstanceUUID string `json:"instance_uuid,omitempty"` 29 30 // The hostname of baremetal host, can't set simultaneously with InstanceUUID. 31 HostName string `json:"host_name,omitempty"` 32 33 // Mount mode of this volume. 34 Mode AttachMode `json:"mode,omitempty"` 35 } 36 37 // ToVolumeAttachMap assembles a request body based on the contents of a 38 // AttachOpts. 39 func (opts AttachOpts) ToVolumeAttachMap() (map[string]interface{}, error) { 40 return gophercloud.BuildRequestBody(opts, "os-attach") 41 } 42 43 // Attach will attach a volume based on the values in AttachOpts. 44 func Attach(client *gophercloud.ServiceClient, id string, opts AttachOptsBuilder) (r AttachResult) { 45 b, err := opts.ToVolumeAttachMap() 46 if err != nil { 47 r.Err = err 48 return 49 } 50 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{ 51 OkCodes: []int{202}, 52 }) 53 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 54 return 55 } 56 57 // BeginDetaching will mark the volume as detaching. 58 func BeginDetaching(client *gophercloud.ServiceClient, id string) (r BeginDetachingResult) { 59 b := map[string]interface{}{"os-begin_detaching": make(map[string]interface{})} 60 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{ 61 OkCodes: []int{202}, 62 }) 63 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 64 return 65 } 66 67 // DetachOptsBuilder allows extensions to add additional parameters to the 68 // Detach request. 69 type DetachOptsBuilder interface { 70 ToVolumeDetachMap() (map[string]interface{}, error) 71 } 72 73 // DetachOpts contains options for detaching a Volume. 74 type DetachOpts struct { 75 // AttachmentID is the ID of the attachment between a volume and instance. 76 AttachmentID string `json:"attachment_id,omitempty"` 77 } 78 79 // ToVolumeDetachMap assembles a request body based on the contents of a 80 // DetachOpts. 81 func (opts DetachOpts) ToVolumeDetachMap() (map[string]interface{}, error) { 82 return gophercloud.BuildRequestBody(opts, "os-detach") 83 } 84 85 // Detach will detach a volume based on volume ID. 86 func Detach(client *gophercloud.ServiceClient, id string, opts DetachOptsBuilder) (r DetachResult) { 87 b, err := opts.ToVolumeDetachMap() 88 if err != nil { 89 r.Err = err 90 return 91 } 92 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{ 93 OkCodes: []int{202}, 94 }) 95 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 96 return 97 } 98 99 // Reserve will reserve a volume based on volume ID. 100 func Reserve(client *gophercloud.ServiceClient, id string) (r ReserveResult) { 101 b := map[string]interface{}{"os-reserve": make(map[string]interface{})} 102 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{ 103 OkCodes: []int{200, 201, 202}, 104 }) 105 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 106 return 107 } 108 109 // Unreserve will unreserve a volume based on volume ID. 110 func Unreserve(client *gophercloud.ServiceClient, id string) (r UnreserveResult) { 111 b := map[string]interface{}{"os-unreserve": make(map[string]interface{})} 112 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{ 113 OkCodes: []int{200, 201, 202}, 114 }) 115 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 116 return 117 } 118 119 // InitializeConnectionOptsBuilder allows extensions to add additional parameters to the 120 // InitializeConnection request. 121 type InitializeConnectionOptsBuilder interface { 122 ToVolumeInitializeConnectionMap() (map[string]interface{}, error) 123 } 124 125 // InitializeConnectionOpts hosts options for InitializeConnection. 126 // The fields are specific to the storage driver in use and the destination 127 // attachment. 128 type InitializeConnectionOpts struct { 129 IP string `json:"ip,omitempty"` 130 Host string `json:"host,omitempty"` 131 Initiator string `json:"initiator,omitempty"` 132 Wwpns []string `json:"wwpns,omitempty"` 133 Wwnns string `json:"wwnns,omitempty"` 134 Multipath *bool `json:"multipath,omitempty"` 135 Platform string `json:"platform,omitempty"` 136 OSType string `json:"os_type,omitempty"` 137 } 138 139 // ToVolumeInitializeConnectionMap assembles a request body based on the contents of a 140 // InitializeConnectionOpts. 141 func (opts InitializeConnectionOpts) ToVolumeInitializeConnectionMap() (map[string]interface{}, error) { 142 b, err := gophercloud.BuildRequestBody(opts, "connector") 143 return map[string]interface{}{"os-initialize_connection": b}, err 144 } 145 146 // InitializeConnection initializes an iSCSI connection by volume ID. 147 func InitializeConnection(client *gophercloud.ServiceClient, id string, opts InitializeConnectionOptsBuilder) (r InitializeConnectionResult) { 148 b, err := opts.ToVolumeInitializeConnectionMap() 149 if err != nil { 150 r.Err = err 151 return 152 } 153 resp, err := client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 154 OkCodes: []int{200, 201, 202}, 155 }) 156 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 157 return 158 } 159 160 // TerminateConnectionOptsBuilder allows extensions to add additional parameters to the 161 // TerminateConnection request. 162 type TerminateConnectionOptsBuilder interface { 163 ToVolumeTerminateConnectionMap() (map[string]interface{}, error) 164 } 165 166 // TerminateConnectionOpts hosts options for TerminateConnection. 167 type TerminateConnectionOpts struct { 168 IP string `json:"ip,omitempty"` 169 Host string `json:"host,omitempty"` 170 Initiator string `json:"initiator,omitempty"` 171 Wwpns []string `json:"wwpns,omitempty"` 172 Wwnns string `json:"wwnns,omitempty"` 173 Multipath *bool `json:"multipath,omitempty"` 174 Platform string `json:"platform,omitempty"` 175 OSType string `json:"os_type,omitempty"` 176 } 177 178 // ToVolumeTerminateConnectionMap assembles a request body based on the contents of a 179 // TerminateConnectionOpts. 180 func (opts TerminateConnectionOpts) ToVolumeTerminateConnectionMap() (map[string]interface{}, error) { 181 b, err := gophercloud.BuildRequestBody(opts, "connector") 182 return map[string]interface{}{"os-terminate_connection": b}, err 183 } 184 185 // TerminateConnection terminates an iSCSI connection by volume ID. 186 func TerminateConnection(client *gophercloud.ServiceClient, id string, opts TerminateConnectionOptsBuilder) (r TerminateConnectionResult) { 187 b, err := opts.ToVolumeTerminateConnectionMap() 188 if err != nil { 189 r.Err = err 190 return 191 } 192 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{ 193 OkCodes: []int{202}, 194 }) 195 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 196 return 197 } 198 199 // ExtendSizeOptsBuilder allows extensions to add additional parameters to the 200 // ExtendSize request. 201 type ExtendSizeOptsBuilder interface { 202 ToVolumeExtendSizeMap() (map[string]interface{}, error) 203 } 204 205 // ExtendSizeOpts contains options for extending the size of an existing Volume. 206 // This object is passed to the volumes.ExtendSize function. 207 type ExtendSizeOpts struct { 208 // NewSize is the new size of the volume, in GB. 209 NewSize int `json:"new_size" required:"true"` 210 } 211 212 // ToVolumeExtendSizeMap assembles a request body based on the contents of an 213 // ExtendSizeOpts. 214 func (opts ExtendSizeOpts) ToVolumeExtendSizeMap() (map[string]interface{}, error) { 215 return gophercloud.BuildRequestBody(opts, "os-extend") 216 } 217 218 // ExtendSize will extend the size of the volume based on the provided information. 219 // This operation does not return a response body. 220 func ExtendSize(client *gophercloud.ServiceClient, id string, opts ExtendSizeOptsBuilder) (r ExtendSizeResult) { 221 b, err := opts.ToVolumeExtendSizeMap() 222 if err != nil { 223 r.Err = err 224 return 225 } 226 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{ 227 OkCodes: []int{202}, 228 }) 229 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 230 return 231 } 232 233 // UploadImageOptsBuilder allows extensions to add additional parameters to the 234 // UploadImage request. 235 type UploadImageOptsBuilder interface { 236 ToVolumeUploadImageMap() (map[string]interface{}, error) 237 } 238 239 // UploadImageOpts contains options for uploading a Volume to image storage. 240 type UploadImageOpts struct { 241 // Container format, may be bare, ofv, ova, etc. 242 ContainerFormat string `json:"container_format,omitempty"` 243 244 // Disk format, may be raw, qcow2, vhd, vdi, vmdk, etc. 245 DiskFormat string `json:"disk_format,omitempty"` 246 247 // The name of image that will be stored in glance. 248 ImageName string `json:"image_name,omitempty"` 249 250 // Force image creation, usable if volume attached to instance. 251 Force bool `json:"force,omitempty"` 252 253 // Visibility defines who can see/use the image. 254 // supported since 3.1 microversion 255 Visibility string `json:"visibility,omitempty"` 256 257 // whether the image is not deletable. 258 // supported since 3.1 microversion 259 Protected bool `json:"protected,omitempty"` 260 } 261 262 // ToVolumeUploadImageMap assembles a request body based on the contents of a 263 // UploadImageOpts. 264 func (opts UploadImageOpts) ToVolumeUploadImageMap() (map[string]interface{}, error) { 265 return gophercloud.BuildRequestBody(opts, "os-volume_upload_image") 266 } 267 268 // UploadImage will upload an image based on the values in UploadImageOptsBuilder. 269 func UploadImage(client *gophercloud.ServiceClient, id string, opts UploadImageOptsBuilder) (r UploadImageResult) { 270 b, err := opts.ToVolumeUploadImageMap() 271 if err != nil { 272 r.Err = err 273 return 274 } 275 resp, err := client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 276 OkCodes: []int{202}, 277 }) 278 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 279 return 280 } 281 282 // ForceDelete will delete the volume regardless of state. 283 func ForceDelete(client *gophercloud.ServiceClient, id string) (r ForceDeleteResult) { 284 resp, err := client.Post(actionURL(client, id), map[string]interface{}{"os-force_delete": ""}, nil, nil) 285 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 286 return 287 } 288 289 // ImageMetadataOptsBuilder allows extensions to add additional parameters to the 290 // ImageMetadataRequest request. 291 type ImageMetadataOptsBuilder interface { 292 ToImageMetadataMap() (map[string]interface{}, error) 293 } 294 295 // ImageMetadataOpts contains options for setting image metadata to a volume. 296 type ImageMetadataOpts struct { 297 // The image metadata to add to the volume as a set of metadata key and value pairs. 298 Metadata map[string]string `json:"metadata"` 299 } 300 301 // ToImageMetadataMap assembles a request body based on the contents of a 302 // ImageMetadataOpts. 303 func (opts ImageMetadataOpts) ToImageMetadataMap() (map[string]interface{}, error) { 304 return gophercloud.BuildRequestBody(opts, "os-set_image_metadata") 305 } 306 307 // SetImageMetadata will set image metadata on a volume based on the values in ImageMetadataOptsBuilder. 308 func SetImageMetadata(client *gophercloud.ServiceClient, id string, opts ImageMetadataOptsBuilder) (r SetImageMetadataResult) { 309 b, err := opts.ToImageMetadataMap() 310 if err != nil { 311 r.Err = err 312 return 313 } 314 resp, err := client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 315 OkCodes: []int{200}, 316 }) 317 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 318 return 319 } 320 321 // BootableOpts contains options for setting bootable status to a volume. 322 type BootableOpts struct { 323 // Enables or disables the bootable attribute. You can boot an instance from a bootable volume. 324 Bootable bool `json:"bootable"` 325 } 326 327 // ToBootableMap assembles a request body based on the contents of a 328 // BootableOpts. 329 func (opts BootableOpts) ToBootableMap() (map[string]interface{}, error) { 330 return gophercloud.BuildRequestBody(opts, "os-set_bootable") 331 } 332 333 // SetBootable will set bootable status on a volume based on the values in BootableOpts 334 func SetBootable(client *gophercloud.ServiceClient, id string, opts BootableOpts) (r SetBootableResult) { 335 b, err := opts.ToBootableMap() 336 if err != nil { 337 r.Err = err 338 return 339 } 340 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{ 341 OkCodes: []int{200}, 342 }) 343 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 344 return 345 } 346 347 // MigrationPolicy type represents a migration_policy when changing types. 348 type MigrationPolicy string 349 350 // Supported attributes for MigrationPolicy attribute for changeType operations. 351 const ( 352 MigrationPolicyNever MigrationPolicy = "never" 353 MigrationPolicyOnDemand MigrationPolicy = "on-demand" 354 ) 355 356 // ChangeTypeOptsBuilder allows extensions to add additional parameters to the 357 // ChangeType request. 358 type ChangeTypeOptsBuilder interface { 359 ToVolumeChangeTypeMap() (map[string]interface{}, error) 360 } 361 362 // ChangeTypeOpts contains options for changing the type of an existing Volume. 363 // This object is passed to the volumes.ChangeType function. 364 type ChangeTypeOpts struct { 365 // NewType is the name of the new volume type of the volume. 366 NewType string `json:"new_type" required:"true"` 367 368 // MigrationPolicy specifies if the volume should be migrated when it is 369 // re-typed. Possible values are "on-demand" or "never". If not specified, 370 // the default is "never". 371 MigrationPolicy MigrationPolicy `json:"migration_policy,omitempty"` 372 } 373 374 // ToVolumeChangeTypeMap assembles a request body based on the contents of an 375 // ChangeTypeOpts. 376 func (opts ChangeTypeOpts) ToVolumeChangeTypeMap() (map[string]interface{}, error) { 377 return gophercloud.BuildRequestBody(opts, "os-retype") 378 } 379 380 // ChangeType will change the volume type of the volume based on the provided information. 381 // This operation does not return a response body. 382 func ChangeType(client *gophercloud.ServiceClient, id string, opts ChangeTypeOptsBuilder) (r ChangeTypeResult) { 383 b, err := opts.ToVolumeChangeTypeMap() 384 if err != nil { 385 r.Err = err 386 return 387 } 388 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{ 389 OkCodes: []int{202}, 390 }) 391 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 392 return 393 } 394 395 // ReImageOpts contains options for Re-image a volume. 396 type ReImageOpts struct { 397 // New image id 398 ImageID string `json:"image_id"` 399 // set true to re-image volumes in reserved state 400 ReImageReserved bool `json:"reimage_reserved"` 401 } 402 403 // ToReImageMap assembles a request body based on the contents of a ReImageOpts. 404 func (opts ReImageOpts) ToReImageMap() (map[string]interface{}, error) { 405 return gophercloud.BuildRequestBody(opts, "os-reimage") 406 } 407 408 // ReImage will re-image a volume based on the values in ReImageOpts 409 func ReImage(client *gophercloud.ServiceClient, id string, opts ReImageOpts) (r ReImageResult) { 410 b, err := opts.ToReImageMap() 411 if err != nil { 412 r.Err = err 413 return 414 } 415 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{ 416 OkCodes: []int{202}, 417 }) 418 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 419 return 420 } 421 422 // ResetStatusOptsBuilder allows extensions to add additional parameters to the 423 // ResetStatus request. 424 type ResetStatusOptsBuilder interface { 425 ToResetStatusMap() (map[string]interface{}, error) 426 } 427 428 // ResetStatusOpts contains options for resetting a Volume status. 429 // For more information about these parameters, please, refer to the Block Storage API V3, 430 // Volume Actions, ResetStatus volume documentation. 431 type ResetStatusOpts struct { 432 // Status is a volume status to reset to. 433 Status string `json:"status"` 434 // MigrationStatus is a volume migration status to reset to. 435 MigrationStatus string `json:"migration_status,omitempty"` 436 // AttachStatus is a volume attach status to reset to. 437 AttachStatus string `json:"attach_status,omitempty"` 438 } 439 440 // ToResetStatusMap assembles a request body based on the contents of a 441 // ResetStatusOpts. 442 func (opts ResetStatusOpts) ToResetStatusMap() (map[string]interface{}, error) { 443 return gophercloud.BuildRequestBody(opts, "os-reset_status") 444 } 445 446 // ResetStatus will reset the existing volume status. ResetStatusResult contains only the error. 447 // To extract it, call the ExtractErr method on the ResetStatusResult. 448 func ResetStatus(client *gophercloud.ServiceClient, id string, opts ResetStatusOptsBuilder) (r ResetStatusResult) { 449 b, err := opts.ToResetStatusMap() 450 if err != nil { 451 r.Err = err 452 return 453 } 454 455 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{ 456 OkCodes: []int{202}, 457 }) 458 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 459 return 460 }