-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add data source for virtual machines IDs and names
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |