Add context to DecortAPICall method
This commit is contained in:
@@ -196,7 +196,7 @@ func flattenCompute(d *schema.ResourceData, compFacts string) error {
|
||||
}
|
||||
|
||||
func dataSourceComputeRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
|
||||
compFacts, err := utilityComputeCheckPresence(d, m)
|
||||
compFacts, err := utilityComputeCheckPresence(ctx, d, m)
|
||||
if compFacts == "" {
|
||||
// if empty string is returned from utilityComputeCheckPresence then there is no
|
||||
// such Compute and err tells so - just return it to the calling party
|
||||
|
||||
@@ -112,7 +112,7 @@ func resourceComputeCreate(ctx context.Context, d *schema.ResourceData, m interf
|
||||
}
|
||||
}
|
||||
|
||||
apiResp, err := c.DecortAPICall("POST", computeCreateAPI, urlValues)
|
||||
apiResp, err := c.DecortAPICall(ctx, "POST", computeCreateAPI, urlValues)
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
@@ -124,41 +124,26 @@ func resourceComputeCreate(ctx context.Context, d *schema.ResourceData, m interf
|
||||
log.Debugf("resourceComputeCreate: new simple Compute ID %d, name %s created", compId, d.Get("name").(string))
|
||||
|
||||
// Configure data disks if any
|
||||
extraDisksOk := true
|
||||
argVal, argSet = d.GetOk("extra_disks")
|
||||
if argSet && argVal.(*schema.Set).Len() > 0 {
|
||||
// urlValues.Add("desc", argVal.(string))
|
||||
log.Debugf("resourceComputeCreate: calling utilityComputeExtraDisksConfigure to attach %d extra disk(s)", argVal.(*schema.Set).Len())
|
||||
err = utilityComputeExtraDisksConfigure(d, m, false) // do_delta=false, as we are working on a new compute
|
||||
err = utilityComputeExtraDisksConfigure(ctx, d, m, false) // do_delta=false, as we are working on a new compute
|
||||
if err != nil {
|
||||
log.Errorf("resourceComputeCreate: error when attaching extra disk(s) to a new Compute ID %d: %v", compId, err)
|
||||
extraDisksOk = false
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
}
|
||||
if extraDisksOk {
|
||||
d.SetPartial("extra_disks")
|
||||
}
|
||||
|
||||
// Configure external networks if any
|
||||
netsOk := true
|
||||
argVal, argSet = d.GetOk("network")
|
||||
if argSet && argVal.(*schema.Set).Len() > 0 {
|
||||
log.Debugf("resourceComputeCreate: calling utilityComputeNetworksConfigure to attach %d network(s)", argVal.(*schema.Set).Len())
|
||||
err = utilityComputeNetworksConfigure(d, m, false) // do_delta=false, as we are working on a new compute
|
||||
err = utilityComputeNetworksConfigure(ctx, d, m, false) // do_delta=false, as we are working on a new compute
|
||||
if err != nil {
|
||||
log.Errorf("resourceComputeCreate: error when attaching networks to a new Compute ID %d: %s", compId, err)
|
||||
netsOk = false
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
}
|
||||
if netsOk {
|
||||
// there were no errors reported when configuring networks
|
||||
d.SetPartial("network")
|
||||
}
|
||||
|
||||
if extraDisksOk && netsOk {
|
||||
// if there were no errors in setting any of the subresources, we may leave Partial mode
|
||||
d.Partial(false)
|
||||
}
|
||||
|
||||
// Note bene: we created compute in a STOPPED state (this is required to properly attach 1st network interface),
|
||||
// now we need to start it before we report the sequence complete
|
||||
@@ -166,7 +151,7 @@ func resourceComputeCreate(ctx context.Context, d *schema.ResourceData, m interf
|
||||
reqValues := &url.Values{}
|
||||
reqValues.Add("computeId", fmt.Sprintf("%d", compId))
|
||||
log.Debugf("resourceComputeCreate: starting Compute ID %d after completing its resource configuration", compId)
|
||||
if _, err := c.DecortAPICall("POST", ComputeStartAPI, reqValues); err != nil {
|
||||
if _, err := c.DecortAPICall(ctx, "POST", ComputeStartAPI, reqValues); err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
}
|
||||
@@ -184,7 +169,7 @@ func resourceComputeRead(ctx context.Context, d *schema.ResourceData, m interfac
|
||||
log.Debugf("resourceComputeRead: called for Compute name %s, RG ID %d",
|
||||
d.Get("name").(string), d.Get("rg_id").(int))
|
||||
|
||||
compFacts, err := utilityComputeCheckPresence(d, m)
|
||||
compFacts, err := utilityComputeCheckPresence(ctx, d, m)
|
||||
if compFacts == "" {
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
@@ -222,8 +207,6 @@ func resourceComputeUpdate(ctx context.Context, d *schema.ResourceData, m interf
|
||||
doUpdate := false
|
||||
params.Add("computeId", d.Id())
|
||||
|
||||
d.Partial(true)
|
||||
|
||||
oldCpu, newCpu := d.GetChange("cpu")
|
||||
if oldCpu.(int) != newCpu.(int) {
|
||||
params.Add("cpu", fmt.Sprintf("%d", newCpu.(int)))
|
||||
@@ -245,13 +228,10 @@ func resourceComputeUpdate(ctx context.Context, d *schema.ResourceData, m interf
|
||||
oldCpu.(int), newCpu.(int),
|
||||
oldRam.(int), newRam.(int))
|
||||
params.Add("force", "true")
|
||||
_, err := c.DecortAPICall("POST", ComputeResizeAPI, params)
|
||||
_, err := c.DecortAPICall(ctx, "POST", ComputeResizeAPI, params)
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
|
||||
d.SetPartial("cpu")
|
||||
d.SetPartial("ram")
|
||||
}
|
||||
|
||||
// 2. Resize (grow) Boot disk
|
||||
@@ -262,23 +242,22 @@ func resourceComputeUpdate(ctx context.Context, d *schema.ResourceData, m interf
|
||||
bdsParams.Add("size", fmt.Sprintf("%d", newSize.(int)))
|
||||
log.Debugf("resourceComputeUpdate: compute ID %s, boot disk ID %d resize %d -> %d",
|
||||
d.Id(), d.Get("boot_disk_id").(int), oldSize.(int), newSize.(int))
|
||||
_, err := c.DecortAPICall("POST", DisksResizeAPI, bdsParams)
|
||||
_, err := c.DecortAPICall(ctx, "POST", DisksResizeAPI, bdsParams)
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
d.SetPartial("boot_disk_size")
|
||||
} else if oldSize.(int) > newSize.(int) {
|
||||
log.Warnf("resourceComputeUpdate: compute ID %s - shrinking boot disk is not allowed", d.Id())
|
||||
}
|
||||
|
||||
// 3. Calculate and apply changes to data disks
|
||||
err := utilityComputeExtraDisksConfigure(d, m, true) // pass do_delta = true to apply changes, if any
|
||||
err := utilityComputeExtraDisksConfigure(ctx, d, m, true) // pass do_delta = true to apply changes, if any
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
|
||||
// 4. Calculate and apply changes to network connections
|
||||
err = utilityComputeNetworksConfigure(d, m, true) // pass do_delta = true to apply changes, if any
|
||||
err = utilityComputeNetworksConfigure(ctx, d, m, true) // pass do_delta = true to apply changes, if any
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
@@ -287,11 +266,11 @@ func resourceComputeUpdate(ctx context.Context, d *schema.ResourceData, m interf
|
||||
params := &url.Values{}
|
||||
params.Add("computeId", d.Id())
|
||||
if d.Get("started").(bool) {
|
||||
if _, err := c.DecortAPICall("POST", ComputeStartAPI, params); err != nil {
|
||||
if _, err := c.DecortAPICall(ctx, "POST", ComputeStartAPI, params); err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
} else {
|
||||
if _, err := c.DecortAPICall("POST", ComputeStopAPI, params); err != nil {
|
||||
if _, err := c.DecortAPICall(ctx, "POST", ComputeStopAPI, params); err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
}
|
||||
@@ -310,7 +289,7 @@ 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))
|
||||
|
||||
compFacts, err := utilityComputeCheckPresence(d, m)
|
||||
compFacts, err := utilityComputeCheckPresence(ctx, d, m)
|
||||
if compFacts == "" {
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
@@ -340,7 +319,7 @@ func resourceComputeDelete(ctx context.Context, d *schema.ResourceData, m interf
|
||||
detachParams.Add("computeId", d.Id())
|
||||
detachParams.Add("diskId", fmt.Sprintf("%d", diskFacts.ID))
|
||||
|
||||
_, err = c.DecortAPICall("POST", ComputeDiskDetachAPI, detachParams)
|
||||
_, err = c.DecortAPICall(ctx, "POST", ComputeDiskDetachAPI, detachParams)
|
||||
if err != nil {
|
||||
// We do not fail compute deletion on data disk detach errors
|
||||
log.Errorf("resourceComputeDelete: error when detaching Disk ID %d: %s", diskFacts.ID, err)
|
||||
@@ -353,7 +332,7 @@ func resourceComputeDelete(ctx context.Context, d *schema.ResourceData, m interf
|
||||
params.Add("permanently", "1")
|
||||
// TODO: this is for the upcoming API update - params.Add("detachdisks", "1")
|
||||
|
||||
_, err = c.DecortAPICall("POST", ComputeDeleteAPI, params)
|
||||
_, err = c.DecortAPICall(ctx, "POST", ComputeDeleteAPI, params)
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
@@ -361,12 +340,12 @@ func resourceComputeDelete(ctx context.Context, d *schema.ResourceData, m interf
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceComputeExists(d *schema.ResourceData, m interface{}) (bool, error) {
|
||||
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(d, m)
|
||||
compFacts, err := utilityComputeCheckPresence(ctx, d, m)
|
||||
if compFacts == "" {
|
||||
if err != nil {
|
||||
return false, err
|
||||
@@ -384,7 +363,6 @@ func ResourceCompute() *schema.Resource {
|
||||
ReadContext: resourceComputeRead,
|
||||
UpdateContext: resourceComputeUpdate,
|
||||
DeleteContext: resourceComputeDelete,
|
||||
Exists: resourceComputeExists,
|
||||
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
|
||||
@@ -32,6 +32,7 @@ Documentation: https://github.com/rudecs/terraform-provider-decort/wiki
|
||||
package kvmvm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
@@ -43,7 +44,7 @@ import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
)
|
||||
|
||||
func utilityComputeExtraDisksConfigure(d *schema.ResourceData, m interface{}, do_delta bool) error {
|
||||
func utilityComputeExtraDisksConfigure(ctx context.Context, d *schema.ResourceData, m interface{}, do_delta bool) error {
|
||||
// d is filled with data according to computeResource schema, so extra disks config is retrieved via "extra_disks" key
|
||||
// If do_delta is true, this function will identify changes between new and existing specs for extra disks and try to
|
||||
// update compute configuration accordingly
|
||||
@@ -71,7 +72,7 @@ func utilityComputeExtraDisksConfigure(d *schema.ResourceData, m interface{}, do
|
||||
urlValues := &url.Values{}
|
||||
urlValues.Add("computeId", d.Id())
|
||||
urlValues.Add("diskId", fmt.Sprintf("%d", disk.(int)))
|
||||
_, err := c.DecortAPICall("POST", ComputeDiskAttachAPI, urlValues)
|
||||
_, err := c.DecortAPICall(ctx, "POST", ComputeDiskAttachAPI, urlValues)
|
||||
if err != nil {
|
||||
// failed to attach extra disk - partial resource update
|
||||
apiErrCount++
|
||||
@@ -94,7 +95,7 @@ func utilityComputeExtraDisksConfigure(d *schema.ResourceData, m interface{}, do
|
||||
urlValues := &url.Values{}
|
||||
urlValues.Add("computeId", d.Id())
|
||||
urlValues.Add("diskId", fmt.Sprintf("%d", diskId.(int)))
|
||||
_, err := c.DecortAPICall("POST", ComputeDiskDetachAPI, urlValues)
|
||||
_, err := c.DecortAPICall(ctx, "POST", ComputeDiskDetachAPI, urlValues)
|
||||
if err != nil {
|
||||
// failed to detach disk - there will be partial resource update
|
||||
log.Errorf("utilityComputeExtraDisksConfigure: failed to detach disk ID %d from Compute ID %s: %s", diskId.(int), d.Id(), err)
|
||||
@@ -109,7 +110,7 @@ func utilityComputeExtraDisksConfigure(d *schema.ResourceData, m interface{}, do
|
||||
urlValues := &url.Values{}
|
||||
urlValues.Add("computeId", d.Id())
|
||||
urlValues.Add("diskId", fmt.Sprintf("%d", diskId.(int)))
|
||||
_, err := c.DecortAPICall("POST", ComputeDiskAttachAPI, urlValues)
|
||||
_, err := c.DecortAPICall(ctx, "POST", ComputeDiskAttachAPI, urlValues)
|
||||
if err != nil {
|
||||
// failed to attach disk - there will be partial resource update
|
||||
log.Errorf("utilityComputeExtraDisksConfigure: failed to attach disk ID %d to Compute ID %s: %s", diskId.(int), d.Id(), err)
|
||||
@@ -127,7 +128,7 @@ func utilityComputeExtraDisksConfigure(d *schema.ResourceData, m interface{}, do
|
||||
return nil
|
||||
}
|
||||
|
||||
func utilityComputeNetworksConfigure(d *schema.ResourceData, m interface{}, do_delta bool) error {
|
||||
func utilityComputeNetworksConfigure(ctx context.Context, d *schema.ResourceData, m interface{}, do_delta bool) error {
|
||||
// "d" is filled with data according to computeResource schema, so extra networks config is retrieved via "network" key
|
||||
// If do_delta is true, this function will identify changes between new and existing specs for network and try to
|
||||
// update compute configuration accordingly
|
||||
@@ -156,7 +157,7 @@ func utilityComputeNetworksConfigure(d *schema.ResourceData, m interface{}, do_d
|
||||
if ipSet {
|
||||
urlValues.Add("ipAddr", ipaddr.(string))
|
||||
}
|
||||
_, err := c.DecortAPICall("POST", ComputeNetAttachAPI, urlValues)
|
||||
_, err := c.DecortAPICall(ctx, "POST", ComputeNetAttachAPI, urlValues)
|
||||
if err != nil {
|
||||
// failed to attach network - partial resource update
|
||||
apiErrCount++
|
||||
@@ -180,7 +181,7 @@ func utilityComputeNetworksConfigure(d *schema.ResourceData, m interface{}, do_d
|
||||
urlValues.Add("computeId", d.Id())
|
||||
urlValues.Add("ipAddr", net_data["ip_address"].(string))
|
||||
urlValues.Add("mac", net_data["mac"].(string))
|
||||
_, err := c.DecortAPICall("POST", ComputeNetDetachAPI, urlValues)
|
||||
_, err := c.DecortAPICall(ctx, "POST", ComputeNetDetachAPI, urlValues)
|
||||
if err != nil {
|
||||
// failed to detach this network - there will be partial resource update
|
||||
log.Errorf("utilityComputeNetworksConfigure: failed to detach net ID %d of type %s from Compute ID %s: %s",
|
||||
@@ -201,7 +202,7 @@ func utilityComputeNetworksConfigure(d *schema.ResourceData, m interface{}, do_d
|
||||
if net_data["ip_address"].(string) != "" {
|
||||
urlValues.Add("ipAddr", net_data["ip_address"].(string))
|
||||
}
|
||||
_, err := c.DecortAPICall("POST", ComputeNetAttachAPI, urlValues)
|
||||
_, err := c.DecortAPICall(ctx, "POST", ComputeNetAttachAPI, urlValues)
|
||||
if err != nil {
|
||||
// failed to attach this network - there will be partial resource update
|
||||
log.Errorf("utilityComputeNetworksConfigure: failed to attach net ID %d of type %s to Compute ID %s: %s",
|
||||
@@ -220,7 +221,7 @@ func utilityComputeNetworksConfigure(d *schema.ResourceData, m interface{}, do_d
|
||||
return nil
|
||||
}
|
||||
|
||||
func utilityComputeCheckPresence(d *schema.ResourceData, m interface{}) (string, error) {
|
||||
func utilityComputeCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (string, error) {
|
||||
// This function tries to locate Compute by one of the following approaches:
|
||||
// - if compute_id is specified - locate by compute ID
|
||||
// - if compute_name is specified - locate by a combination of compute name and resource
|
||||
@@ -255,7 +256,7 @@ func utilityComputeCheckPresence(d *schema.ResourceData, m interface{}) (string,
|
||||
// compute ID is specified, try to get compute instance straight by this ID
|
||||
log.Debugf("utilityComputeCheckPresence: locating compute by its ID %d", theId)
|
||||
urlValues.Add("computeId", fmt.Sprintf("%d", theId))
|
||||
computeFacts, err := c.DecortAPICall("POST", ComputeGetAPI, urlValues)
|
||||
computeFacts, err := c.DecortAPICall(ctx, "POST", ComputeGetAPI, urlValues)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -275,7 +276,7 @@ func utilityComputeCheckPresence(d *schema.ResourceData, m interface{}) (string,
|
||||
}
|
||||
|
||||
urlValues.Add("rgId", fmt.Sprintf("%d", rgId))
|
||||
apiResp, err := c.DecortAPICall("POST", RgListComputesAPI, urlValues)
|
||||
apiResp, err := c.DecortAPICall(ctx, "POST", RgListComputesAPI, urlValues)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -297,7 +298,7 @@ func utilityComputeCheckPresence(d *schema.ResourceData, m interface{}) (string,
|
||||
// we found the Compute we need - now get detailed information via compute/get API
|
||||
cgetValues := &url.Values{}
|
||||
cgetValues.Add("computeId", fmt.Sprintf("%d", item.ID))
|
||||
apiResp, err = c.DecortAPICall("POST", ComputeGetAPI, cgetValues)
|
||||
apiResp, err = c.DecortAPICall(ctx, "POST", ComputeGetAPI, cgetValues)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user