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
  • Loading branch information
GennadySpb authored and fbreckle committed Feb 16, 2024
1 parent 6e222c3 commit 4e398e2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
toolchain go1.21.3

require (
github.com/fbreckle/go-netbox v0.0.0-20240129094642-97f73f7dfeb5
github.com/fbreckle/go-netbox v0.0.0-20240216113358-5684f8ef416b
github.com/fbreckle/terraform-plugin-docs v0.0.0-20220812121758-a828466500d3
github.com/go-openapi/runtime v0.27.1
github.com/go-openapi/strfmt v0.22.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/fbreckle/go-netbox v0.0.0-20240129094642-97f73f7dfeb5 h1:C7JoMpqT+UBCqcONFeaW9b9KsVqQ+BpOniY7FUR0GRI=
github.com/fbreckle/go-netbox v0.0.0-20240129094642-97f73f7dfeb5/go.mod h1:3U3/m/hna9Ntd3sbHBYwZ1IqbP2+coRzoXw3mCfu3kM=
github.com/fbreckle/go-netbox v0.0.0-20240216113358-5684f8ef416b h1:MT8csDWYQvhJyA+mprXZshw94JODLOP1rZlL4Ym8rnk=
github.com/fbreckle/go-netbox v0.0.0-20240216113358-5684f8ef416b/go.mod h1:3U3/m/hna9Ntd3sbHBYwZ1IqbP2+coRzoXw3mCfu3kM=
github.com/fbreckle/terraform-plugin-docs v0.0.0-20220812121758-a828466500d3 h1:DMSpM0btVedE2Tt1vfDHWQhf2obzjAe1F0/j8/CyfW4=
github.com/fbreckle/terraform-plugin-docs v0.0.0-20220812121758-a828466500d3/go.mod h1:j3HmJySEjx6hOAOPDjGzmzpVNDQq9SNnnF+Vm22d2rs=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
Expand Down
9 changes: 6 additions & 3 deletions netbox/resource_netbox_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
77 changes: 77 additions & 0 deletions netbox/resource_netbox_location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 4e398e2

Please sign in to comment.