-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Changes from all commits
7d9975c
3a8519d
99aacf1
f2d931f
2c2b9b8
0950eae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceAzureRMVirtualMachineScaleSet_orchestrated(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_orchestrated(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet(data.ResourceName, "id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
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) | ||
} | ||
|
||
func testAccDataSourceAzureRMVirtualMachineScaleSet_orchestrated(data acceptance.TestData) string { | ||
template := testAccAzureRMWindowsVirtualMachine_orchestratedZonal(data) | ||
return fmt.Sprintf(` | ||
%s | ||
|
||
data "azurerm_virtual_machine_scale_set" "test" { | ||
name = azurerm_orchestrated_virtual_machine_scale_set.test.name | ||
resource_group_name = azurerm_resource_group.test.name | ||
} | ||
`, template) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
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) | ||
} | ||
|
||
if resp.ID == nil || *resp.ID == "" { | ||
return fmt.Errorf("Error reading Virtual Machine Scale Set %q (Resource Group %q): ID is empty or nil", name, resGroup) | ||
} | ||
d.SetId(*resp.ID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 type of the Managed Identity assigned to the Virtual Machine Scale Set. | ||
|
||
## 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. |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.