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) +}