1
1
mirror of https://github.com/adammck/terraform-inventory synced 2024-11-26 11:53:48 +01:00

Add group by AWS tags

This commit is contained in:
Adam Mckaig 2015-12-15 00:01:17 -05:00
parent 420247c637
commit b8a9261d05
2 changed files with 39 additions and 4 deletions

@ -26,7 +26,9 @@ const exampleStateFile = `
"id": "i-aaaaaaaa",
"attributes": {
"id": "i-aaaaaaaa",
"private_ip": "10.0.0.1"
"private_ip": "10.0.0.1",
"tags.#": "1",
"tags.Role": "Web"
}
}
},
@ -139,14 +141,18 @@ const expectedListOutput = `
"digitalocean_droplet": ["192.168.0.3"],
"cloudstack_instance": ["10.2.1.5"],
"vsphere_virtual_machine": ["10.20.30.40"],
"openstack_compute_instance_v2": ["10.120.0.226"]
"openstack_compute_instance_v2": ["10.120.0.226"],
"role_web": ["10.0.0.1"]
}
`
const expectedHostOneOutput = `
{
"id":"i-aaaaaaaa",
"private_ip":"10.0.0.1"
"private_ip":"10.0.0.1",
"tags.#": "1",
"tags.Role": "Web"
}
`

@ -4,6 +4,7 @@ import (
"fmt"
"regexp"
"strconv"
"strings"
)
// keyNames contains the names of the keys to check for in each resource in the
@ -81,11 +82,39 @@ func (r Resource) IsSupported() bool {
// Groups returns the list of Ansible groups which this resource should be
// included in.
func (r Resource) Groups() []string {
return []string{
groups := []string{
r.baseName,
r.NameWithCounter(),
r.resourceType,
}
for k, v := range r.Tags() {
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 "aws_instance":
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
}
}
}
return t
}
// Attributes returns a map containing everything we know about this resource.