diff --git a/nsxt/data_source_nsxt_policy_vm_test.go b/nsxt/data_source_nsxt_policy_vm_test.go index e8c8abb37..210a6c3cf 100644 --- a/nsxt/data_source_nsxt_policy_vm_test.go +++ b/nsxt/data_source_nsxt_policy_vm_test.go @@ -16,6 +16,7 @@ func TestAccDataSourceNsxtPolicyVM_basic(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) + testAccOnlyLocalManager(t) testAccEnvDefined(t, "NSXT_TEST_VM_ID") testAccEnvDefined(t, "NSXT_TEST_VM_NAME") }, diff --git a/nsxt/data_source_nsxt_policy_vms.go b/nsxt/data_source_nsxt_policy_vms.go index 273a460f6..b71d46bfa 100644 --- a/nsxt/data_source_nsxt_policy_vms.go +++ b/nsxt/data_source_nsxt_policy_vms.go @@ -5,14 +5,28 @@ package nsxt import ( "fmt" + "strings" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" ) var valueTypeValues = []string{"bios_id", "external_id", "instance_id"} +var stateMap = map[string]string{ + "running": model.VirtualMachine_POWER_STATE_VM_RUNNING, + "stopped": model.VirtualMachine_POWER_STATE_VM_STOPPED, + "suspended": model.VirtualMachine_POWER_STATE_VM_SUSPENDED, + "unknown": model.VirtualMachine_POWER_STATE_UNKNOWN, +} + func dataSourceNsxtPolicyVMs() *schema.Resource { + stateMapKeys := []string{} + for k := range stateMap { + stateMapKeys = append(stateMapKeys, k) + } + return &schema.Resource{ Read: dataSourceNsxtPolicyVMsRead, @@ -25,6 +39,17 @@ func dataSourceNsxtPolicyVMs() *schema.Resource { ValidateFunc: validation.StringInSlice(valueTypeValues, false), Default: "bios_id", }, + "state": { + Type: schema.TypeString, + Description: "Power state of the VM", + Optional: true, + ValidateFunc: validation.StringInSlice(stateMapKeys, false), + }, + "guest_os": { + Type: schema.TypeString, + Description: "Operating system", + Optional: true, + }, "items": { Type: schema.TypeMap, Description: "Mapping of VM instance ID by display name", @@ -41,6 +66,8 @@ func dataSourceNsxtPolicyVMsRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) valueType := d.Get("value_type").(string) + state := d.Get("state").(string) + osPrefix := d.Get("guest_os").(string) vmMap := make(map[string]interface{}) allVMs, err := listAllPolicyVirtualMachines(connector, m) @@ -49,6 +76,20 @@ func dataSourceNsxtPolicyVMsRead(d *schema.ResourceData, m interface{}) error { } for _, vm := range allVMs { + if state != "" { + if vm.PowerState != nil && *vm.PowerState != stateMap[state] { + continue + } + } + + if osPrefix != "" { + if vm.GuestInfo != nil && vm.GuestInfo.OsName != nil { + osName := strings.ToLower(*vm.GuestInfo.OsName) + if !strings.HasPrefix(osName, strings.ToLower(osPrefix)) { + continue + } + } + } if vm.DisplayName == nil { continue } diff --git a/nsxt/data_source_nsxt_policy_vms_test.go b/nsxt/data_source_nsxt_policy_vms_test.go index 0275a3728..6b658c698 100644 --- a/nsxt/data_source_nsxt_policy_vms_test.go +++ b/nsxt/data_source_nsxt_policy_vms_test.go @@ -18,6 +18,7 @@ func TestAccDataSourceNsxtPolicyVMs_basic(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) + testAccOnlyLocalManager(t) testAccEnvDefined(t, "NSXT_TEST_VM_NAME") }, Providers: testAccProviders, @@ -47,6 +48,28 @@ func TestAccDataSourceNsxtPolicyVMs_basic(t *testing.T) { }) } +func TestAccDataSourceNsxtPolicyVMs_filter(t *testing.T) { + testResourceName := "data.nsxt_policy_vms.test" + checkResourceName := "nsxt_policy_group.check" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccOnlyLocalManager(t) + }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccNsxtPolicyVMsTemplateFilter(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(testResourceName, "id"), + resource.TestCheckResourceAttrSet(checkResourceName, "display_name"), + ), + }, + }, + }) +} + func testAccNsxtPolicyVMsTemplate(valueType string) string { return fmt.Sprintf(` data "nsxt_policy_vms" "test" { @@ -61,3 +84,15 @@ resource "nsxt_policy_group" "check" { display_name = data.nsxt_policy_vms.test.items["%s"] }`, valueType, getTestVMName(), getTestVMName()) } + +func testAccNsxtPolicyVMsTemplateFilter() string { + return fmt.Sprintf(` +data "nsxt_policy_vms" "test" { + state = "running" + guest_os = "ubuntu" +} + +resource "nsxt_policy_group" "check" { + display_name = length(data.nsxt_policy_vms.test.items) +}`) +} diff --git a/website/docs/d/policy_vms.html.markdown b/website/docs/d/policy_vms.html.markdown index e9ae96006..8c0c46a64 100644 --- a/website/docs/d/policy_vms.html.markdown +++ b/website/docs/d/policy_vms.html.markdown @@ -15,6 +15,8 @@ This data source is applicable to NSX Policy Manager and VMC. ```hcl data "nsxt_policy_vms" "all" { + state = "running" + guest_os = "ubuntu" value_type = "bios_id" } @@ -31,6 +33,8 @@ resource "nsxt_policy_vm_tags" "test" { ## Argument Reference * `value_tupe` - (Optional) Type of VM ID the user is interested in. Possible values are `bios_id`, `external_id`, `instance_id`. Default is `bios_id`. +* `state` - (Optional) Filter results by power state of the machine. +* `guest_os` - (Optional) Filter results by operating system of the machine. The match is case insensitive and prefix-based. ## Attributes Reference