diff --git a/builtin/providers/azurerm/resource_arm_virtual_machine.go b/builtin/providers/azurerm/resource_arm_virtual_machine.go index 389a82953..9a1607472 100644 --- a/builtin/providers/azurerm/resource_arm_virtual_machine.go +++ b/builtin/providers/azurerm/resource_arm_virtual_machine.go @@ -434,6 +434,11 @@ func resourceArmVirtualMachine() *schema.Resource { Set: schema.HashString, }, + "primary_network_interface_id": { + Type: schema.TypeString, + Optional: true, + }, + "tags": tagsSchema(), }, } @@ -648,6 +653,15 @@ func resourceArmVirtualMachineRead(d *schema.ResourceData, meta interface{}) err if err := d.Set("network_interface_ids", flattenAzureRmVirtualMachineNetworkInterfaces(resp.VirtualMachineProperties.NetworkProfile)); err != nil { return fmt.Errorf("[DEBUG] Error setting Virtual Machine Storage Network Interfaces: %#v", err) } + + if resp.VirtualMachineProperties.NetworkProfile.NetworkInterfaces != nil { + for _, nic := range *resp.VirtualMachineProperties.NetworkProfile.NetworkInterfaces { + if nic.NetworkInterfaceReferenceProperties != nil && *nic.NetworkInterfaceReferenceProperties.Primary { + d.Set("primary_network_interface_id", nic.ID) + break + } + } + } } flattenAndSetTags(d, resp.Tags) @@ -1263,14 +1277,20 @@ func expandAzureRmVirtualMachineImageReference(d *schema.ResourceData) (*compute func expandAzureRmVirtualMachineNetworkProfile(d *schema.ResourceData) compute.NetworkProfile { nicIds := d.Get("network_interface_ids").(*schema.Set).List() + primaryNicId := d.Get("primary_network_interface_id").(string) network_interfaces := make([]compute.NetworkInterfaceReference, 0, len(nicIds)) network_profile := compute.NetworkProfile{} for _, nic := range nicIds { id := nic.(string) + primary := id == primaryNicId + network_interface := compute.NetworkInterfaceReference{ ID: &id, + NetworkInterfaceReferenceProperties: &compute.NetworkInterfaceReferenceProperties{ + Primary: &primary, + }, } network_interfaces = append(network_interfaces, network_interface) } diff --git a/builtin/providers/azurerm/resource_arm_virtual_machine_test.go b/builtin/providers/azurerm/resource_arm_virtual_machine_test.go index b81ca08c0..b3a42728a 100644 --- a/builtin/providers/azurerm/resource_arm_virtual_machine_test.go +++ b/builtin/providers/azurerm/resource_arm_virtual_machine_test.go @@ -580,6 +580,25 @@ func TestAccAzureRMVirtualMachine_windowsLicenseType(t *testing.T) { }) } +func TestAccAzureRMVirtualMachine_primaryNetworkInterfaceId(t *testing.T) { + var vm compute.VirtualMachine + ri := acctest.RandInt() + config := fmt.Sprintf(testAccAzureRMVirtualMachine_primaryNetworkInterfaceId, ri, ri, ri, ri, ri, ri, ri) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMVirtualMachineDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm), + ), + }, + }, + }) +} + var testAccAzureRMVirtualMachine_basicLinuxMachine = ` resource "azurerm_resource_group" "test" { name = "acctestRG-%d" @@ -2465,3 +2484,105 @@ resource "azurerm_virtual_machine" "test" { } } ` + +var testAccAzureRMVirtualMachine_primaryNetworkInterfaceId = ` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "West US" +} + +resource "azurerm_virtual_network" "test" { + name = "acctvn-%d" + address_space = ["10.0.0.0/16"] + location = "West US" + resource_group_name = "${azurerm_resource_group.test.name}" +} + +resource "azurerm_subnet" "test" { + name = "acctsub-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + virtual_network_name = "${azurerm_virtual_network.test.name}" + address_prefix = "10.0.2.0/24" +} + +resource "azurerm_network_interface" "test" { + name = "acctni-%d" + location = "West US" + 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_network_interface" "test2" { + name = "acctni2-%d" + location = "West US" + resource_group_name = "${azurerm_resource_group.test.name}" + + ip_configuration { + name = "testconfiguration2" + subnet_id = "${azurerm_subnet.test.id}" + private_ip_address_allocation = "dynamic" + } +} + +resource "azurerm_storage_account" "test" { + name = "accsa%d" + resource_group_name = "${azurerm_resource_group.test.name}" + location = "westus" + account_type = "Standard_LRS" + + tags { + environment = "staging" + } +} + +resource "azurerm_storage_container" "test" { + name = "vhds" + resource_group_name = "${azurerm_resource_group.test.name}" + storage_account_name = "${azurerm_storage_account.test.name}" + container_access_type = "private" +} + +resource "azurerm_virtual_machine" "test" { + name = "acctvm-%d" + location = "West US" + resource_group_name = "${azurerm_resource_group.test.name}" + network_interface_ids = ["${azurerm_network_interface.test.id}","${azurerm_network_interface.test2.id}"] + primary_network_interface_id = "${azurerm_network_interface.test.id}" + vm_size = "Standard_A3" + + storage_image_reference { + publisher = "Canonical" + offer = "UbuntuServer" + sku = "14.04.2-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" + disk_size_gb = "45" + } + + os_profile { + computer_name = "hostname" + admin_username = "testadmin" + admin_password = "Password1234!" + } + + os_profile_linux_config { + disable_password_authentication = false + } + + tags { + environment = "Production" + cost-center = "Ops" + } +} +` diff --git a/website/source/docs/providers/azurerm/r/virtual_machine.html.markdown b/website/source/docs/providers/azurerm/r/virtual_machine.html.markdown index fa60bd027..e6cc76524 100644 --- a/website/source/docs/providers/azurerm/r/virtual_machine.html.markdown +++ b/website/source/docs/providers/azurerm/r/virtual_machine.html.markdown @@ -220,6 +220,7 @@ The following arguments are supported: * `os_profile_linux_config` - (Required, when a linux machine) A Linux config block as documented below. * `os_profile_secrets` - (Optional) A collection of Secret blocks as documented below. * `network_interface_ids` - (Required) Specifies the list of resource IDs for the network interfaces associated with the virtual machine. +* `primary_network_interface_id` - (Optional) Specifies the resource ID for the primary network interface associated with the virtual machine. * `tags` - (Optional) A mapping of tags to assign to the resource. For more information on the different example configurations, please check out the [azure documentation](https://msdn.microsoft.com/en-us/library/mt163591.aspx#Anchor_2)