Skip to content

Commit 6dd6fbe

Browse files
authored
website: Migration guide updates for provider.DataSourceType and provider.ResourceType deprecation (#473)
Reference: #472
1 parent 46b5573 commit 6dd6fbe

File tree

11 files changed

+159
-142
lines changed

11 files changed

+159
-142
lines changed

website/docs/plugin/framework/migrating/attributes-blocks/attribute-schema.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ The following examples show how to migrate portions of the
8484
For a complete example, clone the
8585
`terraform-provider-http` repository and compare the `data_source.go` file in
8686
[v2.2.0](https://github.com/hashicorp/terraform-provider-http/blob/v2.2.0/internal/provider/data_source.go)
87-
with the `data_source_http.go` file in
88-
[v3.0.1](https://github.com/hashicorp/terraform-provider-http/blob/v3.0.1/internal/provider/data_source_http.go).
87+
with the `data_source_http.go` file
88+
[after the migration](https://github.com/hashicorp/terraform-provider-http/blob/8527d5b4546b54cdef246a13befc5745dbbbf740/internal/provider/data_source_http.go).
8989
9090
9191
### SDKv2
@@ -113,7 +113,7 @@ The following shows the same section of provider code after the migration.
113113
This code implements the `url` attribute for the `http` data source with the Framework.
114114
115115
```go
116-
func (d *httpDataSourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
116+
func (d *httpDataSource) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
117117
return tfsdk.Schema{
118118
/* ... */
119119
Attributes: map[string]tfsdk.Attribute{

website/docs/plugin/framework/migrating/attributes-blocks/default-values.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ In the Framework, you set default values with the `PlanModifiers` field on your
4141
struct.
4242
4343
```go
44-
func (d *resourceTypeExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
44+
func (r *resourceExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
4545
return tfsdk.Schema{
4646
/* ... */
4747
Attributes: map[string]tfsdk.Attribute{
@@ -65,8 +65,8 @@ provider.
6565
6666
For a complete example, clone the
6767
`terraform-provider-tls` repository and compare the `resource_private_key.go` file in
68-
[v3.4.0](https://github.com/hashicorp/terraform-provider-tls/blob/v3.4.0/internal/provider/resource_private_key.go) with
69-
[v4.0.1](https://github.com/hashicorp/terraform-provider-tls/blob/v4.0.1/internal/provider/resource_private_key.go).
68+
[v3.4.0](https://github.com/hashicorp/terraform-provider-tls/blob/v3.4.0/internal/provider/resource_private_key.go) with the file
69+
[after the migration](https://github.com/hashicorp/terraform-provider-tls/blob/4dafb105818e45a88532f917e7b170ee2a9bb092/internal/provider/resource_private_key.go).
7070
7171
### SDKv2
7272
@@ -91,7 +91,7 @@ The following shows the same section of code after the migration.
9191
This code implements the `PlanModifiers` field for the `rsa_bits` attribute with the Framework.
9292
9393
```go
94-
func (rt *privateKeyResourceType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
94+
func (r *privateKeyResource) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
9595
return tfsdk.Schema{
9696
Attributes: map[string]tfsdk.Attribute{
9797
"rsa_bits": {

website/docs/plugin/framework/migrating/attributes-blocks/fields.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ provider.
5454
For a complete example, clone the
5555
`terraform-provider-http` repository and compare the `data_source.go` file in
5656
[v2.2.0](https://github.com/hashicorp/terraform-provider-http/blob/v2.2.0/internal/provider/data_source.go)
57-
and the `data_source_http.go` file in
58-
[v3.0.1](https://github.com/hashicorp/terraform-provider-http/blob/v3.0.1/internal/provider/data_source_http.go).
57+
and the `data_source_http.go` file
58+
[after the migration](https://github.com/hashicorp/terraform-provider-http/blob/8527d5b4546b54cdef246a13befc5745dbbbf740/internal/provider/data_source_http.go).
5959
6060
### SDKv2
6161
@@ -79,7 +79,7 @@ The following example from the `data_source_http.go` file shows how the `url` at
7979
to be required with the Framework.
8080
8181
```go
82-
func (d *httpDataSourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
82+
func (d *httpDataSource) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
8383
return tfsdk.Schema{
8484
Attributes: map[string]tfsdk.Attribute{
8585
"url": {

website/docs/plugin/framework/migrating/attributes-blocks/force-new.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ In the Framework, you implement the same behavior by using the `resource.Require
3535
attribute's `tfsdk.Attribute` struct.
3636
3737
```go
38-
func (d *resourceTypeExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
38+
func (r *resourceExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
3939
return tfsdk.Schema{
4040
/* ... */
4141
Attributes: map[string]tfsdk.Attribute{
@@ -64,7 +64,7 @@ The following examples show how to migrate portions of the
6464
For a complete example, clone the
6565
`terraform-random-provider` repository and compare the `resource_password.go` file in
6666
[v3.3.2](https://github.com/hashicorp/terraform-provider-random/blob/v3.3.2/internal/provider/resource_password.go)
67-
with [v3.4.1](https://github.com/hashicorp/terraform-provider-random/blob/v3.4.1/internal/provider/resource_password.go).
67+
with the file [after the migration](https://github.com/hashicorp/terraform-provider-random/blob/04292d3e31f71ff16b82512082baed037bb1069c/internal/provider/resource_password.go).
6868
6969
### SDKv2
7070
@@ -94,7 +94,7 @@ This code forces the replacement of a `random_password` resource when the value
9494
The example does this using the `PlanModifiers` field within the `random_password` attribute's schema.
9595
9696
```go
97-
func (r *passwordResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
97+
func (r *passwordResource) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
9898
return tfsdk.Schema{
9999
Attributes: map[string]tfsdk.Attribute{
100100
"keepers": {

website/docs/plugin/framework/migrating/attributes-blocks/types.mdx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func resourceExample() *schema.Resource {
6868
In the Framework, you set your attribute's type with the `Type` field on your attribute's `tfsdk.Attribute` struct.
6969
7070
```go
71-
func (d *resourceTypeExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
71+
func (r *resourceExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
7272
return tfsdk.Schema{
7373
/* ... */
7474
Attributes: map[string]tfsdk.Attribute{
@@ -121,8 +121,8 @@ The following examples show how to migrate portions of the
121121
For a complete example, clone the
122122
`terraform-provider-http` repository and compare the `data_source.go` file in
123123
[v2.2.0](https://github.com/hashicorp/terraform-provider-http/blob/v2.2.0/internal/provider/data_source.go)
124-
and the `data_source_http.go` file in
125-
[v3.0.1](https://github.com/hashicorp/terraform-provider-http/blob/v3.0.1/internal/provider/data_source_http.go).
124+
and the `data_source_http.go` file
125+
[after the migration](https://github.com/hashicorp/terraform-provider-http/blob/8527d5b4546b54cdef246a13befc5745dbbbf740/internal/provider/data_source_http.go).
126126
127127
### SDKv2
128128
@@ -146,7 +146,7 @@ The following example from the `data_source_http.go` file shows how the type of
146146
source is defined with the Framework after the migration.
147147
148148
```go
149-
func (d *httpDataSourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
149+
func (d *httpDataSource) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
150150
return tfsdk.Schema{
151151
Attributes: map[string]tfsdk.Attribute{
152152
"url": {
@@ -162,8 +162,8 @@ The following examples show how to migrate portions of the
162162
For a complete example, clone the
163163
`terraform-provider-tls` repository and compare the `common_cert.go` file in
164164
[v3.4.0](https://github.com/hashicorp/terraform-provider-tls/blob/v3.4.0/internal/provider/common_cert.go)
165-
and the `resource_cert_request.go` file in
166-
[v4.0.1](https://github.com/hashicorp/terraform-provider-tls/blob/v4.0.1/internal/provider/resource_cert_request.go).
165+
and the `resource_cert_request.go` file
166+
[after the migration](https://github.com/hashicorp/terraform-provider-tls/blob/4dafb105818e45a88532f917e7b170ee2a9bb092/internal/provider/resource_cert_request.go).
167167
168168
### SDKv2
169169
@@ -190,7 +190,7 @@ The following example from the `data_source_http.go` file shows how the type of
190190
source is defined with the Framework after the migration.
191191
192192
```go
193-
func (rt *certRequestResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
193+
func (r *certRequestResource) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
194194
return tfsdk.Schema{
195195
Attributes: map[string]tfsdk.Attribute{
196196
"dns_names": {

website/docs/plugin/framework/migrating/attributes-blocks/validators-custom.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ The following examples show how to migrate portions of the
6767
For a complete example, clone the
6868
`terraform-provider-random` repository and compare the `resource_password.go` file in
6969
[v3.3.2](https://github.com/hashicorp/terraform-provider-random/blob/v3.3.2/internal/provider/resource_password.go)
70-
with [v3.4.1](https://github.com/hashicorp/terraform-provider-random/blob/v3.4.1/internal/provider/resource_password.go).
70+
with the file [after the migration](https://github.com/hashicorp/terraform-provider-random/blob/04292d3e31f71ff16b82512082baed037bb1069c/internal/provider/resource_password.go).
7171
7272
### SDKv2
7373
@@ -94,7 +94,7 @@ This code validates that the `random_password`'s `length` attribute is greater t
9494
validator.
9595
9696
```go
97-
func (r *passwordResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
97+
func (r *passwordResource) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
9898
return tfsdk.Schema{
9999
Attributes: map[string]tfsdk.Attribute{
100100
"length": {

website/docs/plugin/framework/migrating/data-sources/index.mdx

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,48 +45,49 @@ schema.Resource {
4545
4646
## Framework
4747
48-
In the Framework, you define data sources by adding them to the map returned by your provider's `GetDataSources` function.
48+
In the Framework, you define data sources by adding them to the map returned by your provider's `DataSources` method.
4949
50-
The `GetDataSources` function on your `provider.Provider` returns a map from the data source name (string) to a type
51-
that implements the `DataSourceType` interface for each data source your provider supports.
50+
The `DataSources` method on your `provider.Provider` returns a slice of functions that return types
51+
that implement the `datasource.DataSource` interface for each data source your provider supports.
5252
5353
The following code shows how you add a data source to your provider with the Framework.
5454
5555
```go
56-
func (p *provider) GetDataSources(ctx context.Context) (map[string]provider.DataSourceType, diag.Diagnostics) {
57-
return map[string]provider.DataSourceType{
56+
func (p *provider) DataSources(ctx context.Context) []func() datasource.DataSource {
57+
return []func() datasource.DataSource{
5858
/* ... */
59-
}, nil
59+
}
6060
}
6161
```
6262
63-
Like the `provider.ResourceType` interface, `provider.DataSourceType` requires `GetSchema` and `NewResource` functions.
64-
These functions work the same way for data sources as they do for resources.
63+
Like the `resource.Resource` interface, `datasource.DataSource` requires `GetSchema` and `Metadata` methods.
64+
These methods work the same way for data sources as they do for resources. The `Read` method is also required.
6565
66-
The `GetSchema` function returns a `tfsdk.Schema` struct which defines your data source's attributes. This is the same
66+
The `GetSchema` method returns a `tfsdk.Schema` struct which defines your data source's attributes. This is the same
6767
struct you use to define provider and resource attributes.
6868
69-
The `NewResource` function returns a type that you define. The type implements the `resource.Resource` interface,
70-
including the CRUD functions for your resource.
69+
The `Metadata` method returns a type name that you define.
70+
71+
The `Read` method implements the logic for writing into the Terraform state.
7172
72-
The following code shows how you define a `provider.DataSourceType` which implements these two functions with the
73+
The following code shows how you define a `datasource.DataSource` which implements these methods with the
7374
Framework.
7475
7576
```go
76-
type dataSourceTypeExample struct{}
77+
type dataSourceExample struct{}
7778

78-
func (r *dataSourceTypeExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
79+
func (d *dataSourceExample) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
7980
/* ... */
8081
}
8182

82-
func (r *dataSourceTypeExample) NewDataSource(ctx context.Context, p provider.Provider) (datasource.DataSource, diag.Diagnostics) {
83+
func (d *dataSourceExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
8384
/* ... */
8485
}
85-
```
8686

87-
Unlike resources, you only need to implement a read function for your data sources. Refer to the
88-
[Resources - CRUD functions](/plugin/framework/migrating/resources/crud) page in this guide to learn how to define this
89-
function on your `datasource.DataSource` type.
87+
func (d *dataSourceExample) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
88+
/* ... */
89+
}
90+
```
9091
9192
## Migration Notes
9293
@@ -103,8 +104,8 @@ provider.
103104
For a complete example, clone the
104105
`terraform-provider-http` repository and compare the `data_source.go` file in
105106
[v2.2.0](https://github.com/hashicorp/terraform-provider-http/blob/v2.2.0/internal/provider/data_source.go)
106-
and the `data_source_http.go` file in
107-
[v3.0.1](https://github.com/hashicorp/terraform-provider-http/blob/v3.0.1/internal/provider/data_source_http.go).
107+
and the `data_source_http.go` file
108+
[after the migration](https://github.com/hashicorp/terraform-provider-http/blob/8527d5b4546b54cdef246a13befc5745dbbbf740/internal/provider/data_source_http.go).
108109
109110
### SDKv2
110111
@@ -145,18 +146,27 @@ The following example from the `provider.go` file shows how the `http` data sour
145146
the migration.
146147
147148
```go
148-
func (p *provider) GetDataSources(context.Context) (map[string]provider.DataSourceType, diag.Diagnostics) {
149-
return map[string]provider.DataSourceType{
150-
"http": &httpDataSourceType{},
151-
}, nil
149+
func (p *provider) DataSources(context.Context) []func() datasource.DataSource {
150+
return []func() datasource.DataSource{
151+
func() datasource.DataSource {
152+
return &httpDataSource{}
153+
},
154+
}
152155
}
153156
```
154157
155-
This code from the `data_source_http.go` file defines the `GetSchema` function for the `http` data source with the
158+
This code from the `data_source_http.go` file defines the methods for the `http` data source with the
156159
Framework.
157160
158161
```go
159-
func (d *httpDataSourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
162+
func (d *httpDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
163+
// This is unconventional in that the data source name matches the provider name.
164+
// Typically these should have the provider name, an underscore, then the type name.
165+
// e.g. http_request
166+
resp.TypeName = "http"
167+
}
168+
169+
func (d *httpDataSource) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
160170
return tfsdk.Schema{
161171
Attributes: map[string]tfsdk.Attribute{
162172
"url": {
@@ -166,14 +176,7 @@ func (d *httpDataSourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diag
166176
},
167177
/* ... */
168178

169-
func (d *httpDataSourceType) NewDataSource(context.Context, provider.Provider) (datasource.DataSource, diag.Diagnostics) {
170-
return &httpDataSource{}, nil
171-
}
172-
```
173-
174-
This code from the `data_source_http.go` file defines the `Read` function for the `http` data source with the Framework.
175-
176-
```go
177179
func (d *httpDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
178180
/* ... */
181+
}
179182
```

0 commit comments

Comments
 (0)