Skip to content

Commit

Permalink
Merge pull request #85 from Sanverik/add-new-attributes
Browse files Browse the repository at this point in the history
Add vlan_id and role_id for `netbox_prefix` resource
  • Loading branch information
fbreckle authored Oct 27, 2021
2 parents 458c500 + cbb5004 commit 25fa0a0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
8 changes: 8 additions & 0 deletions netbox/resource_netbox_available_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
28 changes: 28 additions & 0 deletions netbox/resource_netbox_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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?)

Expand Down Expand Up @@ -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)
Expand Down
37 changes: 28 additions & 9 deletions netbox/resource_netbox_prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -134,14 +149,16 @@ 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"
status = "active"
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(
Expand All @@ -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),
),
Expand Down
4 changes: 2 additions & 2 deletions netbox/resource_netbox_vlan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 25fa0a0

Please sign in to comment.