1
1
Fork 0
mirror of https://github.com/adammck/terraform-inventory synced 2024-05-25 14:06:03 +02:00
terraform-inventory/resource.go
2017-06-13 14:37:27 -04:00

179 lines
4.9 KiB
Go

package main
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
// 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{
"ipv4_address", // DO and SoftLayer
"public_ip", // AWS
"private_ip", // AWS
"ipaddress", // CS
"ip_address", // VMware
"network_interface.0.ipv4_address", // VMware
"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
"ipv4_address_private", // SoftLayer
}
// 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() != ""
}
// Groups returns the list of Ansible groups which this resource should be
// included in.
func (r Resource) Groups() []string {
groups := []string{
r.baseName,
r.NameWithCounter(),
fmt.Sprintf("type_%s", r.resourceType),
}
for k, v := range r.Tags() {
// Valueless
if v == "" {
g := k
groups = append(groups, g)
// Key-value
} else {
g := fmt.Sprintf("%s_%s", k, v)
groups = append(groups, g)
}
}
return groups
}
// 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)
if len(parts) == 2 && parts[0] == "metadata" && parts[1] != "#" {
kk := strings.ToLower(parts[1])
vv := strings.ToLower(v)
t[kk] = vv
}
}
case "aws_instance":
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
}
}
case "digitalocean_droplet", "google_compute_instance":
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] = ""
}
}
}
return t
}
// 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)
}
// 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 != "" {
return ip
}
} else {
for _, key := range keyNames {
if ip := r.State.Primary.Attributes[key]; ip != "" {
return ip
}
}
}
return ""
}