-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
main.go
195 lines (157 loc) · 5.28 KB
/
main.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
package lock
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
terraformRegistryAddress "github.com/hashicorp/terraform-registry-address"
"github.com/minamijoyo/tfupdate/lock"
"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"
"github.com/updatecli/updatecli/pkg/core/result"
"github.com/updatecli/updatecli/pkg/core/text"
terraformUtils "github.com/updatecli/updatecli/pkg/plugins/resources/terraform"
"github.com/updatecli/updatecli/pkg/plugins/utils"
"github.com/zclconf/go-cty/cty"
)
type TerraformLock struct {
spec Spec
contentRetriever text.TextRetriever
files map[string]file // map of file paths to file contents
lockIndex lock.Index // index is a cached index for updating dependency lock files.
provider terraformRegistryAddress.Provider
}
type file struct {
originalFilePath string
filePath string
content string
}
func New(spec interface{}) (*TerraformLock, error) {
newSpec := Spec{}
err := mapstructure.Decode(spec, &newSpec)
if err != nil {
return nil, err
}
newResource := &TerraformLock{
spec: newSpec,
contentRetriever: &text.Text{},
}
err = newResource.spec.Validate()
if err != nil {
return nil, err
}
newResource.files = make(map[string]file)
// File as unique element of newResource.files
if len(newResource.spec.File) > 0 {
filePath := strings.TrimPrefix(newResource.spec.File, "file://")
newResource.files[filePath] = file{
originalFilePath: filePath,
filePath: filePath,
}
}
// Files
for _, filePath := range newResource.spec.Files {
filePath := strings.TrimPrefix(filePath, "file://")
newResource.files[filePath] = file{
originalFilePath: filePath,
filePath: filePath,
}
}
lockIndex, err := lock.NewDefaultIndex()
if err != nil {
return nil, err
}
newResource.lockIndex = lockIndex
provider, err := terraformRegistryAddress.ParseProviderSource(newResource.spec.Provider)
if err != nil {
return nil, err
}
newResource.provider = provider
return newResource, nil
}
func (t *TerraformLock) Query(resourceFile file) (string, []string, error) {
file, err := terraformUtils.ParseHcl(resourceFile.content, resourceFile.originalFilePath)
if err != nil {
return "", nil, err
}
providerBlock, err := getProviderBlock(file, resourceFile.originalFilePath, t.provider.String())
if err != nil {
return "", nil, err
}
quotedValue := strings.TrimSpace(string(providerBlock.Body().GetAttribute("version").Expr().BuildTokens(nil).Bytes()))
version := strings.Trim(quotedValue, `"`)
hashesTokens := providerBlock.Body().GetAttribute("hashes").Expr().BuildTokens(nil)
var hashes []string
for _, t := range hashesTokens {
if t.Type == hclsyntax.TokenQuotedLit {
hashes = append(hashes, string(t.Bytes))
}
}
return version, hashes, nil
}
func (t *TerraformLock) Apply(filePath string, versionToWrite string, hashesToWrite []string) error {
resourceFile := t.files[filePath]
file, err := terraformUtils.ParseHcl(resourceFile.content, resourceFile.originalFilePath)
if err != nil {
return err
}
providerBlock, err := getProviderBlock(file, resourceFile.originalFilePath, t.provider.String())
if err != nil {
return err
}
providerBlock.Body().SetAttributeValue("version", cty.StringVal(versionToWrite))
if providerBlock.Body().GetAttribute("constraints") != nil && !t.spec.SkipConstraints {
providerBlock.Body().SetAttributeValue("constraints", cty.StringVal(versionToWrite))
}
providerBlock.Body().SetAttributeRaw("hashes", tokensForListPerLine(hashesToWrite))
resourceFile.content = string(hclwrite.Format(file.BuildTokens(nil).Bytes()))
t.files[filePath] = resourceFile
return nil
}
// Read puts the content of the file(s) as value of the y.files map if the file(s) exist(s) or log the non existence of the file
func (t *TerraformLock) Read() error {
var err error
// Retrieve files content
for filePath := range t.files {
f := t.files[filePath]
if t.contentRetriever.FileExists(f.filePath) {
f.content, err = t.contentRetriever.ReadAll(f.filePath)
if err != nil {
return err
}
t.files[filePath] = f
} else {
return fmt.Errorf("%s The specified file %q does not exist", result.FAILURE, f.filePath)
}
}
return nil
}
func (t *TerraformLock) UpdateAbsoluteFilePath(workDir string) {
for filePath := range t.files {
if workDir != "" {
f := t.files[filePath]
f.filePath = utils.JoinFilePathWithWorkingDirectoryPath(f.originalFilePath, workDir)
logrus.Debugf(workDir)
logrus.Debugf("Relative path detected: changing from %q to absolute path from SCM: %q", f.originalFilePath, f.filePath)
t.files[filePath] = f
}
}
}
// Changelog returns the changelog for this resource, or an empty string if not supported
func (t *TerraformLock) Changelog() string {
return ""
}
func (t *TerraformLock) getProviderHashes(version string) ([]string, error) {
pv, err := t.lockIndex.GetOrCreateProviderVersion(context.Background(), t.provider.ForDisplay(), version, t.spec.Platforms)
if err != nil {
return nil, fmt.Errorf("%s failed to query provider locks for provider: %q, version: %q, platforms: %q: %s",
result.FAILURE,
t.spec.Provider,
version,
t.spec.Platforms,
err.Error(),
)
}
return pv.AllHashes(), nil
}