forked from adammck/terraform-inventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
225 lines (203 loc) · 7.24 KB
/
resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
"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
"ipv4_address_private", // SoftLayer
"networks.0.ip4address", // Exoscale
"primaryip", // Joyent Triton
"network_interface.0.addresses.0", // Libvirt
"network.0.address", // Packet
"primary_ip", // Profitbricks
"nic_list.0.ip_endpoint_list.0.ip", // Nutanix
}
// 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)
// - "data." prefix should not parse and be ignored by caller (does not represent a host)
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.
if len(m) != 4 {
return nil, fmt.Errorf("couldn't parse resource 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() != ""
}
// 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
}
}
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
}
}
case "aws_instance", "linode_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 "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
}
}
case "vsphere_virtual_machine":
for k, v := range r.Attributes() {
parts := strings.SplitN(k, ".", 2)
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
}
}
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] = ""
}
}
case "triton_machine", "exoscale_compute":
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.
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)
}
// 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()
}
// 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 ""
}