-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform_modules.go
188 lines (151 loc) · 4.04 KB
/
terraform_modules.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
package testhelpers
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"testing"
"github.com/gruntwork-io/terratest/modules/files"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/mattn/go-zglob"
"github.com/zclconf/go-cty/cty"
)
func UpdateModuleSourcesToLocalPaths(t *testing.T, dst string) {
metadata, err := GetModuleMetadataCatalog()
if err != nil {
t.Fatalf("Error when building the module metadata catalog: %s", err.Error())
}
err = IterateTerraformInDirectory(dst, func(filename string, f *hclwrite.File) error {
hasChanges := false
for _, block := range f.Body().Blocks() {
if block.Type() != "module" || len(block.Labels()) != 1 {
continue
}
source := block.Body().GetAttribute("source").Expr().BuildTokens(nil).Bytes()
path, ok := metadata.Resolve(string(source))
if !ok {
continue
}
target, err := files.CopyTerraformFolderToTemp(path, cleanName(t.Name()))
if err != nil {
return err
}
UpdateModuleSourcesToLocalPaths(t, target)
block.Body().SetAttributeValue("source", cty.StringVal(target))
block.Body().RemoveAttribute("version")
hasChanges = true
}
if hasChanges {
if err := os.WriteFile(filename, f.Bytes(), 0o666); err != nil {
return err
}
}
return nil
})
if err != nil {
t.Fatalf("An error occurred when attempting to resolve all module sources to local paths: %s", err.Error())
}
}
type ModuleMetadata struct {
Organisation string
Name string
Provider string
LocalPath string
}
type ModuleMetadataCatalog struct {
Meta []ModuleMetadata
mx sync.RWMutex
init bool
root string
}
var mmc = initModuleMetadataCatalog()
func (mmc *ModuleMetadataCatalog) Resolve(src string) (string, bool) {
parts := strings.Split(strings.Trim(src, " \""), "/")
if len(parts) != 4 {
return "", false
}
for _, meta := range mmc.Meta {
if meta.Organisation == parts[1] && meta.Name == parts[2] && meta.Provider == parts[3] {
return meta.LocalPath, true
}
}
return "", false
}
func (mmc *ModuleMetadataCatalog) Init() error {
pattern := fmt.Sprintf("%s/**/metadata.json", mmc.root)
matches, err := zglob.Glob(pattern)
if err != nil {
return err
}
filteredMatches := filterMatches(matches, ".terraform")
mmc.Meta = make([]ModuleMetadata, 0)
for _, path := range filteredMatches {
content, err := os.ReadFile(path)
if err != nil {
return err
}
metadata := struct {
Publish struct {
Name string `json:"name"`
Provider string `json:"provider"`
Organisation string `json:"organisation"`
} `json:"publish"`
}{}
err = json.Unmarshal(content, &metadata)
if err != nil {
return err
}
parts := strings.Split(path, string(os.PathSeparator))
dir := strings.Join(parts[:len(parts)-1], string(os.PathSeparator))
module := ModuleMetadata{
Organisation: metadata.Publish.Organisation,
Name: metadata.Publish.Name,
Provider: metadata.Publish.Provider,
LocalPath: dir,
}
if module.Organisation == "" {
module.Organisation = "ovotech"
}
mmc.Meta = append(mmc.Meta, module)
}
mmc.init = true
return nil
}
func (mmc *ModuleMetadataCatalog) SetRoot(root string) {
mmc.root = root
mmc.init = false
}
func GetModuleMetadataCatalog() (*ModuleMetadataCatalog, error) {
if !mmc.init {
if err := mmc.Init(); err != nil {
return nil, err
}
}
return mmc, nil
}
func initModuleMetadataCatalog() *ModuleMetadataCatalog {
mmc := ModuleMetadataCatalog{}
path, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil {
return &mmc
}
mmc.root = strings.TrimSpace(string(path))
mmc.init = false
return &mmc
}
func cleanName(originalName string) string {
parts := strings.Split(originalName, "/")
return parts[len(parts)-1]
}
// filters the matches based on a given pattern and returns a new array
func filterMatches(matches []string, pattern string) []string {
newMatches := make([]string, 0, len(matches))
for _, m := range matches {
if !strings.Contains(m, pattern) {
newMatches = append(newMatches, m)
}
}
return newMatches
}