Skip to content

Commit

Permalink
provider/azure: add custom_data argument for azure_instance resource (#…
Browse files Browse the repository at this point in the history
…8158)

* provider/azure: add custom_data argument for azure_instance resource

* website: update azure doc

* provider/azure: fix whitespace in test templates
  • Loading branch information
rjeczalik authored and stack72 committed Aug 14, 2016
1 parent 44b6d7b commit 760e022
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
41 changes: 41 additions & 0 deletions builtin/providers/azure/resource_azure_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package azure

import (
"bytes"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"fmt"
"log"
"strings"
Expand Down Expand Up @@ -208,6 +210,19 @@ func resourceAzureInstance() *schema.Resource {
Optional: 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 ""
},
},
},
}
}
Expand Down Expand Up @@ -277,6 +292,18 @@ func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) (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 {
// This is pretty ugly, but the Azure SDK leaves me no other choice...
if tp, ok := d.GetOk("ssh_key_thumbprint"); ok {
Expand All @@ -298,6 +325,13 @@ func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) (err
if err != nil {
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 {
Expand Down Expand Up @@ -325,6 +359,13 @@ func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) (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 {
Expand Down
44 changes: 25 additions & 19 deletions builtin/providers/azure/resource_azure_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func TestAccAzureInstance_basic(t *testing.T) {
"azure_instance.foo", "location", "West US"),
resource.TestCheckResourceAttr(
"azure_instance.foo", "endpoint.2462817782.public_port", "22"),
resource.TestCheckResourceAttr(
"azure_instance.foo", "custom_data", "0ea0f28b0c42d6bef7d0c7ab4886324feaa8b5e1"),
),
},
},
Expand Down Expand Up @@ -104,6 +106,8 @@ func TestAccAzureInstance_advanced(t *testing.T) {
"azure_instance.foo", "security_group", "terraform-security-group1"),
resource.TestCheckResourceAttr(
"azure_instance.foo", "endpoint.1814039778.public_port", "3389"),
resource.TestCheckResourceAttr(
"azure_instance.foo", "custom_data", "04c589e0edaa5ffe185d1e5532e77d1b2ac4b948"),
),
},
},
Expand Down Expand Up @@ -437,6 +441,7 @@ resource "azure_instance" "foo" {
location = "West US"
username = "terraform"
password = "Pass!admin123"
custom_data = "# Hello world"
endpoint {
name = "SSH"
Expand All @@ -448,9 +453,9 @@ resource "azure_instance" "foo" {

var testAccAzureInstance_separateHostedService = `
resource "azure_hosted_service" "foo" {
name = "%s"
location = "West US"
ephemeral_contents = true
name = "%s"
location = "West US"
ephemeral_contents = true
}
resource "azure_instance" "foo" {
Expand All @@ -475,16 +480,16 @@ var testAccAzureInstance_advanced = fmt.Sprintf(`
resource "azure_virtual_network" "foo" {
name = "terraform-vnet-advanced-test"
address_space = ["10.1.2.0/24"]
location = "West US"
location = "West US"
subnet {
subnet {
name = "subnet1"
address_prefix = "10.1.2.0/25"
}
address_prefix = "10.1.2.0/25"
}
subnet {
subnet {
name = "subnet2"
address_prefix = "10.1.2.128/25"
address_prefix = "10.1.2.128/25"
}
}
Expand All @@ -501,8 +506,8 @@ resource "azure_security_group_rule" "foo" {
source_port_range = "*"
destination_address_prefix = "*"
destination_port_range = "3389"
action = "Deny"
type = "Inbound"
action = "Deny"
type = "Inbound"
protocol = "TCP"
}
Expand All @@ -518,6 +523,7 @@ resource "azure_instance" "foo" {
security_group = "${azure_security_group.foo.name}"
username = "terraform"
password = "Pass!admin123"
custom_data = "IyBIZWxsbyB3b3JsZA=="
endpoint {
name = "RDP"
Expand All @@ -531,16 +537,16 @@ var testAccAzureInstance_update = fmt.Sprintf(`
resource "azure_virtual_network" "foo" {
name = "terraform-vnet-update-test"
address_space = ["10.1.2.0/24"]
location = "West US"
location = "West US"
subnet {
name = "subnet1"
address_prefix = "10.1.2.0/25"
}
address_prefix = "10.1.2.0/25"
}
subnet {
name = "subnet2"
address_prefix = "10.1.2.128/25"
address_prefix = "10.1.2.128/25"
}
}
Expand All @@ -557,8 +563,8 @@ resource "azure_security_group_rule" "foo" {
source_port_range = "*"
destination_address_prefix = "*"
destination_port_range = "3389"
type = "Inbound"
action = "Deny"
type = "Inbound"
action = "Deny"
protocol = "TCP"
}
Expand All @@ -575,8 +581,8 @@ resource "azure_security_group_rule" "bar" {
source_port_range = "*"
destination_address_prefix = "*"
destination_port_range = "3389"
type = "Inbound"
action = "Deny"
type = "Inbound"
action = "Deny"
protocol = "TCP"
}
Expand Down
2 changes: 2 additions & 0 deletions website/source/docs/providers/azure/r/instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ The following arguments are supported:
* `domain_password` - (Optional) The password for the domain_username account
specified above.

* `custom_data` - (Optional) The custom data to provide when launching the
instance.

The `endpoint` block supports:

Expand Down

0 comments on commit 760e022

Please sign in to comment.