Skip to content

Commit 5f4de8d

Browse files
committed
Address review changes
1 parent e7c4266 commit 5f4de8d

File tree

8 files changed

+731
-387
lines changed

8 files changed

+731
-387
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: ENHANCEMENTS
2+
body: Added data source and resource support for query and path parameters specified
3+
in the [OAS Path Item](https://spec.openapis.org/oas/v3.1.0#path-item-object)
4+
time: 2024-01-16T10:27:58.255575839+01:00
5+
custom:
6+
Issue: "114"

DESIGN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ In these OAS operations, the generator will search the `create` and `read` for s
5050
- Will attempt to use `application/json` content-type first. If not found, will grab the first available content-type with a schema (alphabetical order)
5151
4. `read` operation: [parameters](https://spec.openapis.org/oas/v3.1.0#parameterObject)
5252
- The generator will merge all `query` and `path` parameters to the root of the schema.
53+
- 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
5354

5455
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:
5556

@@ -72,6 +73,7 @@ data_sources:
7273
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):
7374
1. `read` operation: [parameters](https://spec.openapis.org/oas/v3.1.0#parameterObject)
7475
- The generator will merge all `query` and `path` parameters to the root of the schema.
76+
- 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
7577
2. `read` operation: response body in [responses](https://spec.openapis.org/oas/v3.1.0#responsesObject)
7678
- The response body is the only schema **required** for data sources. If not found, the generator will skip the data source without mapping.
7779
- 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)

internal/cmd/testdata/kubernetes/provider_code_spec.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9221,6 +9221,27 @@
92219221
],
92229222
"description": "Most recently observed status of the Deployment."
92239223
}
9224+
},
9225+
{
9226+
"name": "name",
9227+
"string": {
9228+
"computed_optional_required": "computed_optional",
9229+
"description": "name of the Deployment"
9230+
}
9231+
},
9232+
{
9233+
"name": "namespace",
9234+
"string": {
9235+
"computed_optional_required": "computed_optional",
9236+
"description": "object name and auth scope, such as for teams and projects"
9237+
}
9238+
},
9239+
{
9240+
"name": "pretty",
9241+
"string": {
9242+
"computed_optional_required": "computed_optional",
9243+
"description": "If 'true', then the output is pretty printed."
9244+
}
92249245
}
92259246
]
92269247
}

internal/explorer/config_explorer.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,19 @@ func (e configExplorer) FindResources() (map[string]Resource, error) {
8181
continue
8282
}
8383

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

9090
resources[name] = Resource{
91-
CreateOp: createOp,
92-
ReadOp: readOp,
93-
UpdateOp: updateOp,
94-
DeleteOp: deleteOp,
95-
Parameters: parameters,
96-
SchemaOptions: extractSchemaOptions(resourceConfig.SchemaOptions),
91+
CreateOp: createOp,
92+
ReadOp: readOp,
93+
UpdateOp: updateOp,
94+
DeleteOp: deleteOp,
95+
CommonParameters: commonParameters,
96+
SchemaOptions: extractSchemaOptions(resourceConfig.SchemaOptions),
9797
}
9898
}
9999

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

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

120120
dataSources[name] = DataSource{
121-
ReadOp: readOp,
122-
Parameters: parameters,
123-
SchemaOptions: extractSchemaOptions(dataSourceConfig.SchemaOptions),
121+
ReadOp: readOp,
122+
CommonParameters: commonParameters,
123+
SchemaOptions: extractSchemaOptions(dataSourceConfig.SchemaOptions),
124124
}
125125
}
126126
return dataSources, errResult
@@ -160,7 +160,7 @@ func extractOp(paths *high.Paths, oasLocation *config.OpenApiSpecLocation) (*hig
160160
}
161161
}
162162

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

0 commit comments

Comments
 (0)