Skip to content

Commit

Permalink
refactor: finish removing packngo from metal_port code
Browse files Browse the repository at this point in the history
  • Loading branch information
ctreatma committed Oct 15, 2024
1 parent 343c8fc commit 61bd34f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
17 changes: 14 additions & 3 deletions internal/resources/metal/port/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())

Expand All @@ -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())

Expand All @@ -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")
Expand Down
12 changes: 4 additions & 8 deletions internal/resources/metal/port/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
}

Expand Down

0 comments on commit 61bd34f

Please sign in to comment.