Skip to content

Commit

Permalink
fix: Fix provider crash when creating a metal gateway tied to an ipv6…
Browse files Browse the repository at this point in the history
… reservation (#713)

This PR fixes a bug when creating metal gateways associated with IPv6
reservations. Previously the provider would assume the gateway was tied
to an IPv4 reservation, which would result in an error when the
`calculateSubnetSize` function was called.

To fix this, an additional check was added to the `calculateSubnetSize`
function which ensures the address is actually `IPv4` before executing
the private ipv4 subnet logic.

Additionally an acceptance test was added which creates a metal gateway
associated with an IPv6 reservation and VRF.
  • Loading branch information
RaptorGandalf authored Jun 27, 2024
1 parent e3aec85 commit e60c56e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/resources/metal/gateway/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ func (m *DataSourceModel) parse(gw *metalv1.FindMetalGatewayById200Response) dia
type ipReservationCommon interface {
GetCidr() int32
GetPublic() bool
GetAddressFamily() int32
}

func calculateSubnetSize(ip ipReservationCommon) basetypes.Int64Value {
privateIPv4SubnetSize := uint64(0)
if !ip.GetPublic() {
if !ip.GetPublic() && ip.GetAddressFamily() == 4 {
privateIPv4SubnetSize = 1 << (32 - ip.GetCidr())
return types.Int64Value(int64(privateIPv4SubnetSize))
}
Expand Down
61 changes: 61 additions & 0 deletions internal/resources/metal/gateway/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,64 @@ func TestAccMetalGateway_upgradeFromVersion(t *testing.T) {
},
})
}

func TestAccMetalGateway_IPv6Vrf(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheckMetal(t) },
ExternalProviders: acceptance.TestExternalProviders,
ProtoV5ProviderFactories: acceptance.ProtoV5ProviderFactories,
CheckDestroy: testAccMetalGatewayCheckDestroyed,
Steps: []resource.TestStep{
{
Config: testAccMetalGatewayConfig_IPv6Vrf(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(
"equinix_metal_gateway.test", "project_id",
"equinix_metal_project.test", "id"),
resource.TestCheckResourceAttrPair(
"equinix_metal_gateway.test", "ip_reservation_id",
"equinix_metal_reserved_ip_block.test", "id"),
),
},
},
})
}

func testAccMetalGatewayConfig_IPv6Vrf() string {
return `
resource "equinix_metal_project" "test" {
name = "tfacc-gateway-test"
}
resource "equinix_metal_vlan" "test" {
description = "tfacc-vlan VLAN in SV"
metro = "sv"
project_id = equinix_metal_project.test.id
}
resource "equinix_metal_vrf" "test" {
description = "tfacc-vrf VRF in SV"
name = "tfacc-vrf"
metro = "sv"
local_asn = "65000"
ip_ranges = ["2001:d78::/59"]
project_id = equinix_metal_project.test.id
}
resource "equinix_metal_reserved_ip_block" "test" {
project_id = equinix_metal_project.test.id
type = "vrf"
vrf_id = equinix_metal_vrf.test.id
network = "2001:d78::"
metro = "sv"
cidr = 64
}
resource "equinix_metal_gateway" "test" {
project_id = equinix_metal_project.test.id
vlan_id = equinix_metal_vlan.test.id
ip_reservation_id = equinix_metal_reserved_ip_block.test.id
}
`
}

0 comments on commit e60c56e

Please sign in to comment.