mirror of
https://github.com/adammck/terraform-inventory
synced 2024-05-25 01:36:04 +02:00
Support string indices (from for_each) with Terraform 0.12+
This commit is contained in:
parent
a408e89660
commit
5618f546c1
14
parser.go
14
parser.go
|
@ -65,7 +65,7 @@ type moduleStateTerraform0dot12 struct {
|
|||
}
|
||||
type resourceStateTerraform0dot12 struct {
|
||||
Address string `json:"address"`
|
||||
Index *int `json:"index"` // only set by Terraform for counted resources
|
||||
Index *interface{} `json:"index"` // only set by Terraform for counted resources
|
||||
Name string `json:"name"`
|
||||
RawValues map[string]interface{} `json:"values"`
|
||||
Type string `json:"type"`
|
||||
|
@ -324,7 +324,17 @@ func (s *stateTerraform0dot12) resources() []*Resource {
|
|||
}
|
||||
resourceKeyName := rs.Type + "." + modulePrefix + rs.Name
|
||||
if rs.Index != nil {
|
||||
resourceKeyName += "." + strconv.Itoa(*rs.Index)
|
||||
i := *rs.Index
|
||||
switch v := i.(type) {
|
||||
case int:
|
||||
resourceKeyName += "." + strconv.Itoa(v)
|
||||
case float64:
|
||||
resourceKeyName += "." + strconv.Itoa(int(v))
|
||||
case string:
|
||||
resourceKeyName += "." + v
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "Warning: unknown index type %v\n", v)
|
||||
}
|
||||
}
|
||||
|
||||
// Terraform stores resources in a name->map map, but we need the name to
|
||||
|
|
|
@ -943,6 +943,35 @@ const exampleStateFileTerraform0dot12 = `
|
|||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"address": "aws_route53_record.this",
|
||||
"mode": "managed",
|
||||
"type": "aws_route53_record",
|
||||
"name": "this",
|
||||
"index": "testb",
|
||||
"provider_name": "aws",
|
||||
"schema_version": 2,
|
||||
"values": {
|
||||
"alias": [],
|
||||
"allow_overwrite": null,
|
||||
"failover_routing_policy": [],
|
||||
"fqdn": "testb.tv",
|
||||
"geolocation_routing_policy": [],
|
||||
"health_check_id": null,
|
||||
"id": "abc.tv._A",
|
||||
"latency_routing_policy": [],
|
||||
"multivalue_answer_routing_policy": null,
|
||||
"name": "testb.tv",
|
||||
"records": [
|
||||
"2.2.2.2"
|
||||
],
|
||||
"set_identifier": null,
|
||||
"ttl": 300,
|
||||
"type": "A",
|
||||
"weighted_routing_policy": [],
|
||||
"zone_id": "abc"
|
||||
}
|
||||
},
|
||||
{
|
||||
"address": "vsphere_tag.bar",
|
||||
"mode": "managed",
|
||||
|
|
|
@ -41,8 +41,9 @@ func init() {
|
|||
// 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)
|
||||
// - type.[module_]name.resource_name
|
||||
// - "data." prefix should not parse and be ignored by caller (does not represent a host)
|
||||
nameParser = regexp.MustCompile(`^([\w\-]+)\.([\w\-]+)(?:\.(\d+))?$`)
|
||||
nameParser = regexp.MustCompile(`^([\w\-]+)\.([\w\-]+)(?:\.(\d+|[\S+]+))?$`)
|
||||
}
|
||||
|
||||
type Resource struct {
|
||||
|
@ -74,10 +75,10 @@ func NewResource(keyName string, state resourceState) (*Resource, 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.
|
||||
// isn't the case. With Terraform 0.12 for_each syntax, index is a string.
|
||||
c, err = strconv.Atoi(m[3])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
m[2] = fmt.Sprintf("%s.%s", m[2], m[3])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue