a lot of refactoring and fixes
This commit is contained in:
@@ -76,36 +76,6 @@ func parseComputeDisksToExtraDisks(disks []DiskRecord) []interface{} {
|
||||
return result
|
||||
}
|
||||
|
||||
func parseBootDiskSize(disks []DiskRecord) int {
|
||||
// this return value will be used to d.Set("boot_disk_size",) item of dataSourceCompute schema
|
||||
if len(disks) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
for _, value := range disks {
|
||||
if value.Type == "B" {
|
||||
return value.SizeMax
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func parseBootDiskId(disks []DiskRecord) uint {
|
||||
// this return value will be used to d.Set("boot_disk_id",) item of dataSourceCompute schema
|
||||
if len(disks) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
for _, value := range disks {
|
||||
if value.Type == "B" {
|
||||
return value.ID
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func findBootDisk(disks []DiskRecord) *DiskRecord {
|
||||
for _, d := range disks {
|
||||
if d.Type == "B" {
|
||||
|
||||
@@ -131,14 +131,15 @@ func resourceComputeCreate(ctx context.Context, d *schema.ResourceData, m interf
|
||||
cleanup := false
|
||||
defer func() {
|
||||
if cleanup {
|
||||
utilityComputeDetachDisks(ctx, d, m)
|
||||
urlValues := &url.Values{}
|
||||
urlValues.Add("computeId", d.Id())
|
||||
urlValues.Add("permanently", "1")
|
||||
urlValues.Add("detachDisks", "1")
|
||||
|
||||
if _, err := c.DecortAPICall(ctx, "POST", ComputeDeleteAPI, urlValues); err != nil {
|
||||
log.Errorf("resourceComputeCreate: could not delete compute after failed creation: %v", err)
|
||||
}
|
||||
d.SetId("")
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -313,16 +314,12 @@ func resourceComputeDelete(ctx context.Context, d *schema.ResourceData, m interf
|
||||
log.Debugf("resourceComputeDelete: called for Compute name %s, RG ID %d",
|
||||
d.Get("name").(string), d.Get("rg_id").(int))
|
||||
|
||||
if err := utilityComputeDetachDisks(ctx, d, m); err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
|
||||
c := m.(*controller.ControllerCfg)
|
||||
|
||||
params := &url.Values{}
|
||||
params.Add("computeId", d.Id())
|
||||
params.Add("permanently", "1")
|
||||
// TODO: this is for the upcoming API update - params.Add("detachdisks", "1")
|
||||
params.Add("detachDisks", "1")
|
||||
|
||||
if _, err := c.DecortAPICall(ctx, "POST", ComputeDeleteAPI, params); err != nil {
|
||||
return diag.FromErr(err)
|
||||
@@ -331,21 +328,6 @@ func resourceComputeDelete(ctx context.Context, d *schema.ResourceData, m interf
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceComputeExists(ctx context.Context, d *schema.ResourceData, m interface{}) (bool, error) {
|
||||
// Reminder: according to Terraform rules, this function should not modify its ResourceData argument
|
||||
log.Debugf("resourceComputeExist: called for Compute name %s, RG ID %d",
|
||||
d.Get("name").(string), d.Get("rg_id").(int))
|
||||
|
||||
compFacts, err := utilityComputeCheckPresence(ctx, d, m)
|
||||
if compFacts == "" {
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func ResourceCompute() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
SchemaVersion: 1,
|
||||
|
||||
@@ -308,45 +308,3 @@ func utilityComputeCheckPresence(ctx context.Context, d *schema.ResourceData, m
|
||||
|
||||
return "", nil // there should be no error if Compute does not exist
|
||||
}
|
||||
|
||||
func utilityComputeDetachDisks(ctx context.Context, d *schema.ResourceData, m interface{}) error {
|
||||
compFacts, err := utilityComputeCheckPresence(ctx, d, m)
|
||||
if compFacts == "" {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// the target Compute does not exist - in this case according to Terraform best practice
|
||||
// we exit from Destroy method without error
|
||||
return nil
|
||||
}
|
||||
|
||||
c := m.(*controller.ControllerCfg)
|
||||
|
||||
model := ComputeGetResp{}
|
||||
log.Debugf("resourceComputeDelete: ready to unmarshal string %s", compFacts)
|
||||
err = json.Unmarshal([]byte(compFacts), &model)
|
||||
|
||||
if err != nil && len(model.Disks) > 0 {
|
||||
// prepare to detach data disks from compute - do it only if compFacts unmarshalled
|
||||
// properly and the resulting model contains non-empty Disks list
|
||||
for _, diskFacts := range model.Disks {
|
||||
if diskFacts.Type == "B" {
|
||||
// boot disk is never detached on compute delete
|
||||
continue
|
||||
}
|
||||
|
||||
log.Debugf("utilityComputeDetachDisks: ready to detach data disk ID %d from compute ID %s", diskFacts.ID, d.Id())
|
||||
|
||||
detachParams := &url.Values{}
|
||||
detachParams.Add("computeId", d.Id())
|
||||
detachParams.Add("diskId", fmt.Sprintf("%d", diskFacts.ID))
|
||||
|
||||
if _, err := c.DecortAPICall(ctx, "POST", ComputeDiskDetachAPI, detachParams); err != nil {
|
||||
// We do not fail compute deletion on data disk detach errors
|
||||
log.Errorf("utilityComputeDetachDisks: error when detaching Disk ID %d: %s", diskFacts.ID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user