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.
- Create a new datasource declaration file and a corresponding test file
- Add Schema and Read operation implementation
- If there is
labelsfield with typeKeyValueLabelsin the corresponding resource, in the datasource Read operation implementation, after the resource read method, call the functiontpgresource.SetDataSourceLabels(d)to makelabelsandterraform_labelshave all of the labels on the resource. - If there is
annotationsfield with typeKeyValueAnnotationsin the corresponding resource, in the datasource Read operation implementation, after the resource read method, call the functiontpgresource.SetDataSourceAnnotations(d)to makeannotationshave all of the annotations on the resource.
- If there is
- Register the datasource to
handwrittenDatasourcesinmagic-modules/mmv1/third_party/terraform/provider/provider_mmv1_resources.go.tmpl - Implement a test which will create and resources and read the corresponding datasource
- 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#
- 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, forgoogle_compute_instance, the file is calledcompute_instance.html.markdown
- The name of the file is the name of the data source without a
- Modify the documentation as needed according to Handwritten documentation style guide.
- Generate the providers
- Copy and paste the generated documentation into the Hashicorp Registry’s Doc Preview Tool to see how it is rendered.