Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add field 'parent_id' to Location Resource & Data Source #548

Merged
merged 5 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/data-sources/location.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ description: |-
### Optional

- `name` (String)
- `parent_id` (Number)
- `site_id` (Number)
- `slug` (String)

Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/locations.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Read-Only:
- `description` (String)
- `id` (String)
- `name` (String)
- `parent_id` (Number)
- `site_id` (Number)
- `slug` (String)
- `status` (String)
Expand Down
1 change: 1 addition & 0 deletions docs/resources/location.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ resource "netbox_location" "test" {

- `custom_fields` (Map of String)
- `description` (String)
- `parent_id` (Number)
- `site_id` (Number)
- `slug` (String)
- `tags` (Set of String)
Expand Down
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
12 changes: 12 additions & 0 deletions netbox/data_source_netbox_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func dataSourceNetboxLocation() *schema.Resource {
Optional: true,
Computed: true,
},
"parent_id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
},
}
}
Expand All @@ -68,6 +73,10 @@ func dataSourceNetboxLocationRead(d *schema.ResourceData, m interface{}) error {
siteID := fmt.Sprintf("%v", site)
params.SetSiteID(&siteID)
}
if parent, ok := d.Get("parent_id").(int); ok && parent != 0 {
parentID := fmt.Sprintf("%v", parent)
params.SetParentID(&parentID)
}
res, err := api.Dcim.DcimLocationsList(params, nil)

if err != nil {
Expand All @@ -88,6 +97,9 @@ func dataSourceNetboxLocationRead(d *schema.ResourceData, m interface{}) error {
d.Set("site_id", location.Site.ID)
d.Set("slug", location.Slug)

if location.Parent != nil {
d.Set("parent_id", location.Parent.ID)
}
if location.Status != nil {
d.Set("status", location.Status.Value)
}
Expand Down
16 changes: 15 additions & 1 deletion netbox/data_source_netbox_location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
func TestAccNetboxLocationDataSource_basic(t *testing.T) {
testSlug := "location_ds_basic"
testName := testAccGetTestName(testSlug)
testNameSub := testAccGetTestName(testSlug)
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
Expand All @@ -30,6 +31,14 @@ resource "netbox_location" "test" {
tenant_id = netbox_tenant.test.id
}

resource "netbox_location" "test_sub" {
name = "%[2]s"
description = "my-description"
site_id = netbox_site.test.id
tenant_id = netbox_tenant.test.id
parent_id = netbox_location.test.id
}

data "netbox_location" "by_name" {
name = netbox_location.test.name
}
Expand All @@ -39,13 +48,17 @@ data "netbox_location" "by_name_and_site" {
site_id = netbox_site.test.id
}

data "netbox_location" "sub_by_name" {
name = netbox_location.test_sub.name
}

data "netbox_location" "by_id" {
id = netbox_location.test.id
}

data "netbox_location" "by_slug" {
slug = netbox_location.test.slug
}`, testName),
}`, testName, testNameSub),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("data.netbox_location.by_name", "id", "netbox_location.test", "id"),
resource.TestCheckResourceAttrPair("data.netbox_location.by_id", "id", "netbox_location.test", "id"),
Expand All @@ -56,6 +69,7 @@ data "netbox_location" "by_slug" {
resource.TestCheckResourceAttrPair("data.netbox_location.by_name", "site_id", "netbox_location.test", "site_id"),
resource.TestCheckResourceAttrPair("data.netbox_location.by_name_and_site", "site_id", "netbox_location.test", "site_id"),
resource.TestCheckResourceAttrPair("data.netbox_location.by_name", "tenant_id", "netbox_location.test", "tenant_id"),
resource.TestCheckResourceAttrPair("data.netbox_location.sub_by_name", "parent_id", "netbox_location.test", "id"),
),
},
},
Expand Down
10 changes: 10 additions & 0 deletions netbox/data_source_netbox_locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func dataSourceNetboxLocations() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},
"parent_id": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -113,6 +117,8 @@ func dataSourceNetboxLocationsRead(d *schema.ResourceData, m interface{}) error
params.Site = &vString
case "site_id":
params.SiteID = &vString
case "parent_id":
params.ParentID = &vString
case "tenant":
params.Tenant = &vString
case "tenant_id":
Expand Down Expand Up @@ -149,6 +155,10 @@ func dataSourceNetboxLocationsRead(d *schema.ResourceData, m interface{}) error
mapping["site_id"] = v.Site.ID
mapping["description"] = v.Description

if v.Parent != nil {
mapping["parent_id"] = v.Parent.ID
}

if v.Status != nil {
mapping["status"] = v.Status.Value
}
Expand Down
56 changes: 56 additions & 0 deletions netbox/data_source_netbox_locations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ data "netbox_locations" "by_tags" {
resource.TestCheckResourceAttrPair("data.netbox_locations.by_name", "locations.0.name", "netbox_location.test", "name"),
resource.TestCheckResourceAttrPair("data.netbox_locations.by_name", "locations.0.site_id", "netbox_site.test", "id"),
resource.TestCheckResourceAttrPair("data.netbox_locations.by_name", "locations.0.tenant_id", "netbox_tenant.test", "id"),
resource.TestCheckResourceAttr("data.netbox_locations.by_name", "locations.0.parent_id", "0"),
resource.TestCheckResourceAttr("data.netbox_locations.by_name", "locations.0.description", "my-description"),
resource.TestCheckResourceAttr("data.netbox_locations.no_match", "locations.#", "0"),
resource.TestCheckResourceAttr("data.netbox_locations.by_site_slug", "locations.#", "1"),
Expand Down Expand Up @@ -166,3 +167,58 @@ data "netbox_locations" "by_tag" {
},
})
}

func TestAccNetboxLocationsDataSource_sublocations(t *testing.T) {
testSlug := "sublocations_ds_multiple"
testName := testAccGetTestName(testSlug)
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "netbox_site" "test" {
name = "%[1]s"
}

resource "netbox_tenant" "test" {
name = "%[1]s"
}

resource "netbox_location" "parent" {
name = "%[1]s_1"
site_id = netbox_site.test.id
tenant_id = netbox_tenant.test.id
}

resource "netbox_location" "test1" {
name = "%[1]s_1"
parent_id = netbox_location.parent.id
site_id = netbox_site.test.id
tenant_id = netbox_tenant.test.id
}

resource "netbox_location" "test2" {
name = "%[1]s_2"
parent_id = netbox_location.parent.id
site_id = netbox_site.test.id
tenant_id = netbox_tenant.test.id
}

data "netbox_locations" "by_parent" {
filter {
name = "parent_id"
value = netbox_location.parent.id
}
depends_on = [netbox_location.test1, netbox_location.test2]
}`, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.netbox_locations.by_parent", "locations.#", "2"),
resource.TestCheckResourceAttrPair("data.netbox_locations.by_parent", "locations.0.parent_id", "netbox_location.parent", "id"),
resource.TestCheckResourceAttrPair("data.netbox_locations.by_parent", "locations.1.parent_id", "netbox_location.parent", "id"),
//resource.TestCheckResourceAttr("data.netbox_locations.by_parent", "locations.1.parent_id", "223"),
//resource.TestCheckResourceAttr("data.netbox_locations.by_tag", "locations.#", "1"),
),
},
},
})
}
23 changes: 23 additions & 0 deletions netbox/resource_netbox_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ Each location must have a name that is unique within its parent site and locatio
Type: schema.TypeInt,
Optional: true,
},
"parent_id": {
Type: schema.TypeInt,
Optional: true,
},
tagsKey: tagsSchema,
"tenant_id": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -78,6 +82,11 @@ func resourceNetboxLocationCreate(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)))
}

tenantIDValue, ok := d.GetOk("tenant_id")
if ok {
data.Tenant = int64ToPtr(int64(tenantIDValue.(int)))
Expand Down Expand Up @@ -133,6 +142,12 @@ func resourceNetboxLocationRead(d *schema.ResourceData, m interface{}) error {
d.Set("site_id", nil)
}

if res.GetPayload().Parent != nil {
d.Set("parent_id", res.GetPayload().Parent.ID)
} else {
d.Set("parent_id", nil)
}

if res.GetPayload().Tenant != nil {
d.Set("tenant_id", res.GetPayload().Tenant.ID)
} else {
Expand Down Expand Up @@ -172,6 +187,14 @@ func resourceNetboxLocationUpdate(d *schema.ResourceData, m interface{}) error {
data.Site = int64ToPtr(int64(siteIDValue.(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")
if ok {
data.Tenant = int64ToPtr(int64(tenantIDValue.(int)))
Expand Down
90 changes: 89 additions & 1 deletion netbox/resource_netbox_location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
func TestAccNetboxLocation_basic(t *testing.T) {
testSlug := "location_basic"
testName := testAccGetTestName(testSlug)
testNameSub := testAccGetTestName(testSlug)
randomSlug := testAccGetTestName(testSlug)
randomSlugSub := testAccGetTestName(testSlug)
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -35,13 +37,22 @@ resource "netbox_location" "test" {
description = "my-description"
site_id = netbox_site.test.id
tenant_id = netbox_tenant.test.id
}`, testName, randomSlug),
}

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),
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", "tenant_id", "netbox_tenant.test", "id"),
resource.TestCheckResourceAttrPair("netbox_location.test", "id", "netbox_location.test-sub", "parent_id"),
),
},
{
Expand Down Expand Up @@ -79,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
Loading