1
1
mirror of https://github.com/adammck/terraform-inventory synced 2024-11-22 20:01:58 +01:00

Add tags/groups support for GCP (#55)

* Add tags/groups support for GCP
* Add GCP tags test
This commit is contained in:
Adam Mckaig 2017-01-18 21:19:34 -05:00 committed by GitHub
parent 87cfce494f
commit 1de0b94c9f
2 changed files with 38 additions and 4 deletions

@ -153,6 +153,21 @@ const exampleStateFile = `
"ipv4_address":"" "ipv4_address":""
} }
} }
},
"google_compute_instance.eight": {
"type": "123456789",
"primary": {
"id": "123456789",
"attributes": {
"network_interface.0.access_config.0.assigned_nat_ip": "10.0.0.8",
"network_interface.0.access_config.0.nat_ip": "10.0.0.8",
"network_interface.0.address": "10.0.0.8",
"tags.#": "1",
"tags.1201918879": "database",
"tags_fingerprint": "AqbISNuzJIs=",
"zone": "europe-west1-d"
}
}
} }
} }
} }
@ -166,6 +181,7 @@ const expectedListOutput = `
"hosts": [ "hosts": [
"10.0.0.1", "10.0.0.1",
"10.0.0.7", "10.0.0.7",
"10.0.0.8",
"10.0.1.1", "10.0.1.1",
"10.120.0.226", "10.120.0.226",
"10.2.1.5", "10.2.1.5",
@ -188,6 +204,7 @@ const expectedListOutput = `
"five": ["10.20.30.40"], "five": ["10.20.30.40"],
"six": ["10.120.0.226"], "six": ["10.120.0.226"],
"seven": ["10.0.0.7"], "seven": ["10.0.0.7"],
"eight": ["10.0.0.8"],
"one.0": ["10.0.0.1"], "one.0": ["10.0.0.1"],
"dup.0": ["10.0.0.1"], "dup.0": ["10.0.0.1"],
@ -198,6 +215,7 @@ const expectedListOutput = `
"five.0": ["10.20.30.40"], "five.0": ["10.20.30.40"],
"six.0": ["10.120.0.226"], "six.0": ["10.120.0.226"],
"seven.0": ["10.0.0.7"], "seven.0": ["10.0.0.7"],
"eight.0": ["10.0.0.8"],
"type_aws_instance": ["10.0.0.1", "10.0.1.1", "50.0.0.1"], "type_aws_instance": ["10.0.0.1", "10.0.1.1", "50.0.0.1"],
"type_digitalocean_droplet": ["192.168.0.3"], "type_digitalocean_droplet": ["192.168.0.3"],
@ -205,8 +223,10 @@ const expectedListOutput = `
"type_vsphere_virtual_machine": ["10.20.30.40"], "type_vsphere_virtual_machine": ["10.20.30.40"],
"type_openstack_compute_instance_v2": ["10.120.0.226"], "type_openstack_compute_instance_v2": ["10.120.0.226"],
"type_softlayer_virtual_guest": ["10.0.0.7"], "type_softlayer_virtual_guest": ["10.0.0.7"],
"type_google_compute_instance": ["10.0.0.8"],
"role_web": ["10.0.0.1"] "role_web": ["10.0.0.1"],
"database": ["10.0.0.8"]
} }
` `

@ -94,8 +94,15 @@ func (r Resource) Groups() []string {
} }
for k, v := range r.Tags() { for k, v := range r.Tags() {
g := fmt.Sprintf("%s_%s", k, v) // google
groups = append(groups, g) if v == "" {
g := k
groups = append(groups, g)
// aws
} else {
g := fmt.Sprintf("%s_%s", k, v)
groups = append(groups, g)
}
} }
return groups return groups
@ -117,8 +124,15 @@ func (r Resource) Tags() map[string]string {
t[kk] = vv t[kk] = vv
} }
} }
case "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 return t
} }