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

Add custom_fields to the vlan datasource #395

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions netbox/data_source_netbox_vlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func dataSourceNetboxVlan() *schema.Resource {
Computed: true,
Optional: true,
},
"custom_fields": {
Type: schema.TypeMap,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -107,6 +111,9 @@ func dataSourceNetboxVlanRead(d *schema.ResourceData, m interface{}) error {
if vlan.Tenant != nil {
d.Set("tenant", vlan.Tenant.ID)
}
if vlan.CustomFields != nil {
d.Set("custom_fields", vlan.CustomFields)
}

return nil
}
42 changes: 42 additions & 0 deletions netbox/data_source_netbox_vlans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ resource "netbox_vlan" "test_2" {
resource "netbox_vlan" "test_3" {
name = "VLAN1236"
vid = 1236
}
type = "text"
content_types = ["ipam.vlan"]
weight = 100
default = "red"
validation_regex = "^.*$"
}
resource "netbox_custom_field" "test_field2" {
name = "field2"
type = "text"
content_types = ["ipam.vlan"]
weight = 100
default = "red"
validation_regex = "^.*$"
}
resource "netbox_vlan" "test_with_custom_fields" {
name = "VLAN_CUSTOM"
vid = 999

custom_fields = {
field1 = "value1"
field2 = "value2"
}
}`
}

Expand Down Expand Up @@ -59,6 +82,16 @@ data "netbox_vlans" "test" {
}`
}

func testAccNetboxVlansByCustomFields() string {
return `
data "netbox_vlans" "test_custom_fields" {
filter {
name = "vid"
value = "999"
}
}`
}

func TestAccNetboxVlansDataSource_basic(t *testing.T) {
setUp := testAccNetboxVlansSetUp()
// This test cannot be run in parallel with other tests, because other tests create also Vlans
Expand Down Expand Up @@ -94,6 +127,15 @@ func TestAccNetboxVlansDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair("data.netbox_vlans.test", "vlans.1.vid", "netbox_vlan.test_3", "vid"),
),
},
{
Config: setUp + testAccNetboxVlansByCustomFields(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.netbox_vlans.test_custom_fields", "vlans.#", "1"),
resource.TestCheckResourceAttrPair("data.netbox_vlans.test_custom_fields", "vlans.0.vid", "netbox_vlan.test_with_custom_fields", "vid"),
resource.TestCheckResourceAttr("data.netbox_vlans.test_custom_fields", "vlans.0.custom_fields.field1", "value1"),
resource.TestCheckResourceAttr("data.netbox_vlans.test_custom_fields", "vlans.0.custom_fields.field2", "value2"),
),
},
},
})
}
17 changes: 16 additions & 1 deletion netbox/resource_netbox_vlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func resourceNetboxVlan() *schema.Resource {
Optional: true,
Default: "",
},
tagsKey: tagsSchema,
"custom_fields": customFieldsSchema,
tagsKey: tagsSchema,
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
Expand Down Expand Up @@ -100,6 +101,10 @@ func resourceNetboxVlanCreate(d *schema.ResourceData, m interface{}) error {
data.Role = int64ToPtr(int64(roleID.(int)))
}

if customFields, ok := d.GetOk(customFieldsKey); ok {
data.CustomFields = getCustomFields(customFields)
}

data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))

params := ipam.NewIpamVlansCreateParams().WithData(&data)
Expand Down Expand Up @@ -137,6 +142,12 @@ func resourceNetboxVlanRead(d *schema.ResourceData, m interface{}) error {
d.Set("description", vlan.Description)
d.Set(tagsKey, getTagListFromNestedTagList(vlan.Tags))

if vlan.CustomFields != nil {
d.Set(customFieldsKey, vlan.CustomFields)
} else {
d.Set(customFieldsKey, nil)
}

if vlan.Status != nil {
d.Set("status", vlan.Status.Value)
}
Expand Down Expand Up @@ -186,6 +197,10 @@ func resourceNetboxVlanUpdate(d *schema.ResourceData, m interface{}) error {
data.Role = int64ToPtr(int64(roleID.(int)))
}

if customFields, ok := d.GetOk(customFieldsKey); ok {
data.CustomFields = getCustomFields(customFields)
}

data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))

params := ipam.NewIpamVlansUpdateParams().WithID(id).WithData(&data)
Expand Down
56 changes: 56 additions & 0 deletions netbox/resource_netbox_vlan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,62 @@ resource "netbox_vlan_group" "test_group" {
}
`, testName)
}

func testAccNetboxVlanWithCustomFields(testName string) string {
return fmt.Sprintf(`
resource "netbox_custom_field" "test_field1" {
name = "field1"
type = "text"
content_types = ["ipam.vlan"]
weight = 100
default = "red"
validation_regex = "^.*$"
}
resource "netbox_custom_field" "test_field2" {
name = "field2"
type = "text"
content_types = ["ipam.vlan"]
weight = 100
default = "red"
validation_regex = "^.*$"
}
resource "netbox_vlan" "test_with_custom_fields" {
name = "%s"
vid = 888
custom_fields = {
field1 = "value1"
field2 = "value2"
}
}
`, testName)
}

func TestAccNetboxVlan_customFields(t *testing.T) {
testSlug := "vlan_custom_fields"
testName := testAccGetTestName(testSlug)

resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Steps: []resource.TestStep{
{
Config: testAccNetboxVlanWithCustomFields(testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_vlan.test_with_custom_fields", "name", testName),
resource.TestCheckResourceAttr("netbox_vlan.test_with_custom_fields", "vid", "888"),
resource.TestCheckResourceAttr("netbox_vlan.test_with_custom_fields", "custom_fields.field1", "value1"),
resource.TestCheckResourceAttr("netbox_vlan.test_with_custom_fields", "custom_fields.field2", "value2"),
),
},
{
ResourceName: "netbox_vlan.test_with_custom_fields",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccNetboxVlan_basic(t *testing.T) {
testSlug := "vlan_basic"
testName := testAccGetTestName(testSlug)
Expand Down
Loading