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
Copy file name to clipboardExpand all lines: website/docs/plugin/framework/getting-started/example-provider.mdx
+77-109
Original file line number
Diff line number
Diff line change
@@ -6,14 +6,39 @@ description: >-
6
6
7
7
# Simple Example Provider
8
8
9
+
[Terraform providers](/terraform/language/providers) allow Terraform core to communicate with 3rd parties such as cloud providers, SaaS providers, and other APIs.
10
+
Communication between Terraform core and Terraform providers uses gRPC, with core operating as a gRPC client and providers operating as gRPC servers, respectively.
11
+
Terraform provider resources are used to manage infrastructure objects, such as virtual networks and compute instances.
12
+
Terraform provider data sources give a read-only view of infrastructure objects.
13
+
14
+
The purpose of this example is to show the relationship between the components that are required to build a minimal Terraform provider.
15
+
In a real provider the resource and data source would typically interact with a cloud provider via an API but in this example we are simply storing values in state.
16
+
Stripping the Terraform provider back to the bare essentials allows us to focus on the minimum requirements needed to build a functional provider.
17
+
9
18
A Terraform plugin provider requires the following as a minimum:
10
19
11
-
*main
20
+
*provider server
12
21
* provider
13
22
* resource (and/or data source)
14
23
* data source (and/or resource)
15
24
16
-
## Main
25
+
A provider server is the gRPC server that the Terraform core gRPC client communicates with.
26
+
The provider wraps the resource(s) and/or data source(s), and can be used to configure a client which communicates with a 3rd party service via an API.
27
+
Resources are used to manage infrastructure objects.
28
+
Data sources are used to read infrastructure objects.
29
+
30
+
## Provider Server
31
+
32
+
Each provider must implement a gRPC server that supports Terraform-specific connection and handshake handling on startup.
33
+
34
+
A [provider server](/plugin/framework/provider-servers) is required in order for a Terraform provider to:
35
+
* expose resources that can be managed by Terraform core.
36
+
* expose data sources that can be read by Terraform core.
37
+
38
+
The `main()` function is used for defining a provider server.
39
+
40
+
The `provider.New()` returns a function which returns a type that satisfies the `provider.Provider` interface.
41
+
The `provider.Provider` interface defines functions for obtaining the resource(s) and/or data source(s) from a provider.
17
42
18
43
```go
19
44
package main
@@ -47,13 +72,24 @@ func main() {
47
72
}
48
73
```
49
74
50
-
The `main()` function defines the [provider server](/plugin/framework/provider-servers).
75
+
Refer to [Provider Servers](/plugin/framework/provider-servers) for more details.
76
+
77
+
## Provider
51
78
52
-
###provider.New()
79
+
The provider wraps resources and data sources which are typically used for interacting with cloud providers, SaaS providers, or other APIs.
53
80
54
-
`provider.New()` returns a function which returns a type that satisfies the `provider.Provider` interface.
81
+
In this example the provider wraps a resource and a data source which simply interact with Terraform state. Refer to the [tutorial](/tutorials/providers-plugin-framework/providers-plugin-framework-provider-configure) for an example of provider configuration that configures an API client.
55
82
56
-
## Provider
83
+
`New()` returns a function which returns a type that satisfies the `provider.Provider` interface. The `New()` function is called by the [provider server](#provider-server) to obtain the provider.
84
+
85
+
The `exampleProvider` struct implements the `provider.Provider` interface. This interface defines the following functions:
86
+
87
+
-`Schema`: This function returns a provider `schema.Schema` struct that defines the provider schema. Schemas specify the constraints of Terraform configuration blocks. They define what fields a provider, resource, or data source configuration block has, and give Terraform metadata about those fields.
88
+
-[`Configure`]((/plugin/framework/providers#configure-method)): This function lets you configure provider-level data or clients. These configuration values may be from the practitioner Terraform configuration, environment variables, or other means such as reading vendor-specific configuration files.
89
+
-[`Resources`]((/plugin/framework/providers#resources)): This function returns a slice of functions that return types that implement the `resource.Resource` interface. Resources let Terraform manage infrastructure objects, such as a compute instance, an access policy, or disk.
90
+
-[`DataSources`]((/plugin/framework/providers#datasources)): This function returns a slice of functions that return types which implement the `datasource.DataSource` interface. Data sources let Terraform reference external data. For example a database instance.
91
+
92
+
The `exampleProvider` struct also implements the `provider.ProviderWithMetadata` interface which defines the `Metadata` function. The `Metadata` function returns metadata for the provider such as a `TypeName` and `Version`. The `TypeName` is used as a prefix within a provider by for naming [resources](#resource) and [data sources](#data-source).
Refer to [Providers](/plugin/framework/providers) for more details and configuration examples.
126
142
127
-
`Schema(context.Context, provider.SchemaRequest, *provider.SchemaResponse)` returns a provider `schema.Schema` struct which defines the provider schema.
128
-
129
-
#### Configure
130
-
131
-
`Configure(context.Context, ConfigureRequest, *ConfigureResponse)` configures the provider.
132
-
133
-
#### DataSources
134
-
135
-
`DataSources(context.Context)` returns a slice of functions that return types which implement the `datasource.DataSource` interface.
136
-
137
-
#### Resources
138
-
139
-
`Resources(context.Context)` returns a slice of functions that return types which implement the `resource.Resource` interface.
143
+
## Resource
140
144
141
-
### provider.ProviderWithMetadata Interface
145
+
A resource is typically used to manage infrastructure objects such as virtual networks and compute instances.
142
146
143
-
The `exampleProvider` struct also implements the `provider.ProviderWithMetadata` interface which defines the following function:
147
+
In this example the resource simply interacts with Terraform state.
`NewResource()` returns a function which returns a type that satisfies the `resource.Resource` interface. The `NewResource()` function is used within the `provider.Resources` function to make the resource available to the provider.
146
150
147
-
#### Metadata
151
+
The `exampleResource` struct implements the `resource.Resource` interface. This interface defines the following functions:
148
152
149
-
`Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse)` returns metadata for the provider such as a `TypeName` and `Version`.
150
-
151
-
## Resource
153
+
-[`Metadata`](/plugin/framework/resources#metadata-method): This function returns the full name (`TypeName`) of the resource. The full name is used in [Terraform configuration](#resource-configuration) as `resource <full name> <alias>`.
154
+
-`Schema`: This function returns a resource `schema.Schema` struct that defines the resource schema. The schema specifies the constraints of the resource Terraform configuration block. It defines what fields a resource configuration block has, and gives Terraform metadata about those fields. For instance, defining whether a field is required.
155
+
-[`Create`](/plugin/framework/resources/create): This function lets the provider create a new resource of this type.
156
+
-[`Read`](/plugin/framework/resources/read): This function lets the provider read resource values in order to update state.
157
+
-[`Update`](/plugin/framework/resources/update): This function lets the provider update the resource and state.
158
+
-[`Delete`](/plugin/framework/resources/delete): This function lets the provider delete the resource.
`Metadata(context.Context, MetadataRequest, *MetadataResponse)` returns the full name (`TypeName`) of the resource.
279
-
280
-
#### Schema
281
-
282
-
`GetSchema(context.Context, resource.SchemaRequest, *resource.SchemaResponse)` returns a resource `schema.Schema` struct which defines the resource schema.
283
-
284
-
#### Create
274
+
Refer to [Resources](/plugin/framework/resources) for more details and configuration examples.
285
275
286
-
`Create(context.Context, CreateRequest, *CreateResponse)` is called when the provider must create a new resource.
287
-
288
-
#### Read
289
-
290
-
`Read(context.Context, ReadRequest, *ReadResponse)` is called when the provider must read resource values in order to update state.
276
+
## Data Source
291
277
292
-
### Update
278
+
A data source is typically used to provide a read-only view of infrastructure objects.
293
279
294
-
`Update(context.Context, UpdateRequest, *UpdateResponse)` is called to update the state of the resource.
280
+
In this example the data source simply interacts with Terraform state.
295
281
296
-
#### Delete
282
+
`NewDataSource()` returns a function which returns a type that satisfies the `datasource.DataSource` interface. The `NewDataSource()` function is used within the `provider.DataSources` function to make the data source available to the provider.
297
283
298
-
`Delete(context.Context, DeleteRequest, *DeleteResponse)` is called when the provider must delete the resource.
284
+
The `exampleDataSource` struct implements the `datasource.DataSource` interface. This interface defines the following functions:
299
285
300
-
## Data Source
286
+
-[`Metadata`](/plugin/framework/data-sources#metadata-method): This function returns the full name (`TypeName`) of the data source. The full name is used in [Terraform configuration](#data-source-configuration) as `data <full name> <alias>`.
287
+
-`Schema`: This function returns a data source `schema.Schema` struct that defines the data source schema. The schema specifies the constraints of the resource Terraform configuration block. It defines what fields a resource configuration block has, and gives Terraform metadata about those fields. For instance, defining whether a field is optional.
288
+
-[`Read`]((/plugin/framework/data-sources#read-method)): This function lets the provider read data source values in order to update state.
`Metadata(context.Context, MetadataRequest, *MetadataResponse)` returns the full name (`TypeName`) of the data source.
382
-
383
-
#### Schema
384
-
385
-
`Schema(context.Context, datasource.SchemaRequest, *datasource.SchemaResponse)` returns a data source `schema.Schema` struct which defines the data source schema.
386
-
387
-
#### Read
388
-
389
-
`Read(context.Context, ReadRequest, *ReadResponse)` is called when the provider must read data source values in order to update state.
357
+
Refer to [Data Sources](/plugin/framework/data-sources) for more details and configuration examples.
390
358
391
359
## Terraform Configuration
392
360
393
-
Once a plugin provider has been defined, it is executed by specifying configuration and running`terraform apply`.
361
+
With the definitions we have for [provider server](#provider-server), [provider](#provider), [resource](#resource) and [data source](#data-source), we can run the provider by specifying configuration and executing`terraform apply`.
The `configurable_attribute` is defined within the [schema](#resource) as a string type attribute.
404
372
405
-
Examples of the various types of attributes and their representation within Terraform configuration and schema definitions is detailed in Core Concepts. (link needs adding)
373
+
Examples of the various types of attributes and their representation within Terraform configuration and schema definitions is detailed in [Core Concepts](/plugin/framework/getting-started/core-concepts).
406
374
407
375
### Data Source Configuration
408
376
@@ -414,4 +382,4 @@ data "example_datasource" "example" {
414
382
415
383
The `configurable_attribute` is defined within the [schema](#data-source) as a string type attribute.
416
384
417
-
Examples of the various types of attributes and their representation within Terraform configuration and schema definitions is detailed in Core Concepts. (link needs adding)
385
+
Examples of the various types of attributes and their representation within Terraform configuration and schema definitions is detailed in [Core Concepts](/plugin/framework/getting-started/core-concepts).
0 commit comments