Skip to content

Commit

Permalink
Fix type conversion on ID when updating tunnel termination (#616)
Browse files Browse the repository at this point in the history
* Refactor vpn termination test

* Add regression test

* Fix type conversion on ID in tunnel termination
  • Loading branch information
mraerino authored Jul 10, 2024
1 parent f666125 commit 3810615
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 80 deletions.
3 changes: 2 additions & 1 deletion netbox/resource_netbox_vpn_tunnel_termination.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ func resourceNetboxVpnTunnelTerminationUpdate(d *schema.ResourceData, m interfac
id, _ := strconv.ParseInt(d.Id(), 10, 64)
data := models.WritableTunnelTermination{}

data.Tunnel = int64ToPtr(d.Get("tunnel_id").(int64))
tunnelID := int64(d.Get("tunnel_id").(int))
data.Tunnel = &tunnelID
data.Role = d.Get("role").(string)

vmInterfaceID := getOptionalInt(d, "virtual_machine_interface_id")
Expand Down
189 changes: 110 additions & 79 deletions netbox/resource_netbox_vpn_tunnel_termination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,79 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func testAccNetboxVpnTunnelTerminationFullDependencies(testName string) string {
return fmt.Sprintf(`
resource "netbox_site" "test" {
name = "%[1]s"
}
resource "netbox_device_role" "test" {
name = "%[1]s"
color_hex = "ff00ff"
}
resource "netbox_manufacturer" "test" {
name = "%[1]s"
}
resource "netbox_device_type" "test" {
model = "%[1]s"
manufacturer_id = netbox_manufacturer.test.id
}
resource "netbox_device" "test" {
name = "%[1]s"
device_type_id = netbox_device_type.test.id
role_id = netbox_device_role.test.id
site_id = netbox_site.test.id
}
resource "netbox_device_interface" "test" {
name = "eth0"
device_id = netbox_device.test.id
type = "virtual"
}
resource "netbox_ip_address" "device_1" {
ip_address = "2.2.2.0/32"
status = "active"
device_interface_id = netbox_device_interface.test.id
}
resource "netbox_ip_address" "device_2" {
ip_address = "2.2.2.1/32"
status = "active"
device_interface_id = netbox_device_interface.test.id
}
resource "netbox_virtual_machine" "test" {
name = "%[1]s"
site_id = netbox_site.test.id
}
resource "netbox_interface" "test" {
name = "eth0"
virtual_machine_id = netbox_virtual_machine.test.id
}
resource "netbox_ip_address" "vm_1" {
ip_address = "2.2.2.2/32"
status = "active"
virtual_machine_interface_id = netbox_interface.test.id
}
resource "netbox_ip_address" "vm_2" {
ip_address = "2.2.2.3/32"
status = "active"
virtual_machine_interface_id = netbox_interface.test.id
}
resource "netbox_vpn_tunnel_group" "test" {
name = "%[1]s"
description = "%[1]s"
}
resource "netbox_tag" "test" {
name = "%[1]s"
}
resource "netbox_vpn_tunnel" "test" {
name = "%[1]s"
encapsulation = "ipsec-transport"
status = "active"
tunnel_group_id = netbox_vpn_tunnel_group.test.id
}
`, testName)
}

func TestAccNetboxVpnTunnelTermination_basic(t *testing.T) {
testSlug := "vpnterm_basic"
testName := testAccGetTestName(testSlug)
Expand All @@ -18,90 +91,21 @@ func TestAccNetboxVpnTunnelTermination_basic(t *testing.T) {
PreCheck: func() { testAccPreCheck(t) },
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "netbox_site" "test" {
name = "%[1]s"
}
resource "netbox_device_role" "test" {
name = "%[1]s"
color_hex = "ff00ff"
}
resource "netbox_manufacturer" "test" {
name = "%[1]s"
}
resource "netbox_device_type" "test" {
model = "%[1]s"
manufacturer_id = netbox_manufacturer.test.id
}
resource "netbox_device" "test" {
name = "%[1]s"
device_type_id = netbox_device_type.test.id
role_id = netbox_device_role.test.id
site_id = netbox_site.test.id
}
resource "netbox_device_interface" "test" {
name = "eth0"
device_id = netbox_device.test.id
type = "virtual"
}
resource "netbox_ip_address" "device_1" {
ip_address = "2.2.2.0/32"
status = "active"
device_interface_id = netbox_device_interface.test.id
}
resource "netbox_ip_address" "device_2" {
ip_address = "2.2.2.1/32"
status = "active"
device_interface_id = netbox_device_interface.test.id
}
resource "netbox_virtual_machine" "test" {
name = "%[1]s"
site_id = netbox_site.test.id
}
resource "netbox_interface" "test" {
name = "eth0"
virtual_machine_id = netbox_virtual_machine.test.id
}
resource "netbox_ip_address" "vm_1" {
ip_address = "2.2.2.2/32"
status = "active"
virtual_machine_interface_id = netbox_interface.test.id
}
resource "netbox_ip_address" "vm_2" {
ip_address = "2.2.2.3/32"
status = "active"
virtual_machine_interface_id = netbox_interface.test.id
}
resource "netbox_vpn_tunnel_group" "test" {
name = "%[1]s"
description = "%[1]s"
}
resource "netbox_tag" "test" {
name = "%[1]s"
}
resource "netbox_vpn_tunnel" "test" {
name = "%[1]s"
encapsulation = "ipsec-transport"
status = "active"
tunnel_group_id = netbox_vpn_tunnel_group.test.id
}
Config: testAccNetboxVpnTunnelTerminationFullDependencies(testName) + `
resource "netbox_vpn_tunnel_termination" "device" {
role = "peer"
tunnel_id = netbox_vpn_tunnel.test.id
device_interface_id = netbox_device_interface.test.id
role = "peer"
tunnel_id = netbox_vpn_tunnel.test.id
device_interface_id = netbox_device_interface.test.id
tags = [netbox_tag.test.name]
tags = [netbox_tag.test.name]
}
resource "netbox_vpn_tunnel_termination" "vm" {
role = "peer"
tunnel_id = netbox_vpn_tunnel.test.id
virtual_machine_interface_id = netbox_interface.test.id
role = "peer"
tunnel_id = netbox_vpn_tunnel.test.id
virtual_machine_interface_id = netbox_interface.test.id
outside_ip_address_id = netbox_ip_address.vm_1.id
}
`, testName),
outside_ip_address_id = netbox_ip_address.vm_1.id
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("netbox_vpn_tunnel_termination.device", "tunnel_id", "netbox_vpn_tunnel.test", "id"),
resource.TestCheckResourceAttrPair("netbox_vpn_tunnel_termination.vm", "tunnel_id", "netbox_vpn_tunnel.test", "id"),
Expand All @@ -110,6 +114,33 @@ resource "netbox_vpn_tunnel_termination" "vm" {
resource.TestCheckResourceAttr("netbox_vpn_tunnel_termination.device", "tags.0", testName),
),
},
{
Config: testAccNetboxVpnTunnelTerminationFullDependencies(testName) + `
resource "netbox_ip_address" "outside_ip" {
ip_address = "203.0.113.2/24"
status = "active"
}
resource "netbox_vpn_tunnel_termination" "device" {
role = "peer"
tunnel_id = netbox_vpn_tunnel.test.id
device_interface_id = netbox_device_interface.test.id
outside_ip_address_id = netbox_ip_address.outside_ip.id
tags = [netbox_tag.test.name]
}
resource "netbox_vpn_tunnel_termination" "vm" {
role = "peer"
tunnel_id = netbox_vpn_tunnel.test.id
virtual_machine_interface_id = netbox_interface.test.id
outside_ip_address_id = netbox_ip_address.vm_1.id
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("netbox_vpn_tunnel_termination.device", "tunnel_id", "netbox_vpn_tunnel.test", "id"),
resource.TestCheckResourceAttrPair("netbox_vpn_tunnel_termination.device", "outside_ip_address_id", "netbox_ip_address.outside_ip", "id"),
),
},
{
ResourceName: "netbox_vpn_tunnel.test",
ImportState: true,
Expand Down

0 comments on commit 3810615

Please sign in to comment.