Skip to content

Commit 359ec8c

Browse files
committed
Adds the module name to the base name if ENV TF_ADD_MODULE_PATH is set.
1 parent 842b8f7 commit 359ec8c

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

cli.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"os"
88
"sort"
9+
"strings"
910
)
1011

1112
type counterSorter struct {
@@ -61,10 +62,17 @@ func gatherResources(s *state) map[string]interface{} {
6162
tp := fmt.Sprintf("type_%s", res.resourceType)
6263
types[tp] = appendUniq(types[tp], res.Address())
6364

64-
unsortedOrdered[res.baseName] = append(unsortedOrdered[res.baseName], res)
65+
var baseNameArray []string
66+
if os.Getenv("TF_ADD_MODULE_PATH") != "" && res.modulePath != "" {
67+
baseNameArray = append(baseNameArray, res.modulePath)
68+
}
69+
baseNameArray = append(baseNameArray, res.baseName)
70+
baseName := strings.Join(baseNameArray, ".")
71+
72+
unsortedOrdered[baseName] = append(unsortedOrdered[baseName], res)
6573

6674
// store as invdividual host (eg. <name>.<count>)
67-
invdName := fmt.Sprintf("%s.%d", res.baseName, res.counter)
75+
invdName := fmt.Sprintf("%s.%d", baseName, res.counter)
6876
if old, exists := individual[invdName]; exists {
6977
fmt.Fprintf(os.Stderr, "overwriting already existing individual key %s, old: %v, new: %v", invdName, old, res.Address())
7078
}

parser.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (s *state) resources() []*Resource {
6262
// Terraform stores resources in a name->map map, but we need the name to
6363
// decide which groups to include the resource in. So wrap it in a higher-
6464
// level object with both properties.
65-
r, err := NewResource(k, m.ResourceStates[k])
65+
r, err := NewResource(k, m.ResourceStates[k], m.Path)
6666
if err != nil {
6767
continue
6868
}
@@ -76,6 +76,7 @@ func (s *state) resources() []*Resource {
7676
}
7777

7878
type moduleState struct {
79+
Path []string `json:"path"`
7980
ResourceStates map[string]resourceState `json:"resources"`
8081
Outputs map[string]interface{} `json:"outputs"`
8182
}

resource.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ type Resource struct {
5555
resourceType string
5656
baseName string
5757
counter int
58+
modulePath string
5859
}
5960

60-
func NewResource(keyName string, state resourceState) (*Resource, error) {
61+
func NewResource(keyName string, state resourceState, path []string) (*Resource, error) {
6162
m := nameParser.FindStringSubmatch(keyName)
6263

6364
// This should not happen unless our regex changes.
@@ -85,6 +86,7 @@ func NewResource(keyName string, state resourceState) (*Resource, error) {
8586
resourceType: m[1],
8687
baseName: m[2],
8788
counter: c,
89+
modulePath: strings.Join(path, "."),
8890
}, nil
8991
}
9092

0 commit comments

Comments
 (0)