|
| 1 | +--- |
| 2 | +page_title: List Resources Overview |
| 3 | +description: >- |
| 4 | + List resources allows providers to extend existing resources with list capabilities to |
| 5 | + enable searching for a specific resource type within a given scope. Learn how |
| 6 | + to implement list in the Terraform plugin framework. |
| 7 | +--- |
| 8 | + |
| 9 | +# List |
| 10 | + |
| 11 | +<Highlight> |
| 12 | + |
| 13 | +List support is in technical preview and offered without compatibility promises until Terraform 1.14 is generally available. |
| 14 | + |
| 15 | +</Highlight> |
| 16 | + |
| 17 | +[List resources](/terraform/language/v1.14.x/resources/list) are an abstraction that allows Terraform to search for a specific resource type within a given scope, for example listing all EC2 instances within an AWS account or all virtual networks within a resource group. Taking a list configuration as input, remote objects are retrieved and returned to Terraform as list result data which is displayed to the user. |
| 18 | + |
| 19 | +Searching is invoked directly with the Terraform CLI via the `terraform query` command with list blocks authored in a `.tfquery.hcl` file. |
| 20 | + |
| 21 | +When implementing list there needs to be a corresponding resource implementation since the results that are returned rely on the pre-existing definitions of the resource's identity and schema. |
| 22 | + |
| 23 | +This page describes the basic implementation details required for supporting a list resource within the provider. List resources must implement: |
| 24 | + |
| 25 | +- [List](/terraform/plugin/framework/list-resources/list) a resource by receiving Terraform configuration, retrieving remote objects, and returning list result data to Terraform. |
| 26 | + |
| 27 | +Further documentation is available for deeper list resource concepts: |
| 28 | + |
| 29 | +- [Configure](/terraform/plugin/framework/list-resources/configure) a list resource with provider-level data types or clients. |
| 30 | +- [Validate](/terraform/plugin/framework/list-resources/validate-configuration) practitioner configuration against acceptable values. |
| 31 | + |
| 32 | +For details on how to implement a list resource with a non-framework resource see the [guide for non-framework resources](/terraform/plugin/framework/list-resources/non-framework-resource) |
| 33 | + |
| 34 | +See the [Guide for non-Framework Resources](/terraform/plugin/framework/list-resources/non-framework-resource) for more details. |
| 35 | + |
| 36 | +## Define List Resource Type |
| 37 | + |
| 38 | +Implement the [`listresource.ListResource` interface](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/listresource#ListResource). Ensure the [Add List Resource To Provider](#add-list-resource-to-provider) documentation is followed so the list resource becomes part of the provider implementation, and therefore available to practitioners. |
| 39 | + |
| 40 | +## Metadata method |
| 41 | + |
| 42 | +The [`list.ListResource` interface `Metadata` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/list#ListResource.Metadata) defines the name of the resource type that a list resource is associated with. |
| 43 | + |
| 44 | +This name should include the provider type prefix, an underscore, then the resource specific name. For example, a provider named `examplecloud` and a resource that reads "thing" resources would be named `examplecloud_thing`. |
| 45 | + |
| 46 | +Since this method shares the same signature as the [`resource.Resource` interface `Metadata` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/resource#Resource.Metadata), if the list resource extends a framework resource then the Metadata does not need to be defined again since it will already exist for the resource. |
| 47 | + |
| 48 | +In cases where the list resource extends a non-framework resource, the Metadata must be defined. See the [RawV5Schemas and RawV6Schemas Method](#rawv5schemas-and-rawv6schemas-method) section for more details on using a list resource with a non-framework resource. |
| 49 | + |
| 50 | +In this example, the provider defines the `examplecloud` name for itself, and the resource is named `examplecloud_thing`: |
| 51 | + |
| 52 | +```go |
| 53 | +// With the provider.Provider implementation |
| 54 | +func (p *ExampleCloudProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) { |
| 55 | + resp.TypeName = "examplecloud" |
| 56 | +} |
| 57 | + |
| 58 | +// With the list.ListResource implementation |
| 59 | +func (r *ThingListResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 60 | + resp.TypeName = req.ProviderTypeName + "_thing" |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +Alternatively the entire name can be hardcoded: |
| 65 | +```go |
| 66 | +func (r *ThingListResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 67 | + resp.TypeName = "examplecloud_thing" |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## Schema method |
| 72 | + |
| 73 | +The [`list.ListResource` interface `ListResourceConfigSchema` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/list#ListResource.ListResourceConfigSchema) defines a [schema](/terraform/plugin/framework/handling-data/schemas) describing what data is available in the list resource configuration. |
| 74 | + |
| 75 | +## Add List Resource to Provider |
| 76 | + |
| 77 | +List resources become available to practitioners when they are included in the [provider](/terraform/plugin/framework/providers) implementation via the optional [`provider.ProviderWithListResources` interface `ListResources` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/provider#ProviderWithListResources.ListResources). |
| 78 | + |
| 79 | +In this example, the `ThingListResource` type, which implements the `list.ListResource` interface, is added to the provider implementation: |
| 80 | + |
| 81 | +```go |
| 82 | +var _ provider.ProviderWithListResources = (*ExampleCloudProvider)(nil) |
| 83 | + |
| 84 | +func (p *ExampleCloudProvider) ListResources(_ context.Context) []func() list.ListResource { |
| 85 | + return []func() list.ListResource{ |
| 86 | + NewThingListResource, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func NewThingListResource() list.ListResource { |
| 91 | + return &ThingListResource{} |
| 92 | +} |
| 93 | +``` |
0 commit comments