From 4737192b46646ccc3c08c3ae4a504d10f7851ce4 Mon Sep 17 00:00:00 2001 From: Alexander Karl Date: Wed, 13 Sep 2023 09:50:21 +0200 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=94=A7=20Add=20tag=20filter=20for=20ne?= =?UTF-8?q?tbox=5Fvirtual=5Fmachines=20(#466)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🔧 Add tag filter * ✅ Add tests for tag filter * ✏️ Fix typo --- netbox/data_source_netbox_virtual_machines.go | 5 + ...ata_source_netbox_virtual_machines_test.go | 103 ++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/netbox/data_source_netbox_virtual_machines.go b/netbox/data_source_netbox_virtual_machines.go index 4a395c51..d14db2a5 100644 --- a/netbox/data_source_netbox_virtual_machines.go +++ b/netbox/data_source_netbox_virtual_machines.go @@ -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"] @@ -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) } diff --git a/netbox/data_source_netbox_virtual_machines_test.go b/netbox/data_source_netbox_virtual_machines_test.go index e2f476fa..3fcc6eb8 100644 --- a/netbox/data_source_netbox_virtual_machines_test.go +++ b/netbox/data_source_netbox_virtual_machines_test.go @@ -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" { @@ -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 { @@ -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) +}