provider/azure: add custom_data argument for azure_instance resource (#8158)

* provider/azure: add custom_data argument for azure_instance resource

* website: update azure doc

* provider/azure: fix whitespace in test templates
This commit is contained in:
Rafal Jeczalik 2016-08-14 12:02:49 +02:00 committed by Paul Stack
parent 44b6d7bbee
commit 760e022e46
3 changed files with 68 additions and 19 deletions

View File

@ -2,7 +2,9 @@ package azure
import ( import (
"bytes" "bytes"
"crypto/sha1"
"encoding/base64" "encoding/base64"
"encoding/hex"
"fmt" "fmt"
"log" "log"
"strings" "strings"
@ -208,6 +210,19 @@ func resourceAzureInstance() *schema.Resource {
Optional: true, Optional: true,
ForceNew: true, ForceNew: true,
}, },
"custom_data": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
StateFunc: func(v interface{}) string {
if s, ok := v.(string); ok && s != "" {
hash := sha1.Sum([]byte(s))
return hex.EncodeToString(hash[:])
}
return ""
},
},
}, },
} }
} }
@ -277,6 +292,18 @@ func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) (err
return fmt.Errorf("Error configuring the deployment for %s: %s", name, err) return fmt.Errorf("Error configuring the deployment for %s: %s", name, err)
} }
var customData string
if data, ok := d.GetOk("custom_data"); ok {
data := data.(string)
// Ensure the custom_data is not double-encoded.
if _, err := base64.StdEncoding.DecodeString(data); err != nil {
customData = base64.StdEncoding.EncodeToString([]byte(data))
} else {
customData = data
}
}
if osType == linux { if osType == linux {
// This is pretty ugly, but the Azure SDK leaves me no other choice... // This is pretty ugly, but the Azure SDK leaves me no other choice...
if tp, ok := d.GetOk("ssh_key_thumbprint"); ok { if tp, ok := d.GetOk("ssh_key_thumbprint"); ok {
@ -298,6 +325,13 @@ func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) (err
if err != nil { if err != nil {
return fmt.Errorf("Error configuring %s for Linux: %s", name, err) return fmt.Errorf("Error configuring %s for Linux: %s", name, err)
} }
if customData != "" {
err = vmutils.ConfigureWithCustomDataForLinux(&role, customData)
if err != nil {
return fmt.Errorf("Error configuring custom data for %s: %s", name, err)
}
}
} }
if osType == windows { if osType == windows {
@ -325,6 +359,13 @@ func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) (err
return fmt.Errorf("Error configuring %s for WindowsToJoinDomain: %s", name, err) return fmt.Errorf("Error configuring %s for WindowsToJoinDomain: %s", name, err)
} }
} }
if customData != "" {
err = vmutils.ConfigureWithCustomDataForWindows(&role, customData)
if err != nil {
return fmt.Errorf("Error configuring custom data for %s: %s", name, err)
}
}
} }
if s := d.Get("endpoint").(*schema.Set); s.Len() > 0 { if s := d.Get("endpoint").(*schema.Set); s.Len() > 0 {

View File

@ -38,6 +38,8 @@ func TestAccAzureInstance_basic(t *testing.T) {
"azure_instance.foo", "location", "West US"), "azure_instance.foo", "location", "West US"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"azure_instance.foo", "endpoint.2462817782.public_port", "22"), "azure_instance.foo", "endpoint.2462817782.public_port", "22"),
resource.TestCheckResourceAttr(
"azure_instance.foo", "custom_data", "0ea0f28b0c42d6bef7d0c7ab4886324feaa8b5e1"),
), ),
}, },
}, },
@ -104,6 +106,8 @@ func TestAccAzureInstance_advanced(t *testing.T) {
"azure_instance.foo", "security_group", "terraform-security-group1"), "azure_instance.foo", "security_group", "terraform-security-group1"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"azure_instance.foo", "endpoint.1814039778.public_port", "3389"), "azure_instance.foo", "endpoint.1814039778.public_port", "3389"),
resource.TestCheckResourceAttr(
"azure_instance.foo", "custom_data", "04c589e0edaa5ffe185d1e5532e77d1b2ac4b948"),
), ),
}, },
}, },
@ -437,6 +441,7 @@ resource "azure_instance" "foo" {
location = "West US" location = "West US"
username = "terraform" username = "terraform"
password = "Pass!admin123" password = "Pass!admin123"
custom_data = "# Hello world"
endpoint { endpoint {
name = "SSH" name = "SSH"
@ -518,6 +523,7 @@ resource "azure_instance" "foo" {
security_group = "${azure_security_group.foo.name}" security_group = "${azure_security_group.foo.name}"
username = "terraform" username = "terraform"
password = "Pass!admin123" password = "Pass!admin123"
custom_data = "IyBIZWxsbyB3b3JsZA=="
endpoint { endpoint {
name = "RDP" name = "RDP"

View File

@ -124,6 +124,8 @@ The following arguments are supported:
* `domain_password` - (Optional) The password for the domain_username account * `domain_password` - (Optional) The password for the domain_username account
specified above. specified above.
* `custom_data` - (Optional) The custom data to provide when launching the
instance.
The `endpoint` block supports: The `endpoint` block supports: