This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
api_generator.go
139 lines (119 loc) · 3.97 KB
/
api_generator.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
package main
import (
"bufio"
"cloud.google.com/go/storage"
"context"
"encoding/json"
"fmt"
"github.com/mollie/tf-provider-registry-api-generator/versions"
"io/ioutil"
"log"
"path"
"reflect"
"strings"
)
func assertDiscoveryDocument(bucket *storage.BucketHandle) {
content := make(map[string]string)
expect := map[string]string{
"providers.v1": "/v1/providers/",
}
p := path.Join(".well-known", "terraform.json")
err := readJson(bucket, p, &content)
if err != nil {
log.Fatalf("could not read content of %s, %s", p, err)
}
if !reflect.DeepEqual(expect, content) {
log.Printf("INFO: writing content to %s", p)
writeJson(bucket, p, expect)
} else {
log.Printf("INFO: discovery document is up-to-date\n")
}
}
func readJson(bucket *storage.BucketHandle, filename string, object interface{}) error {
r, err := bucket.Object(filename).NewReader(context.Background())
if err != nil {
if err.Error() == "storage: object doesn't exist" {
return nil
}
return fmt.Errorf("ERROR: failed to read file %s, %s", filename, err)
}
defer r.Close()
body, err := ioutil.ReadAll(r)
if err != nil {
return fmt.Errorf("ERROR: failed to read content from %s, %s", filename, err)
}
err = json.Unmarshal(body, &object)
if err != nil {
return fmt.Errorf("ERROR: failed to unmarshal %s, %s", filename, err)
}
return nil
}
func readShasums(bucket *storage.BucketHandle, filename string, shasums map[string]string) error {
r, err := bucket.Object(filename).NewReader(context.Background())
if err != nil {
if err.Error() == "storage: object doesn't exist" {
return nil
}
return fmt.Errorf("ERROR: failed to read file %s, %s", filename, err)
}
defer r.Close()
scanner := bufio.NewScanner(r)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) != 2 {
log.Fatalf("ERROR: expected %s to contain 2 fields on each line, found %d", filename, len(fields))
}
shasums[fields[1]] = fields[0]
}
return nil
}
func writeJson(bucket *storage.BucketHandle, filename string, content interface{}) {
log.Printf("INFO: writing %s", filename)
w := bucket.Object(filename).NewWriter(context.Background())
w.ContentType = "application/json"
w.CacheControl = "no-cache, max-age=60"
encoder := json.NewEncoder(w)
encoder.SetIndent("", " ")
err := encoder.Encode(content)
if err != nil {
log.Fatalf("INFO: failed to write %s, %s", filename, err)
}
if err = w.Close(); err != nil {
log.Fatalf("INFO: failed to close %s, %s", filename, err)
}
}
func writeProviderVersions(bucket *storage.BucketHandle, directory string, newVersions *versions.ProviderVersions) {
var existing versions.ProviderVersions
if err := readJson(bucket, path.Join(directory, "versions"), &existing); err != nil {
log.Fatalf("ERROR: failed to read the %s/versions, %s", directory, err)
}
if reflect.DeepEqual(&existing, newVersions) {
log.Printf("INFO: %s/versions already up-to-date", directory)
return
}
existing.Merge(*newVersions)
writeJson(bucket, path.Join(directory, "versions"), existing)
}
func writeProviderVersion(bucket *storage.BucketHandle, directory string, version *versions.BinaryMetaData) {
filename := path.Join(directory, version.Version, "download", version.Os, version.Arch)
existing := versions.BinaryMetaData{}
if err := readJson(bucket, filename, &existing); err != nil {
log.Fatalf("ERROR: failed to read %s, %s", filename, err)
}
if existing.Equals(version) {
log.Printf("INFO: %s is up-to-date", filename)
return
}
writeJson(bucket, filename, version)
}
func WriteAPIDocuments(bucket *storage.BucketHandle, namespace string, binaries versions.BinaryMetaDataList) {
assertDiscoveryDocument(bucket)
providerDirectory := path.Join("v1", "providers", namespace)
providers := binaries.ExtractVersions()
for _, binary := range binaries {
writeProviderVersion(bucket, path.Join(providerDirectory, binary.TypeName), &binary)
}
for name, versions := range providers {
writeProviderVersions(bucket, path.Join(providerDirectory, name), versions)
}
}