Skip to content

Commit

Permalink
Add data source for virtual machines IDs and names
Browse files Browse the repository at this point in the history
  • Loading branch information
inkel committed Dec 6, 2018
1 parent dbbdc01 commit 2c343a2
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
71 changes: 71 additions & 0 deletions azurerm/data_source_arm_virtual_machines.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package azurerm

import (
"fmt"
"strings"
"time"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func dataSourceArmVirtualMachines() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmVMsRead,
Schema: map[string]*schema.Schema{
"resource_group_name": resourceGroupNameForDataSourceSchema(),

"name_prefix": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},

"ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"names": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceArmVMsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).vmClient
ctx := meta.(*ArmClient).StopContext

resGroup := d.Get("resource_group_name").(string)
namePrefix := d.Get("name_prefix").(string)

resp, err := client.List(ctx, resGroup)
if err != nil {
return fmt.Errorf("Error listing Virtual Machines in the Resource Group %q: %v", resGroup, err)
}

var ids []string
var names []string

for _, vm := range resp.Values() {
if strings.HasPrefix(*vm.Name, namePrefix) {
ids = append(ids, *vm.ID)
names = append(names, *vm.Name)
}
}

d.SetId(time.Now().UTC().String())

if err := d.Set("ids", ids); err != nil {
return fmt.Errorf("Error setting `ids`: %+v", err)
}
if err := d.Set("names", names); err != nil {
return fmt.Errorf("Error setting `names`: %+v", err)
}

return nil
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_subscriptions": dataSourceArmSubscriptions(),
"azurerm_traffic_manager_geographical_location": dataSourceArmTrafficManagerGeographicalLocation(),
"azurerm_virtual_machine": dataSourceArmVirtualMachine(),
"azurerm_virtual_machines": dataSourceArmVirtualMachines(),
"azurerm_virtual_network": dataSourceArmVirtualNetwork(),
"azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(),
},
Expand Down
38 changes: 38 additions & 0 deletions website/docs/d/virtual_machines.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_virtual_machines"
sidebar_current: "docs-azurerm-datasource-virtual-machines-x"
description: |-
Gets information about existing Virtual Machines.
---

# Data Source: azurerm_virtual_machines

Use this data source to access information about existing Virtual Machines.

## Example Usage

```hcl
data "azurerm_virtual_machines" "test" {
name_prefix = "prod-"
resource_group_name = "networking"
}
output "virtual_machine_ids" {
value = "${join(", ", data.azurerm_virtual_machine.test.ids)}"
}
output "virtual_machine_names" {
value = "${join(", ", data.azurerm_virtual_machine.test.names)}"
}
```

## Argument Reference

* `name_prefix` - (Optional) Specifies the prefix to match the name of the Virtual Machines.
* `resource_group_name` - (Required) Specifies the name of the resource group the Virtual Machines are located in.

## Attributes Reference

* `ids` - List of IDs of the Virtual Machines.
* `names` - List of names of the Virtual Machines.

0 comments on commit 2c343a2

Please sign in to comment.