1
1
mirror of https://github.com/adammck/terraform-inventory synced 2024-11-23 00:12:13 +01:00
terraform-inventory/resource.go

252 lines
8.5 KiB
Go
Raw Normal View History

2015-12-15 04:24:41 +01:00
package main
import (
"fmt"
"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{
2020-07-20 17:24:48 +02:00
"ipv4_address", // DO and SoftLayer and HetznerCloud
"public_ip", // AWS
"public_ipv6", // Scaleway
"ipaddress", // CS
"ip_address", // VMware, Docker, Linode
"private_ip", // AWS
"network_interface.0.ipv4_address", // VMware
"default_ip_address", // provider.vsphere v1.1.1
"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
"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
2019-02-14 16:53:36 +01:00
"nic_list.0.ip_endpoint_list.0.ip", // Nutanix
2020-06-22 17:14:15 +02:00
"network_interface.0.nat_ip_address", // Yandex
"network_interface.0.ip_address", // Yandex
2021-07-30 23:09:12 +02:00
"default_ipv4_address", // Telmate/Proxmox
"ssh_host", // Telmate/Proxmox
2015-12-15 04:24:41 +01:00
}
// Formats:
// - type.[module_]name (no `count` attribute; contains module name if we're not in the root module)
// - type.[module_]name.0 (if resource has `count` attribute)
// - type.[module_]name.resource_name
// - "data." prefix should not parse and be ignored by caller (does not represent a host)
nameParser = regexp.MustCompile(`^([\w\-]+)\.([\w\-]+)(?:\.(\d+|[\S+]+))?$`)
2015-12-15 04:24:41 +01:00
}
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
// counterNumeric is 0 for resources created without `count=` attribute or
// having a non-numeric string index
counterNumeric int
// counterStr is set if the resource index (e.g. in `for_each`-constructed
// resources) is not a number.
counterStr string
2015-12-15 04:24:41 +01:00
}
func NewResource(keyName string, state resourceState) (*Resource, error) {
m := nameParser.FindStringSubmatch(keyName)
// This should not happen unless our regex changes.
if len(m) != 4 {
return nil, fmt.Errorf("couldn't parse resource keyName: %s", keyName)
2015-12-15 04:24:41 +01:00
}
counterNumeric := 0
counterStr := ""
2015-12-15 04:24:41 +01:00
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. With Terraform 0.12 for_each syntax, index can also be
// a non-numeric string (loop over any string value).
counterNumeric, err = strconv.Atoi(m[3])
2015-12-15 04:24:41 +01:00
if err != nil {
counterNumeric = 0
counterStr = m[3]
2015-12-15 04:24:41 +01:00
}
}
return &Resource{
State: state,
keyName: keyName,
resourceType: m[1],
baseName: m[2],
counterNumeric: counterNumeric,
counterStr: counterStr,
2015-12-15 04:24:41 +01:00
}, 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 {
case "openstack_compute_instance_v2":
for k, v := range r.Attributes() {
parts := strings.SplitN(k, ".", 2)
// 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] != "%" {
kk := strings.ToLower(parts[1])
vv := strings.ToLower(v)
t[kk] = vv
2019-05-21 16:01:43 +02:00
}
}
case "opentelekomcloud_compute_instance_v2":
for k, v := range r.Attributes() {
parts := strings.SplitN(k, ".", 2)
// 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] == "tag" && parts[1] != "#" && parts[1] != "%" {
kk := strings.ToLower(parts[1])
vv := strings.ToLower(v)
t[kk] = vv
2020-02-21 18:46:37 +01:00
} else if len(parts) == 2 && parts[0] == "metadata" && parts[1] != "#" && parts[1] != "%" {
kk := strings.ToLower(parts[1])
vv := strings.ToLower(v)
t[kk] = vv
}
}
2019-05-12 19:38:28 +02:00
case "aws_instance", "linode_instance":
2015-12-15 06:01:17 +01:00
for k, v := range r.Attributes() {
parts := strings.SplitN(k, ".", 2)
// 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
}
}
2018-12-05 18:52:45 +01:00
case "aws_spot_instance_request":
for k, v := range r.Attributes() {
parts := strings.SplitN(k, ".", 2)
// 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] != "%" {
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)
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
}
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":
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] = ""
}
}
2019-05-12 19:39:40 +02:00
case "triton_machine", "exoscale_compute":
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
}
}
2020-07-20 17:24:48 +02:00
case "yandex_compute_instance", "hcloud_server":
2020-06-22 17:14:15 +02:00
for k, v := range r.Attributes() {
parts := strings.SplitN(k, ".", 2)
// 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] == "labels" && parts[1] != "#" && parts[1] != "%" {
kk := strings.ToLower(parts[1])
vv := strings.ToLower(v)
t[kk] = vv
}
}
2015-12-15 06:01:17 +01:00
}
2020-06-22 17:14:15 +02:00
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
}
// 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 {
if keyName := os.Getenv("TF_KEY_NAME"); keyName != "" {
if ip := r.State.Primary.Attributes[keyName]; ip != "" {
2015-12-15 04:24:41 +01:00
return ip
}
} else {
for _, key := range keyNames {
if ip := r.State.Primary.Attributes[key]; ip != "" {
return ip
}
}
2015-12-15 04:24:41 +01:00
}
return ""
}