From 712fc83a20895b7f4cbd967a2e6e7fe062932538 Mon Sep 17 00:00:00 2001 From: stack72 Date: Sun, 8 May 2016 22:45:13 +0100 Subject: [PATCH] provider/azurerm: Fix an issue with `azurerm_virtual_machine` ssh_keys ssh_keys were throwing an error similar to this: ``` * azurerm_virtual_machine.test: [DEBUG] Error setting Virtual Machine * Storage OS Profile Linux Configuration: &errors.errorString{s:"Invalid * address to set: []string{\"os_profile_linux_config\", \"0\", * \"ssh_keys\"}"} ``` This was because of nesting of Set within a Set in the schema. By changing this to a List within a Set, the schema works as expected. This means we can now set SSH Keys on VMs. This has been tested using a remote-exec and a connection block with the ssh key ``` azurerm_virtual_machine.test: Still creating... (2m10s elapsed) azurerm_virtual_machine.test (remote-exec): Connected! azurerm_virtual_machine.test (remote-exec): CONNECTED! ``` --- .../azurerm/resource_arm_virtual_machine.go | 28 ++++++------------- .../azurerm/r/virtual_machine.html.markdown | 4 ++- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/builtin/providers/azurerm/resource_arm_virtual_machine.go b/builtin/providers/azurerm/resource_arm_virtual_machine.go index 93e49fa27..4145e8f02 100644 --- a/builtin/providers/azurerm/resource_arm_virtual_machine.go +++ b/builtin/providers/azurerm/resource_arm_virtual_machine.go @@ -277,7 +277,7 @@ func resourceArmVirtualMachine() *schema.Resource { Required: true, }, "ssh_keys": &schema.Schema{ - Type: schema.TypeSet, + Type: schema.TypeList, Optional: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -291,7 +291,6 @@ func resourceArmVirtualMachine() *schema.Resource { }, }, }, - Set: resourceArmVirtualMachineStorageOsProfileLinuxConfigSshKeyHash, }, }, }, @@ -491,7 +490,7 @@ func resourceArmVirtualMachineRead(d *schema.ResourceData, meta interface{}) err } if resp.Properties.OsProfile.LinuxConfiguration != nil { - if err := d.Set("os_profile_linux_config", schema.NewSet(resourceArmVirtualMachineStorageOsProfileLinuxConfigHash, flattenAzureRmVirtualMachineOsProfileLinuxConfiguration(resp.Properties.OsProfile.LinuxConfiguration))); err != nil { + if err := d.Set("os_profile_linux_config", flattenAzureRmVirtualMachineOsProfileLinuxConfiguration(resp.Properties.OsProfile.LinuxConfiguration)); err != nil { return fmt.Errorf("[DEBUG] Error setting Virtual Machine Storage OS Profile Linux Configuration: %#v", err) } } @@ -586,17 +585,6 @@ func resourceArmVirtualMachineStorageOsDiskHash(v interface{}) int { return hashcode.String(buf.String()) } -func resourceArmVirtualMachineStorageOsProfileLinuxConfigSshKeyHash(v interface{}) int { - var buf bytes.Buffer - m := v.(map[string]interface{}) - buf.WriteString(fmt.Sprintf("%s-", m["path"].(string))) - if m["key_data"] != nil { - buf.WriteString(fmt.Sprintf("%s-", m["key_data"].(string))) - } - - return hashcode.String(buf.String()) -} - func resourceArmVirtualMachineStorageOsProfileLinuxConfigHash(v interface{}) int { var buf bytes.Buffer m := v.(map[string]interface{}) @@ -747,15 +735,15 @@ func flattenAzureRmVirtualMachineOsProfileWindowsConfiguration(config *compute.W } func flattenAzureRmVirtualMachineOsProfileLinuxConfiguration(config *compute.LinuxConfiguration) []interface{} { - result := map[string]interface{}{ - "disable_password_authentication": *config.DisablePasswordAuthentication, - } + + result := make(map[string]interface{}) + result["disable_password_authentication"] = *config.DisablePasswordAuthentication if config.SSH != nil && len(*config.SSH.PublicKeys) > 0 { - ssh_keys := make([]map[string]interface{}, 0, len(*config.SSH.PublicKeys)) + ssh_keys := make([]map[string]interface{}, len(*config.SSH.PublicKeys)) for _, i := range *config.SSH.PublicKeys { key := make(map[string]interface{}) - key["name"] = *i.Path + key["path"] = *i.Path if i.KeyData != nil { key["key_data"] = *i.KeyData @@ -910,7 +898,7 @@ func expandAzureRmVirtualMachineOsProfileLinuxConfig(d *schema.ResourceData) (*c DisablePasswordAuthentication: &disablePasswordAuth, } - linuxKeys := linuxConfig["ssh_keys"].(*schema.Set).List() + linuxKeys := linuxConfig["ssh_keys"].([]interface{}) sshPublicKeys := make([]compute.SSHPublicKey, 0, len(linuxKeys)) for _, key := range linuxKeys { sshKey := key.(map[string]interface{}) 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 d2b6233bf..892f8ba91 100644 --- a/website/source/docs/providers/azurerm/r/virtual_machine.html.markdown +++ b/website/source/docs/providers/azurerm/r/virtual_machine.html.markdown @@ -175,7 +175,9 @@ For more information on the different example configurations, please check out t `os_profile_linux_config` supports the following: * `disable_password_authentication` - (Required) Specifies whether password authentication should be disabled. -* `ssh_keys` - (Optional) Specifies a collection of `path` and `key_data` to be placed on the virtual machine. +* `ssh_keys` - (Optional) Specifies a collection of `path` and `key_data` to be placed on the virtual machine. + +~> **Note:** Please note that the only allowed `path` is `/home//.ssh/authorized_keys` due to a limitation of Azure_ `os_profile_secrets` supports the following: