Skip to content

Commit 7fd9e0d

Browse files
Discovery downloads JSON schemas (#174)
* Discovery downloads JSON schemas * Configurable list of URLs
1 parent 86e49dc commit 7fd9e0d

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ update_submodules:: init_submodules
2929
done
3030

3131
discovery:: update_submodules codegen
32-
$(WORKING_DIR)/bin/$(CODEGEN) discovery $(CFN_SCHEMA_DIR) ${VERSION}
32+
$(WORKING_DIR)/bin/$(CODEGEN) discovery $(CFN_SCHEMA_DIR) ${VERSION} https://schema.cloudformation.us-east-1.amazonaws.com/CloudformationSchema.zip,https://schema.cloudformation.us-west-2.amazonaws.com/CloudformationSchema.zip
3333

3434
ensure:: init_submodules
3535
@echo "GO111MODULE=on go mod tidy"

provider/cmd/pulumi-gen-aws-native/main.go

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
package main
44

55
import (
6+
"archive/zip"
67
"bytes"
78
"compress/gzip"
89
ctx "context"
910
"encoding/json"
1011
"flag"
1112
"fmt"
13+
"io"
1214
"io/ioutil"
15+
"net/http"
1316
"os"
1417
"path/filepath"
1518
"sort"
@@ -32,7 +35,7 @@ import (
3235

3336
func main() {
3437
flag.Usage = func() {
35-
const usageFormat = "Usage: %s <languages> <schema-file> <root-dir>"
38+
const usageFormat = "Usage: %s <languages> <schema-folder> <version> (<schema urls>)"
3639
_, err := fmt.Fprintf(flag.CommandLine.Output(), usageFormat, os.Args[0])
3740
contract.IgnoreError(err)
3841
flag.PrintDefaults()
@@ -52,9 +55,18 @@ func main() {
5255
genDir := filepath.Join(".", "provider", "cmd", "pulumi-gen-aws-native")
5356

5457
if languages == "discovery" {
58+
if len(args) < 4 {
59+
flag.Usage()
60+
return
61+
}
62+
jsonSchemaUrls := strings.Split(args[3], ",")
63+
5564
if err := writeSupportedResourceTypes(genDir); err != nil {
5665
panic(err)
5766
}
67+
if err := downloadCloudFormationSchemas(jsonSchemaUrls, filepath.Join(".", schemaFolder)); err != nil {
68+
panic(err)
69+
}
5870
return
5971
}
6072

@@ -181,6 +193,51 @@ func writeSupportedResourceTypes(outDir string) error {
181193
return emitFile(outDir, supportedResourcesFile, []byte(val))
182194
}
183195

196+
func downloadCloudFormationSchemas(urls []string, outDir string) error {
197+
for _, url := range urls {
198+
resp, err := http.Get(url)
199+
if err != nil {
200+
return err
201+
}
202+
defer resp.Body.Close()
203+
204+
body, err := ioutil.ReadAll(resp.Body)
205+
if err != nil {
206+
return err
207+
}
208+
209+
zipReader, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
210+
if err != nil {
211+
return err
212+
}
213+
214+
// Read all the files from zip archive
215+
for _, f := range zipReader.File {
216+
outPath := filepath.Join(outDir, f.Name)
217+
218+
outFile, err := os.OpenFile(outPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
219+
if err != nil {
220+
return err
221+
}
222+
223+
rc, err := f.Open()
224+
if err != nil {
225+
return err
226+
}
227+
228+
_, err = io.Copy(outFile, rc)
229+
if err != nil {
230+
return err
231+
}
232+
233+
outFile.Close()
234+
rc.Close()
235+
}
236+
}
237+
238+
return nil
239+
}
240+
184241
func generate(ppkg *pschema.Package, language string) (map[string][]byte, error) {
185242
toolDescription := "the Pulumi SDK Generator"
186243
extraFiles := map[string][]byte{}

0 commit comments

Comments
 (0)