BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News HashiCorp Adds Automated Generation of Terraform Provider Code

HashiCorp Adds Automated Generation of Terraform Provider Code

This item in japanese

HashiCorp has released a technical preview of their Terraform provider code generation toolset. This includes a new tool that generates Terraform provider code from an OpenAPI specification. The release also includes a tool that can generate Terraform plugin framework code from a provider code specification.

Terraform providers are executable binaries, written in Go, that provide an interface between the Terraform core and other services over an RPC interface. As noted by Austin Valle, senior software engineer at HashiCorp, creating providers can be a repetitive task that leads to inconsistencies in the code layer. Valle describes the code generation solution as a "scalable code generation ecosystem based on a shared specification that can be extended by the existing provider developer community."

Terraform code generator workflow

Terraform code generator workflow (credit: HashiCorp)

For workflows that interact with an OpenAPI documented specification, the OpenAPI provider spec generator can be used to generate the provider code specification. Note that this tool requires the OpenAPI specification to be at either version 3.1.x or 3.0.x. The generator takes the OAS file and a generator config file as input. Once run, it outputs a provider code specification which can be used by the framework code generator to create Terraform provider code.

The generator config is a YAML file used to map the API operations and components to the appropriate Terraform resource, data source, or provider. For example, generating a data source specification is done using a map data_sources:

data_sources:
  thing:
    read:
      path: /thing/{id}
      method: GET

The provider code specification is a versioned interface implemented as a JSON schema with associated Go bindings. The interface is hierarchical and details the structures from their top-level form, such as data sources, through to the details of the individual types (e.g., required, sensitive).

The provider code specification can be used with the new Framework Code Generator to generate Terraform Provider code. This CLI tool presents two commands: generate and scaffold. The generate command takes the provider code specification as input and creates Terraform plugin framework code. This is generated as Go files with a suffix of _gen.go and are not meant for further edits.

The scaffold command is used to create Terraform plugin framework code, in Go, from pre-defined templates. These are meant to be a starting point to simplify the plugin framework generation process.

At release, the generator supports outputting schema and data model types. With schema, all framework functionality is included with no limits on nesting. If provided within the specification, data model types will be converted to external Go types. The GitHub repo notes there are plans to extend the generator to support CRUD logic.

As noted within the design documentation, code generation is meant to be vendor and tool-agnostic and extensible. It notes that the OpenAPI implementation is meant to be the first implementation with other potential sources for generation including IDL, developer-generated scripts, or API SDK information. At the time of release, there are no provided timeframes for additional source support.

The OpenAPI Provider Spec Generator, Terraform Plugin Framework Code Generator, and associated schema are all available on GitHub under the MPL-2.0 license. All code generator components are currently in a tech preview. Questions and feedback are being addressed in the Plugin Development Community Forum.

About the Author

Rate this Article

Adoption
Style

BT