diff --git a/decort/data_source_compute.go b/decort/data_source_compute.go index 7fd2cc1..0ec6044 100644 --- a/decort/data_source_compute.go +++ b/decort/data_source_compute.go @@ -43,18 +43,14 @@ func parseComputeDisksToExtraDisks(disks []DiskRecord) []interface{} { length := len(disks) log.Debugf("parseComputeDisksToExtraDisks: called for %d disks", length) - if length == 1 && disks[0].Type == "B" { + if length == 0 || ( length == 1 && disks[0].Type == "B" ) { + // the disk list is empty (which is kind of strange - diskless compute?), or // there is only one disk in the list and it is a boot disk; - // as we skip boot disks, the result will be of 0 length - length = 0 + // as we skip boot disks, the result will be of 0 length anyway + return make([]interface{}, 0) } result := make([]interface{}, length-1) - - if length == 0 { - return result - } - idx := 0 for _, value := range disks { if value.Type == "B" { diff --git a/decort/resource_disk.go b/decort/resource_disk.go index fbf8182..76cc3b6 100644 --- a/decort/resource_disk.go +++ b/decort/resource_disk.go @@ -119,7 +119,7 @@ func resourceDiskUpdate(d *schema.ResourceData, m interface{}) error { } func resourceDiskDelete(d *schema.ResourceData, m interface{}) error { - // NOTE: this function tries to destroy target Disk "permanently", so + // NOTE: this function tries to detach and destroy target Disk "permanently", so // there is no way to restore it. // If, however, the disk is attached to a compute, the method will // fail (by failing the underpinning DECORt API call, which is issued with detach=false) @@ -135,7 +135,13 @@ func resourceDiskDelete(d *schema.ResourceData, m interface{}) error { params := &url.Values{} params.Add("diskId", d.Id()) - params.Add("detach", "false") + // NOTE: we are not force-detaching disk from a compute (if attached) this protecting + // data that may be on that disk from destruction. + // However, this may change in the future, as TF state management logic may want + // to delete disk resource BEFORE it is detached from compute instance, and, while + // perfectly OK from data preservation viewpoint, this is breaking expected TF workflow + // in the eyes of an experienced TF user + params.Add("detach", "false") params.Add("permanently", "true") controller := m.(*ControllerCfg) diff --git a/decort/utility_compute.go b/decort/utility_compute.go index 7490061..0437581 100644 --- a/decort/utility_compute.go +++ b/decort/utility_compute.go @@ -86,10 +86,7 @@ func (ctrl *ControllerCfg) utilityComputeExtraDisksConfigure(d *schema.ResourceD return nil } - attach_list := make([]int, 0, MaxExtraDisksPerCompute) - detach_list := make([]int, 0, MaxExtraDisksPerCompute) - attIdx := 0 - detIdx := 0 + var attach_list, detach_list []int match := false for _, oDisk := range old_disks { @@ -101,8 +98,7 @@ func (ctrl *ControllerCfg) utilityComputeExtraDisksConfigure(d *schema.ResourceD } } if !match { - detach_list[detIdx] = oDisk.(int) - detIdx++ + detach_list = append(detach_list, oDisk.(int)) } } log.Debugf("utilityComputeExtraDisksConfigure: detach list has %d items for Compute ID %s", len(detach_list), d.Id()) @@ -116,8 +112,7 @@ func (ctrl *ControllerCfg) utilityComputeExtraDisksConfigure(d *schema.ResourceD } } if !match { - attach_list[attIdx] = nDisk.(int) - attIdx++ + attach_list = append(attach_list, nDisk.(int)) } } log.Debugf("utilityComputeExtraDisksConfigure: attach list has %d items for Compute ID %s", len(attach_list), d.Id()) @@ -213,10 +208,7 @@ func (ctrl *ControllerCfg) utilityComputeNetworksConfigure(d *schema.ResourceDat return nil } - attachList := make([]ComputeNetMgmtRecord, 0, MaxNetworksPerCompute) - detachList := make([]ComputeNetMgmtRecord, 0, MaxNetworksPerCompute) - attIdx := 0 - detIdx := 0 + var attachList, detachList []ComputeNetMgmtRecord match := false for _, oRunner := range oldNets { @@ -230,11 +222,13 @@ func (ctrl *ControllerCfg) utilityComputeNetworksConfigure(d *schema.ResourceDat } } if !match { - detachList[attIdx].ID = oSpecs["net_id"].(int) - detachList[detIdx].Type = oSpecs["net_type"].(string) - detachList[detIdx].IPAddress = oSpecs["ip_address"].(string) - detachList[detIdx].MAC = oSpecs["mac"].(string) - detIdx++ + newItem := ComputeNetMgmtRecord{ + ID: oSpecs["net_id"].(int), + Type: oSpecs["net_type"].(string), + IPAddress: oSpecs["ip_address"].(string), + MAC: oSpecs["mac"].(string), + } + detachList = append(detachList, newItem) } } log.Debugf("utilityComputeNetworksConfigure: detach list has %d items for Compute ID %s", len(detachList), d.Id()) @@ -250,14 +244,16 @@ func (ctrl *ControllerCfg) utilityComputeNetworksConfigure(d *schema.ResourceDat } } if !match { - attachList[attIdx].ID = nSpecs["net_id"].(int) - attachList[detIdx].Type = nSpecs["net_type"].(string) + newItem := ComputeNetMgmtRecord{ + ID: nSpecs["net_id"].(int), + Type: nSpecs["net_type"].(string), + } if nSpecs["ip_address"] != nil { - attachList[detIdx].IPAddress = nSpecs["ip_address"].(string) + newItem.IPAddress = nSpecs["ip_address"].(string) } else { - attachList[detIdx].IPAddress = "" // make sure it is empty, if not coming from the schema + newItem.IPAddress = "" // make sure it is empty, if not coming from the schema } - attIdx++ + attachList = append(attachList, newItem) } } log.Debugf("utilityComputeNetworksConfigure: attach list has %d items for Compute ID %s", len(attachList), d.Id())