github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/blockstorage/extensions/volumeactions/requests.go (about) 1 package volumeactions 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 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 golangsdk.BuildRequestBody(opts, "os-attach") 41 } 42 43 // Attach will attach a volume based on the values in AttachOpts. 44 func Attach(client *golangsdk.ServiceClient, id string, opts AttachOptsBuilder) (r AttachResult) { 45 b, err := opts.ToVolumeAttachMap() 46 if err != nil { 47 r.Err = err 48 return 49 } 50 _, r.Err = client.Post(actionURL(client, id), b, nil, &golangsdk.RequestOpts{ 51 OkCodes: []int{202}, 52 }) 53 return 54 } 55 56 // BeginDetach will mark the volume as detaching. 57 func BeginDetaching(client *golangsdk.ServiceClient, id string) (r BeginDetachingResult) { 58 b := map[string]interface{}{"os-begin_detaching": make(map[string]interface{})} 59 _, r.Err = client.Post(actionURL(client, id), b, nil, &golangsdk.RequestOpts{ 60 OkCodes: []int{202}, 61 }) 62 return 63 } 64 65 // DetachOptsBuilder allows extensions to add additional parameters to the 66 // Detach request. 67 type DetachOptsBuilder interface { 68 ToVolumeDetachMap() (map[string]interface{}, error) 69 } 70 71 // DetachOpts contains options for detaching a Volume. 72 type DetachOpts struct { 73 // AttachmentID is the ID of the attachment between a volume and instance. 74 AttachmentID string `json:"attachment_id,omitempty"` 75 } 76 77 // ToVolumeDetachMap assembles a request body based on the contents of a 78 // DetachOpts. 79 func (opts DetachOpts) ToVolumeDetachMap() (map[string]interface{}, error) { 80 return golangsdk.BuildRequestBody(opts, "os-detach") 81 } 82 83 // Detach will detach a volume based on volume ID. 84 func Detach(client *golangsdk.ServiceClient, id string, opts DetachOptsBuilder) (r DetachResult) { 85 b, err := opts.ToVolumeDetachMap() 86 if err != nil { 87 r.Err = err 88 return 89 } 90 _, r.Err = client.Post(actionURL(client, id), b, nil, &golangsdk.RequestOpts{ 91 OkCodes: []int{202}, 92 }) 93 return 94 } 95 96 // Reserve will reserve a volume based on volume ID. 97 func Reserve(client *golangsdk.ServiceClient, id string) (r ReserveResult) { 98 b := map[string]interface{}{"os-reserve": make(map[string]interface{})} 99 _, r.Err = client.Post(actionURL(client, id), b, nil, &golangsdk.RequestOpts{ 100 OkCodes: []int{200, 201, 202}, 101 }) 102 return 103 } 104 105 // Unreserve will unreserve a volume based on volume ID. 106 func Unreserve(client *golangsdk.ServiceClient, id string) (r UnreserveResult) { 107 b := map[string]interface{}{"os-unreserve": make(map[string]interface{})} 108 _, r.Err = client.Post(actionURL(client, id), b, nil, &golangsdk.RequestOpts{ 109 OkCodes: []int{200, 201, 202}, 110 }) 111 return 112 } 113 114 // InitializeConnectionOptsBuilder allows extensions to add additional parameters to the 115 // InitializeConnection request. 116 type InitializeConnectionOptsBuilder interface { 117 ToVolumeInitializeConnectionMap() (map[string]interface{}, error) 118 } 119 120 // InitializeConnectionOpts hosts options for InitializeConnection. 121 // The fields are specific to the storage driver in use and the destination 122 // attachment. 123 type InitializeConnectionOpts struct { 124 IP string `json:"ip,omitempty"` 125 Host string `json:"host,omitempty"` 126 Initiator string `json:"initiator,omitempty"` 127 Wwpns []string `json:"wwpns,omitempty"` 128 Wwnns string `json:"wwnns,omitempty"` 129 Multipath *bool `json:"multipath,omitempty"` 130 Platform string `json:"platform,omitempty"` 131 OSType string `json:"os_type,omitempty"` 132 } 133 134 // ToVolumeInitializeConnectionMap assembles a request body based on the contents of a 135 // InitializeConnectionOpts. 136 func (opts InitializeConnectionOpts) ToVolumeInitializeConnectionMap() (map[string]interface{}, error) { 137 b, err := golangsdk.BuildRequestBody(opts, "connector") 138 return map[string]interface{}{"os-initialize_connection": b}, err 139 } 140 141 // InitializeConnection initializes an iSCSI connection by volume ID. 142 func InitializeConnection(client *golangsdk.ServiceClient, id string, opts InitializeConnectionOptsBuilder) (r InitializeConnectionResult) { 143 b, err := opts.ToVolumeInitializeConnectionMap() 144 if err != nil { 145 r.Err = err 146 return 147 } 148 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ 149 OkCodes: []int{200, 201, 202}, 150 }) 151 return 152 } 153 154 // TerminateConnectionOptsBuilder allows extensions to add additional parameters to the 155 // TerminateConnection request. 156 type TerminateConnectionOptsBuilder interface { 157 ToVolumeTerminateConnectionMap() (map[string]interface{}, error) 158 } 159 160 // TerminateConnectionOpts hosts options for TerminateConnection. 161 type TerminateConnectionOpts struct { 162 IP string `json:"ip,omitempty"` 163 Host string `json:"host,omitempty"` 164 Initiator string `json:"initiator,omitempty"` 165 Wwpns []string `json:"wwpns,omitempty"` 166 Wwnns string `json:"wwnns,omitempty"` 167 Multipath *bool `json:"multipath,omitempty"` 168 Platform string `json:"platform,omitempty"` 169 OSType string `json:"os_type,omitempty"` 170 } 171 172 // ToVolumeTerminateConnectionMap assembles a request body based on the contents of a 173 // TerminateConnectionOpts. 174 func (opts TerminateConnectionOpts) ToVolumeTerminateConnectionMap() (map[string]interface{}, error) { 175 b, err := golangsdk.BuildRequestBody(opts, "connector") 176 return map[string]interface{}{"os-terminate_connection": b}, err 177 } 178 179 // TerminateConnection terminates an iSCSI connection by volume ID. 180 func TerminateConnection(client *golangsdk.ServiceClient, id string, opts TerminateConnectionOptsBuilder) (r TerminateConnectionResult) { 181 b, err := opts.ToVolumeTerminateConnectionMap() 182 if err != nil { 183 r.Err = err 184 return 185 } 186 _, r.Err = client.Post(actionURL(client, id), b, nil, &golangsdk.RequestOpts{ 187 OkCodes: []int{202}, 188 }) 189 return 190 } 191 192 // ExtendSizeOptsBuilder allows extensions to add additional parameters to the 193 // ExtendSize request. 194 type ExtendSizeOptsBuilder interface { 195 ToVolumeExtendSizeMap() (map[string]interface{}, error) 196 } 197 198 // ExtendSizeOpts contains options for extending the size of an existing Volume. 199 // This object is passed to the volumes.ExtendSize function. 200 type ExtendSizeOpts struct { 201 // NewSize is the new size of the volume, in GB. 202 NewSize int `json:"new_size" required:"true"` 203 } 204 205 // ToVolumeExtendSizeMap assembles a request body based on the contents of an 206 // ExtendSizeOpts. 207 func (opts ExtendSizeOpts) ToVolumeExtendSizeMap() (map[string]interface{}, error) { 208 return golangsdk.BuildRequestBody(opts, "os-extend") 209 } 210 211 // ExtendSize will extend the size of the volume based on the provided information. 212 // This operation does not return a response body. 213 func ExtendSize(client *golangsdk.ServiceClient, id string, opts ExtendSizeOptsBuilder) (r ExtendSizeResult) { 214 b, err := opts.ToVolumeExtendSizeMap() 215 if err != nil { 216 r.Err = err 217 return 218 } 219 _, r.Err = client.Post(actionURL(client, id), b, nil, &golangsdk.RequestOpts{ 220 OkCodes: []int{202}, 221 }) 222 return 223 } 224 225 // UploadImageOptsBuilder allows extensions to add additional parameters to the 226 // UploadImage request. 227 type UploadImageOptsBuilder interface { 228 ToVolumeUploadImageMap() (map[string]interface{}, error) 229 } 230 231 // UploadImageOpts contains options for uploading a Volume to image storage. 232 type UploadImageOpts struct { 233 // Container format, may be bare, ofv, ova, etc. 234 ContainerFormat string `json:"container_format,omitempty"` 235 236 // Disk format, may be raw, qcow2, vhd, vdi, vmdk, etc. 237 DiskFormat string `json:"disk_format,omitempty"` 238 239 // The name of image that will be stored in glance. 240 ImageName string `json:"image_name,omitempty"` 241 242 // Force image creation, usable if volume attached to instance. 243 Force bool `json:"force,omitempty"` 244 } 245 246 // ToVolumeUploadImageMap assembles a request body based on the contents of a 247 // UploadImageOpts. 248 func (opts UploadImageOpts) ToVolumeUploadImageMap() (map[string]interface{}, error) { 249 return golangsdk.BuildRequestBody(opts, "os-volume_upload_image") 250 } 251 252 // UploadImage will upload an image based on the values in UploadImageOptsBuilder. 253 func UploadImage(client *golangsdk.ServiceClient, id string, opts UploadImageOptsBuilder) (r UploadImageResult) { 254 b, err := opts.ToVolumeUploadImageMap() 255 if err != nil { 256 r.Err = err 257 return 258 } 259 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ 260 OkCodes: []int{202}, 261 }) 262 return 263 } 264 265 // ForceDelete will delete the volume regardless of state. 266 func ForceDelete(client *golangsdk.ServiceClient, id string) (r ForceDeleteResult) { 267 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"os-force_delete": ""}, nil, nil) 268 return 269 }