github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/ecs/v1/block_devices/requests.go (about) 1 package block_devices 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 "github.com/chnsz/golangsdk/openstack/ecs/v1/jobs" 6 ) 7 8 var requestOpts = golangsdk.RequestOpts{ 9 MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, 10 } 11 12 // Get is a method to obtain the detail of volume attachment for specified cloud server. 13 func Get(c *golangsdk.ServiceClient, server_id string, volume_id string) (r GetResult) { 14 _, r.Err = c.Get(getURL(c, server_id, volume_id), &r.Body, nil) 15 return 16 } 17 18 // List is a method to obtain the list of volume attachment details for specified cloud server. 19 func List(c *golangsdk.ServiceClient, serverId string) ([]VolumeAttachment, error) { 20 var rst golangsdk.Result 21 _, err := c.Get(rootURL(c, serverId), &rst.Body, &golangsdk.RequestOpts{ 22 MoreHeaders: requestOpts.MoreHeaders, 23 }) 24 if err == nil { 25 var r []VolumeAttachment 26 rst.ExtractIntoSlicePtr(&r, "volumeAttachments") 27 return r, nil 28 } 29 return nil, err 30 } 31 32 // AttachOpts is the structure required by the Attach method to mount the disk. 33 type AttachOpts struct { 34 // Indicates the disk device name. 35 // NOTE: 36 // The new disk device name cannot be the same as an existing one. 37 // This parameter is mandatory for Xen ECSs. Set the parameter value to /dev/sda for the system disks of such ECSs 38 // and to /dev/sdx for data disks, where x is a letter in alphabetical order. For example, if there are two data 39 // disks, set the device names of the two data disks to /dev/sdb and /dev/sdc, respectively. If you set a device 40 // name starting with /dev/vd, the system uses /dev/sd by default. 41 // For KVM ECSs, set the parameter value to /dev/vda for system disks. The device names for data disks of KVM ECSs 42 // are optional. If the device names of data disks are required, set them in alphabetical order. For example, if 43 // there are two data disks, set the device names of the two data disks to /dev/vdb and /dev/vdc, respectively. 44 // If you set a device name starting with /dev/sd, the system uses /dev/vd by default. 45 Device string `json:"device,omitempty"` 46 // Specifies the ID of the disk to be attached. The value is in UUID format. 47 VolumeId string `json:"volumeId" required:"true"` 48 // Specifies the ID of the ECS to which the disk will be attached. 49 ServerId string `json:"-" required:"true"` 50 } 51 52 // Attach is a method to mount a disk to a specified cloud server. 53 func Attach(c *golangsdk.ServiceClient, opts AttachOpts) (*jobs.Job, error) { 54 b, err := golangsdk.BuildRequestBody(opts, "volumeAttachment") 55 if err != nil { 56 return nil, err 57 } 58 59 var r jobs.Job 60 _, err = c.Post(attachURL(c, opts.ServerId), b, &r, &golangsdk.RequestOpts{ 61 MoreHeaders: requestOpts.MoreHeaders, 62 }) 63 return &r, err 64 } 65 66 // DetachOpts is the structure required by the Detach method to unmount a disk from the cloud server. 67 type DetachOpts struct { 68 // Indicates whether to forcibly detach a data disk. 69 // If yes, set it to 1. 70 // If no, set it to 0. 71 // Defaults to 0. 72 DeleteFlag int `q:"delete_flag"` 73 // Specifies the ID of the ECS to which the disk will be detached. 74 ServerId string `json:"-" required:"true"` 75 } 76 77 // Detach is a method to unmount a disk from a specified cloud server. 78 func Detach(c *golangsdk.ServiceClient, volumeId string, opts DetachOpts) (*jobs.Job, error) { 79 url := detachURL(c, opts.ServerId, volumeId) 80 query, err := golangsdk.BuildQueryString(opts) 81 if err != nil { 82 return nil, err 83 } 84 url += query.String() 85 86 var r jobs.Job 87 _, err = c.DeleteWithResponse(url, &r, &golangsdk.RequestOpts{ 88 MoreHeaders: requestOpts.MoreHeaders, 89 }) 90 return &r, err 91 }