From 53e0120cad284cc6ccbb2a2cefd1cf990c54501d Mon Sep 17 00:00:00 2001 From: Gennady Lipenkov Date: Wed, 14 Feb 2024 17:48:46 +0300 Subject: [PATCH] Add acctest + use 'parent_id' without `omitempty` property in go-netbox --- go.mod | 2 +- go.sum | 6 +- netbox/resource_netbox_location.go | 9 ++- netbox/resource_netbox_location_test.go | 77 +++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index b83d19ae..b6b4a60c 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index e727176c..cd736b60 100644 --- a/go.sum +++ b/go.sum @@ -45,8 +45,10 @@ 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-20240214080130-3b369d7c46cf h1:0OeAp5fo4+XNa/dw0Si/k4tvWFMDs+LIjH7YkJa/o8Y= +github.com/fbreckle/go-netbox v0.0.0-20240214080130-3b369d7c46cf/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= diff --git a/netbox/resource_netbox_location.go b/netbox/resource_netbox_location.go index 4c826bb3..73d63b9c 100644 --- a/netbox/resource_netbox_location.go +++ b/netbox/resource_netbox_location.go @@ -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") diff --git a/netbox/resource_netbox_location_test.go b/netbox/resource_netbox_location_test.go index a904d5ea..bffee866 100644 --- a/netbox/resource_netbox_location_test.go +++ b/netbox/resource_netbox_location_test.go @@ -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",