github.com/gophercloud/gophercloud@v1.11.0/openstack/compute/v2/extensions/volumeattach/requests.go (about) 1 package volumeattach 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 // List returns a Pager that allows you to iterate over a collection of 9 // VolumeAttachments. 10 func List(client *gophercloud.ServiceClient, serverID string) pagination.Pager { 11 return pagination.NewPager(client, listURL(client, serverID), func(r pagination.PageResult) pagination.Page { 12 return VolumeAttachmentPage{pagination.SinglePageBase(r)} 13 }) 14 } 15 16 // CreateOptsBuilder allows extensions to add parameters to the Create request. 17 type CreateOptsBuilder interface { 18 ToVolumeAttachmentCreateMap() (map[string]interface{}, error) 19 } 20 21 // CreateOpts specifies volume attachment creation or import parameters. 22 type CreateOpts struct { 23 // Device is the device that the volume will attach to the instance as. 24 // Omit for "auto". 25 Device string `json:"device,omitempty"` 26 27 // VolumeID is the ID of the volume to attach to the instance. 28 VolumeID string `json:"volumeId" required:"true"` 29 30 // Tag is a device role tag that can be applied to a volume when attaching 31 // it to the VM. Requires 2.49 microversion 32 Tag string `json:"tag,omitempty"` 33 34 // DeleteOnTermination specifies whether or not to delete the volume when the server 35 // is destroyed. Requires 2.79 microversion 36 DeleteOnTermination bool `json:"delete_on_termination,omitempty"` 37 } 38 39 // ToVolumeAttachmentCreateMap constructs a request body from CreateOpts. 40 func (opts CreateOpts) ToVolumeAttachmentCreateMap() (map[string]interface{}, error) { 41 return gophercloud.BuildRequestBody(opts, "volumeAttachment") 42 } 43 44 // Create requests the creation of a new volume attachment on the server. 45 func Create(client *gophercloud.ServiceClient, serverID string, opts CreateOptsBuilder) (r CreateResult) { 46 b, err := opts.ToVolumeAttachmentCreateMap() 47 if err != nil { 48 r.Err = err 49 return 50 } 51 resp, err := client.Post(createURL(client, serverID), b, &r.Body, &gophercloud.RequestOpts{ 52 OkCodes: []int{200}, 53 }) 54 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 55 return 56 } 57 58 // Get returns public data about a previously created VolumeAttachment. 59 func Get(client *gophercloud.ServiceClient, serverID, volumeID string) (r GetResult) { 60 resp, err := client.Get(getURL(client, serverID, volumeID), &r.Body, nil) 61 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 62 return 63 } 64 65 // Delete requests the deletion of a previous stored VolumeAttachment from 66 // the server. 67 func Delete(client *gophercloud.ServiceClient, serverID, volumeID string) (r DeleteResult) { 68 resp, err := client.Delete(deleteURL(client, serverID, volumeID), nil) 69 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 70 return 71 }