mirror of
https://github.com/adammck/terraform-inventory
synced 2024-11-22 20:01:58 +01:00
Sort resource names for consistent output
This commit is contained in:
parent
885469243c
commit
0a61a938ad
22
parser.go
22
parser.go
@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type state struct {
|
||||
@ -33,12 +34,12 @@ func (s *state) resources() []*Resource {
|
||||
inst := make([]*Resource, 0)
|
||||
|
||||
for _, m := range s.Modules {
|
||||
for k, rs := range m.ResourceStates {
|
||||
for _, k := range m.resourceKeys() {
|
||||
|
||||
// Terraform stores resources in a name->map map, but we need the name to
|
||||
// decide which groups to include the resource in. So wrap it in a higher-
|
||||
// level object with both properties.
|
||||
r, err := NewResource(k, rs)
|
||||
r, err := NewResource(k, m.ResourceStates[k])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@ -56,6 +57,23 @@ type moduleState struct {
|
||||
ResourceStates map[string]resourceState `json:"resources"`
|
||||
}
|
||||
|
||||
// resourceKeys returns a sorted slice of the key names of the resources in this
|
||||
// module. Do this instead of range over ResourceStates, to ensure that the
|
||||
// output is consistent.
|
||||
func (ms *moduleState) resourceKeys() []string {
|
||||
lk := len(ms.ResourceStates)
|
||||
keys := make([]string, lk, lk)
|
||||
i := 0
|
||||
|
||||
for k := range ms.ResourceStates {
|
||||
keys[i] = k
|
||||
i += 1
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
}
|
||||
|
||||
type resourceState struct {
|
||||
|
||||
// Populated from statefile
|
||||
|
Loading…
Reference in New Issue
Block a user