Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Virtual Machine Scale Set Data Source #7141

Merged
merged 6 commits into from Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions azurerm/internal/services/compute/registration.go
Expand Up @@ -35,6 +35,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
"azurerm_shared_image": dataSourceArmSharedImage(),
"azurerm_snapshot": dataSourceArmSnapshot(),
"azurerm_virtual_machine": dataSourceArmVirtualMachine(),
"azurerm_virtual_machine_scale_set": dataSourceArmVirtualMachineScaleSet(),
}
}

Expand Down
Expand Up @@ -91,7 +91,7 @@ resource "azurerm_subnet" "test" {
name = "internal"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefix = "10.0.2.0/24"
address_prefixes = ["10.0.2.0/24"]
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
@@ -0,0 +1,72 @@
package tests

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
)

func TestAccDataSourceAzureRMVirtualMachineScaleSet_basicLinux(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_virtual_machine_scale_set", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMVirtualMachineScaleSet_basicLinux(data),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(data.ResourceName, "identity.#", "1"),
resource.TestCheckResourceAttr(data.ResourceName, "identity.0.type", "SystemAssigned"),
resource.TestCheckResourceAttrSet(data.ResourceName, "identity.0.principal_id"),
),
},
},
})
}

func TestAccDataSourceAzureRMVirtualMachineScaleSet_basicWindows(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_virtual_machine_scale_set", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMVirtualMachineDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMVirtualMachineScaleSet_basicWindows(data),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(data.ResourceName, "identity.#", "1"),
resource.TestCheckResourceAttr(data.ResourceName, "identity.0.type", "SystemAssigned"),
resource.TestCheckResourceAttrSet(data.ResourceName, "identity.0.principal_id"),
),
},
},
})
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also add a test case for the azurerm_orchestrated_virtual_machine_scale_set? I suppose it would also be useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ArcturusZhang Does the Orchestrated Scale Set resource set export those identity properties? I'm not sure it would be possible if not

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, orchestrated VMSS does not support the identity yet, therefore if you import an orchestrated VMSS into this data source, the identity block would be empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ArcturusZhang Got it, test added.

func testAccDataSourceAzureRMVirtualMachineScaleSet_basicLinux(data acceptance.TestData) string {
template := testAccAzureRMLinuxVirtualMachineScaleSet_identitySystemAssigned(data)
return fmt.Sprintf(`
%s

data "azurerm_virtual_machine_scale_set" "test" {
name = azurerm_linux_virtual_machine_scale_set.test.name
resource_group_name = azurerm_resource_group.test.name
}
`, template)
}

func testAccDataSourceAzureRMVirtualMachineScaleSet_basicWindows(data acceptance.TestData) string {
template := testAccAzureRMWindowsVirtualMachineScaleSet_identitySystemAssigned(data)
return fmt.Sprintf(`
%s

data "azurerm_virtual_machine_scale_set" "test" {
name = azurerm_windows_virtual_machine_scale_set.test.name
resource_group_name = azurerm_resource_group.test.name
}
`, template)
}
Expand Up @@ -95,7 +95,7 @@ resource "azurerm_subnet" "test" {
name = "internal"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefix = "10.0.2.0/24"
address_prefixes = ["10.0.2.0/24"]
}
`, name, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
@@ -0,0 +1,87 @@
package compute

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmVirtualMachineScaleSet() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmVirtualMachineScaleSetRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"location": azure.SchemaLocationForDataSource(),

"identity": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Computed: true,
},

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

"principal_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceArmVirtualMachineScaleSetRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.VMScaleSetClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

resGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)

resp, err := client.Get(ctx, resGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Virtual Machine Scale Set %q (Resource Group %q) was not found", name, resGroup)
}

return fmt.Errorf("Error making Read request on Virtual Machine Scale Set %q (Resource Group %q): %+v", name, resGroup, err)
}

d.SetId(*resp.ID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we first check if the ID is non-nil and non-empty before actually set the ID? Otherwise this would give the user a panic (if nil) or unexpected result (empty ID)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, added


if err := d.Set("identity", FlattenVirtualMachineScaleSetIdentity(resp.Identity)); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

return nil
}
4 changes: 4 additions & 0 deletions website/azurerm.erb
Expand Up @@ -601,6 +601,10 @@
<a href="/docs/providers/azurerm/d/virtual_machine.html">azurerm_virtual_machine</a>
</li>

<li>
<a href="/docs/providers/azurerm/d/virtual_machine_scale_set.html">azurerm_virtual_machine_scale_set</a>
</li>

<li>
<a href="/docs/providers/azurerm/d/virtual_network.html">azurerm_virtual_network</a>
</li>
Expand Down
56 changes: 56 additions & 0 deletions website/docs/d/virtual_machine_scale_set.html.markdown
@@ -0,0 +1,56 @@
---
subcategory: "Compute"
layout: "azurerm"
page_title: "Azure Resource Manager: Data Source: azurerm_virtual_machine_scale_set"
description: |-
Gets information about an existing Virtual Machine Scale Set.
---

# Data Source: azurerm_virtual_machine_scale_set

Use this data source to access information about an existing Virtual Machine Scale Set.

## Example Usage

```hcl
data "azurerm_virtual_machine_scale_set" "example" {
name = "existing"
resource_group_name = "existing"
}

output "id" {
value = data.azurerm_virtual_machine_scale_set.example.id
}
```

## Arguments Reference

The following arguments are supported:

* `name` - (Required) The name of this Virtual Machine Scale Set.

* `resource_group_name` - (Required) The name of the Resource Group where the Virtual Machine Scale Set exists.

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:

* `id` - The ID of the Virtual Machine Scale Set.

* `identity` - A `identity` block as defined below.

---

A `identity` block exports the following:

* `identity_ids` - The list of User Managed Identity ID's which are assigned to the Virtual Machine Scale Set.

* `principal_id` - The ID of the System Managed Service Principal assigned to the Virtual Machine Scale Set.

* `type` - The identity types of the Managed Identity assigned to the Virtual Machine Scale Set.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A minor typo

Suggested change
* `type` - The identity types of the Managed Identity assigned to the Virtual Machine Scale Set.
* `type` - The identity type of the Managed Identity assigned to the Virtual Machine Scale Set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:

* `read` - (Defaults to 5 minutes) Used when retrieving the Virtual Machine Scale Set.