Add a datasource

Add a datasource #

Note: only handwritten datasources are currently supported

Datasources are like terraform resources except they don’t create anything. They are simply read-only operations that will expose some sort of values needed for subsequent resource operations. If you’re adding a field to an existing datasource, check the Resource section. Everything there will be mostly consistent with the type of change you’ll need to make. For adding a new datasource there are 5 steps to doing so.

  1. Create a new datasource declaration file and a corresponding test file
  2. Add Schema and Read operation implementation
    • If there is labels field with type KeyValueLabels in the corresponding resource, in the datasource Read operation implementation, after the resource read method, call the function tpgresource.SetDataSourceLabels(d) to make labels and terraform_labels have all of the labels on the resource.
    • If there is annotations field with type KeyValueAnnotations in the corresponding resource, in the datasource Read operation implementation, after the resource read method, call the function tpgresource.SetDataSourceAnnotations(d) to make annotations have all of the annotations on the resource.
  3. Register the datasource to handwrittenDatasources in magic-modules/mmv1/third_party/terraform/provider/provider_mmv1_resources.go.erb
  4. Implement a test which will create and resources and read the corresponding datasource
  5. Add documentation

For creating a datasource based off an existing resource you can make use of the schema directly. Otherwise implementing the schema directly, similar to normal resource creation, is the desired path.

Resourceless Datasources #

Datasources not backed by a resource are possible to add as well. They follow the same general steps as adding a resource-based datasource, except that a full Read method will need to be defined for them rather than calling a resource’s Read method.

Note that while resource-based datasources can depend on the resource read method for API calls, resourceless datasources need to make them themselves. An HTTP-based client that’s properly configured with logging and retries must be used, such as a client from the https://github.com/googleapis/google-api-go-client library, or the raw HTTP client used in MMV1 through SendRequest.

Add documentation #

  1. Open the data source documentation in magic-modules/third_party/terraform/website/docs/d/ using an editor of your choice.
    • The name of the file is the name of the data source without a google_ prefix. For example, for google_compute_instance, the file is called compute_instance.html.markdown
  2. Modify the documentation as needed according to Handwritten documentation style guide.
  3. Generate the providers
  4. Copy and paste the generated documentation into the Hashicorp Registry’s Doc Preview Tool to see how it is rendered.