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

data.azurerm_virtual_machine - export identity attribute #6826

Merged
merged 4 commits into from May 11, 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
Expand Up @@ -8,109 +8,67 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
)

func TestAccDataSourceVirtualMachine_basic(t *testing.T) {
func TestAccDataSourceVirtualMachine_basicLinux(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_virtual_machine", "test")

name := fmt.Sprintf("acctvm-%d", data.RandomInteger)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVirtualMachine_basic(data),
Check: resource.TestCheckResourceAttr(data.ResourceName, "name", name),
Config: testAccDataSourceAzureRMVirtualMachine_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"),
resource.TestCheckResourceAttrSet(data.ResourceName, "identity.0.tenant_id"),
),
},
},
})
}

func testAccDataSourceVirtualMachine_basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%[1]d"
location = "%[2]s"
}
func TestAccDataSourceAzureRMVirtualMachine_basicWindows(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_virtual_machine", "test")

resource "azurerm_virtual_network" "test" {
name = "acctvn-%[1]d"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMVirtualMachineDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMVirtualMachine_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"),
resource.TestCheckResourceAttrSet(data.ResourceName, "identity.0.tenant_id"),
),
},
},
})
}

resource "azurerm_subnet" "test" {
name = "acctsub-%[1]d"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefix = "10.0.2.0/24"
}
func testAccDataSourceAzureRMVirtualMachine_basicLinux(data acceptance.TestData) string {
template := testLinuxVirtualMachine_identitySystemAssigned(data)
return fmt.Sprintf(`
%s

resource "azurerm_network_interface" "test" {
name = "acctni-%[1]d"
location = azurerm_resource_group.test.location
data "azurerm_virtual_machine" "test" {
name = azurerm_linux_virtual_machine.test.name
resource_group_name = azurerm_resource_group.test.name

ip_configuration {
name = "testconfiguration1"
subnet_id = azurerm_subnet.test.id
private_ip_address_allocation = "dynamic"
}
}

resource "azurerm_storage_account" "test" {
name = "accsa%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_storage_container" "test" {
name = "vhds"
storage_account_name = azurerm_storage_account.test.name
container_access_type = "private"
`, template)
}

resource "azurerm_virtual_machine" "test" {
name = "acctvm-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
network_interface_ids = [azurerm_network_interface.test.id]
vm_size = "Standard_D1_v2"

storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}

storage_os_disk {
name = "myosdisk1"
vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
caching = "ReadWrite"
create_option = "FromImage"
}

os_profile {
computer_name = "linuxhost01"
admin_username = "testadmin"
admin_password = "Password1234!"
}

os_profile_linux_config {
disable_password_authentication = false
}
}
func testAccDataSourceAzureRMVirtualMachine_basicWindows(data acceptance.TestData) string {
template := testWindowsVirtualMachine_identitySystemAssigned(data)
return fmt.Sprintf(`
%s

data "azurerm_virtual_machine" "test" {
name = azurerm_linux_virtual_machine.test.name
resource_group_name = azurerm_resource_group.test.name
name = azurerm_virtual_machine.test.name
}
`, data.RandomInteger, data.Locations.Primary)
`, template)
}
11 changes: 11 additions & 0 deletions azurerm/internal/services/compute/virtual_machine.go
Expand Up @@ -96,6 +96,11 @@ func virtualMachineIdentitySchema() *schema.Schema {
Type: schema.TypeString,
Computed: true,
},

"tenant_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
}
Expand Down Expand Up @@ -149,11 +154,17 @@ func flattenVirtualMachineIdentity(input *compute.VirtualMachineIdentity) []inte
principalId = *input.PrincipalID
}

tenantId := ""
if input.TenantID != nil {
tenantId = *input.TenantID
}

return []interface{}{
map[string]interface{}{
"type": string(input.Type),
"identity_ids": identityIds,
"principal_id": principalId,
"tenant_id": tenantId,
},
}
}
Expand Down
41 changes: 39 additions & 2 deletions azurerm/internal/services/compute/virtual_machine_data_source.go
Expand Up @@ -21,13 +21,46 @@ func dataSourceArmVirtualMachine() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"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,
},

"tenant_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
Expand All @@ -51,5 +84,9 @@ func dataSourceArmVirtualMachineRead(d *schema.ResourceData, meta interface{}) e

d.SetId(*resp.ID)

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

return nil
}
19 changes: 19 additions & 0 deletions website/docs/d/virtual_machine.html.markdown
Expand Up @@ -13,6 +13,10 @@ Use this data source to access information about an existing Virtual Machine.
## Example Usage

```hcl
provider "azurerm" {
features {}
}

data "azurerm_virtual_machine" "example" {
name = "production"
resource_group_name = "networking"
Expand All @@ -26,12 +30,27 @@ output "virtual_machine_id" {
## Argument Reference

* `name` - Specifies the name of the Virtual Machine.

* `resource_group_name` - Specifies the name of the resource group the Virtual Machine is located in.

## Attributes Reference

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

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

---

An `identity` block exports the following:

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

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

* `tenant_id` - The ID of the Tenant of the System Managed Service Principal assigned to the Virtual Machine.

* `type` - The identity type of the Managed Identity assigned to the Virtual Machine.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:
Expand Down
6 changes: 6 additions & 0 deletions website/docs/r/linux_virtual_machine.html.markdown
Expand Up @@ -27,6 +27,10 @@ Manages a Linux Virtual Machine.
This example provisions a basic Linux Virtual Machine on an internal network. Additional examples of how to use the `azurerm_linux_virtual_machine` resource can be found [in the ./examples/virtual-machine/linux` directory within the Github Repository](https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/examples/virtual-machines/linux).

```hcl
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "West Europe"
Expand Down Expand Up @@ -282,6 +286,8 @@ An `identity` block exports the following:

* `principal_id` - The ID of the System Managed Service Principal.

* `tenant_id` - The ID of the Tenant the System Managed Service Principal is assigned in.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:
Expand Down
6 changes: 6 additions & 0 deletions website/docs/r/windows_virtual_machine.html.markdown
Expand Up @@ -27,6 +27,10 @@ Manages a Windows Virtual Machine.
This example provisions a basic Windows Virtual Machine on an internal network. Additional examples of how to use the `azurerm_windows_virtual_machine` resource can be found [in the ./examples/virtual-machine/windows` directory within the Github Repository](https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/examples/virtual-machine/windows).

```hcl
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "West Europe"
Expand Down Expand Up @@ -283,6 +287,8 @@ An `identity` block exports the following:

* `principal_id` - The ID of the System Managed Service Principal.

* `tenant_id` - The ID of the Tenant the System Managed Service Principal is assigned in.

## Timeouts

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