2015-04-24 18:18:24 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-05-22 15:31:14 +02:00
|
|
|
"encoding/base64"
|
2015-04-24 18:18:24 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-05-28 00:50:45 +02:00
|
|
|
"strings"
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-06-05 16:12:21 +02:00
|
|
|
"github.com/Azure/azure-sdk-for-go/management"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/management/hostedservice"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/management/osimage"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/management/virtualmachine"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/management/virtualmachineimage"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/management/vmutils"
|
2015-04-24 18:18:24 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-05-22 15:31:14 +02:00
|
|
|
linux = "Linux"
|
|
|
|
windows = "Windows"
|
|
|
|
osDiskBlobStorageURL = "http://%s.blob.core.windows.net/vhds/%s.vhd"
|
2015-04-24 18:18:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAzureInstance() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAzureInstanceCreate,
|
|
|
|
Read: resourceAzureInstanceRead,
|
|
|
|
Update: resourceAzureInstanceUpdate,
|
|
|
|
Delete: resourceAzureInstanceDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
"hosted_service_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-04-24 18:18:24 +02:00
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"image": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"size": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
2015-04-30 10:58:45 +02:00
|
|
|
"subnet": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-05-18 15:40:45 +02:00
|
|
|
Computed: true,
|
2015-04-30 10:58:45 +02:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"virtual_network": &schema.Schema{
|
2015-04-24 18:18:24 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-06-05 16:12:21 +02:00
|
|
|
"storage_service_name": &schema.Schema{
|
2015-04-24 18:18:24 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"reverse_dns": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"location": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"automatic_updates": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2015-05-29 00:10:21 +02:00
|
|
|
Default: false,
|
2015-04-24 18:18:24 +02:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"time_zone": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"username": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"password": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ssh_key_thumbprint": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"endpoint": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"protocol": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2015-05-29 00:10:21 +02:00
|
|
|
Optional: true,
|
|
|
|
Default: "tcp",
|
2015-04-24 18:18:24 +02:00
|
|
|
},
|
|
|
|
|
2015-04-30 10:58:45 +02:00
|
|
|
"public_port": &schema.Schema{
|
2015-04-24 18:18:24 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
2015-04-30 10:58:45 +02:00
|
|
|
"private_port": &schema.Schema{
|
2015-04-24 18:18:24 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceAzureEndpointHash,
|
|
|
|
},
|
|
|
|
|
2015-05-18 15:40:45 +02:00
|
|
|
"security_group": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2015-04-30 10:58:45 +02:00
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2015-04-24 18:18:24 +02:00
|
|
|
"ip_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"vip_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-08-18 17:05:20 +02:00
|
|
|
|
|
|
|
"domain_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"domain_username": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"domain_password": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"domain_ou": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2015-04-24 18:18:24 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) (err error) {
|
2015-06-16 19:54:52 +02:00
|
|
|
azureClient := meta.(*Client)
|
|
|
|
mc := azureClient.mgmtClient
|
|
|
|
hostedServiceClient := azureClient.hostedServiceClient
|
|
|
|
vmClient := azureClient.vmClient
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-05-12 08:30:47 +02:00
|
|
|
name := d.Get("name").(string)
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-05-12 08:30:47 +02:00
|
|
|
// Compute/set the description
|
|
|
|
description := d.Get("description").(string)
|
|
|
|
if description == "" {
|
|
|
|
description = name
|
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-05-12 08:30:47 +02:00
|
|
|
// Retrieve the needed details of the image
|
2015-05-18 18:46:02 +02:00
|
|
|
configureForImage, osType, err := retrieveImageDetails(
|
2015-06-16 19:54:52 +02:00
|
|
|
meta,
|
2015-05-18 18:46:02 +02:00
|
|
|
d.Get("image").(string),
|
2015-05-28 00:50:45 +02:00
|
|
|
name,
|
2015-06-05 16:12:21 +02:00
|
|
|
d.Get("storage_service_name").(string),
|
2015-05-18 18:46:02 +02:00
|
|
|
)
|
2015-05-12 08:30:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-05-22 15:31:14 +02:00
|
|
|
// Verify if we have all required parameters
|
|
|
|
if err := verifyInstanceParameters(d, osType); err != nil {
|
2015-05-12 08:30:47 +02:00
|
|
|
return err
|
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
var hostedServiceName string
|
|
|
|
// check if hosted service name parameter was given:
|
|
|
|
if serviceName, ok := d.GetOk("hosted_service_name"); !ok {
|
|
|
|
// if not provided; just use the name of the instance to create a new one:
|
|
|
|
hostedServiceName = name
|
|
|
|
d.Set("hosted_service_name", hostedServiceName)
|
2015-05-22 15:31:14 +02:00
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
p := hostedservice.CreateHostedServiceParameters{
|
|
|
|
ServiceName: hostedServiceName,
|
|
|
|
Label: base64.StdEncoding.EncodeToString([]byte(name)),
|
|
|
|
Description: fmt.Sprintf("Cloud Service created automatically for instance %s", name),
|
|
|
|
Location: d.Get("location").(string),
|
|
|
|
ReverseDNSFqdn: d.Get("reverse_dns").(string),
|
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
log.Printf("[DEBUG] Creating Cloud Service for instance: %s", name)
|
|
|
|
err = hostedServiceClient.CreateHostedService(p)
|
2015-05-12 08:30:47 +02:00
|
|
|
if err != nil {
|
2015-06-26 14:48:55 +02:00
|
|
|
return fmt.Errorf("Error creating Cloud Service for instance %s: %s", name, err)
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
2015-06-26 14:48:55 +02:00
|
|
|
} else {
|
|
|
|
// else; use the provided hosted service name:
|
|
|
|
hostedServiceName = serviceName.(string)
|
|
|
|
}
|
2015-05-12 08:30:47 +02:00
|
|
|
|
|
|
|
// Create a new role for the instance
|
2015-05-18 18:46:02 +02:00
|
|
|
role := vmutils.NewVMConfiguration(name, d.Get("size").(string))
|
2015-05-12 08:30:47 +02:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] Configuring deployment from image...")
|
2015-05-18 18:46:02 +02:00
|
|
|
err = configureForImage(&role)
|
2015-05-12 08:30:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error configuring the deployment for %s: %s", name, err)
|
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-05-12 08:30:47 +02:00
|
|
|
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 {
|
|
|
|
err = vmutils.ConfigureForLinux(
|
|
|
|
&role,
|
|
|
|
name,
|
|
|
|
d.Get("username").(string),
|
|
|
|
d.Get("password").(string),
|
|
|
|
tp.(string),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
err = vmutils.ConfigureForLinux(
|
2015-04-24 18:18:24 +02:00
|
|
|
&role,
|
|
|
|
name,
|
|
|
|
d.Get("username").(string),
|
|
|
|
d.Get("password").(string),
|
|
|
|
)
|
|
|
|
}
|
2015-05-12 08:30:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error configuring %s for Linux: %s", name, err)
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
2015-05-12 08:30:47 +02:00
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-05-12 08:30:47 +02:00
|
|
|
if osType == windows {
|
|
|
|
err = vmutils.ConfigureForWindows(
|
|
|
|
&role,
|
|
|
|
name,
|
|
|
|
d.Get("username").(string),
|
|
|
|
d.Get("password").(string),
|
|
|
|
d.Get("automatic_updates").(bool),
|
|
|
|
d.Get("time_zone").(string),
|
|
|
|
)
|
2015-04-24 18:18:24 +02:00
|
|
|
if err != nil {
|
2015-05-12 08:30:47 +02:00
|
|
|
return fmt.Errorf("Error configuring %s for Windows: %s", name, err)
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
2015-10-07 22:35:06 +02:00
|
|
|
|
2015-08-18 17:05:20 +02:00
|
|
|
if domain_name, ok := d.GetOk("domain_name"); ok {
|
|
|
|
err = vmutils.ConfigureWindowsToJoinDomain(
|
2015-10-07 22:35:06 +02:00
|
|
|
&role,
|
|
|
|
d.Get("domain_username").(string),
|
|
|
|
d.Get("domain_password").(string),
|
|
|
|
domain_name.(string),
|
2015-08-18 17:05:20 +02:00
|
|
|
d.Get("domain_ou").(string),
|
2015-10-07 22:35:06 +02:00
|
|
|
)
|
2015-08-18 17:05:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error configuring %s for WindowsToJoinDomain: %s", name, err)
|
|
|
|
}
|
|
|
|
}
|
2015-05-12 08:30:47 +02:00
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-05-12 08:30:47 +02:00
|
|
|
if s := d.Get("endpoint").(*schema.Set); s.Len() > 0 {
|
|
|
|
for _, v := range s.List() {
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
err := vmutils.ConfigureWithExternalPort(
|
|
|
|
&role,
|
|
|
|
m["name"].(string),
|
|
|
|
m["private_port"].(int),
|
|
|
|
m["public_port"].(int),
|
|
|
|
endpointProtocol(m["protocol"].(string)),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error adding endpoint %s for instance %s: %s", m["name"].(string), name, err)
|
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
2015-05-12 08:30:47 +02:00
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-05-18 15:40:45 +02:00
|
|
|
if subnet, ok := d.GetOk("subnet"); ok {
|
|
|
|
err = vmutils.ConfigureWithSubnet(&role, subnet.(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error associating subnet %s with instance %s: %s", d.Get("subnet").(string), name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sg, ok := d.GetOk("security_group"); ok {
|
|
|
|
err = vmutils.ConfigureWithSecurityGroup(&role, sg.(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error associating security group %s with instance %s: %s", sg.(string), name, err)
|
|
|
|
}
|
2015-05-12 08:30:47 +02:00
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-05-12 08:30:47 +02:00
|
|
|
options := virtualmachine.CreateDeploymentOptions{
|
|
|
|
VirtualNetworkName: d.Get("virtual_network").(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Creating the new instance...")
|
2015-06-26 14:48:55 +02:00
|
|
|
req, err := vmClient.CreateDeployment(role, hostedServiceName, options)
|
2015-05-12 08:30:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating instance %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Waiting for the new instance to be created...")
|
2015-05-22 15:31:14 +02:00
|
|
|
if err := mc.WaitForOperation(req, nil); err != nil {
|
2015-05-12 08:30:47 +02:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for instance %s to be created: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(name)
|
2015-04-24 18:18:24 +02:00
|
|
|
|
|
|
|
return resourceAzureInstanceRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureInstanceRead(d *schema.ResourceData, meta interface{}) error {
|
2015-06-16 19:54:52 +02:00
|
|
|
azureClient := meta.(*Client)
|
|
|
|
hostedServiceClient := azureClient.hostedServiceClient
|
|
|
|
vmClient := azureClient.vmClient
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
|
|
|
// check if the instance belongs to an independent hosted service
|
|
|
|
// or it had one created for it.
|
|
|
|
var hostedServiceName string
|
|
|
|
if serviceName, ok := d.GetOk("hosted_service_name"); ok {
|
|
|
|
// if independent; use that hosted service name:
|
|
|
|
hostedServiceName = serviceName.(string)
|
|
|
|
} else {
|
|
|
|
// else; suppose it's the instance's name:
|
|
|
|
hostedServiceName = name
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Retrieving Cloud Service for instance: %s", name)
|
|
|
|
cs, err := hostedServiceClient.GetHostedService(hostedServiceName)
|
2015-04-24 18:18:24 +02:00
|
|
|
if err != nil {
|
2015-06-26 14:48:55 +02:00
|
|
|
return fmt.Errorf("Error retrieving Cloud Service of instance %s (%q): %s", name, hostedServiceName, err)
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
2015-05-18 18:46:02 +02:00
|
|
|
d.Set("reverse_dns", cs.ReverseDNSFqdn)
|
2015-04-24 18:18:24 +02:00
|
|
|
d.Set("location", cs.Location)
|
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
log.Printf("[DEBUG] Retrieving instance: %s", name)
|
|
|
|
dpmt, err := vmClient.GetDeployment(hostedServiceName, name)
|
2015-04-24 18:18:24 +02:00
|
|
|
if err != nil {
|
2015-06-01 11:18:48 +02:00
|
|
|
if management.IsResourceNotFoundError(err) {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-26 14:48:55 +02:00
|
|
|
return fmt.Errorf("Error retrieving instance %s: %s", name, err)
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
2015-04-30 10:58:45 +02:00
|
|
|
if len(dpmt.RoleList) != 1 {
|
|
|
|
return fmt.Errorf(
|
2015-06-26 14:48:55 +02:00
|
|
|
"Instance %s has an unexpected number of roles: %d", name, len(dpmt.RoleList))
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
2015-05-18 15:40:45 +02:00
|
|
|
|
2015-04-30 10:58:45 +02:00
|
|
|
d.Set("size", dpmt.RoleList[0].RoleSize)
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-04-30 10:58:45 +02:00
|
|
|
if len(dpmt.RoleInstanceList) != 1 {
|
|
|
|
return fmt.Errorf(
|
2015-05-18 15:40:45 +02:00
|
|
|
"Instance %s has an unexpected number of role instances: %d",
|
2015-06-26 14:48:55 +02:00
|
|
|
name, len(dpmt.RoleInstanceList))
|
2015-04-30 10:58:45 +02:00
|
|
|
}
|
2015-05-18 18:46:02 +02:00
|
|
|
d.Set("ip_address", dpmt.RoleInstanceList[0].IPAddress)
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-04-30 10:58:45 +02:00
|
|
|
if len(dpmt.RoleInstanceList[0].InstanceEndpoints) > 0 {
|
|
|
|
d.Set("vip_address", dpmt.RoleInstanceList[0].InstanceEndpoints[0].Vip)
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
2015-05-18 15:40:45 +02:00
|
|
|
// Find the network configuration set
|
|
|
|
for _, c := range dpmt.RoleList[0].ConfigurationSets {
|
2015-04-30 10:58:45 +02:00
|
|
|
if c.ConfigurationSetType == virtualmachine.ConfigurationSetTypeNetwork {
|
2015-05-18 15:40:45 +02:00
|
|
|
// Create a new set to hold all configured endpoints
|
|
|
|
endpoints := &schema.Set{
|
|
|
|
F: resourceAzureEndpointHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through all endpoints
|
|
|
|
for _, ep := range c.InputEndpoints {
|
2015-04-30 10:58:45 +02:00
|
|
|
endpoint := map[string]interface{}{}
|
|
|
|
|
|
|
|
// Update the values
|
|
|
|
endpoint["name"] = ep.Name
|
|
|
|
endpoint["protocol"] = string(ep.Protocol)
|
|
|
|
endpoint["public_port"] = ep.Port
|
|
|
|
endpoint["private_port"] = ep.LocalPort
|
|
|
|
endpoints.Add(endpoint)
|
|
|
|
}
|
|
|
|
d.Set("endpoint", endpoints)
|
2015-05-18 15:40:45 +02:00
|
|
|
|
|
|
|
// Update the subnet
|
|
|
|
switch len(c.SubnetNames) {
|
|
|
|
case 1:
|
|
|
|
d.Set("subnet", c.SubnetNames[0])
|
|
|
|
case 0:
|
|
|
|
d.Set("subnet", "")
|
|
|
|
default:
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Instance %s has an unexpected number of associated subnets %d",
|
2015-06-26 14:48:55 +02:00
|
|
|
name, len(dpmt.RoleInstanceList))
|
2015-05-18 15:40:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the security group
|
|
|
|
d.Set("security_group", c.NetworkSecurityGroup)
|
2015-04-30 10:58:45 +02:00
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
connType := "ssh"
|
2015-04-30 10:58:45 +02:00
|
|
|
if dpmt.RoleList[0].OSVirtualHardDisk.OS == windows {
|
2015-05-22 15:31:14 +02:00
|
|
|
connType = "winrm"
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the connection info for any configured provisioners
|
|
|
|
d.SetConnInfo(map[string]string{
|
2015-05-22 15:31:14 +02:00
|
|
|
"type": connType,
|
|
|
|
"host": dpmt.VirtualIPs[0].Address,
|
|
|
|
"user": d.Get("username").(string),
|
|
|
|
"password": d.Get("password").(string),
|
2015-04-24 18:18:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-06-16 19:54:52 +02:00
|
|
|
azureClient := meta.(*Client)
|
|
|
|
mc := azureClient.mgmtClient
|
|
|
|
vmClient := azureClient.vmClient
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-04-30 10:58:45 +02:00
|
|
|
// First check if anything we can update changed, and if not just return
|
2015-05-18 15:40:45 +02:00
|
|
|
if !d.HasChange("size") && !d.HasChange("endpoint") && !d.HasChange("security_group") {
|
2015-04-30 10:58:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
hostedServiceName := d.Get("hosted_service_name").(string)
|
|
|
|
|
2015-04-30 10:58:45 +02:00
|
|
|
// Get the current role
|
2015-06-26 14:48:55 +02:00
|
|
|
role, err := vmClient.GetRole(hostedServiceName, name, name)
|
2015-04-30 10:58:45 +02:00
|
|
|
if err != nil {
|
2015-06-26 14:48:55 +02:00
|
|
|
return fmt.Errorf("Error retrieving role of instance %s: %s", name, err)
|
2015-04-30 10:58:45 +02:00
|
|
|
}
|
|
|
|
|
2015-05-22 15:31:14 +02:00
|
|
|
// Verify if we have all required parameters
|
|
|
|
if err := verifyInstanceParameters(d, role.OSVirtualHardDisk.OS); err != nil {
|
2015-04-30 10:58:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("size") {
|
2015-04-24 18:18:24 +02:00
|
|
|
role.RoleSize = d.Get("size").(string)
|
2015-04-30 10:58:45 +02:00
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-04-30 10:58:45 +02:00
|
|
|
if d.HasChange("endpoint") {
|
|
|
|
_, n := d.GetChange("endpoint")
|
|
|
|
|
|
|
|
// Delete the existing endpoints
|
2015-05-18 15:40:45 +02:00
|
|
|
for i, c := range role.ConfigurationSets {
|
2015-04-30 10:58:45 +02:00
|
|
|
if c.ConfigurationSetType == virtualmachine.ConfigurationSetTypeNetwork {
|
|
|
|
c.InputEndpoints = nil
|
2015-05-18 15:40:45 +02:00
|
|
|
role.ConfigurationSets[i] = c
|
2015-04-30 10:58:45 +02:00
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
2015-04-30 10:58:45 +02:00
|
|
|
// And add the ones we still want
|
|
|
|
if s := n.(*schema.Set); s.Len() > 0 {
|
|
|
|
for _, v := range s.List() {
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
err := vmutils.ConfigureWithExternalPort(
|
|
|
|
role,
|
|
|
|
m["name"].(string),
|
|
|
|
m["private_port"].(int),
|
|
|
|
m["public_port"].(int),
|
|
|
|
endpointProtocol(m["protocol"].(string)),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
2015-06-26 14:48:55 +02:00
|
|
|
"Error adding endpoint %s for instance %s: %s", m["name"].(string), name, err)
|
2015-04-30 10:58:45 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-18 15:40:45 +02:00
|
|
|
if d.HasChange("security_group") {
|
|
|
|
sg := d.Get("security_group").(string)
|
|
|
|
err := vmutils.ConfigureWithSecurityGroup(role, sg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
2015-06-26 14:48:55 +02:00
|
|
|
"Error associating security group %s with instance %s: %s", sg, name, err)
|
2015-05-18 15:40:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 10:58:45 +02:00
|
|
|
// Update the adjusted role
|
2015-06-26 14:48:55 +02:00
|
|
|
req, err := vmClient.UpdateRole(hostedServiceName, name, name, *role)
|
2015-04-30 10:58:45 +02:00
|
|
|
if err != nil {
|
2015-06-26 14:48:55 +02:00
|
|
|
return fmt.Errorf("Error updating role of instance %s: %s", name, err)
|
2015-04-30 10:58:45 +02:00
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-05-22 15:31:14 +02:00
|
|
|
if err := mc.WaitForOperation(req, nil); err != nil {
|
2015-04-30 10:58:45 +02:00
|
|
|
return fmt.Errorf(
|
2015-06-26 14:48:55 +02:00
|
|
|
"Error waiting for role of instance %s to be updated: %s", name, err)
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return resourceAzureInstanceRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureInstanceDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-06-16 19:54:52 +02:00
|
|
|
azureClient := meta.(*Client)
|
|
|
|
mc := azureClient.mgmtClient
|
2015-06-26 14:48:55 +02:00
|
|
|
vmClient := azureClient.vmClient
|
2015-06-16 19:54:52 +02:00
|
|
|
hostedServiceClient := azureClient.hostedServiceClient
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
hostedServiceName := d.Get("hosted_service_name").(string)
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
log.Printf("[DEBUG] Deleting instance: %s", name)
|
2015-04-24 18:18:24 +02:00
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
// check if the instance had a hosted service created especially for it:
|
|
|
|
if name == hostedServiceName {
|
|
|
|
// if so; we must delete the associated hosted service as well:
|
|
|
|
req, err := hostedServiceClient.DeleteHostedService(name, true)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting instance and hosted service %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until the hosted service and the instance it contains is deleted:
|
|
|
|
if err := mc.WaitForOperation(req, nil); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for instance %s to be deleted: %s", name, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// else; just delete the instance:
|
|
|
|
reqID, err := vmClient.DeleteDeployment(hostedServiceName, name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting instance %s off hosted service %s: %s", name, hostedServiceName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// and wait for the deletion:
|
|
|
|
if err := mc.WaitForOperation(reqID, nil); err != nil {
|
|
|
|
return fmt.Errorf("Error waiting for intance %s to be deleted off the hosted service %s: %s",
|
|
|
|
name, hostedServiceName, err)
|
|
|
|
}
|
|
|
|
}
|
2015-04-24 18:18:24 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureEndpointHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["protocol"].(string)))
|
2015-04-30 10:58:45 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["public_port"].(int)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["private_port"].(int)))
|
2015-04-24 18:18:24 +02:00
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
|
|
|
|
2015-05-18 18:46:02 +02:00
|
|
|
func retrieveImageDetails(
|
2015-06-16 19:54:52 +02:00
|
|
|
meta interface{},
|
2015-05-18 18:46:02 +02:00
|
|
|
label string,
|
2015-05-28 00:50:45 +02:00
|
|
|
name string,
|
2015-05-18 18:46:02 +02:00
|
|
|
storage string) (func(*virtualmachine.Role) error, string, error) {
|
2015-06-16 19:54:52 +02:00
|
|
|
|
|
|
|
azureClient := meta.(*Client)
|
|
|
|
vmImageClient := azureClient.vmImageClient
|
|
|
|
osImageClient := azureClient.osImageClient
|
|
|
|
|
|
|
|
configureForImage, osType, VMLabels, err := retrieveVMImageDetails(vmImageClient, label)
|
2015-05-18 15:40:45 +02:00
|
|
|
if err == nil {
|
2015-05-18 18:46:02 +02:00
|
|
|
return configureForImage, osType, nil
|
2015-05-18 15:40:45 +02:00
|
|
|
}
|
|
|
|
|
2015-06-16 19:54:52 +02:00
|
|
|
configureForImage, osType, OSLabels, err := retrieveOSImageDetails(osImageClient, label, name, storage)
|
2015-05-18 15:40:45 +02:00
|
|
|
if err == nil {
|
2015-05-18 18:46:02 +02:00
|
|
|
return configureForImage, osType, nil
|
2015-05-18 15:40:45 +02:00
|
|
|
}
|
|
|
|
|
2015-07-27 17:23:42 +02:00
|
|
|
if err == PlatformStorageError {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2015-05-28 00:50:45 +02:00
|
|
|
return nil, "", fmt.Errorf("Could not find image with label '%s'. Available images are: %s",
|
|
|
|
label, strings.Join(append(VMLabels, OSLabels...), ", "))
|
2015-05-18 15:40:45 +02:00
|
|
|
}
|
|
|
|
|
2015-05-22 15:31:14 +02:00
|
|
|
func retrieveVMImageDetails(
|
2015-06-16 19:54:52 +02:00
|
|
|
vmImageClient virtualmachineimage.Client,
|
2015-05-28 00:50:45 +02:00
|
|
|
label string) (func(*virtualmachine.Role) error, string, []string, error) {
|
2015-06-16 19:54:52 +02:00
|
|
|
imgs, err := vmImageClient.ListVirtualMachineImages()
|
2015-04-24 18:18:24 +02:00
|
|
|
if err != nil {
|
2015-05-28 00:50:45 +02:00
|
|
|
return nil, "", nil, fmt.Errorf("Error retrieving image details: %s", err)
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
2015-05-28 00:50:45 +02:00
|
|
|
var labels []string
|
2015-05-22 15:31:14 +02:00
|
|
|
for _, img := range imgs.VMImages {
|
2015-04-24 18:18:24 +02:00
|
|
|
if img.Label == label {
|
2015-05-22 15:31:14 +02:00
|
|
|
if img.OSDiskConfiguration.OS != linux && img.OSDiskConfiguration.OS != windows {
|
2015-05-28 00:50:45 +02:00
|
|
|
return nil, "", nil, fmt.Errorf("Unsupported image OS: %s", img.OSDiskConfiguration.OS)
|
2015-05-18 18:46:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
configureForImage := func(role *virtualmachine.Role) error {
|
2015-05-22 15:31:14 +02:00
|
|
|
return vmutils.ConfigureDeploymentFromVMImage(
|
2015-05-18 18:46:02 +02:00
|
|
|
role,
|
|
|
|
img.Name,
|
2015-05-22 15:31:14 +02:00
|
|
|
"",
|
|
|
|
true,
|
2015-05-18 18:46:02 +02:00
|
|
|
)
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
2015-05-18 18:46:02 +02:00
|
|
|
|
2015-05-28 00:50:45 +02:00
|
|
|
return configureForImage, img.OSDiskConfiguration.OS, nil, nil
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
2015-05-28 00:50:45 +02:00
|
|
|
|
|
|
|
labels = append(labels, img.Label)
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
2015-05-28 00:50:45 +02:00
|
|
|
return nil, "", labels, fmt.Errorf("Could not find image with label '%s'", label)
|
2015-05-18 15:40:45 +02:00
|
|
|
}
|
|
|
|
|
2015-05-22 15:31:14 +02:00
|
|
|
func retrieveOSImageDetails(
|
2015-06-16 19:54:52 +02:00
|
|
|
osImageClient osimage.OSImageClient,
|
2015-05-28 00:50:45 +02:00
|
|
|
label string,
|
|
|
|
name string,
|
|
|
|
storage string) (func(*virtualmachine.Role) error, string, []string, error) {
|
2015-06-16 19:54:52 +02:00
|
|
|
imgs, err := osImageClient.ListOSImages()
|
2015-05-18 15:40:45 +02:00
|
|
|
if err != nil {
|
2015-05-28 00:50:45 +02:00
|
|
|
return nil, "", nil, fmt.Errorf("Error retrieving image details: %s", err)
|
2015-05-18 15:40:45 +02:00
|
|
|
}
|
|
|
|
|
2015-05-28 00:50:45 +02:00
|
|
|
var labels []string
|
2015-05-22 15:31:14 +02:00
|
|
|
for _, img := range imgs.OSImages {
|
2015-05-18 18:46:02 +02:00
|
|
|
if img.Label == label {
|
2015-05-22 15:31:14 +02:00
|
|
|
if img.OS != linux && img.OS != windows {
|
2015-05-28 00:50:45 +02:00
|
|
|
return nil, "", nil, fmt.Errorf("Unsupported image OS: %s", img.OS)
|
2015-05-22 15:31:14 +02:00
|
|
|
}
|
|
|
|
if img.MediaLink == "" {
|
|
|
|
if storage == "" {
|
2015-07-30 16:27:13 +02:00
|
|
|
return nil, "", nil, PlatformStorageError
|
2015-05-22 15:31:14 +02:00
|
|
|
}
|
2015-05-28 00:50:45 +02:00
|
|
|
img.MediaLink = fmt.Sprintf(osDiskBlobStorageURL, storage, name)
|
2015-05-18 15:40:45 +02:00
|
|
|
}
|
2015-05-18 18:46:02 +02:00
|
|
|
|
|
|
|
configureForImage := func(role *virtualmachine.Role) error {
|
2015-05-22 15:31:14 +02:00
|
|
|
return vmutils.ConfigureDeploymentFromPlatformImage(
|
2015-05-18 18:46:02 +02:00
|
|
|
role,
|
|
|
|
img.Name,
|
2015-05-22 15:31:14 +02:00
|
|
|
img.MediaLink,
|
|
|
|
label,
|
2015-05-18 18:46:02 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2015-05-28 00:50:45 +02:00
|
|
|
return configureForImage, img.OS, nil, nil
|
2015-05-18 15:40:45 +02:00
|
|
|
}
|
2015-05-28 00:50:45 +02:00
|
|
|
|
|
|
|
labels = append(labels, img.Label)
|
2015-05-18 15:40:45 +02:00
|
|
|
}
|
|
|
|
|
2015-05-28 00:50:45 +02:00
|
|
|
return nil, "", labels, fmt.Errorf("Could not find image with label '%s'", label)
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
2015-04-30 10:58:45 +02:00
|
|
|
func endpointProtocol(p string) virtualmachine.InputEndpointProtocol {
|
|
|
|
if p == "tcp" {
|
2015-05-18 18:46:02 +02:00
|
|
|
return virtualmachine.InputEndpointProtocolTCP
|
2015-04-30 10:58:45 +02:00
|
|
|
}
|
|
|
|
|
2015-05-18 18:46:02 +02:00
|
|
|
return virtualmachine.InputEndpointProtocolUDP
|
2015-04-30 10:58:45 +02:00
|
|
|
}
|
|
|
|
|
2015-05-22 15:31:14 +02:00
|
|
|
func verifyInstanceParameters(d *schema.ResourceData, osType string) error {
|
2015-04-24 18:18:24 +02:00
|
|
|
if osType == linux {
|
|
|
|
_, pass := d.GetOk("password")
|
|
|
|
_, key := d.GetOk("ssh_key_thumbprint")
|
|
|
|
|
|
|
|
if !pass && !key {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"You must supply a 'password' and/or a 'ssh_key_thumbprint' when using a Linux image")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if osType == windows {
|
|
|
|
if _, ok := d.GetOk("password"); !ok {
|
|
|
|
return fmt.Errorf("You must supply a 'password' when using a Windows image")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := d.GetOk("time_zone"); !ok {
|
|
|
|
return fmt.Errorf("You must supply a 'time_zone' when using a Windows image")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 10:58:45 +02:00
|
|
|
if _, ok := d.GetOk("subnet"); ok {
|
|
|
|
if _, ok := d.GetOk("virtual_network"); !ok {
|
|
|
|
return fmt.Errorf("You must also supply a 'virtual_network' when supplying a 'subnet'")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := d.Get("endpoint").(*schema.Set); s.Len() > 0 {
|
|
|
|
for _, v := range s.List() {
|
|
|
|
protocol := v.(map[string]interface{})["protocol"].(string)
|
|
|
|
|
|
|
|
if protocol != "tcp" && protocol != "udp" {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Invalid endpoint protocol %s! Valid options are 'tcp' and 'udp'.", protocol)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 18:18:24 +02:00
|
|
|
return nil
|
|
|
|
}
|