Skip to content

Commit

Permalink
fix: maintain provider context in resources and datasources (#581)
Browse files Browse the repository at this point in the history
We recently introduced equinix-sdk-go as a context-aware replacement SDK
for packngo and _also_ migrated some resources and datasources to use
context-aware CRUD lifecycle methods. In those parallel changes we
missed some opportunities to pass the terraform provider context into
the SDK.

This updates the provider to replace all non-testing usage of
`context.TODO()` and `context.Background()` with references to the
pre-existing terraform provider context.

Relates to #539
  • Loading branch information
ctreatma authored Feb 21, 2024
1 parent 6668e1d commit 070703c
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 19 deletions.
6 changes: 3 additions & 3 deletions equinix/data_source_metal_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func dataSourceMetalDevices() *schema.Resource {
return datalist.NewResource(dataListConfig)
}

func getDevices(d *schema.ResourceData, meta interface{}, extra map[string]interface{}) ([]interface{}, error) {
func getDevices(ctx context.Context, d *schema.ResourceData, meta interface{}, extra map[string]interface{}) ([]interface{}, error) {
client := meta.(*config.Config).NewMetalClientForSDK(d)
projectID := extra["project_id"].(string)
orgID := extra["organization_id"].(string)
Expand All @@ -68,7 +68,7 @@ func getDevices(d *schema.ResourceData, meta interface{}, extra map[string]inter

if len(projectID) > 0 {
query := client.DevicesApi.FindProjectDevices(
context.Background(), projectID).Include(deviceCommonIncludes)
ctx, projectID).Include(deviceCommonIncludes)
if len(search) > 0 {
query = query.Search(search)
}
Expand All @@ -77,7 +77,7 @@ func getDevices(d *schema.ResourceData, meta interface{}, extra map[string]inter

if len(orgID) > 0 {
query := client.DevicesApi.FindOrganizationDevices(
context.Background(), orgID).Include(deviceCommonIncludes)
ctx, orgID).Include(deviceCommonIncludes)
if len(search) > 0 {
query = query.Search(search)
}
Expand Down
3 changes: 2 additions & 1 deletion equinix/data_source_metal_plans.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package equinix

import (
"context"
"fmt"

"github.com/equinix/terraform-provider-equinix/internal/converters"
Expand All @@ -24,7 +25,7 @@ func dataSourceMetalPlans() *schema.Resource {
return datalist.NewResource(dataListConfig)
}

func getPlans(_ *schema.ResourceData, meta interface{}, extra map[string]interface{}) ([]interface{}, error) {
func getPlans(_ context.Context, _ *schema.ResourceData, meta interface{}, extra map[string]interface{}) ([]interface{}, error) {
client := meta.(*config.Config).Metal
opts := &packngo.ListOptions{
Includes: []string{"available_in", "available_in_metros"},
Expand Down
6 changes: 3 additions & 3 deletions equinix/helpers_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ func getPorts(ps []metalv1.Port) []map[string]interface{} {
return ret
}

func hwReservationStateRefreshFunc(client *metalv1.APIClient, reservationId, instanceId string) retry.StateRefreshFunc {
func hwReservationStateRefreshFunc(ctx context.Context, client *metalv1.APIClient, reservationId, instanceId string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
r, _, err := client.HardwareReservationsApi.FindHardwareReservationById(context.TODO(), reservationId).Include([]string{"device"}).Execute()
r, _, err := client.HardwareReservationsApi.FindHardwareReservationById(ctx, reservationId).Include([]string{"device"}).Execute()
state := deprovisioning
switch {
case err != nil:
Expand All @@ -135,7 +135,7 @@ func waitUntilReservationProvisionable(ctx context.Context, client *metalv1.APIC
stateConf := &retry.StateChangeConf{
Pending: []string{deprovisioning},
Target: []string{provisionable, reprovisioned},
Refresh: hwReservationStateRefreshFunc(client, reservationId, instanceId),
Refresh: hwReservationStateRefreshFunc(ctx, client, reservationId, instanceId),
Timeout: timeout,
Delay: delay,
MinTimeout: minTimeout,
Expand Down
2 changes: 1 addition & 1 deletion equinix/resource_metal_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ func resourceMetalDeviceCreate(ctx context.Context, d *schema.ResourceData, meta
func resourceMetalDeviceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*config.Config).NewMetalClientForSDK(d)

device, resp, err := client.DevicesApi.FindDeviceById(context.Background(), d.Id()).Include(deviceCommonIncludes).Execute()
device, resp, err := client.DevicesApi.FindDeviceById(ctx, d.Id()).Include(deviceCommonIncludes).Execute()
if err != nil {
err = equinix_errors.FriendlyErrorForMetalGo(err, resp)

Expand Down
4 changes: 2 additions & 2 deletions internal/datalist/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type ResourceConfig struct {
// Return all of the records on which the data list resource should operate.
// The `meta` argument is the same meta argument passed into the resource's Read
// function.
GetRecords func(d *schema.ResourceData, meta interface{}, extra map[string]interface{}) ([]interface{}, error)
GetRecords func(ctx context.Context, d *schema.ResourceData, meta interface{}, extra map[string]interface{}) ([]interface{}, error)

// Extra parameters to expose on the datasource alongside `filter` and `sort`.
ExtraQuerySchema map[string]*schema.Schema
Expand Down Expand Up @@ -89,7 +89,7 @@ func dataListResourceRead(config *ResourceConfig) schema.ReadContextFunc {
extra[attr] = d.Get(attr)
}

records, err := config.GetRecords(d, meta, extra)
records, err := config.GetRecords(ctx, d, meta, extra)
if err != nil {
return diag.Errorf("Unable to load records: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/metal/project_ssh_key/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (r *DataSource) Read(
)

// Use API client to list SSH keys
keysList, _, err := client.SSHKeysApi.FindProjectSSHKeys(context.Background(), projectID).Query(search).Execute()
keysList, _, err := client.SSHKeysApi.FindProjectSSHKeys(ctx, projectID).Query(search).Execute()
if err != nil {
err = equinix_errors.FriendlyError(err)
resp.Diagnostics.AddError(
Expand Down
8 changes: 4 additions & 4 deletions internal/resources/metal/project_ssh_key/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (r *Resource) Create(
projectId := plan.ProjectID.ValueString()

// Create API resource
key, _, err := client.SSHKeysApi.CreateProjectSSHKey(context.Background(), projectId).SSHKeyCreateInput(*createRequest).Execute()
key, _, err := client.SSHKeysApi.CreateProjectSSHKey(ctx, projectId).SSHKeyCreateInput(*createRequest).Execute()
if err != nil {
resp.Diagnostics.AddError(
"Failed to create Project SSH Key",
Expand Down Expand Up @@ -86,7 +86,7 @@ func (r *Resource) Read(
id := state.ID.ValueString()

// Use API client to get the current state of the resource
key, _, err := client.SSHKeysApi.FindSSHKeyById(context.Background(), id).Include(nil).Execute()
key, _, err := client.SSHKeysApi.FindSSHKeyById(ctx, id).Include(nil).Execute()
if err != nil {
err = equinix_errors.FriendlyError(err)

Expand Down Expand Up @@ -143,7 +143,7 @@ func (r *Resource) Update(
}

// Update the resource
key, _, err := client.SSHKeysApi.UpdateSSHKey(context.Background(), id).SSHKeyInput(*updateRequest).Execute()
key, _, err := client.SSHKeysApi.UpdateSSHKey(ctx, id).SSHKeyInput(*updateRequest).Execute()
if err != nil {
err = equinix_errors.FriendlyError(err)
resp.Diagnostics.AddError(
Expand Down Expand Up @@ -181,7 +181,7 @@ func (r *Resource) Delete(
id := state.ID.ValueString()

// Use API client to delete the resource
deleteResp, err := client.SSHKeysApi.DeleteSSHKey(context.Background(), id).Execute()
deleteResp, err := client.SSHKeysApi.DeleteSSHKey(ctx, id).Execute()
if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(deleteResp, err) != nil {
err = equinix_errors.FriendlyError(err)
resp.Diagnostics.AddError(
Expand Down
8 changes: 4 additions & 4 deletions internal/resources/metal/ssh_key/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (r *Resource) Create(
}

// Create API resource
key, _, err := client.SSHKeysApi.CreateSSHKey(context.Background()).SSHKeyCreateInput(*createRequest).Execute()
key, _, err := client.SSHKeysApi.CreateSSHKey(ctx).SSHKeyCreateInput(*createRequest).Execute()
if err != nil {
resp.Diagnostics.AddError(
"Failed to create SSH Key",
Expand Down Expand Up @@ -84,7 +84,7 @@ func (r *Resource) Read(
id := state.ID.ValueString()

// Use API client to get the current state of the resource
key, _, err := client.SSHKeysApi.FindSSHKeyById(context.Background(), id).Include(nil).Execute()
key, _, err := client.SSHKeysApi.FindSSHKeyById(ctx, id).Include(nil).Execute()
if err != nil {
err = equinix_errors.FriendlyError(err)

Expand Down Expand Up @@ -141,7 +141,7 @@ func (r *Resource) Update(
}

// Update the resource
key, _, err := client.SSHKeysApi.UpdateSSHKey(context.Background(), id).SSHKeyInput(*updateRequest).Execute()
key, _, err := client.SSHKeysApi.UpdateSSHKey(ctx, id).SSHKeyInput(*updateRequest).Execute()
if err != nil {
err = equinix_errors.FriendlyError(err)
resp.Diagnostics.AddError(
Expand Down Expand Up @@ -179,7 +179,7 @@ func (r *Resource) Delete(
id := state.ID.ValueString()

// Use API client to delete the resource
deleteResp, err := client.SSHKeysApi.DeleteSSHKey(context.Background(), id).Execute()
deleteResp, err := client.SSHKeysApi.DeleteSSHKey(ctx, id).Execute()
if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(deleteResp, err) != nil {
err = equinix_errors.FriendlyError(err)
resp.Diagnostics.AddError(
Expand Down

0 comments on commit 070703c

Please sign in to comment.