Skip to content

Commit

Permalink
Address review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
randomswdev committed Jan 16, 2024
1 parent 3a68def commit 301689c
Show file tree
Hide file tree
Showing 8 changed files with 731 additions and 387 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20240116-102758.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ENHANCEMENTS
body: Added data source and resource support for query and path parameters specified
in the [OAS Path Item](https://spec.openapis.org/oas/v3.1.0#path-item-object)
time: 2024-01-16T10:27:58.255575839+01:00
custom:
Issue: "114"
2 changes: 2 additions & 0 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ In these OAS operations, the generator will search the `create` and `read` for s
- Will attempt to use `application/json` content-type first. If not found, will grab the first available content-type with a schema (alphabetical order)
4. `read` operation: [parameters](https://spec.openapis.org/oas/v3.1.0#parameterObject)
- The generator will merge all `query` and `path` parameters to the root of the schema.
- The generator will consider as parameters the ones in the [OAS Path Item](https://spec.openapis.org/oas/v3.1.0#path-item-object) and the ones in the [OAS Operation](https://spec.openapis.org/oas/v3.1.0#operation-object), merged based on the rules in the specification

All schemas found will be deep merged together, with the `requestBody` schema from the `create` operation being the **main schema** that the others will be merged on top. The deep merge has the following characteristics:

Expand All @@ -72,6 +73,7 @@ data_sources:
The generator uses the `read` operation to map to the provider code specification. Multiple schemas will have the [OAS types mapped to Provider Attributes](#oas-types-to-provider-attributes) and then be merged together; with the final result being the [Data Source](https://developer.hashicorp.com/terraform/plugin/code-generation/specification#data-source) `schema`. The schemas that will be merged together (in priority order):
1. `read` operation: [parameters](https://spec.openapis.org/oas/v3.1.0#parameterObject)
- The generator will merge all `query` and `path` parameters to the root of the schema.
- The generator will consider as parameters the ones in the [Path Item Object](https://spec.openapis.org/oas/v3.1.0#path-item-object) and the ones in the [Operation Object](https://spec.openapis.org/oas/v3.1.0#operation-object), merged based on the rules in the specification
2. `read` operation: response body in [responses](https://spec.openapis.org/oas/v3.1.0#responsesObject)
- The response body is the only schema **required** for data sources. If not found, the generator will skip the data source without mapping.
- Will attempt to use `200` or `201` response body. If not found, will grab the first available `2xx` response code with a schema (lexicographic order)
Expand Down
21 changes: 21 additions & 0 deletions internal/cmd/testdata/kubernetes/provider_code_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -9221,6 +9221,27 @@
],
"description": "Most recently observed status of the Deployment."
}
},
{
"name": "name",
"string": {
"computed_optional_required": "computed_optional",
"description": "name of the Deployment"
}
},
{
"name": "namespace",
"string": {
"computed_optional_required": "computed_optional",
"description": "object name and auth scope, such as for teams and projects"
}
},
{
"name": "pretty",
"string": {
"computed_optional_required": "computed_optional",
"description": "If 'true', then the output is pretty printed."
}
}
]
}
Expand Down
28 changes: 14 additions & 14 deletions internal/explorer/config_explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ func (e configExplorer) FindResources() (map[string]Resource, error) {
continue
}

parameters, err := extractParameters(e.spec.Paths, resourceConfig.Read.Path)
commonParameters, err := extractCommonParameters(e.spec.Paths, resourceConfig.Read.Path)
if err != nil {
errResult = errors.Join(errResult, fmt.Errorf("failed to extract '%s.delete': %w", name, err))
errResult = errors.Join(errResult, fmt.Errorf("failed to extract '%s' common parameters: %w", name, err))
continue
}

resources[name] = Resource{
CreateOp: createOp,
ReadOp: readOp,
UpdateOp: updateOp,
DeleteOp: deleteOp,
Parameters: parameters,
SchemaOptions: extractSchemaOptions(resourceConfig.SchemaOptions),
CreateOp: createOp,
ReadOp: readOp,
UpdateOp: updateOp,
DeleteOp: deleteOp,
CommonParameters: commonParameters,
SchemaOptions: extractSchemaOptions(resourceConfig.SchemaOptions),
}
}

Expand All @@ -111,16 +111,16 @@ func (e configExplorer) FindDataSources() (map[string]DataSource, error) {
continue
}

parameters, err := extractParameters(e.spec.Paths, dataSourceConfig.Read.Path)
commonParameters, err := extractCommonParameters(e.spec.Paths, dataSourceConfig.Read.Path)
if err != nil {
errResult = errors.Join(errResult, fmt.Errorf("failed to extract '%s.delete': %w", name, err))
errResult = errors.Join(errResult, fmt.Errorf("failed to extract '%s' common parameters: %w", name, err))
continue
}

dataSources[name] = DataSource{
ReadOp: readOp,
Parameters: parameters,
SchemaOptions: extractSchemaOptions(dataSourceConfig.SchemaOptions),
ReadOp: readOp,
CommonParameters: commonParameters,
SchemaOptions: extractSchemaOptions(dataSourceConfig.SchemaOptions),
}
}
return dataSources, errResult
Expand Down Expand Up @@ -160,7 +160,7 @@ func extractOp(paths *high.Paths, oasLocation *config.OpenApiSpecLocation) (*hig
}
}

func extractParameters(paths *high.Paths, path string) ([]*high.Parameter, error) {
func extractCommonParameters(paths *high.Paths, path string) ([]*high.Parameter, error) {
// No need to search OAS if not defined
if paths.PathItems.GetOrZero(path) == nil {
return nil, fmt.Errorf("path '%s' not found in OpenAPI spec", path)
Expand Down
Loading

0 comments on commit 301689c

Please sign in to comment.