-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3831 from hashicorp/refactor/9-arm-importer-trans…
…forms-and-sdk-models `tools/importer-rest-api-specs`: splitting the `dataapigeneratorjson` package into transforms and stages
- Loading branch information
Showing
59 changed files
with
1,777 additions
and
1,437 deletions.
There are no files selected for viewing
38 changes: 0 additions & 38 deletions
38
tools/importer-rest-api-specs/components/dataapigeneratorjson/definition_version.go
This file was deleted.
Oops, something went wrong.
123 changes: 123 additions & 0 deletions
123
tools/importer-rest-api-specs/components/dataapigeneratorjson/filesystem.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package dataapigeneratorjson | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/pandora/tools/data-api-sdk/v1/models" | ||
) | ||
|
||
var directoryPermissions = os.FileMode(0755) | ||
|
||
// filePath is a typealias to make it clearer what's being returned from this generationStage. | ||
// This represents the path to a file on disk | ||
type filePath = string | ||
|
||
// fileBody is a typealias to make it clearer what's being returned from this generationStage. | ||
type fileBody = []byte | ||
|
||
// fileSystem is an abstraction around a set of files within a working directory. | ||
// It's intended to allow a representation around the API Definitions working directory | ||
// containing a reference to both the files that _would_ be written (during saving/testing) | ||
// and that exist (when loading the API Definitions from disk). | ||
type fileSystem struct { | ||
f map[filePath]fileBody | ||
} | ||
|
||
func newFileSystem() *fileSystem { | ||
return &fileSystem{ | ||
f: map[filePath]fileBody{}, | ||
} | ||
} | ||
|
||
// stage stages the specified body at the specified path, ensuring that it's unique. | ||
// This doesn't persist the file to disk, which is handled in persistFileSystem. | ||
func (f *fileSystem) stage(path filePath, body any) error { | ||
if _, existing := f.f[path]; existing { | ||
return fmt.Errorf("a duplicate file exists at the path %q", path) | ||
} | ||
|
||
fileExtension := strings.ToLower(filepath.Ext(path)) | ||
if fileExtension == ".hcl" { | ||
str, ok := body.(string) | ||
if !ok { | ||
return fmt.Errorf("`body` must be a string for an `*.hcl` file but got %T", body) | ||
} | ||
|
||
f.f[path] = []byte(str) | ||
return nil | ||
} | ||
|
||
if fileExtension == ".json" { | ||
bytes, err := json.MarshalIndent(body, "", " ") | ||
if err != nil { | ||
return fmt.Errorf("marshalling file body for %q as json: %+v", path, err) | ||
} | ||
|
||
f.f[path] = bytes | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("internal-error: unexpected file extension %q for %q", fileExtension, path) | ||
} | ||
|
||
func persistFileSystem(workingDirectory string, dataType models.SourceDataType, serviceName string, input *fileSystem, logger hclog.Logger) error { | ||
rootDir := filepath.Join(workingDirectory, string(dataType)) | ||
|
||
// Delete any existing directory with this service name | ||
serviceDir := filepath.Join(rootDir, serviceName) | ||
logger.Debug(fmt.Sprintf("Removing any existing Directory for Service %q", serviceName)) | ||
_ = os.RemoveAll(serviceDir) | ||
if err := os.MkdirAll(serviceDir, directoryPermissions); err != nil { | ||
return fmt.Errorf("recreating directory %q: %+v", serviceDir, err) | ||
} | ||
|
||
// pull out a list of directories | ||
directories := uniqueDirectories(input.f) | ||
logger.Debug(fmt.Sprintf("Creating directories for Service %q", serviceName)) | ||
for _, dir := range directories { | ||
dirPath := filepath.Join(rootDir, dir) | ||
if err := os.MkdirAll(dirPath, directoryPermissions); err != nil { | ||
return fmt.Errorf("creating directory %q: %+v", dirPath, err) | ||
} | ||
} | ||
|
||
// write the files | ||
for filePath, fileBody := range input.f { | ||
fileFullPath := filepath.Join(rootDir, filePath) | ||
logger.Trace(fmt.Sprintf("Writing file %q", fileFullPath)) | ||
file, err := os.Create(fileFullPath) | ||
if err != nil { | ||
return fmt.Errorf("opening %q: %+v", fileFullPath, err) | ||
} | ||
|
||
_, _ = file.Write(fileBody) | ||
_ = file.Close() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func uniqueDirectories(input map[filePath]fileBody) []string { | ||
directories := make(map[string]struct{}) | ||
for path := range input { | ||
dir := filepath.Dir(path) | ||
directories[dir] = struct{}{} | ||
} | ||
|
||
out := make([]string, 0) | ||
for k := range directories { | ||
out = append(out, k) | ||
} | ||
sort.Strings(out) | ||
|
||
return out | ||
} |
28 changes: 0 additions & 28 deletions
28
tools/importer-rest-api-specs/components/dataapigeneratorjson/generate_api_versions.go
This file was deleted.
Oops, something went wrong.
83 changes: 0 additions & 83 deletions
83
tools/importer-rest-api-specs/components/dataapigeneratorjson/generate_package.go
This file was deleted.
Oops, something went wrong.
34 changes: 0 additions & 34 deletions
34
tools/importer-rest-api-specs/components/dataapigeneratorjson/generate_service.go
This file was deleted.
Oops, something went wrong.
64 changes: 0 additions & 64 deletions
64
tools/importer-rest-api-specs/components/dataapigeneratorjson/generate_terraform.go
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.