Add context to DecortAPICall method
This commit is contained in:
@@ -97,7 +97,7 @@ func flattenVins(d *schema.ResourceData, vins_facts string) diag.Diagnostics {
|
||||
}
|
||||
|
||||
func dataSourceVinsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
|
||||
vinsFacts, err := utilityVinsCheckPresence(d, m)
|
||||
vinsFacts, err := utilityVinsCheckPresence(ctx, d, m)
|
||||
if vinsFacts == "" {
|
||||
// if empty string is returned from utilityVinsCheckPresence then there is no
|
||||
// such ViNS and err tells so - just return it to the calling party
|
||||
|
||||
@@ -67,7 +67,7 @@ func flattenVinsList(vl VinsList) []map[string]interface{} {
|
||||
}
|
||||
|
||||
func dataSourceVinsListRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
|
||||
vinsList, err := utilityVinsListCheckPresence(d, m)
|
||||
vinsList, err := utilityVinsListCheckPresence(ctx, d, m)
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ func resourceVinsCreate(ctx context.Context, d *schema.ResourceData, m interface
|
||||
urlValues.Add("desc", argVal.(string))
|
||||
}
|
||||
|
||||
apiResp, err := c.DecortAPICall("POST", apiToCall, urlValues)
|
||||
apiResp, err := c.DecortAPICall(ctx, "POST", apiToCall, urlValues)
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
@@ -133,7 +133,7 @@ func resourceVinsCreate(ctx context.Context, d *schema.ResourceData, m interface
|
||||
}
|
||||
|
||||
func resourceVinsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
|
||||
vinsFacts, err := utilityVinsCheckPresence(d, m)
|
||||
vinsFacts, err := utilityVinsCheckPresence(ctx, d, m)
|
||||
if vinsFacts == "" {
|
||||
// if empty string is returned from utilityVinsCheckPresence then there is no
|
||||
// such ViNS and err tells so - just return it to the calling party
|
||||
@@ -161,7 +161,7 @@ func resourceVinsUpdate(ctx context.Context, d *schema.ResourceData, m interface
|
||||
|
||||
if oldExtNetId.(int) > 0 {
|
||||
// there was preexisting external net connection - disconnect ViNS
|
||||
_, err := c.DecortAPICall("POST", VinsExtNetDisconnectAPI, extnetParams)
|
||||
_, err := c.DecortAPICall(ctx, "POST", VinsExtNetDisconnectAPI, extnetParams)
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
@@ -170,7 +170,7 @@ func resourceVinsUpdate(ctx context.Context, d *schema.ResourceData, m interface
|
||||
if newExtNedId.(int) > 0 {
|
||||
// new external network connection requested - connect ViNS
|
||||
extnetParams.Add("netId", fmt.Sprintf("%d", newExtNedId.(int)))
|
||||
_, err := c.DecortAPICall("POST", VinsExtNetConnectAPI, extnetParams)
|
||||
_, err := c.DecortAPICall(ctx, "POST", VinsExtNetConnectAPI, extnetParams)
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
@@ -186,7 +186,7 @@ func resourceVinsDelete(ctx context.Context, d *schema.ResourceData, m interface
|
||||
log.Debugf("resourceVinsDelete: called for ViNS ID / name %s / %s, Account ID %d, RG ID %d",
|
||||
d.Id(), d.Get("name").(string), d.Get("account_id").(int), d.Get("rg_id").(int))
|
||||
|
||||
vinsFacts, err := utilityVinsCheckPresence(d, m)
|
||||
vinsFacts, err := utilityVinsCheckPresence(ctx, d, m)
|
||||
if vinsFacts == "" {
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
@@ -202,7 +202,7 @@ func resourceVinsDelete(ctx context.Context, d *schema.ResourceData, m interface
|
||||
params.Add("permanently", "1") // delete ViNS immediately bypassing recycle bin
|
||||
|
||||
c := m.(*controller.ControllerCfg)
|
||||
_, err = c.DecortAPICall("POST", VinsDeleteAPI, params)
|
||||
_, err = c.DecortAPICall(ctx, "POST", VinsDeleteAPI, params)
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
@@ -210,12 +210,12 @@ func resourceVinsDelete(ctx context.Context, d *schema.ResourceData, m interface
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceVinsExists(d *schema.ResourceData, m interface{}) (bool, error) {
|
||||
func resourceVinsExists(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("resourceVinsExists: called for ViNS name %s, Account ID %d, RG ID %d",
|
||||
d.Get("name").(string), d.Get("account_id").(int), d.Get("rg_id").(int))
|
||||
|
||||
vinsFacts, err := utilityVinsCheckPresence(d, m)
|
||||
vinsFacts, err := utilityVinsCheckPresence(ctx, d, m)
|
||||
if vinsFacts == "" {
|
||||
if err != nil {
|
||||
return false, err
|
||||
@@ -305,7 +305,6 @@ func ResourceVins() *schema.Resource {
|
||||
ReadContext: resourceVinsRead,
|
||||
UpdateContext: resourceVinsUpdate,
|
||||
DeleteContext: resourceVinsDelete,
|
||||
Exists: resourceVinsExists,
|
||||
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
|
||||
@@ -32,6 +32,7 @@ Documentation: https://github.com/rudecs/terraform-provider-decort/wiki
|
||||
package vins
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
@@ -45,7 +46,7 @@ import (
|
||||
|
||||
// On success this function returns a string, as returned by API vins/get, which could be unmarshalled
|
||||
// into VinsGetResp structure
|
||||
func utilityVinsCheckPresence(d *schema.ResourceData, m interface{}) (string, error) {
|
||||
func utilityVinsCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (string, error) {
|
||||
// This function tries to locate ViNS by one of the following algorithms depending
|
||||
// on the parameters passed:
|
||||
// - if resource group ID is specified -> it looks for a ViNS at the RG level
|
||||
@@ -80,7 +81,7 @@ func utilityVinsCheckPresence(d *schema.ResourceData, m interface{}) (string, er
|
||||
// ViNS ID is specified, try to get compute instance straight by this ID
|
||||
log.Debugf("utilityVinsCheckPresence: locating ViNS by its ID %d", theId)
|
||||
urlValues.Add("vinsId", fmt.Sprintf("%d", theId))
|
||||
vinsFacts, err := c.DecortAPICall("POST", VinsGetAPI, urlValues)
|
||||
vinsFacts, err := c.DecortAPICall(ctx, "POST", VinsGetAPI, urlValues)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -111,7 +112,7 @@ func utilityVinsCheckPresence(d *schema.ResourceData, m interface{}) (string, er
|
||||
urlValues.Add("accountId", fmt.Sprintf("%d", accountId.(int)))
|
||||
}
|
||||
|
||||
apiResp, err := c.DecortAPICall("POST", VinsSearchAPI, urlValues)
|
||||
apiResp, err := c.DecortAPICall(ctx, "POST", VinsSearchAPI, urlValues)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -140,7 +141,7 @@ func utilityVinsCheckPresence(d *schema.ResourceData, m interface{}) (string, er
|
||||
// manage ViNS, so we have to get detailed info by calling API vins/get
|
||||
rqValues := &url.Values{}
|
||||
rqValues.Add("vinsId", fmt.Sprintf("%d", item.ID))
|
||||
vinsGetResp, err := c.DecortAPICall("POST", VinsGetAPI, rqValues)
|
||||
vinsGetResp, err := c.DecortAPICall(ctx, "POST", VinsGetAPI, rqValues)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ Documentation: https://github.com/rudecs/terraform-provider-decort/wiki
|
||||
package vins
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"strconv"
|
||||
@@ -42,7 +43,7 @@ import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
)
|
||||
|
||||
func utilityVinsListCheckPresence(d *schema.ResourceData, m interface{}) (VinsList, error) {
|
||||
func utilityVinsListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (VinsList, error) {
|
||||
vinsList := VinsList{}
|
||||
c := m.(*controller.ControllerCfg)
|
||||
urlValues := &url.Values{}
|
||||
@@ -58,7 +59,7 @@ func utilityVinsListCheckPresence(d *schema.ResourceData, m interface{}) (VinsLi
|
||||
}
|
||||
|
||||
log.Debugf("utilityVinsListCheckPresence")
|
||||
vinsListRaw, err := c.DecortAPICall("POST", VinsListAPI, urlValues)
|
||||
vinsListRaw, err := c.DecortAPICall(ctx, "POST", VinsListAPI, urlValues)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user