Dont send ssh_keys if length is 0. (#11804)

This commit is contained in:
Robert Rudduck 2017-02-10 05:52:33 -06:00 committed by Paul Stack
parent 045bb1039f
commit da218a37c2
2 changed files with 195 additions and 2 deletions

View File

@ -1113,8 +1113,10 @@ func expandAzureRmVirtualMachineOsProfileLinuxConfig(d *schema.ResourceData) (*c
sshPublicKeys = append(sshPublicKeys, sshPublicKey)
}
config.SSH = &compute.SSHConfiguration{
PublicKeys: &sshPublicKeys,
if len(sshPublicKeys) > 0 {
config.SSH = &compute.SSHConfiguration{
PublicKeys: &sshPublicKeys,
}
}
return config, nil

View File

@ -3,6 +3,7 @@ package azurerm
import (
"fmt"
"net/http"
"strings"
"testing"
"github.com/Azure/azure-sdk-for-go/arm/compute"
@ -411,6 +412,32 @@ func TestAccAzureRMVirtualMachine_plan(t *testing.T) {
})
}
func TestAccAzureRMVirtualMachine_changeSSHKey(t *testing.T) {
var vm compute.VirtualMachine
ri := strings.ToLower(acctest.RandString(10))
preConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_linuxMachineWithSSH, ri, ri, ri, ri, ri, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_linuxMachineWithSSHRemoved, ri, ri, ri, ri, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMVirtualMachineDestroy,
Steps: []resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm),
),
},
{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm),
),
},
},
})
}
func testCheckAzureRMVirtualMachineExists(name string, vm *compute.VirtualMachine) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
@ -2274,3 +2301,167 @@ resource "azurerm_virtual_machine" "test" {
}
}
`
var testAccAzureRMVirtualMachine_linuxMachineWithSSH = `
resource "azurerm_resource_group" "test" {
name = "acctestrg%s"
location = "southcentralus"
}
resource "azurerm_virtual_network" "test" {
name = "acctvn%s"
address_space = ["10.0.0.0/16"]
location = "southcentralus"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_subnet" "test" {
name = "acctsub%s"
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%s"
location = "southcentralus"
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%s"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "southcentralus"
account_type = "Standard_LRS"
}
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%s"
location = "southcentralus"
resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0"
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%s"
admin_username = "testadmin"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = true
ssh_keys {
path = "/home/testadmin/.ssh/authorized_keys"
key_data = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCfGyt5W1eJVpDIxlyvAWO594j/azEGohmlxYe7mgSfmUCWjuzILI6nHuHbxhpBDIZJhQ+JAeduXpii61dmThbI89ghGMhzea0OlT3p12e093zqa4goB9g40jdNKmJArER3pMVqs6hmv8y3GlUNkMDSmuoyI8AYzX4n26cUKZbwXQ== mk@mk3"
}
}
}
`
var testAccAzureRMVirtualMachine_linuxMachineWithSSHRemoved = `
resource "azurerm_resource_group" "test" {
name = "acctestrg%s"
location = "southcentralus"
}
resource "azurerm_virtual_network" "test" {
name = "acctvn%s"
address_space = ["10.0.0.0/16"]
location = "southcentralus"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_subnet" "test" {
name = "acctsub%s"
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%s"
location = "southcentralus"
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%s"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "southcentralus"
account_type = "Standard_LRS"
}
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%s"
location = "southcentralus"
resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0"
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%s"
admin_username = "testadmin"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = true
}
}
`