-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproviders.go
338 lines (289 loc) · 9.13 KB
/
providers.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package testhelpers
import (
"archive/zip"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"testing"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclwrite"
)
// GetProviderConstraintE returns the version string for the given provider
// or an error if the provider cannot be found
func GetProviderConstraintE(srcDir, provider string) (string, error) {
files, err := os.ReadDir(srcDir)
if err != nil {
return "", fmt.Errorf("Error: %s", err)
}
vRegexp := regexp.MustCompile("version\\s*=\\s*\"([^\"]+)\"")
for _, file := range files {
if file.IsDir() {
continue
}
if !strings.HasSuffix(file.Name(), ".tf") {
continue
}
filename := fmt.Sprintf("%s/%s", srcDir, file.Name())
content, err := ioutil.ReadFile(filename)
if err != nil {
return "", fmt.Errorf("Error: %s", err)
}
f, diag := hclwrite.ParseConfig(content, file.Name(), hcl.Pos{Line: 1, Column: 1})
if diag.HasErrors() {
return "", errors.New(diag.Error())
}
for _, block := range f.Body().Blocks() {
if block.Type() != "terraform" {
continue
}
for _, block := range block.Body().Blocks() {
if block.Type() != "required_providers" {
continue
}
provMap := block.Body().GetAttribute(provider)
if provMap == nil {
continue
}
val := provMap.BuildTokens(nil).Bytes()
constraint := vRegexp.FindSubmatch(val)
if constraint == nil || len(constraint) < 2 {
continue
}
return string(constraint[1]), nil
}
}
}
return "", fmt.Errorf("provider %s not found", provider)
}
// GetProviderConstraint returns the version string for the given provider or
// fails the test if the provider is not found
func GetProviderConstraint(t *testing.T, srcDir, provider string) string {
constraint, err := GetProviderConstraintE(srcDir, provider)
if err != nil {
t.Fatalf(err.Error())
}
return constraint
}
// GetBinaryUrlE will return the correct download URL for the provider binary version requested
// based on the operating system and architecture by fetching it from the Hashicorp releases API
//
// Usage:
// * version is the version of provider to download.
func GetBinaryUrl(version string, providerName string) (string, error) {
var binaryUrl string
operatingSystem := runtime.GOOS
architecture := runtime.GOARCH
releasesApiReq := fmt.Sprintf("https://api.releases.hashicorp.com/v1/releases/terraform-provider-"+providerName+"/%s", version)
resp, err := http.Get(releasesApiReq)
if err != nil {
return "", fmt.Errorf("Error: %s", err)
}
body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
return "", fmt.Errorf("Error: %s", err)
}
var result struct {
Builds []struct {
Arch string `json:"arch"`
Os string `json:"os"`
Url string `json:"url"`
}
}
if err := json.Unmarshal(body, &result); err != nil {
return "", err
}
for _, res := range result.Builds {
if res.Arch == architecture && res.Os == operatingSystem {
binaryUrl = res.Url
}
}
if len(binaryUrl) > 0 {
return binaryUrl, nil
} else {
return "", errors.New("Unable to find an appropriate binary download URL for the underlying OS and architecture")
}
}
// GetBinaryPath will return the cache path required to store the provider cache
//
// Usage:
// * cachePath is the path required to store the provider cache
func GetBinaryPath() (cachePath string) {
homeDirectory, _ := os.UserHomeDir()
binaryPath := filepath.Join(homeDirectory, ".terraform.d/plugin-cache")
return binaryPath
}
// DownloadProviderVersionE will download the specified version of the provider into the ~/.terraform.d/plugin-cache directory
// Usage:
// * version is the version of provider to download.
// * sourceAddress is the sourceAddress of provider to download.
// * providerName is the name of provider to download.
func DownloadProviderVersionE(version string, sourceAddress string, providerName string) (binaryPath string, err error) {
operatingSystem := runtime.GOOS
architecture := runtime.GOARCH
// Initialise all path variables
binaryDownloadDirectory := GetBinaryPath()
binaryPath = binaryDownloadDirectory + "/registry.terraform.io/" + sourceAddress + "/" + version
// Don't do anything if the required binary already exists
_, err = os.Stat(binaryPath)
if err == nil {
return binaryPath, nil
}
if !errors.Is(err, os.ErrNotExist) {
return "", fmt.Errorf("unexpected error: %w", err)
}
// Create ~/.terraform.d/plugin-cache directory if it doesn't exist
// https://gist.github.com/ivanzoid/5040166bb3f0c82575b52c2ca5f5a60c
if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
os.MkdirAll(binaryPath, os.ModeDir|0o755)
}
var binaryUrl string
binaryUrl, err = GetBinaryUrl(version, providerName)
if err != nil {
return "", fmt.Errorf("Error: %s", err)
}
req := fmt.Sprintf(binaryUrl)
resp, err := http.Get(req)
if err != nil {
return "", fmt.Errorf("Error: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", nil
}
// Create the file
out, err := os.Create("/tmp/" + version + "_binary.zip")
if err != nil {
return "", fmt.Errorf("Error: %s", err)
}
// Write the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return "", fmt.Errorf("Error: %s", err)
}
// Sample code to extract zip file taken from https://stackoverflow.com/questions/20357223/easy-way-to-unzip-file-with-golang
r, err := zip.OpenReader(out.Name())
if err != nil {
return "", fmt.Errorf("Error: %s", err)
}
// Cleanup temp directories
defer func() {
if tempErr := r.Close(); tempErr != nil {
err = tempErr
}
}()
defer os.Remove(out.Name())
zipExtractPath := binaryDownloadDirectory + "/bin_" + version
os.MkdirAll(zipExtractPath, 0o755)
// Cleanup zip files
defer os.RemoveAll(zipExtractPath)
for _, f := range r.File {
err := extractAndWriteFile(zipExtractPath, f)
if err != nil {
return "", fmt.Errorf("Error: %s", err)
}
}
// Move file from temporarily extracted location
oldBinaryLocation := zipExtractPath + "/"
err = os.Rename(oldBinaryLocation, binaryPath+"/"+operatingSystem+"_"+architecture)
if err != nil {
return "", fmt.Errorf("Error: %s", err)
}
return binaryDownloadDirectory, nil
}
// DownloadProviderVersion will download the specified version of provider into the ~/.terraform.d/plugin-cache directory.
//
// Usage:
// * version is the version of provider to download.
// * sourceAddress is the sourceAddress of provider to download.
// * providerName is the name of provider to download.
func DownloadProviderVersion(t *testing.T, version string, sourceAddress string, providerName string) string {
binaryPath, err := DownloadProviderVersionE(version, sourceAddress, providerName)
if err != nil {
t.Fatalf(err.Error())
}
return binaryPath
}
// DownloadRequiredProviders will download the specified version of provider into the ~/.terraform.d/plugin-cache directory.
//
// Usage:
// * provider is the name of provider to download.
func DownloadRequiredProviders(t *testing.T, srcDir string, provider string) {
constraint := GetProviderConstraint(t, srcDir, provider)
available := GetAvailableVersions(t, "terraform-provider-"+provider)
testVers := GetMatchingVersions(t, constraint, available)
sourceAddress := GetSourceAddress(t, srcDir, provider)
for _, version := range testVers {
version := version
DownloadProviderVersion(t, version, sourceAddress, provider)
}
}
// GetSourceAddress returns the source string for the given provider or
// fails the test if the provider is not found
// Usage:
// * provider is the name of provider to download.
func GetSourceAddress(t *testing.T, srcDir, provider string) string {
constraint, err := GetSourceAddressE(srcDir, provider, "source")
if err != nil {
t.Fatalf(err.Error())
}
return constraint
}
// GetSourceAddressE returns the source string for the given provider
// or an error if the provider cannot be found
// Usage:
// * attrribute is the name of attrribute to return.
func GetSourceAddressE(srcDir, provider string, attrribute string) (string, error) {
files, err := os.ReadDir(srcDir)
if err != nil {
fmt.Errorf("Error: %s", err)
return "", err
}
vRegexp := regexp.MustCompile(attrribute + "\\s*=\\s*\"([^\"]+)\"")
for _, file := range files {
if file.IsDir() {
continue
}
if !strings.HasSuffix(file.Name(), ".tf") {
continue
}
filename := fmt.Sprintf("%s/%s", srcDir, file.Name())
content, err := ioutil.ReadFile(filename)
if err != nil {
return "", fmt.Errorf("Error: %s", err)
}
f, diag := hclwrite.ParseConfig(content, file.Name(), hcl.Pos{Line: 1, Column: 1})
if diag.HasErrors() {
return "", errors.New(diag.Error())
}
for _, block := range f.Body().Blocks() {
if block.Type() != "terraform" {
continue
}
for _, block := range block.Body().Blocks() {
if block.Type() != "required_providers" {
continue
}
provMap := block.Body().GetAttribute(provider)
if provMap == nil {
continue
}
val := provMap.BuildTokens(nil).Bytes()
constraint := vRegexp.FindSubmatch(val)
if constraint == nil || len(constraint) < 2 {
continue
}
return string(constraint[1]), nil
}
}
}
return "", fmt.Errorf("provider %s not found", provider)
}