Skip to content

Commit

Permalink
fix: correctly parse vlan field of VLAN interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
skatsaounis committed Jul 30, 2024
1 parent 7153222 commit d273e55
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 27 deletions.
18 changes: 12 additions & 6 deletions maas/resource_maas_network_interface_bond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func testAccMaasNetworkInterfaceBond(name string, machine string, fabric string) string {
func testAccMaasNetworkInterfaceBond(name string, machine string, fabric string, mtu int) string {
return fmt.Sprintf(`
data "maas_fabric" "default" {
name = "%s"
Expand Down Expand Up @@ -56,12 +56,12 @@ resource "maas_network_interface_bond" "test" {
bond_updelay = 1
bond_xmit_hash_policy = "layer2"
mac_address = "01:12:34:56:78:9A"
mtu = 9000
mtu = %d
parents = [maas_network_interface_physical.nic1.name, maas_network_interface_physical.nic2.name]
tags = ["tag1", "tag2"]
vlan = data.maas_vlan.default.id
}
`, fabric, machine, name)
`, fabric, machine, name, mtu)
}

func TestAccResourceMaasNetworkInterfaceBond_basic(t *testing.T) {
Expand All @@ -83,7 +83,6 @@ func TestAccResourceMaasNetworkInterfaceBond_basic(t *testing.T) {
resource.TestCheckResourceAttr("maas_network_interface_bond.test", "bond_updelay", "1"),
resource.TestCheckResourceAttr("maas_network_interface_bond.test", "bond_xmit_hash_policy", "layer2"),
resource.TestCheckResourceAttr("maas_network_interface_bond.test", "mac_address", "01:12:34:56:78:9A"),
resource.TestCheckResourceAttr("maas_network_interface_bond.test", "mtu", "9000"),
resource.TestCheckResourceAttr("maas_network_interface_bond.test", "parents.#", "2"),
resource.TestCheckResourceAttr("maas_network_interface_bond.test", "parents.0", "enp109s0f0"),
resource.TestCheckResourceAttr("maas_network_interface_bond.test", "parents.1", "enp109s0f1"),
Expand All @@ -100,8 +99,15 @@ func TestAccResourceMaasNetworkInterfaceBond_basic(t *testing.T) {
ErrorCheck: func(err error) error { return err },
Steps: []resource.TestStep{
{
Config: testAccMaasNetworkInterfaceBond(name, machine, fabric),
Check: resource.ComposeTestCheckFunc(checks...),
Config: testAccMaasNetworkInterfaceBond(name, machine, fabric, 1500),
Check: resource.ComposeTestCheckFunc(
append(checks, resource.TestCheckResourceAttr("maas_network_interface_bond.test", "mtu", "1500"))...),
},
// Test update
{
Config: testAccMaasNetworkInterfaceBond(name, machine, fabric, 9000),
Check: resource.ComposeTestCheckFunc(
append(checks, resource.TestCheckResourceAttr("maas_network_interface_bond.test", "mtu", "9000"))...),
},
// Test import
{
Expand Down
18 changes: 12 additions & 6 deletions maas/resource_maas_network_interface_bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func testAccMaasNetworkInterfaceBridge(name string, machine string, fabric string) string {
func testAccMaasNetworkInterfaceBridge(name string, machine string, fabric string, mtu int) string {
return fmt.Sprintf(`
data "maas_fabric" "default" {
name = "%s"
Expand Down Expand Up @@ -45,12 +45,12 @@ resource "maas_network_interface_bridge" "test" {
bridge_stp = true
bridge_type = "standard"
mac_address = "01:12:34:56:78:9A"
mtu = 9000
mtu = %d
parent = maas_network_interface_physical.nic1.name
tags = ["tag1", "tag2"]
vlan = data.maas_vlan.default.id
}
`, fabric, machine, name)
`, fabric, machine, name, mtu)
}

func TestAccResourceMaasNetworkInterfaceBridge_basic(t *testing.T) {
Expand All @@ -68,7 +68,6 @@ func TestAccResourceMaasNetworkInterfaceBridge_basic(t *testing.T) {
resource.TestCheckResourceAttr("maas_network_interface_bridge.test", "bridge_stp", "true"),
resource.TestCheckResourceAttr("maas_network_interface_bridge.test", "bridge_type", "standard"),
resource.TestCheckResourceAttr("maas_network_interface_bridge.test", "mac_address", "01:12:34:56:78:9A"),
resource.TestCheckResourceAttr("maas_network_interface_bridge.test", "mtu", "9000"),
resource.TestCheckResourceAttr("maas_network_interface_bridge.test", "parent", "bridge0.3342"),
resource.TestCheckResourceAttr("maas_network_interface_bridge.test", "tags.#", "2"),
resource.TestCheckResourceAttr("maas_network_interface_bridge.test", "tags.0", "tag1"),
Expand All @@ -83,8 +82,15 @@ func TestAccResourceMaasNetworkInterfaceBridge_basic(t *testing.T) {
ErrorCheck: func(err error) error { return err },
Steps: []resource.TestStep{
{
Config: testAccMaasNetworkInterfaceBridge(name, machine, fabric),
Check: resource.ComposeTestCheckFunc(checks...),
Config: testAccMaasNetworkInterfaceBridge(name, machine, fabric, 1500),
Check: resource.ComposeTestCheckFunc(
append(checks, resource.TestCheckResourceAttr("maas_network_interface_bridge.test", "mtu", "1500"))...),
},
// Test update
{
Config: testAccMaasNetworkInterfaceBridge(name, machine, fabric, 9000),
Check: resource.ComposeTestCheckFunc(
append(checks, resource.TestCheckResourceAttr("maas_network_interface_bridge.test", "mtu", "9000"))...),
},
// Test import
{
Expand Down
18 changes: 12 additions & 6 deletions maas/resource_maas_network_interface_physical_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func testAccMaasNetworkInterfacePhysical(name string, machine string, fabric string) string {
func testAccMaasNetworkInterfacePhysical(name string, machine string, fabric string, mtu int) string {
return fmt.Sprintf(`
data "maas_fabric" "default" {
name = "%s"
Expand All @@ -34,11 +34,11 @@ resource "maas_network_interface_physical" "test" {
machine = data.maas_machine.machine.id
name = "%s"
mac_address = "01:12:34:56:78:9A"
mtu = 9000
mtu = %d
tags = ["tag1", "tag2"]
vlan = data.maas_vlan.default.id
}
`, fabric, machine, name)
`, fabric, machine, name, mtu)
}

func TestAccResourceMaasNetworkInterfacePhysical_basic(t *testing.T) {
Expand All @@ -52,7 +52,6 @@ func TestAccResourceMaasNetworkInterfacePhysical_basic(t *testing.T) {
testAccMaasNetworkInterfacePhysicalCheckExists("maas_network_interface_physical.test", &networkInterfacePhysical),
resource.TestCheckResourceAttr("maas_network_interface_physical.test", "name", name),
resource.TestCheckResourceAttr("maas_network_interface_physical.test", "mac_address", "01:12:34:56:78:9A"),
resource.TestCheckResourceAttr("maas_network_interface_physical.test", "mtu", "9000"),
resource.TestCheckResourceAttr("maas_network_interface_physical.test", "tags.#", "2"),
resource.TestCheckResourceAttr("maas_network_interface_physical.test", "tags.0", "tag1"),
resource.TestCheckResourceAttr("maas_network_interface_physical.test", "tags.1", "tag2"),
Expand All @@ -66,8 +65,15 @@ func TestAccResourceMaasNetworkInterfacePhysical_basic(t *testing.T) {
ErrorCheck: func(err error) error { return err },
Steps: []resource.TestStep{
{
Config: testAccMaasNetworkInterfacePhysical(name, machine, fabric),
Check: resource.ComposeTestCheckFunc(checks...),
Config: testAccMaasNetworkInterfacePhysical(name, machine, fabric, 1500),
Check: resource.ComposeTestCheckFunc(
append(checks, resource.TestCheckResourceAttr("maas_network_interface_physical.test", "mtu", "1500"))...),
},
// Test update
{
Config: testAccMaasNetworkInterfacePhysical(name, machine, fabric, 9000),
Check: resource.ComposeTestCheckFunc(
append(checks, resource.TestCheckResourceAttr("maas_network_interface_physical.test", "mtu", "9000"))...),
},
// Test import
{
Expand Down
7 changes: 4 additions & 3 deletions maas/resource_maas_network_interface_vlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func resourceNetworkInterfaceVlanCreate(ctx context.Context, d *schema.ResourceD
if err != nil {
return diag.FromErr(err)
}
vlan, err := getVlan(client, fabric.ID, fmt.Sprintf("%v", d.Get("vlan").(int)))
vlan, err := getVlan(client, fabric.ID, strconv.Itoa(d.Get("vlan").(int)))
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -136,14 +136,15 @@ func resourceNetworkInterfaceVlanRead(ctx context.Context, d *schema.ResourceDat
"vlan": networkInterface.VLAN.ID,
}
if _, ok := d.GetOk("fabric"); !ok {
tfState["fabric"] = fmt.Sprintf("%v", networkInterface.VLAN.FabricID)
tfState["fabric"] = strconv.Itoa(networkInterface.VLAN.FabricID)
}
if err := setTerraformState(d, tfState); err != nil {
return diag.FromErr(err)
}

return nil
}

func resourceNetworkInterfaceVlanUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*client.Client)

Expand All @@ -167,7 +168,7 @@ func resourceNetworkInterfaceVlanUpdate(ctx context.Context, d *schema.ResourceD
return diag.FromErr(err)
}

vlan, err := getVlan(client, fabric.ID, d.Get("vlan").(string))
vlan, err := getVlan(client, fabric.ID, strconv.Itoa(d.Get("vlan").(int)))
if err != nil {
return diag.FromErr(err)
}
Expand Down
18 changes: 12 additions & 6 deletions maas/resource_maas_network_interface_vlan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func testAccMaasNetworkInterfaceVLAN(machine string, fabric string) string {
func testAccMaasNetworkInterfaceVLAN(machine string, fabric string, mtu int) string {
return fmt.Sprintf(`
data "maas_fabric" "default" {
name = "%s"
Expand Down Expand Up @@ -46,12 +46,12 @@ resource "maas_network_interface_vlan" "test" {
machine = data.maas_machine.machine.id
accept_ra = false
fabric = data.maas_fabric.default.id
mtu = 9000
mtu = %d
parent = maas_network_interface_physical.nic1.name
tags = ["tag1", "tag2"]
vlan = maas_vlan.tf_vlan.id
}
`, fabric, machine)
`, fabric, machine, mtu)
}

func TestAccResourceMaasNetworkInterfaceVLAN_basic(t *testing.T) {
Expand All @@ -63,7 +63,6 @@ func TestAccResourceMaasNetworkInterfaceVLAN_basic(t *testing.T) {
checks := []resource.TestCheckFunc{
testAccMaasNetworkInterfaceVLANCheckExists("maas_network_interface_vlan.test", &networkInterfaceVLAN),
resource.TestCheckResourceAttr("maas_network_interface_vlan.test", "accept_ra", "false"),
resource.TestCheckResourceAttr("maas_network_interface_vlan.test", "mtu", "9000"),
resource.TestCheckResourceAttr("maas_network_interface_vlan.test", "parent", "bond0"),
resource.TestCheckResourceAttr("maas_network_interface_vlan.test", "tags.#", "2"),
resource.TestCheckResourceAttr("maas_network_interface_vlan.test", "tags.0", "tag1"),
Expand All @@ -78,8 +77,15 @@ func TestAccResourceMaasNetworkInterfaceVLAN_basic(t *testing.T) {
ErrorCheck: func(err error) error { return err },
Steps: []resource.TestStep{
{
Config: testAccMaasNetworkInterfaceVLAN(machine, fabric),
Check: resource.ComposeTestCheckFunc(checks...),
Config: testAccMaasNetworkInterfaceVLAN(machine, fabric, 1500),
Check: resource.ComposeTestCheckFunc(
append(checks, resource.TestCheckResourceAttr("maas_network_interface_vlan.test", "mtu", "1500"))...),
},
// Test update
{
Config: testAccMaasNetworkInterfaceVLAN(machine, fabric, 9000),
Check: resource.ComposeTestCheckFunc(
append(checks, resource.TestCheckResourceAttr("maas_network_interface_vlan.test", "mtu", "9000"))...),
},
// Test import
{
Expand Down

0 comments on commit d273e55

Please sign in to comment.