You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
chore: Defines new schema scaffolding command using terraform code generation tools (#1827)
* chore: New schema scaffolding command using terraform code generation tools
* shell linting fixes
* addressing reviews related to contributing file
* addressing reviews: remove redundant comments and improvements in script
* adding validation when input name is not provided
* shell linting fix
* add new line at end of file
Co-authored-by: Andrea Angiolillo <[email protected]>
* refactor: moving schema-scaffold script over to scripts folder
* rename input param of scaffold command from name to resource_name for alignment with docs command
* include example for running command in makefile
---------
Co-authored-by: Andrea Angiolillo <[email protected]>
-[Code and Test Best Practices](#code-and-test-best-practices)
14
14
-[Creating New Resource and Data Sources](#creating-new-resources-and-data-sources)
15
+
-[Scaffolding Initial Code and File Structure](#scaffolding-initial-code-and-file-structure)
16
+
-[Scaffolding Schema and Model Definitions](#scaffolding-schema-and-model-definitions)
15
17
-[Documentation Best Practices](#documentation-best-practices)
16
18
-[Discovering New API features](#discovering-new-api-features)
17
19
@@ -309,17 +311,51 @@ To do this you can:
309
311
310
312
### Creating New Resource and Data Sources
311
313
312
-
A scaffolding command was defined with the intention of speeding up development process, while also preserving common conventions throughout our codebase.
314
+
A set of commands have been defined with the intention of speeding up development process, while also preserving common conventions throughout our codebase.
315
+
316
+
#### Scaffolding Initial Code and File Structure
313
317
314
318
This command can be used the following way:
315
319
```bash
316
-
make scaffold name=streamInstance type=resource
320
+
make scaffold resource_name=streamInstance type=resource
317
321
```
318
-
-**name**: The name of the resource, which must be defined in camel case.
322
+
-**resource_name**: The name of the resource, which must be defined in camel case.
319
323
-**type**: Describes the type of resource being created. There are 3 different types: `resource`, `data-source`, `plural-data-source`.
320
324
321
325
This will generate resource/data source files and accompanying test files needed for starting the development, and will contain multiple comments with `TODO:` statements which give guidance for the development.
322
326
327
+
#### Scaffolding Schema and Model Definitions
328
+
329
+
Complementary to the `scaffold` command, there is a command which generates the initial Terraform schema definition and associated Go types for a resource or data source. This processes leverages [Code Generation Tools](https://developer.hashicorp.com/terraform/plugin/code-generation) developed by HashiCorp, which in turn make use of the [Atlas Admin API](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/) OpenAPI Specification.
330
+
331
+
##### Running the command
332
+
333
+
Both `tfplugingen-openapi` and `tfplugingen-framework` must be installed. This can be done by running `make tools`.
334
+
335
+
The command takes a single argument which specifies the resource or data source where the code generation is run, defined in camel case, e.g.:
336
+
```bash
337
+
make scaffold-schemas resource_name=streamInstance
338
+
```
339
+
340
+
As a pre-requiste, the relevant resource/data source directory must define a configuration file in the path `./internal/service/<resource_name>/tfplugingen/generator_config.yml`. The content of this file will define which resource and/or data source schemas will be generated by providing the API endpoints they are mapped to. See the [Generator Config](https://developer.hashicorp.com/terraform/plugin/code-generation/openapi-generator#generator-config) documentation for more information on configuration options. An example defined in our repository can be found in [searchdeployment/tfplugingen/generator_config.yml](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/master/internal/service/searchdeployment/tfplugingen/generator_config.yml).
341
+
342
+
As a result of the execution, the schema definitions and associated model types will be defined in separate files depending on the resources and data sources that were configured in the generator_config.yml file:
343
+
-`data_source_<resource_name>_schema.go`
344
+
-`resource_<resource_name>_schema.go`
345
+
346
+
Note: if the resulting file paths already exist the content will be stored in files with a `_gen.go` postfix, and in this case any content will be overwritten. This can be useful for comparing the latest autogenerated schema against the existing implementation.
347
+
348
+
##### Considerations over generated schema and types
349
+
350
+
- Generated Go type should include a TF prefix to follow the convention in our codebase, this will not be present in generated code.
351
+
- Some attribute names may need to be adjusted if there is a difference in how they are named in Terraform vs the API. An examples of this is `group_id` → `project_id`.
352
+
- Inferred characteristics of an attribute (computed, optional, required) may not always be an accurate representation and should be revised. Details of inference logic can be found in [OAS Types to Provider Attributes](https://github.com/hashicorp/terraform-plugin-codegen-openapi/blob/main/DESIGN.md#oas-types-to-provider-attributes).
353
+
- Missing [sensitive](https://developer.hashicorp.com/terraform/plugin/framework/handling-data/attributes/string#sensitive) field in attributes.
354
+
- Missing plan modifiers such as `RequiresReplace()` in attributes.
355
+
- Terraform specific attributes such as [timeouts](https://developer.hashicorp.com/terraform/plugin/framework/resources/timeouts#specifying-timeouts-in-configuration) need to be included manually.
356
+
- If nested attributes are defined a set of helper functions are generated for using the model. The usage of the generated functions can be considered optional as the current documentation is not very clear on the usage (More details in [terraform-plugin-codegen-framework/issues/80](https://github.com/hashicorp/terraform-plugin-codegen-framework/issues/80)).
357
+
358
+
323
359
## Documentation Best Practices
324
360
325
361
- In our documentation, when a resource field allows a maximum of only one item, we do not format that field as an array. Instead, we create a subsection specifically for this field. Within this new subsection, we enumerate all the attributes of the field. Let's illustrate this with an example: [cloud_backup_schedule.html.markdown](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/master/website/docs/r/cloud_backup_schedule.html.markdown?plain=1#L207)
0 commit comments