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:
parent
44b6d7bbee
commit
760e022e46
|
@ -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 {
|
||||||
|
|
|
@ -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"
|
||||||
|
@ -448,9 +453,9 @@ resource "azure_instance" "foo" {
|
||||||
|
|
||||||
var testAccAzureInstance_separateHostedService = `
|
var testAccAzureInstance_separateHostedService = `
|
||||||
resource "azure_hosted_service" "foo" {
|
resource "azure_hosted_service" "foo" {
|
||||||
name = "%s"
|
name = "%s"
|
||||||
location = "West US"
|
location = "West US"
|
||||||
ephemeral_contents = true
|
ephemeral_contents = true
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "azure_instance" "foo" {
|
resource "azure_instance" "foo" {
|
||||||
|
@ -475,16 +480,16 @@ var testAccAzureInstance_advanced = fmt.Sprintf(`
|
||||||
resource "azure_virtual_network" "foo" {
|
resource "azure_virtual_network" "foo" {
|
||||||
name = "terraform-vnet-advanced-test"
|
name = "terraform-vnet-advanced-test"
|
||||||
address_space = ["10.1.2.0/24"]
|
address_space = ["10.1.2.0/24"]
|
||||||
location = "West US"
|
location = "West US"
|
||||||
|
|
||||||
subnet {
|
subnet {
|
||||||
name = "subnet1"
|
name = "subnet1"
|
||||||
address_prefix = "10.1.2.0/25"
|
address_prefix = "10.1.2.0/25"
|
||||||
}
|
}
|
||||||
|
|
||||||
subnet {
|
subnet {
|
||||||
name = "subnet2"
|
name = "subnet2"
|
||||||
address_prefix = "10.1.2.128/25"
|
address_prefix = "10.1.2.128/25"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -501,8 +506,8 @@ resource "azure_security_group_rule" "foo" {
|
||||||
source_port_range = "*"
|
source_port_range = "*"
|
||||||
destination_address_prefix = "*"
|
destination_address_prefix = "*"
|
||||||
destination_port_range = "3389"
|
destination_port_range = "3389"
|
||||||
action = "Deny"
|
action = "Deny"
|
||||||
type = "Inbound"
|
type = "Inbound"
|
||||||
protocol = "TCP"
|
protocol = "TCP"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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"
|
||||||
|
@ -531,16 +537,16 @@ var testAccAzureInstance_update = fmt.Sprintf(`
|
||||||
resource "azure_virtual_network" "foo" {
|
resource "azure_virtual_network" "foo" {
|
||||||
name = "terraform-vnet-update-test"
|
name = "terraform-vnet-update-test"
|
||||||
address_space = ["10.1.2.0/24"]
|
address_space = ["10.1.2.0/24"]
|
||||||
location = "West US"
|
location = "West US"
|
||||||
|
|
||||||
subnet {
|
subnet {
|
||||||
name = "subnet1"
|
name = "subnet1"
|
||||||
address_prefix = "10.1.2.0/25"
|
address_prefix = "10.1.2.0/25"
|
||||||
}
|
}
|
||||||
|
|
||||||
subnet {
|
subnet {
|
||||||
name = "subnet2"
|
name = "subnet2"
|
||||||
address_prefix = "10.1.2.128/25"
|
address_prefix = "10.1.2.128/25"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -557,8 +563,8 @@ resource "azure_security_group_rule" "foo" {
|
||||||
source_port_range = "*"
|
source_port_range = "*"
|
||||||
destination_address_prefix = "*"
|
destination_address_prefix = "*"
|
||||||
destination_port_range = "3389"
|
destination_port_range = "3389"
|
||||||
type = "Inbound"
|
type = "Inbound"
|
||||||
action = "Deny"
|
action = "Deny"
|
||||||
protocol = "TCP"
|
protocol = "TCP"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -575,8 +581,8 @@ resource "azure_security_group_rule" "bar" {
|
||||||
source_port_range = "*"
|
source_port_range = "*"
|
||||||
destination_address_prefix = "*"
|
destination_address_prefix = "*"
|
||||||
destination_port_range = "3389"
|
destination_port_range = "3389"
|
||||||
type = "Inbound"
|
type = "Inbound"
|
||||||
action = "Deny"
|
action = "Deny"
|
||||||
protocol = "TCP"
|
protocol = "TCP"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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:
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue