Implement name update for disk resource

rc-1.0
Sergey Shubin svs1370 4 years ago
parent 5fc08e9dde
commit 3aefa580dc

@ -499,6 +499,11 @@ type DisksListResp []DiskRecord
// //
const DisksResizeAPI = "/restmachine/cloudapi/disks/resize2" const DisksResizeAPI = "/restmachine/cloudapi/disks/resize2"
//
// structures related to /cloudapi/disks/resize
//
const DisksRenameAPI = "/restmachine/cloudapi/disks/rename"
// //
// structures related to /cloudapi/disks/delete // structures related to /cloudapi/disks/delete
// //

@ -85,37 +85,53 @@ func resourceDiskRead(d *schema.ResourceData, m interface{}) error {
} }
func resourceDiskUpdate(d *schema.ResourceData, m interface{}) error { func resourceDiskUpdate(d *schema.ResourceData, m interface{}) error {
// update will only change Disk size and, to keep data safe, will not allow // Update will only change the following attributes of the disk:
// shrinking disk. // - Size; to keep data safe, shrinking disk is not allowed.
// Attempt to reduce disk size will throw an error // - Name
log.Debugf("resourceDiskUpdate: called for Disk ID / name % d / %s, Account ID %d", //
d.Get("disk_id").(int), d.Get("name").(string), d.Get("account_id").(int)) // Attempt to change disk type will throw an error and mark disk
// resource as partially updated
log.Debugf("resourceDiskUpdate: called for Disk ID / name %s / %s, Account ID %d",
d.Id(), d.Get("name").(string), d.Get("account_id").(int))
d.Partial(true) d.Partial(true)
controller := m.(*ControllerCfg)
oldSize, newSize := d.GetChange("size") oldSize, newSize := d.GetChange("size")
if oldSize.(int) > newSize.(int) { if oldSize.(int) < newSize.(int) {
return fmt.Errorf("resourceDiskUpdate: Disk ID %d - shrinking disk size from %d to %d not allowed", log.Debugf("resourceDiskUpdate: resizing disk ID %s - %d GB -> %d GB",
d.Get("disk_id").(int), oldSize.(int), newSize.(int)) d.Id(), oldSize.(int), newSize.(int))
sizeParams := &url.Values{}
sizeParams.Add("diskId", d.Id())
sizeParams.Add("size", fmt.Sprintf("%d", newSize.(int)))
_, err := controller.decortAPICall("POST", DisksResizeAPI, sizeParams)
if err != nil {
return err
}
d.SetPartial("size")
} else if oldSize.(int) > newSize.(int) {
return fmt.Errorf("resourceDiskUpdate: Disk ID %s - reducing disk size is not allowed", d.Id())
} }
if oldSize.(int) == newSize.(int) { oldName, newName := d.GetChange("name")
log.Debugf("resourceDiskUpdate: Disk ID %d - no size change required", d.Get("disk_id").(int)) if oldName.(string) != newName.(string) {
// and there is no need to re-read disk specs either log.Debugf("resourceDiskUpdate: renaming disk ID %d - %s -> %s",
d.Partial(false) d.Get("disk_id").(int), oldName.(string), newName.(string))
return nil renameParams := &url.Values{}
renameParams.Add("diskId", d.Id())
renameParams.Add("name", newName.(string))
_, err := controller.decortAPICall("POST", DisksRenameAPI, renameParams)
if err != nil {
return err
}
d.SetPartial("name")
} }
params := &url.Values{} oldType, newType := d.GetChange("type")
params.Add("diskId", d.Id()) if oldType.(string) != newType.(string) {
params.Add("size", fmt.Sprintf("%d", newSize.(int))) return fmt.Errorf("resourceDiskUpdate: Disk ID %s - changing type of existing disk not allowed", d.Id())
controller := m.(*ControllerCfg)
_, err := controller.decortAPICall("POST", DisksResizeAPI, params)
if err != nil {
return err
} }
d.SetPartial("size")
d.Partial(false) d.Partial(false)

Loading…
Cancel
Save