Skip to content

Commit

Permalink
iterating over all resources
Browse files Browse the repository at this point in the history
  • Loading branch information
AgustinBettati committed Oct 17, 2024
1 parent 56261da commit 5fb9add
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ vendor/
*.winfile eol=crlf
/.vs
node_modules

#used for schema code generation but is not commited to avoid constant updates
tools/codegen/open-api-spec.yml
28 changes: 19 additions & 9 deletions tools/codegen/codespec/api_to_provider_spec_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,37 @@ import (
"github.com/mongodb/terraform-provider-mongodbatlas/tools/codegen/openapi"
)

func ToCodeSpecModel(atlasAdminAPISpecFilePath, configPath string, resourceName SnakeCaseString) (*Model, error) {
func ToCodeSpecModel(atlasAdminAPISpecFilePath, configPath string, resourceName *string) (*Model, error) {
apiSpec, err := openapi.ParseAtlasAdminAPI(atlasAdminAPISpecFilePath)
if err != nil {
return nil, fmt.Errorf("unable to parse Atlas Admin API: %v", err)
}

config, err := config.ParseGenConfigYAML(configPath)
configModel, err := config.ParseGenConfigYAML(configPath)
if err != nil {
return nil, fmt.Errorf("unable to parse config file: %v", err)
}

// find resource operations, schemas, etc from OAS
oasResource, err := getAPISpecResource(apiSpec.Model, config.Resources[resourceName.SnakeCase()], resourceName)
if err != nil {
return nil, fmt.Errorf("unable to get APISpecResource schema: %v", err)
resourceConfigsToIterate := configModel.Resources
if resourceName != nil { // only generate a specific resource
resourceConfigsToIterate = map[string]config.Resource{
*resourceName: configModel.Resources[*resourceName],
}
}

// map OAS resource model to CodeSpecModel
codeSpecResource := apiSpecResourceToCodeSpecModel(oasResource, config.Resources[resourceName.SnakeCase()], resourceName)
var results []Resource
for name, resourceConfig := range resourceConfigsToIterate {
log.Printf("Generating resource: %s", name)
// find resource operations, schemas, etc from OAS
oasResource, err := getAPISpecResource(apiSpec.Model, resourceConfig, SnakeCaseString(name))
if err != nil {
return nil, fmt.Errorf("unable to get APISpecResource schema: %v", err)
}
// map OAS resource model to CodeSpecModel
results = append(results, *apiSpecResourceToCodeSpecModel(oasResource, resourceConfig, SnakeCaseString(name)))
}

return &Model{Resources: []Resource{*codeSpecResource}}, nil
return &Model{Resources: results}, nil
}

func apiSpecResourceToCodeSpecModel(oasResource APISpecResource, resourceConfig config.Resource, name SnakeCaseString) *Resource {
Expand Down
4 changes: 2 additions & 2 deletions tools/codegen/codespec/api_to_provider_spec_mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestConvertToProviderSpec(t *testing.T) {

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
result, err := codespec.ToCodeSpecModel(tc.inputOpenAPISpecPath, tc.inputConfigPath, codespec.SnakeCaseString(tc.inputResourceName))
result, err := codespec.ToCodeSpecModel(tc.inputOpenAPISpecPath, tc.inputConfigPath, &tc.inputResourceName)
require.NoError(t, err)
assert.Equal(t, tc.expectedResult, result, "Expected result to match the specified structure")
})
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestConvertToProviderSpec_nested(t *testing.T) {

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
result, err := codespec.ToCodeSpecModel(tc.inputOpenAPISpecPath, tc.inputConfigPath, codespec.SnakeCaseString(tc.inputResourceName))
result, err := codespec.ToCodeSpecModel(tc.inputOpenAPISpecPath, tc.inputConfigPath, &tc.inputResourceName)
require.NoError(t, err)
assert.Equal(t, tc.expectedResult, result, "Expected result to match the specified structure")
})
Expand Down
4 changes: 2 additions & 2 deletions tools/codegen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ resources:
],
definition: "stringvalidator.ConflictsWith(path.MatchRoot(\"name\"))"
}]

prefix_path:
computability:
optional: true
computed: true

timeouts: ["create", "read", "delete"]
timeouts: ["create", "read", "delete"]
11 changes: 2 additions & 9 deletions tools/codegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,17 @@ import (
const (
atlasAdminAPISpecURL = "https://raw.githubusercontent.com/mongodb/atlas-sdk-go/main/openapi/atlas-api-transformed.yaml"
configPath = "tools/codegen/config.yml"
specFilePath = "open-api-spec.yml"
specFilePath = "tools/codegen/open-api-spec.yml"
)

func main() {
resourceName := getOsArg()
if resourceName == nil {
log.Fatal("No resource name provided")
}
if !codespec.ValidateSnakeCase(*resourceName) {
log.Fatal("Resource name must be snake case")
}
log.Printf("Resource name: %s\n", *resourceName)

if err := openapi.DownloadOpenAPISpec(atlasAdminAPISpecURL, specFilePath); err != nil {
log.Fatalf("an error occurred when downloading Atlas Admin API spec: %v", err)
}

model, err := codespec.ToCodeSpecModel(specFilePath, configPath, codespec.SnakeCaseString(*resourceName))
model, err := codespec.ToCodeSpecModel(specFilePath, configPath, resourceName)
if err != nil {
log.Fatalf("an error occurred while generating codespec.Model: %v", err)
}
Expand Down

0 comments on commit 5fb9add

Please sign in to comment.