Skip to content

Commit aecdc48

Browse files
Atadiloadammck
authored andcommitted
Add tags support to VSphere provider (#88)
1 parent 2365c9b commit aecdc48

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

cli.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func gatherResources(s *state) map[string]interface{} {
5353

5454
unsortedOrdered := make(map[string][]*Resource)
5555

56+
resourceIDNames := s.mapResourceIDNames()
5657
for _, res := range s.resources() {
5758
// place in list of all resources
5859
all.Hosts = appendUniq(all.Hosts, res.Hostname())
@@ -77,6 +78,10 @@ func gatherResources(s *state) map[string]interface{} {
7778
if v != "" {
7879
tag = fmt.Sprintf("%s_%s", k, v)
7980
}
81+
// if v is a resource ID, then tag should be resource name
82+
if _, exists := resourceIDNames[v]; exists {
83+
tag = resourceIDNames[v]
84+
}
8085
tags[tag] = appendUniq(tags[tag], res.Hostname())
8186
}
8287
}

parser.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"io/ioutil"
77
"sort"
8+
"strings"
89
)
910

1011
type state struct {
@@ -52,13 +53,27 @@ func (s *state) outputs() []*Output {
5253
return inst
5354
}
5455

56+
// map of resource ID -> resource Name
57+
func (s *state) mapResourceIDNames() map[string]string {
58+
t := map[string]string{}
59+
60+
for _, m := range s.Modules {
61+
for _, k := range m.resourceKeys() {
62+
if m.ResourceStates[k].Primary.ID != "" && m.ResourceStates[k].Primary.Attributes["name"] != "" {
63+
kk := strings.ToLower(m.ResourceStates[k].Primary.ID)
64+
t[kk] = m.ResourceStates[k].Primary.Attributes["name"]
65+
}
66+
}
67+
}
68+
return t
69+
}
70+
5571
// resources returns a slice of the Resources found in the statefile.
5672
func (s *state) resources() []*Resource {
5773
inst := make([]*Resource, 0)
5874

5975
for _, m := range s.Modules {
6076
for _, k := range m.resourceKeys() {
61-
6277
// Terraform stores resources in a name->map map, but we need the name to
6378
// decide which groups to include the resource in. So wrap it in a higher-
6479
// level object with both properties.

parser_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,18 @@ const exampleStateFile = `
329329
"primary": {
330330
"id": "422cfa4a-c6bb-3405-0335-2d9b2034405f",
331331
"attributes": {
332-
"default_ip_address": "10.20.30.50"
332+
"default_ip_address": "10.20.30.50",
333+
"tags.#": "1",
334+
"tags.1357913579": "urn:vmomi:InventoryServiceTag:00000000-0001-4957-81fa-1234567890ab:GLOBAL"
335+
}
336+
}
337+
},
338+
"data.vsphere_tag.testTag1": {
339+
"type": "vsphere_tag",
340+
"primary": {
341+
"id": "urn:vmomi:InventoryServiceTag:00000000-0001-4957-81fa-1234567890ab:GLOBAL",
342+
"attributes": {
343+
"name": "testTag1"
333344
}
334345
}
335346
},
@@ -432,6 +443,7 @@ const expectedListOutput = `
432443
"ten": ["10.0.0.10"],
433444
"eleven": ["10.0.0.11"],
434445
"twelve": ["10.20.30.50"],
446+
"testTag1": ["10.20.30.50"],
435447
"thirteen": ["10.0.0.13"],
436448
"fourteen": ["192.168.102.14"],
437449
"sixteen": ["10.0.0.16"],
@@ -600,6 +612,9 @@ olddatacenter="\u003c0.7_format"
600612
[ten.0]
601613
10.0.0.10
602614
615+
[testTag1]
616+
10.20.30.50
617+
603618
[thirteen]
604619
10.0.0.13
605620

resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,17 @@ func (r Resource) Tags() map[string]string {
126126
case "vsphere_virtual_machine":
127127
for k, v := range r.Attributes() {
128128
parts := strings.SplitN(k, ".", 2)
129+
129130
if len(parts) == 2 && parts[0] == "custom_configuration_parameters" && parts[1] != "#" && parts[1] != "%" {
130131
kk := strings.ToLower(parts[1])
131132
vv := strings.ToLower(v)
132133
t[kk] = vv
133134
}
135+
if len(parts) == 2 && parts[0] == "tags" && parts[1] != "#" && parts[1] != "%" {
136+
kk := strings.ToLower(parts[1])
137+
vv := strings.ToLower(v)
138+
t[kk] = vv
139+
}
134140
}
135141
case "digitalocean_droplet", "google_compute_instance", "scaleway_server":
136142
for k, v := range r.Attributes() {

0 commit comments

Comments
 (0)