Skip to content

Commit

Permalink
πŸ”§ Add tag filter for netbox_virtual_machines (e-breuninger#466)
Browse files Browse the repository at this point in the history
* πŸ”§ Add tag filter

* βœ… Add tests for tag filter

* ✏️ Fix typo
  • Loading branch information
twink0r committed Sep 15, 2023
1 parent 209e947 commit 4737192
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
5 changes: 5 additions & 0 deletions netbox/data_source_netbox_virtual_machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func dataSourceNetboxVirtualMachineRead(d *schema.ResourceData, m interface{}) e

if filter, ok := d.GetOk("filter"); ok {
var filterParams = filter.(*schema.Set)
var tags []string
for _, f := range filterParams.List() {
k := f.(map[string]interface{})["name"]
v := f.(map[string]interface{})["value"]
Expand All @@ -168,6 +169,10 @@ func dataSourceNetboxVirtualMachineRead(d *schema.ResourceData, m interface{}) e
case "site":
var siteString = v.(string)
params.Site = &siteString
case "tag":
var tagString = v.(string)
tags = append(tags, tagString)
params.Tag = tags
default:
return fmt.Errorf("'%s' is not a supported filter parameter", k)
}
Expand Down
103 changes: 103 additions & 0 deletions netbox/data_source_netbox_virtual_machines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ func TestAccNetboxVirtualMachinesDataSource_basic(t *testing.T) {
})
}

func TestAccNetboxVirtualMachinesDataSource_tags(t *testing.T) {
testSlug := "vm_ds_tags"
testName := testAccGetTestName(testSlug)
dependencies := testAccNetboxVirtualMachineDataSourceDependenciesWithTags(testName)
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: dependencies,
},
{
Config: dependencies + testAccNetboxVirtualMachineDataSourceTagA(testName) + testAccNetboxVirtualMachineDataSourceTagB(testName) + testAccNetboxVirtualMachineDataSourceTagAB(testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.netbox_virtual_machines.tag-a", "vms.#", "2"),
resource.TestCheckResourceAttr("data.netbox_virtual_machines.tag-b", "vms.#", "2"),
resource.TestCheckResourceAttr("data.netbox_virtual_machines.tag-ab", "vms.#", "1"),
),
},
},
})
}

func testAccNetboxVirtualMachineDataSourceDependencies(testName string) string {
return testAccNetboxVirtualMachineFullDependencies(testName) + fmt.Sprintf(`
resource "netbox_virtual_machine" "test0" {
Expand Down Expand Up @@ -96,6 +118,53 @@ resource "netbox_virtual_machine" "test3" {
`, testName)
}

func testAccNetboxVirtualMachineDataSourceDependenciesWithTags(testName string) string {
return testAccNetboxVirtualMachineFullDependencies(testName) + fmt.Sprintf(`
resource "netbox_tag" "servicea" {
name = "%[1]s_service-a"
}
resource "netbox_tag" "serviceb" {
name = "%[1]s_service-b"
}
resource "netbox_virtual_machine" "test0" {
name = "%[1]s_0"
cluster_id = netbox_cluster.test.id
site_id = netbox_site.test.id
comments = "thisisacomment"
memory_mb = 1024
disk_size_gb = 256
tenant_id = netbox_tenant.test.id
role_id = netbox_device_role.test.id
platform_id = netbox_platform.test.id
vcpus = 4
tags = [
netbox_tag.servicea.name,
netbox_tag.serviceb.name,
]
}
resource "netbox_virtual_machine" "test1" {
name = "%[1]s_1"
cluster_id = netbox_cluster.test.id
site_id = netbox_site.test.id
tags = [
netbox_tag.servicea.name,
]
}
resource "netbox_virtual_machine" "test2" {
name = "%[1]s_2_regex"
cluster_id = netbox_cluster.test.id
site_id = netbox_site.test.id
tags = [
netbox_tag.serviceb.name,
]
}
`, testName)
}

const testAccNetboxVirtualMachineDataSourceFilterCluster = `
data "netbox_virtual_machines" "test" {
filter {
Expand Down Expand Up @@ -127,3 +196,37 @@ data "netbox_virtual_machines" "test" {
value = netbox_cluster.test.id
}
}`

func testAccNetboxVirtualMachineDataSourceTagA(testName string) string {
return fmt.Sprintf(`
data "netbox_virtual_machines" "tag-a" {
filter {
name = "tag"
value = "%[1]s_service-a"
}
}`, testName)
}

func testAccNetboxVirtualMachineDataSourceTagB(testName string) string {
return fmt.Sprintf(`
data "netbox_virtual_machines" "tag-b" {
filter {
name = "tag"
value = "%[1]s_service-b"
}
}`, testName)
}

func testAccNetboxVirtualMachineDataSourceTagAB(testName string) string {
return fmt.Sprintf(`
data "netbox_virtual_machines" "tag-ab" {
filter {
name = "tag"
value = "%[1]s_service-a"
}
filter {
name = "tag"
value = "%[1]s_service-b"
}
}`, testName)
}

0 comments on commit 4737192

Please sign in to comment.