Skip to content

Commit

Permalink
Return custom_fields from data.netbox_prefix
Browse files Browse the repository at this point in the history
Add a test to ensure the data is returned correctly too.
  • Loading branch information
ad8lmondy authored and fbreckle committed Jun 17, 2024
1 parent ebfd910 commit d252161
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
8 changes: 8 additions & 0 deletions netbox/data_source_netbox_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func dataSourceNetboxPrefix() *schema.Resource {
ValidateFunc: validation.IsCIDR,
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "site_id", "role_id", "cidr", "tag"},
},
customFieldsKey: customFieldsSchema,
"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -175,6 +176,13 @@ func dataSourceNetboxPrefixRead(d *schema.ResourceData, m interface{}) error {
d.Set("family", int(*result.Family.Value))
d.Set("tags", getTagListFromNestedTagList(result.Tags))

cf := getCustomFields(result.CustomFields)
if cf != nil {
d.Set(customFieldsKey, cf)
}
if result.Role != nil {
d.Set("role_id", result.Role.ID)
}
if result.Vrf != nil {
d.Set("vrf_id", result.Vrf.ID)
}
Expand Down
40 changes: 40 additions & 0 deletions netbox/data_source_netbox_prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package netbox

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -118,3 +119,42 @@ data "netbox_prefix" "by_family" {
},
})
}

func TestAccNetboxPrefixDataSource_customFields(t *testing.T) {
testSlug := "prefix_customfields"
testPrefix := "10.0.0.0/24"
testField := strings.ReplaceAll(testAccGetTestName(testSlug), "-", "_")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "netbox_custom_field" "test" {
name = "%[1]s"
type = "text"
content_types = ["ipam.prefix"]
weight = 100
}
resource "netbox_prefix" "test" {
prefix = "%[2]s"
status = "active"
custom_fields = {
"${netbox_custom_field.test.name}" = "test value"
}
}
data "netbox_prefix" "test_output" {
depends_on = [netbox_prefix.test]
prefix = "%[2]s"
}`, testField, testPrefix),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.netbox_prefix.test_output", "status", "active"),
resource.TestCheckResourceAttr("data.netbox_prefix.test_output", "prefix", testPrefix),
resource.TestCheckResourceAttr("data.netbox_prefix.test_output", "custom_fields."+testField, "test value"),
),
},
},
})
}

0 comments on commit d252161

Please sign in to comment.