Skip to content

Commit

Permalink
Enhance dev workflow and load spec on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
danischm committed Sep 24, 2024
1 parent ee4c8e4 commit fc64b56
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ terraform-provider-meraki

# Keep windows files with windows line endings
*.winfile eol=crlf

gen/models/*
11 changes: 11 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ default: testacc
testacc:
TF_ACC=1 go test -v $(TESTARGS) -timeout 120m ./internal/provider

# Create new definition and build the code
.PHONY: def
def:
go run gen/load_models.go
go run ./gen/definition.go "$(DEF_PATH)" "$(DEF_NAME)"
go run ./gen/generator.go "$(DEF_PATH)" "$(DEF_NAME)"
go run golang.org/x/tools/cmd/goimports -w internal/provider/
terraform fmt -recursive ./examples/
go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
go run gen/doc_category.go

# Update all definitions from OpenAPI spec
.PHONY: update
update:
Expand Down
9 changes: 9 additions & 0 deletions gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ func main() {
return
}

resourceName := ""
if len(os.Args) == 2 {
resourceName = os.Args[2]
}

// Load configs
var configs []yamlconfig.YamlConfig
files, _ := os.ReadDir(definitionsPath)
Expand All @@ -282,6 +287,10 @@ func main() {

var providerConfig []string
for i := range configs {
if resourceName != "" && configs[i].Name != resourceName {
continue
}
fmt.Printf("Generating: %s\n", configs[i].Name)
// Iterate over templates and render files
for _, t := range templates {
if (configs[i].NoImport && t.path == "./gen/templates/import.sh") ||
Expand Down
68 changes: 68 additions & 0 deletions gen/load_models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright © 2024 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Mozilla Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://mozilla.org/MPL/2.0/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: MPL-2.0

//go:build ignore

package main

import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
)

var models = []string{
"https://raw.githubusercontent.com/meraki/openapi/refs/tags/v1.50.0/openapi/spec3.json",
}

const (
modelsPath = "./gen/models/"
)

func main() {
for _, model := range models {
f := strings.Split(model, "/")
path := filepath.Join(modelsPath, f[len(f)-1])
if _, err := os.Stat(path); err != nil {
err := downloadModel(path, model)
if err != nil {
panic(err)
}
fmt.Println("Downloaded model: " + path)
}
}
}

func downloadModel(filepath string, url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()

_, err = io.Copy(out, resp.Body)
return err
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (

// Run "go generate" to format example terraform files and generate the docs for the registry/website

// Load the models
//go:generate go run gen/load_models.go

// Run the resource and datasource generation tool.
//go:generate go run gen/generator.go

Expand Down

0 comments on commit fc64b56

Please sign in to comment.