Skip to content

Commit

Permalink
Add acctest + use 'parent_id' without omitempty property in go-netbox
Browse files Browse the repository at this point in the history
GennadySpb authored and fbreckle committed Feb 16, 2024
1 parent 23e1d95 commit d781a16
Showing 2 changed files with 83 additions and 3 deletions.
9 changes: 6 additions & 3 deletions netbox/resource_netbox_location.go
Original file line number Diff line number Diff line change
@@ -187,9 +187,12 @@ func resourceNetboxLocationUpdate(d *schema.ResourceData, m interface{}) error {
data.Site = int64ToPtr(int64(siteIDValue.(int)))
}

parentIDValue, ok := d.GetOk("parent_id")
if ok {
data.Parent = int64ToPtr(int64(parentIDValue.(int)))
parentIDValue := d.Get("parent_id")
data.Parent = int64ToPtr(int64(parentIDValue.(int)))

// remove parent (set null) if we got zero ID
if parentIDValue == 0 {
data.Parent = nil
}

tenantIDValue, ok := d.GetOk("tenant_id")
77 changes: 77 additions & 0 deletions netbox/resource_netbox_location_test.go
Original file line number Diff line number Diff line change
@@ -90,6 +90,83 @@ resource "netbox_location" "test" {
})
}

func TestAccNetboxLocation_updateParent(t *testing.T) {
testSlug := "loc_upd_parent"
testName := testAccGetTestName(testSlug)
testNameSub := testAccGetTestName(testSlug)
randomSlug := testAccGetTestName(testSlug)
randomSlugSub := testAccGetTestName(testSlug)
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Steps: []resource.TestStep{
{
Config: testAccNetboxLocationUpdateParent1(testName, randomSlug, testNameSub, randomSlugSub),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_location.test", "name", testName),
resource.TestCheckResourceAttr("netbox_location.test", "slug", randomSlug),
resource.TestCheckResourceAttr("netbox_location.test", "description", "my-description"),
resource.TestCheckResourceAttrPair("netbox_location.test", "site_id", "netbox_site.test", "id"),
resource.TestCheckResourceAttrPair("netbox_location.test", "id", "netbox_location.test_sub", "parent_id"),
),
},
{
Config: testAccNetboxLocationUpdateParent2(testName, randomSlug, testNameSub, randomSlugSub),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_location.test", "name", testName),
resource.TestCheckResourceAttr("netbox_location.test", "slug", randomSlug),
resource.TestCheckResourceAttr("netbox_location.test", "description", "my-description"),
resource.TestCheckResourceAttr("netbox_location.test_sub", "parent_id", "0"),
),
},
},
})
}

func testAccNetboxLocationUpdateParent1(testName string, randomSlug string, testNameSub string, randomSlugSub string) string {
return fmt.Sprintf(`
resource "netbox_site" "test" {
name = "%[1]s"
}
resource "netbox_location" "test" {
name = "%[1]s"
slug = "%[2]s"
description = "my-description"
site_id = netbox_site.test.id
}
resource "netbox_location" "test_sub" {
name = "%[3]s"
slug = "%[4]s"
description = "my-description"
parent_id = netbox_location.test.id
site_id = netbox_site.test.id
}`, testName, randomSlug, testNameSub, randomSlugSub)
}

func testAccNetboxLocationUpdateParent2(testName string, randomSlug string, testNameSub string, randomSlugSub string) string {
return fmt.Sprintf(`
resource "netbox_site" "test" {
name = "%[1]s"
}
resource "netbox_location" "test" {
name = "%[1]s"
slug = "%[2]s"
description = "my-description"
site_id = netbox_site.test.id
}
resource "netbox_location" "test_sub" {
name = "%[3]s"
slug = "%[4]s"
description = "my-description"
parent_id = "0"
site_id = netbox_site.test.id
}`, testName, randomSlug, testNameSub, randomSlugSub)
}

func init() {
resource.AddTestSweepers("netbox_location", &resource.Sweeper{
Name: "netbox_location",

0 comments on commit d781a16

Please sign in to comment.