2015-12-15 04:24:41 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-02-17 08:45:45 +01:00
|
|
|
"os"
|
2015-12-15 04:24:41 +01:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2015-12-15 06:01:17 +01:00
|
|
|
"strings"
|
2015-12-15 04:24:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// keyNames contains the names of the keys to check for in each resource in the
|
|
|
|
// state file. This allows us to support multiple types of resource without too
|
|
|
|
// much fuss.
|
|
|
|
var keyNames []string
|
|
|
|
var nameParser *regexp.Regexp
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
keyNames = []string{
|
2016-11-16 02:14:54 +01:00
|
|
|
"ipv4_address", // DO and SoftLayer
|
2016-01-29 11:25:08 +01:00
|
|
|
"public_ip", // AWS
|
2017-12-21 21:15:16 +01:00
|
|
|
"public_ipv6", // Scaleway
|
2016-01-29 11:25:08 +01:00
|
|
|
"private_ip", // AWS
|
|
|
|
"ipaddress", // CS
|
2018-03-08 19:01:13 +01:00
|
|
|
"ip_address", // VMware, Docker
|
2017-06-13 20:37:27 +02:00
|
|
|
"network_interface.0.ipv4_address", // VMware
|
2018-01-24 20:32:25 +01:00
|
|
|
"default_ip_address", // provider.vsphere v1.1.1
|
2016-01-29 11:25:08 +01:00
|
|
|
"access_ip_v4", // OpenStack
|
|
|
|
"floating_ip", // OpenStack
|
|
|
|
"network_interface.0.access_config.0.nat_ip", // GCE
|
|
|
|
"network_interface.0.access_config.0.assigned_nat_ip", // GCE
|
|
|
|
"network_interface.0.address", // GCE
|
2016-11-16 02:14:54 +01:00
|
|
|
"ipv4_address_private", // SoftLayer
|
2017-07-12 05:18:20 +02:00
|
|
|
"networks.0.ip4address", // Exoscale
|
2017-09-07 21:36:52 +02:00
|
|
|
"primaryip", // Joyent Triton
|
2018-06-01 16:45:50 +02:00
|
|
|
"network_interface.0.addresses.0", // Libvirt
|
2018-06-01 16:08:22 +02:00
|
|
|
"network.0.address", // Packet
|
2018-06-18 18:51:21 +02:00
|
|
|
"primary_ip", // Profitbricks
|
2015-12-15 04:24:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// type.name.0
|
|
|
|
nameParser = regexp.MustCompile(`^(\w+)\.([\w\-]+)(?:\.(\d+))?$`)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Resource struct {
|
|
|
|
|
|
|
|
// The state (as unmarshalled from the statefile) which this resource wraps.
|
|
|
|
// Everything which Terraform knows about the resource can be found in here.
|
|
|
|
State resourceState
|
|
|
|
|
|
|
|
// The key name of the resource, provided to the constructor. Unfortunately,
|
|
|
|
// it seems like the counter index can only be found here.
|
|
|
|
keyName string
|
|
|
|
|
|
|
|
// Extracted from keyName
|
|
|
|
resourceType string
|
|
|
|
baseName string
|
|
|
|
counter int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewResource(keyName string, state resourceState) (*Resource, error) {
|
|
|
|
m := nameParser.FindStringSubmatch(keyName)
|
|
|
|
|
|
|
|
// This should not happen unless our regex changes.
|
|
|
|
// TODO: Warn instead of silently ignore error?
|
|
|
|
if len(m) != 4 {
|
|
|
|
return nil, fmt.Errorf("couldn't parse keyName: %s", keyName)
|
|
|
|
}
|
|
|
|
|
|
|
|
var c int
|
|
|
|
var err error
|
|
|
|
if m[3] != "" {
|
|
|
|
|
|
|
|
// The third section should be the index, if it's present. Not sure what
|
|
|
|
// else we can do other than panic (which seems highly undesirable) if that
|
|
|
|
// isn't the case.
|
|
|
|
c, err = strconv.Atoi(m[3])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Resource{
|
|
|
|
State: state,
|
|
|
|
keyName: keyName,
|
|
|
|
resourceType: m[1],
|
|
|
|
baseName: m[2],
|
|
|
|
counter: c,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Resource) IsSupported() bool {
|
|
|
|
return r.Address() != ""
|
|
|
|
}
|
|
|
|
|
2015-12-15 06:01:17 +01:00
|
|
|
// Tags returns a map of arbitrary key/value pairs explicitly associated with
|
|
|
|
// the resource. Different providers have different mechanisms for attaching
|
|
|
|
// these.
|
|
|
|
func (r Resource) Tags() map[string]string {
|
|
|
|
t := map[string]string{}
|
|
|
|
|
|
|
|
switch r.resourceType {
|
2017-06-09 21:16:21 +02:00
|
|
|
case "openstack_compute_instance_v2":
|
|
|
|
for k, v := range r.Attributes() {
|
|
|
|
parts := strings.SplitN(k, ".", 2)
|
2017-09-19 16:56:50 +02:00
|
|
|
// At some point Terraform changed the key for counts of attributes to end with ".%"
|
|
|
|
// instead of ".#". Both need to be considered as Terraform still supports state
|
|
|
|
// files using the old format.
|
|
|
|
if len(parts) == 2 && parts[0] == "metadata" && parts[1] != "#" && parts[1] != "%" {
|
2017-06-09 21:16:21 +02:00
|
|
|
kk := strings.ToLower(parts[1])
|
|
|
|
vv := strings.ToLower(v)
|
|
|
|
t[kk] = vv
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 06:01:17 +01:00
|
|
|
case "aws_instance":
|
|
|
|
for k, v := range r.Attributes() {
|
|
|
|
parts := strings.SplitN(k, ".", 2)
|
2017-06-09 20:59:21 +02:00
|
|
|
// At some point Terraform changed the key for counts of attributes to end with ".%"
|
|
|
|
// instead of ".#". Both need to be considered as Terraform still supports state
|
|
|
|
// files using the old format.
|
|
|
|
if len(parts) == 2 && parts[0] == "tags" && parts[1] != "#" && parts[1] != "%" {
|
2015-12-15 06:01:17 +01:00
|
|
|
kk := strings.ToLower(parts[1])
|
|
|
|
vv := strings.ToLower(v)
|
|
|
|
t[kk] = vv
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 19:24:02 +02:00
|
|
|
case "vsphere_virtual_machine":
|
|
|
|
for k, v := range r.Attributes() {
|
|
|
|
parts := strings.SplitN(k, ".", 2)
|
2018-09-20 16:37:44 +02:00
|
|
|
|
2017-06-20 19:24:02 +02:00
|
|
|
if len(parts) == 2 && parts[0] == "custom_configuration_parameters" && parts[1] != "#" && parts[1] != "%" {
|
|
|
|
kk := strings.ToLower(parts[1])
|
|
|
|
vv := strings.ToLower(v)
|
|
|
|
t[kk] = vv
|
|
|
|
}
|
2018-09-20 16:37:44 +02:00
|
|
|
if len(parts) == 2 && parts[0] == "tags" && parts[1] != "#" && parts[1] != "%" {
|
|
|
|
kk := strings.ToLower(parts[1])
|
|
|
|
vv := strings.ToLower(v)
|
|
|
|
t[kk] = vv
|
|
|
|
}
|
2017-06-20 19:24:02 +02:00
|
|
|
}
|
2017-11-27 17:04:29 +01:00
|
|
|
case "digitalocean_droplet", "google_compute_instance", "scaleway_server":
|
2017-01-19 03:19:34 +01:00
|
|
|
for k, v := range r.Attributes() {
|
|
|
|
parts := strings.SplitN(k, ".", 2)
|
|
|
|
if len(parts) == 2 && parts[0] == "tags" && parts[1] != "#" {
|
|
|
|
vv := strings.ToLower(v)
|
|
|
|
t[vv] = ""
|
|
|
|
}
|
|
|
|
}
|
2017-10-20 17:32:34 +02:00
|
|
|
case "triton_machine":
|
2017-09-08 17:40:08 +02:00
|
|
|
for k, v := range r.Attributes() {
|
|
|
|
parts := strings.SplitN(k, ".", 2)
|
|
|
|
if len(parts) == 2 && parts[0] == "tags" && parts[1] != "%" {
|
|
|
|
kk := strings.ToLower(parts[1])
|
|
|
|
vv := strings.ToLower(v)
|
|
|
|
t[kk] = vv
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 06:01:17 +01:00
|
|
|
}
|
|
|
|
return t
|
2015-12-15 04:24:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attributes returns a map containing everything we know about this resource.
|
|
|
|
func (r Resource) Attributes() map[string]string {
|
|
|
|
return r.State.Primary.Attributes
|
|
|
|
}
|
|
|
|
|
|
|
|
// NameWithCounter returns the resource name with its counter. For resources
|
|
|
|
// created without a 'count=' attribute, this will always be zero.
|
|
|
|
func (r Resource) NameWithCounter() string {
|
|
|
|
return fmt.Sprintf("%s.%d", r.baseName, r.counter)
|
|
|
|
}
|
|
|
|
|
2018-09-20 16:22:53 +02:00
|
|
|
// Hostname returns the hostname of this resource.
|
|
|
|
func (r Resource) Hostname() string {
|
|
|
|
if keyName := os.Getenv("TF_HOSTNAME_KEY_NAME"); keyName != "" {
|
|
|
|
if ip := r.State.Primary.Attributes[keyName]; ip != "" {
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Address()
|
|
|
|
}
|
|
|
|
|
2015-12-15 04:24:41 +01:00
|
|
|
// Address returns the IP address of this resource.
|
|
|
|
func (r Resource) Address() string {
|
2016-02-17 16:37:51 +01:00
|
|
|
if keyName := os.Getenv("TF_KEY_NAME"); keyName != "" {
|
2016-02-17 08:45:45 +01:00
|
|
|
if ip := r.State.Primary.Attributes[keyName]; ip != "" {
|
2015-12-15 04:24:41 +01:00
|
|
|
return ip
|
|
|
|
}
|
2016-02-17 08:45:45 +01:00
|
|
|
} else {
|
|
|
|
for _, key := range keyNames {
|
|
|
|
if ip := r.State.Primary.Attributes[key]; ip != "" {
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 04:24:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|