diff --git a/netbox/resource_netbox_available_prefix.go b/netbox/resource_netbox_available_prefix.go index d9c60442..f47e8fb5 100644 --- a/netbox/resource_netbox_available_prefix.go +++ b/netbox/resource_netbox_available_prefix.go @@ -61,6 +61,14 @@ func resourceNetboxAvailablePrefix() *schema.Resource { Type: schema.TypeInt, Optional: true, }, + "vlan_id": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + }, + "role_id": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + }, "tags": { Type: schema.TypeSet, Elem: &schema.Schema{ diff --git a/netbox/resource_netbox_prefix.go b/netbox/resource_netbox_prefix.go index 2e77676f..35d04244 100644 --- a/netbox/resource_netbox_prefix.go +++ b/netbox/resource_netbox_prefix.go @@ -48,6 +48,14 @@ func resourceNetboxPrefix() *schema.Resource { Type: schema.TypeInt, Optional: true, }, + "vlan_id": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + }, + "role_id": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + }, "tags": &schema.Schema{ Type: schema.TypeSet, Elem: &schema.Schema{ @@ -132,6 +140,18 @@ func resourceNetboxPrefixRead(d *schema.ResourceData, m interface{}) error { d.Set("site_id", nil) } + if res.GetPayload().Vlan != nil { + d.Set("vlan_id", res.GetPayload().Vlan.ID) + } else { + d.Set("vlan_id", nil) + } + + if res.GetPayload().Role != nil { + d.Set("role_id", res.GetPayload().Role.ID) + } else { + d.Set("role_id", nil) + } + d.Set("tags", getTagListFromNestedTagList(res.GetPayload().Tags)) // FIGURE OUT NESTED VRF AND NESTED VLAN (from maybe interfaces?) @@ -165,6 +185,14 @@ func resourceNetboxPrefixUpdate(d *schema.ResourceData, m interface{}) error { data.Site = int64ToPtr(int64(siteID.(int))) } + if vlanID, ok := d.GetOk("vlan_id"); ok { + data.Vlan = int64ToPtr(int64(vlanID.(int))) + } + + if roleID, ok := d.GetOk("role_id"); ok { + data.Role = int64ToPtr(int64(roleID.(int))) + } + data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get("tags")) params := ipam.NewIpamPrefixesUpdateParams().WithID(id).WithData(&data) diff --git a/netbox/resource_netbox_prefix_test.go b/netbox/resource_netbox_prefix_test.go index 25d93aa2..3c4c50a3 100644 --- a/netbox/resource_netbox_prefix_test.go +++ b/netbox/resource_netbox_prefix_test.go @@ -12,7 +12,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) -func testAccNetboxPrefixFullDependencies(testName string) string { +func testAccNetboxPrefixFullDependencies(testName string, testSlug string, testVid string) string { return fmt.Sprintf(` resource "netbox_tag" "test" { name = "%[1]s" @@ -30,20 +30,35 @@ resource "netbox_site" "test" { name = "%[1]s" status = "active" } -`, testName) + +resource "netbox_ipam_role" "test" { + name = "%[1]s" + slug = "%[2]s" +} + +resource "netbox_vlan" "test" { + name = "%[1]s" + vid = "%[3]s" + status = "active" + description = "Test" + tags = [] +} +`, testName, testSlug, testVid) } func TestAccNetboxPrefix_basic(t *testing.T) { testPrefix := "1.1.1.0/25" testSlug := "prefix" + testVid := "123" + randomSlug := testAccGetTestName(testSlug) testDesc := "test prefix" testName := testAccGetTestName(testSlug) resource.ParallelTest(t, resource.TestCase{ Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(` + Config: testAccNetboxPrefixFullDependencies(testName, randomSlug, testVid) + fmt.Sprintf(` resource "netbox_prefix" "test" { prefix = "%s" description = "%s" @@ -59,7 +74,7 @@ resource "netbox_prefix" "test" { ), }, { - Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(` + Config: testAccNetboxPrefixFullDependencies(testName, randomSlug, testVid) + fmt.Sprintf(` resource "netbox_prefix" "test" { prefix = "%s" description = "%s" @@ -69,7 +84,7 @@ resource "netbox_prefix" "test" { ExpectError: regexp.MustCompile("expected status to be one of .*"), }, { - Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(` + Config: testAccNetboxPrefixFullDependencies(testName, randomSlug, testVid) + fmt.Sprintf(` resource "netbox_prefix" "test" { prefix = "%s" description = "%s" @@ -82,7 +97,7 @@ resource "netbox_prefix" "test" { ), }, { - Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(` + Config: testAccNetboxPrefixFullDependencies(testName, randomSlug, testVid) + fmt.Sprintf(` resource "netbox_prefix" "test" { prefix = "%s" description = "%s" @@ -95,7 +110,7 @@ resource "netbox_prefix" "test" { ), }, { - Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(` + Config: testAccNetboxPrefixFullDependencies(testName, randomSlug, testVid) + fmt.Sprintf(` resource "netbox_prefix" "test" { prefix = "%s" description = "%s 2" @@ -114,7 +129,7 @@ resource "netbox_prefix" "test" { ), }, { - Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(` + Config: testAccNetboxPrefixFullDependencies(testName, randomSlug, testVid) + fmt.Sprintf(` resource "netbox_prefix" "test" { prefix = "%s" description = "%s 2" @@ -134,7 +149,7 @@ resource "netbox_prefix" "test" { ), }, { - Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(` + Config: testAccNetboxPrefixFullDependencies(testName, randomSlug, testVid) + fmt.Sprintf(` resource "netbox_prefix" "test" { prefix = "%s" description = "%s 2" @@ -142,6 +157,8 @@ resource "netbox_prefix" "test" { vrf_id = netbox_vrf.test.id tenant_id = netbox_tenant.test.id site_id = netbox_site.test.id + vlan_id = netbox_vlan.test.id + role_id = netbox_ipam_role.test.id tags = [netbox_tag.test.name] }`, testPrefix, testDesc), Check: resource.ComposeTestCheckFunc( @@ -151,6 +168,8 @@ resource "netbox_prefix" "test" { resource.TestCheckResourceAttrPair("netbox_prefix.test", "vrf_id", "netbox_vrf.test", "id"), resource.TestCheckResourceAttrPair("netbox_prefix.test", "tenant_id", "netbox_tenant.test", "id"), resource.TestCheckResourceAttrPair("netbox_prefix.test", "site_id", "netbox_site.test", "id"), + resource.TestCheckResourceAttrPair("netbox_prefix.test", "vlan_id", "netbox_vlan.test", "id"), + resource.TestCheckResourceAttrPair("netbox_prefix.test", "role_id", "netbox_ipam_role.test", "id"), resource.TestCheckResourceAttr("netbox_prefix.test", "tags.#", "1"), resource.TestCheckResourceAttr("netbox_prefix.test", "tags.0", testName), ), diff --git a/netbox/resource_netbox_vlan_test.go b/netbox/resource_netbox_vlan_test.go index f6e403ca..9b2295f7 100644 --- a/netbox/resource_netbox_vlan_test.go +++ b/netbox/resource_netbox_vlan_test.go @@ -38,7 +38,7 @@ func TestAccNetboxVlan_basic(t *testing.T) { PreCheck: func() { testAccPreCheck(t) }, Steps: []resource.TestStep{ { - Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(` + Config: testAccNetboxVlanFullDependencies(testName) + fmt.Sprintf(` resource "netbox_vlan" "test_basic" { name = "%s" vid = "%s" @@ -73,7 +73,7 @@ func TestAccNetboxVlan_with_dependencies(t *testing.T) { PreCheck: func() { testAccPreCheck(t) }, Steps: []resource.TestStep{ { - Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(` + Config: testAccNetboxVlanFullDependencies(testName) + fmt.Sprintf(` resource "netbox_vlan" "test_with_dependencies" { name = "%s" vid = "%s"