2016-02-10 21:30:32 +01:00
|
|
|
package triton
|
|
|
|
|
|
|
|
import (
|
2017-05-10 23:54:57 +02:00
|
|
|
"context"
|
2016-02-10 21:30:32 +01:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
|
2016-04-19 16:42:29 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2017-03-31 00:25:27 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2016-02-10 21:30:32 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2017-03-31 00:25:27 +02:00
|
|
|
"github.com/joyent/triton-go"
|
2016-02-10 21:30:32 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
machineStateRunning = "running"
|
|
|
|
machineStateDeleted = "deleted"
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
machineStateChangeTimeout = 10 * time.Minute
|
2016-02-10 21:30:32 +01:00
|
|
|
|
|
|
|
resourceMachineMetadataKeys = map[string]string{
|
|
|
|
// semantics: "schema_name": "metadata_name"
|
|
|
|
"root_authorized_keys": "root_authorized_keys",
|
|
|
|
"user_script": "user-script",
|
|
|
|
"user_data": "user-data",
|
|
|
|
"administrator_pw": "administrator-pw",
|
2017-03-18 02:03:39 +01:00
|
|
|
"cloud_config": "cloud-init:user-data",
|
2016-02-10 21:30:32 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceMachine() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
2017-03-31 00:25:27 +02:00
|
|
|
Create: resourceMachineCreate,
|
|
|
|
Exists: resourceMachineExists,
|
|
|
|
Read: resourceMachineRead,
|
|
|
|
Update: resourceMachineUpdate,
|
|
|
|
Delete: resourceMachineDelete,
|
|
|
|
Timeouts: slowResourceTimeout,
|
2016-05-05 02:30:27 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
2017-03-31 00:25:27 +02:00
|
|
|
State: schema.ImportStatePassthrough,
|
2016-05-05 02:30:27 +02:00
|
|
|
},
|
2016-02-10 21:30:32 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Friendly name for machine",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ValidateFunc: resourceMachineValidateName,
|
|
|
|
},
|
|
|
|
"type": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Machine type (smartmachine or virtualmachine)",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"dataset": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Dataset URN with which the machine was provisioned",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"memory": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Amount of memory allocated to the machine (in Mb)",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"disk": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Amount of disk allocated to the machine (in Gb)",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"ips": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "IP addresses assigned to the machine",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Computed: true,
|
|
|
|
Elem: &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"tags": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Machine tags",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeMap,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"created": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "When the machine was created",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"updated": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "When the machine was updated",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"package": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "The package for use for provisioning",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"image": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "UUID of the image",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"primaryip": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Primary (public) IP address for the machine",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-04-19 16:42:29 +02:00
|
|
|
"nic": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Network interface",
|
2016-04-19 16:42:29 +02:00
|
|
|
Type: schema.TypeSet,
|
2016-02-10 21:30:32 +01:00
|
|
|
Computed: true,
|
2016-04-19 16:42:29 +02:00
|
|
|
Optional: true,
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
return hashcode.String(m["network"].(string))
|
|
|
|
},
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"ip": {
|
|
|
|
Description: "NIC's IPv4 address",
|
|
|
|
Computed: true,
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
"mac": {
|
|
|
|
Description: "NIC's MAC address",
|
|
|
|
Computed: true,
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
"primary": {
|
|
|
|
Description: "Whether this is the machine's primary NIC",
|
|
|
|
Computed: true,
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
},
|
|
|
|
"netmask": {
|
|
|
|
Description: "IPv4 netmask",
|
|
|
|
Computed: true,
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
"gateway": {
|
|
|
|
Description: "IPv4 gateway",
|
|
|
|
Computed: true,
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
"network": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "ID of the network to which the NIC is attached",
|
2016-04-19 16:42:29 +02:00
|
|
|
Required: true,
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
2017-03-31 00:25:27 +02:00
|
|
|
"state": {
|
|
|
|
Description: "Provisioning state of the NIC",
|
|
|
|
Computed: true,
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
2016-04-19 16:42:29 +02:00
|
|
|
},
|
2016-02-10 21:30:32 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"firewall_enabled": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Whether to enable the firewall for this machine",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
2016-06-26 23:18:17 +02:00
|
|
|
"domain_names": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "List of domain names from Triton CNS",
|
2016-06-26 23:18:17 +02:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Computed: true,
|
|
|
|
Elem: &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
},
|
2016-02-10 21:30:32 +01:00
|
|
|
|
|
|
|
// computed resources from metadata
|
|
|
|
"root_authorized_keys": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Authorized keys for the root user on this machine",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"user_script": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "User script to run on boot (every boot on SmartMachines)",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2017-03-18 02:03:39 +01:00
|
|
|
"cloud_config": {
|
|
|
|
Description: "copied to machine on boot",
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-02-10 21:30:32 +01:00
|
|
|
"user_data": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Data copied to machine on boot",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"administrator_pw": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Administrator's initial password (Windows only)",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-04-19 16:42:29 +02:00
|
|
|
|
|
|
|
// deprecated fields
|
|
|
|
"networks": {
|
2017-03-31 00:25:27 +02:00
|
|
|
Description: "Desired network IDs",
|
2016-04-19 16:42:29 +02:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
Deprecated: "Networks is deprecated, please use `nic`",
|
|
|
|
Elem: &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
},
|
2016-02-10 21:30:32 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceMachineCreate(d *schema.ResourceData, meta interface{}) error {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
|
|
|
var networks []string
|
|
|
|
for _, network := range d.Get("networks").([]interface{}) {
|
|
|
|
networks = append(networks, network.(string))
|
|
|
|
}
|
2016-04-19 16:42:29 +02:00
|
|
|
nics := d.Get("nic").(*schema.Set)
|
|
|
|
for _, nicI := range nics.List() {
|
|
|
|
nic := nicI.(map[string]interface{})
|
|
|
|
networks = append(networks, nic["network"].(string))
|
|
|
|
}
|
2016-02-10 21:30:32 +01:00
|
|
|
|
|
|
|
metadata := map[string]string{}
|
|
|
|
for schemaName, metadataKey := range resourceMachineMetadataKeys {
|
|
|
|
if v, ok := d.GetOk(schemaName); ok {
|
|
|
|
metadata[metadataKey] = v.(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tags := map[string]string{}
|
|
|
|
for k, v := range d.Get("tags").(map[string]interface{}) {
|
|
|
|
tags[k] = v.(string)
|
|
|
|
}
|
|
|
|
|
2017-05-10 23:54:57 +02:00
|
|
|
machine, err := client.Machines().CreateMachine(context.Background(), &triton.CreateMachineInput{
|
2016-02-10 21:30:32 +01:00
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Package: d.Get("package").(string),
|
|
|
|
Image: d.Get("image").(string),
|
|
|
|
Networks: networks,
|
|
|
|
Metadata: metadata,
|
|
|
|
Tags: tags,
|
|
|
|
FirewallEnabled: d.Get("firewall_enabled").(bool),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
d.SetId(machine.ID)
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Target: []string{fmt.Sprintf(machineStateRunning)},
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
2017-05-10 23:54:57 +02:00
|
|
|
getResp, err := client.Machines().GetMachine(context.Background(), &triton.GetMachineInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getResp, getResp.State, nil
|
|
|
|
},
|
|
|
|
Timeout: machineStateChangeTimeout,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
_, err = stateConf.WaitForState()
|
2016-02-10 21:30:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
// refresh state after it provisions
|
|
|
|
return resourceMachineRead(d, meta)
|
2016-02-10 21:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceMachineExists(d *schema.ResourceData, meta interface{}) (bool, error) {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
2017-05-10 23:54:57 +02:00
|
|
|
return resourceExists(client.Machines().GetMachine(context.Background(), &triton.GetMachineInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
}))
|
2016-02-10 21:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceMachineRead(d *schema.ResourceData, meta interface{}) error {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
2017-05-10 23:54:57 +02:00
|
|
|
machine, err := client.Machines().GetMachine(context.Background(), &triton.GetMachineInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
})
|
2016-02-10 21:30:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-10 23:54:57 +02:00
|
|
|
nics, err := client.Machines().ListNICs(context.Background(), &triton.ListNICsInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
MachineID: d.Id(),
|
|
|
|
})
|
2016-04-19 16:42:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-10 21:30:32 +01:00
|
|
|
d.Set("name", machine.Name)
|
|
|
|
d.Set("type", machine.Type)
|
|
|
|
d.Set("state", machine.State)
|
2017-03-31 00:25:27 +02:00
|
|
|
d.Set("dataset", machine.Image)
|
|
|
|
d.Set("image", machine.Image)
|
2016-02-10 21:30:32 +01:00
|
|
|
d.Set("memory", machine.Memory)
|
|
|
|
d.Set("disk", machine.Disk)
|
|
|
|
d.Set("ips", machine.IPs)
|
|
|
|
d.Set("tags", machine.Tags)
|
|
|
|
d.Set("created", machine.Created)
|
|
|
|
d.Set("updated", machine.Updated)
|
|
|
|
d.Set("package", machine.Package)
|
|
|
|
d.Set("image", machine.Image)
|
|
|
|
d.Set("primaryip", machine.PrimaryIP)
|
|
|
|
d.Set("firewall_enabled", machine.FirewallEnabled)
|
2016-06-26 23:18:17 +02:00
|
|
|
d.Set("domain_names", machine.DomainNames)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
2016-04-19 16:42:29 +02:00
|
|
|
// create and update NICs
|
|
|
|
var (
|
|
|
|
machineNICs []map[string]interface{}
|
|
|
|
networks []string
|
|
|
|
)
|
|
|
|
for _, nic := range nics {
|
|
|
|
machineNICs = append(
|
|
|
|
machineNICs,
|
|
|
|
map[string]interface{}{
|
|
|
|
"ip": nic.IP,
|
|
|
|
"mac": nic.MAC,
|
|
|
|
"primary": nic.Primary,
|
|
|
|
"netmask": nic.Netmask,
|
|
|
|
"gateway": nic.Gateway,
|
|
|
|
"state": nic.State,
|
|
|
|
"network": nic.Network,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
networks = append(networks, nic.Network)
|
|
|
|
}
|
|
|
|
d.Set("nic", machineNICs)
|
|
|
|
d.Set("networks", networks)
|
|
|
|
|
2016-02-10 21:30:32 +01:00
|
|
|
// computed attributes from metadata
|
|
|
|
for schemaName, metadataKey := range resourceMachineMetadataKeys {
|
|
|
|
d.Set(schemaName, machine.Metadata[metadataKey])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceMachineUpdate(d *schema.ResourceData, meta interface{}) error {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
|
|
|
d.Partial(true)
|
|
|
|
|
|
|
|
if d.HasChange("name") {
|
2017-03-31 00:25:27 +02:00
|
|
|
oldNameInterface, newNameInterface := d.GetChange("name")
|
|
|
|
oldName := oldNameInterface.(string)
|
|
|
|
newName := newNameInterface.(string)
|
|
|
|
|
2017-05-10 23:54:57 +02:00
|
|
|
err := client.Machines().RenameMachine(context.Background(), &triton.RenameMachineInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
Name: newName,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-02-10 21:30:32 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{oldName},
|
|
|
|
Target: []string{newName},
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
2017-05-10 23:54:57 +02:00
|
|
|
getResp, err := client.Machines().GetMachine(context.Background(), &triton.GetMachineInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getResp, getResp.Name, nil
|
2016-02-10 21:30:32 +01:00
|
|
|
},
|
2017-03-31 00:25:27 +02:00
|
|
|
Timeout: machineStateChangeTimeout,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
_, err = stateConf.WaitForState()
|
2016-02-10 21:30:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("name")
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("tags") {
|
|
|
|
tags := map[string]string{}
|
|
|
|
for k, v := range d.Get("tags").(map[string]interface{}) {
|
|
|
|
tags[k] = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if len(tags) == 0 {
|
2017-05-10 23:54:57 +02:00
|
|
|
err = client.Machines().DeleteMachineTags(context.Background(), &triton.DeleteMachineTagsInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
})
|
2016-02-10 21:30:32 +01:00
|
|
|
} else {
|
2017-05-10 23:54:57 +02:00
|
|
|
err = client.Machines().ReplaceMachineTags(context.Background(), &triton.ReplaceMachineTagsInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
Tags: tags,
|
|
|
|
})
|
2016-02-10 21:30:32 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
expectedTagsMD5 := stableMapHash(tags)
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Target: []string{expectedTagsMD5},
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
2017-05-10 23:54:57 +02:00
|
|
|
getResp, err := client.Machines().GetMachine(context.Background(), &triton.GetMachineInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getResp, stableMapHash(getResp.Tags), nil
|
2016-02-10 21:30:32 +01:00
|
|
|
},
|
2017-03-31 00:25:27 +02:00
|
|
|
Timeout: machineStateChangeTimeout,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
_, err = stateConf.WaitForState()
|
2016-02-10 21:30:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("tags")
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("package") {
|
2017-03-31 00:25:27 +02:00
|
|
|
newPackage := d.Get("package").(string)
|
|
|
|
|
2017-05-10 23:54:57 +02:00
|
|
|
err := client.Machines().ResizeMachine(context.Background(), &triton.ResizeMachineInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
Package: newPackage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-02-10 21:30:32 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Target: []string{fmt.Sprintf("%s@%s", newPackage, "running")},
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
2017-05-10 23:54:57 +02:00
|
|
|
getResp, err := client.Machines().GetMachine(context.Background(), &triton.GetMachineInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getResp, fmt.Sprintf("%s@%s", getResp.Package, getResp.State), nil
|
2016-02-10 21:30:32 +01:00
|
|
|
},
|
2017-03-31 00:25:27 +02:00
|
|
|
Timeout: machineStateChangeTimeout,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
_, err = stateConf.WaitForState()
|
2016-02-10 21:30:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("package")
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("firewall_enabled") {
|
2017-03-31 00:25:27 +02:00
|
|
|
enable := d.Get("firewall_enabled").(bool)
|
|
|
|
|
2016-02-10 21:30:32 +01:00
|
|
|
var err error
|
2017-03-31 00:25:27 +02:00
|
|
|
if enable {
|
2017-05-10 23:54:57 +02:00
|
|
|
err = client.Machines().EnableMachineFirewall(context.Background(), &triton.EnableMachineFirewallInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
})
|
2016-02-10 21:30:32 +01:00
|
|
|
} else {
|
2017-05-10 23:54:57 +02:00
|
|
|
err = client.Machines().DisableMachineFirewall(context.Background(), &triton.DisableMachineFirewallInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
})
|
2016-02-10 21:30:32 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Target: []string{fmt.Sprintf("%t", enable)},
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
2017-05-10 23:54:57 +02:00
|
|
|
getResp, err := client.Machines().GetMachine(context.Background(), &triton.GetMachineInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2016-04-12 20:10:53 +02:00
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
return getResp, fmt.Sprintf("%t", getResp.FirewallEnabled), nil
|
|
|
|
},
|
|
|
|
Timeout: machineStateChangeTimeout,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
_, err = stateConf.WaitForState()
|
2016-04-12 20:10:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-10 21:30:32 +01:00
|
|
|
d.SetPartial("firewall_enabled")
|
|
|
|
}
|
|
|
|
|
2016-04-19 16:42:29 +02:00
|
|
|
if d.HasChange("nic") {
|
|
|
|
o, n := d.GetChange("nic")
|
|
|
|
if o == nil {
|
|
|
|
o = new(schema.Set)
|
|
|
|
}
|
|
|
|
if n == nil {
|
|
|
|
n = new(schema.Set)
|
|
|
|
}
|
|
|
|
|
|
|
|
oldNICs := o.(*schema.Set)
|
2017-03-31 00:25:27 +02:00
|
|
|
newNICs := n.(*schema.Set)
|
2016-04-19 16:42:29 +02:00
|
|
|
|
|
|
|
for _, nicI := range newNICs.Difference(oldNICs).List() {
|
|
|
|
nic := nicI.(map[string]interface{})
|
2017-05-10 23:54:57 +02:00
|
|
|
if _, err := client.Machines().AddNIC(context.Background(), &triton.AddNICInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
MachineID: d.Id(),
|
|
|
|
Network: nic["network"].(string),
|
|
|
|
}); err != nil {
|
2016-04-19 16:42:29 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, nicI := range oldNICs.Difference(newNICs).List() {
|
|
|
|
nic := nicI.(map[string]interface{})
|
2017-05-10 23:54:57 +02:00
|
|
|
if err := client.Machines().RemoveNIC(context.Background(), &triton.RemoveNICInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
MachineID: d.Id(),
|
|
|
|
MAC: nic["mac"].(string),
|
|
|
|
}); err != nil {
|
2016-04-19 16:42:29 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("nic")
|
|
|
|
}
|
|
|
|
|
2016-02-10 21:30:32 +01:00
|
|
|
metadata := map[string]string{}
|
|
|
|
for schemaName, metadataKey := range resourceMachineMetadataKeys {
|
|
|
|
if d.HasChange(schemaName) {
|
|
|
|
metadata[metadataKey] = d.Get(schemaName).(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(metadata) > 0 {
|
2017-05-10 23:54:57 +02:00
|
|
|
if _, err := client.Machines().UpdateMachineMetadata(context.Background(), &triton.UpdateMachineMetadataInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
Metadata: metadata,
|
|
|
|
}); err != nil {
|
2016-02-10 21:30:32 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Target: []string{"converged"},
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
2017-05-10 23:54:57 +02:00
|
|
|
getResp, err := client.Machines().GetMachine(context.Background(), &triton.GetMachineInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2016-04-21 01:14:06 +02:00
|
|
|
for k, v := range metadata {
|
2017-03-31 00:25:27 +02:00
|
|
|
if upstream, ok := getResp.Metadata[k]; !ok || v != upstream {
|
|
|
|
return getResp, "converging", nil
|
2016-04-21 01:14:06 +02:00
|
|
|
}
|
|
|
|
}
|
2017-03-31 00:25:27 +02:00
|
|
|
|
|
|
|
return getResp, "converged", nil
|
2016-02-10 21:30:32 +01:00
|
|
|
},
|
2017-03-31 00:25:27 +02:00
|
|
|
Timeout: machineStateChangeTimeout,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
_, err := stateConf.WaitForState()
|
2016-02-10 21:30:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for schemaName := range resourceMachineMetadataKeys {
|
|
|
|
if d.HasChange(schemaName) {
|
|
|
|
d.SetPartial(schemaName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Partial(false)
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
return resourceMachineRead(d, meta)
|
2016-02-10 21:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceMachineDelete(d *schema.ResourceData, meta interface{}) error {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
2017-05-10 23:54:57 +02:00
|
|
|
err := client.Machines().DeleteMachine(context.Background(), &triton.DeleteMachineInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
})
|
2016-02-10 21:30:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Target: []string{machineStateDeleted},
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
2017-05-10 23:54:57 +02:00
|
|
|
getResp, err := client.Machines().GetMachine(context.Background(), &triton.GetMachineInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
ID: d.Id(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if triton.IsResourceNotFound(err) {
|
|
|
|
return nil, "deleted", nil
|
|
|
|
}
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2016-02-10 21:30:32 +01:00
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
return getResp, getResp.State, nil
|
|
|
|
},
|
|
|
|
Timeout: machineStateChangeTimeout,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
_, err = stateConf.WaitForState()
|
2016-02-10 21:30:32 +01:00
|
|
|
if err != nil {
|
2017-03-31 00:25:27 +02:00
|
|
|
return err
|
2016-02-10 21:30:32 +01:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
return nil
|
2016-02-10 21:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceMachineValidateName(value interface{}, name string) (warnings []string, errors []error) {
|
|
|
|
warnings = []string{}
|
|
|
|
errors = []error{}
|
|
|
|
|
|
|
|
r := regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9\_\.\-]*$`)
|
|
|
|
if !r.Match([]byte(value.(string))) {
|
|
|
|
errors = append(errors, fmt.Errorf(`"%s" is not a valid %s`, value.(string), name))
|
|
|
|
}
|
|
|
|
|
|
|
|
return warnings, errors
|
|
|
|
}
|