Skip to content

Commit

Permalink
Add filtering options to vms data source
Browse files Browse the repository at this point in the history
VMs data source map can now be filtered by power state and
operation system.

Signed-off-by: Anna Khmelnitsky <[email protected]>
  • Loading branch information
annakhm committed Apr 6, 2023
1 parent 1d58a12 commit 5cb1616
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions nsxt/data_source_nsxt_policy_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
},
Expand Down
41 changes: 41 additions & 0 deletions nsxt/data_source_nsxt_policy_vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -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",
Expand All @@ -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)
Expand All @@ -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
}
Expand Down
35 changes: 35 additions & 0 deletions nsxt/data_source_nsxt_policy_vms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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" {
Expand All @@ -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)
}`)
}
3 changes: 3 additions & 0 deletions website/docs/d/policy_vms.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This data source is applicable to NSX Policy Manager and VMC.

```hcl
data "nsxt_policy_vms" "all" {
state = "running"
value_type = "bios_id"
}
Expand All @@ -31,6 +32,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.
* `os` - (Optional) Filter results by operating system of the machine. The match is case insensitive and prefix-based.

## Attributes Reference

Expand Down

0 comments on commit 5cb1616

Please sign in to comment.