From 98440ba78d984afc5b6a6b3e1630de8a4eeb7fb5 Mon Sep 17 00:00:00 2001 From: Charles Treatman Date: Fri, 27 Sep 2024 11:03:25 -0500 Subject: [PATCH] refactor: finish removing packngo from metal_port code --- internal/resources/metal/port/helpers.go | 17 ++++++++++++++--- internal/resources/metal/port/resource_test.go | 12 ++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/internal/resources/metal/port/helpers.go b/internal/resources/metal/port/helpers.go index ded05ded2..6fb07bee6 100644 --- a/internal/resources/metal/port/helpers.go +++ b/internal/resources/metal/port/helpers.go @@ -15,7 +15,6 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/packethost/packngo" "github.com/pkg/errors" ) @@ -196,8 +195,8 @@ func createAndWaitForBatch(ctx context.Context, start time.Time, cpr *ClientPort stateChangeConf := &retry.StateChangeConf{ Delay: 5 * time.Second, - Pending: []string{string(packngo.VLANAssignmentBatchQueued), string(packngo.VLANAssignmentBatchInProgress)}, - Target: []string{string(packngo.VLANAssignmentBatchCompleted)}, + Pending: []string{string(metalv1.PORTVLANASSIGNMENTBATCHSTATE_QUEUED), string(metalv1.PORTVLANASSIGNMENTBATCHSTATE_IN_PROGRESS)}, + Target: []string{string(metalv1.PORTVLANASSIGNMENTBATCHSTATE_COMPLETED)}, MinTimeout: 5 * time.Second, Timeout: ctxTimeout - time.Since(start) - 30*time.Second, Refresh: func() (result interface{}, state string, err error) { @@ -243,6 +242,9 @@ func updateNativeVlan(ctx context.Context, cpr *ClientPortResource) error { } func processBondAction(ctx context.Context, cpr *ClientPortResource, actionIsBond bool) error { + // There's no good alternative to GetOkExists until metal_port + // is converted to terraform-plugin-framework + // nolint:staticcheck wantsBondedRaw, wantsBondedOk := cpr.Resource.GetOkExists("bonded") wantsBonded := wantsBondedRaw.(bool) // only act if the necessary action is the one specified in doBond @@ -285,6 +287,9 @@ func makeDisbond(ctx context.Context, cpr *ClientPortResource) error { } func convertToL2(ctx context.Context, cpr *ClientPortResource) error { + // There's no good alternative to GetOkExists until metal_port + // is converted to terraform-plugin-framework + // nolint:staticcheck l2, l2Ok := cpr.Resource.GetOkExists("layer2") isLayer2 := slices.Contains(l2Types, cpr.Port.GetNetworkType()) @@ -299,6 +304,9 @@ func convertToL2(ctx context.Context, cpr *ClientPortResource) error { } func convertToL3(ctx context.Context, cpr *ClientPortResource) error { + // There's no good alternative to GetOkExists until metal_port + // is converted to terraform-plugin-framework + // nolint:staticcheck l2, l2Ok := cpr.Resource.GetOkExists("layer2") isLayer2 := slices.Contains(l2Types, cpr.Port.GetNetworkType()) @@ -324,6 +332,9 @@ func portSanityChecks(_ context.Context, cpr *ClientPortResource) error { isBondPort := cpr.Port.GetType() == "NetworkBondPort" // Constraint: Only bond ports have layer2 mode + // There's no good alternative to GetOkExists until metal_port + // is converted to terraform-plugin-framework + // nolint:staticcheck l2Raw, l2Ok := cpr.Resource.GetOkExists("layer2") if !isBondPort && l2Ok { return fmt.Errorf("layer2 flag can be set only for bond ports") diff --git a/internal/resources/metal/port/resource_test.go b/internal/resources/metal/port/resource_test.go index 1766a8c74..ece2d33f4 100644 --- a/internal/resources/metal/port/resource_test.go +++ b/internal/resources/metal/port/resource_test.go @@ -12,11 +12,9 @@ import ( "github.com/equinix/terraform-provider-equinix/internal/config" "github.com/equinix/terraform-provider-equinix/internal/resources/metal/port" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" - "github.com/packethost/packngo" ) var ( @@ -400,10 +398,8 @@ func testAccWaitForPortActive(deviceName, portName string) resource.ImportStateI } meta := acceptance.TestAccProvider.Meta() - rd := new(schema.ResourceData) - meta.(*config.Config).AddModuleToMetalUserAgent(rd) - client := meta.(*config.Config).Metal - device, _, err := client.Devices.Get(rs.Primary.ID, &packngo.GetOptions{Includes: []string{"ports"}}) + client := meta.(*config.Config).NewMetalClientForTesting() + device, _, err := client.DevicesApi.FindDeviceById(context.Background(), rs.Primary.ID).Include([]string{"ports"}).Execute() if err != nil { return "", fmt.Errorf("error while fetching device with Id [%s], error: %w", rs.Primary.ID, err) } @@ -415,8 +411,8 @@ func testAccWaitForPortActive(deviceName, portName string) resource.ImportStateI } for _, port := range device.NetworkPorts { - if port.Name == portName { - return port.ID, nil + if port.GetName() == portName { + return port.GetId(), nil } }