Skip to content

Commit

Permalink
website: Migration guide updates for provider.DataSourceType and prov…
Browse files Browse the repository at this point in the history
…ider.ResourceType deprecation (hashicorp#473)

Reference: hashicorp#472
  • Loading branch information
bflad committed Sep 13, 2022
1 parent 46b5573 commit 6dd6fbe
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ The following examples show how to migrate portions of the
For a complete example, clone the
`terraform-provider-http` repository and compare the `data_source.go` file in
[v2.2.0](https://github.com/hashicorp/terraform-provider-http/blob/v2.2.0/internal/provider/data_source.go)
with the `data_source_http.go` file in
[v3.0.1](https://github.com/hashicorp/terraform-provider-http/blob/v3.0.1/internal/provider/data_source_http.go).
with the `data_source_http.go` file
[after the migration](https://github.com/hashicorp/terraform-provider-http/blob/8527d5b4546b54cdef246a13befc5745dbbbf740/internal/provider/data_source_http.go).
### SDKv2
Expand Down Expand Up @@ -113,7 +113,7 @@ The following shows the same section of provider code after the migration.
This code implements the `url` attribute for the `http` data source with the Framework.
```go
func (d *httpDataSourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
func (d *httpDataSource) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
/* ... */
Attributes: map[string]tfsdk.Attribute{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ In the Framework, you set default values with the `PlanModifiers` field on your
struct.
```go
func (d *resourceTypeExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
func (r *resourceExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
/* ... */
Attributes: map[string]tfsdk.Attribute{
Expand All @@ -65,8 +65,8 @@ provider.
For a complete example, clone the
`terraform-provider-tls` repository and compare the `resource_private_key.go` file in
[v3.4.0](https://github.com/hashicorp/terraform-provider-tls/blob/v3.4.0/internal/provider/resource_private_key.go) with
[v4.0.1](https://github.com/hashicorp/terraform-provider-tls/blob/v4.0.1/internal/provider/resource_private_key.go).
[v3.4.0](https://github.com/hashicorp/terraform-provider-tls/blob/v3.4.0/internal/provider/resource_private_key.go) with the file
[after the migration](https://github.com/hashicorp/terraform-provider-tls/blob/4dafb105818e45a88532f917e7b170ee2a9bb092/internal/provider/resource_private_key.go).
### SDKv2
Expand All @@ -91,7 +91,7 @@ The following shows the same section of code after the migration.
This code implements the `PlanModifiers` field for the `rsa_bits` attribute with the Framework.
```go
func (rt *privateKeyResourceType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
func (r *privateKeyResource) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"rsa_bits": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ provider.
For a complete example, clone the
`terraform-provider-http` repository and compare the `data_source.go` file in
[v2.2.0](https://github.com/hashicorp/terraform-provider-http/blob/v2.2.0/internal/provider/data_source.go)
and the `data_source_http.go` file in
[v3.0.1](https://github.com/hashicorp/terraform-provider-http/blob/v3.0.1/internal/provider/data_source_http.go).
and the `data_source_http.go` file
[after the migration](https://github.com/hashicorp/terraform-provider-http/blob/8527d5b4546b54cdef246a13befc5745dbbbf740/internal/provider/data_source_http.go).
### SDKv2
Expand All @@ -79,7 +79,7 @@ The following example from the `data_source_http.go` file shows how the `url` at
to be required with the Framework.
```go
func (d *httpDataSourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
func (d *httpDataSource) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"url": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ In the Framework, you implement the same behavior by using the `resource.Require
attribute's `tfsdk.Attribute` struct.
```go
func (d *resourceTypeExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
func (r *resourceExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
/* ... */
Attributes: map[string]tfsdk.Attribute{
Expand Down Expand Up @@ -64,7 +64,7 @@ The following examples show how to migrate portions of the
For a complete example, clone the
`terraform-random-provider` repository and compare the `resource_password.go` file in
[v3.3.2](https://github.com/hashicorp/terraform-provider-random/blob/v3.3.2/internal/provider/resource_password.go)
with [v3.4.1](https://github.com/hashicorp/terraform-provider-random/blob/v3.4.1/internal/provider/resource_password.go).
with the file [after the migration](https://github.com/hashicorp/terraform-provider-random/blob/04292d3e31f71ff16b82512082baed037bb1069c/internal/provider/resource_password.go).
### SDKv2
Expand Down Expand Up @@ -94,7 +94,7 @@ This code forces the replacement of a `random_password` resource when the value
The example does this using the `PlanModifiers` field within the `random_password` attribute's schema.
```go
func (r *passwordResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
func (r *passwordResource) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"keepers": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func resourceExample() *schema.Resource {
In the Framework, you set your attribute's type with the `Type` field on your attribute's `tfsdk.Attribute` struct.
```go
func (d *resourceTypeExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
func (r *resourceExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
/* ... */
Attributes: map[string]tfsdk.Attribute{
Expand Down Expand Up @@ -121,8 +121,8 @@ The following examples show how to migrate portions of the
For a complete example, clone the
`terraform-provider-http` repository and compare the `data_source.go` file in
[v2.2.0](https://github.com/hashicorp/terraform-provider-http/blob/v2.2.0/internal/provider/data_source.go)
and the `data_source_http.go` file in
[v3.0.1](https://github.com/hashicorp/terraform-provider-http/blob/v3.0.1/internal/provider/data_source_http.go).
and the `data_source_http.go` file
[after the migration](https://github.com/hashicorp/terraform-provider-http/blob/8527d5b4546b54cdef246a13befc5745dbbbf740/internal/provider/data_source_http.go).
### SDKv2
Expand All @@ -146,7 +146,7 @@ The following example from the `data_source_http.go` file shows how the type of
source is defined with the Framework after the migration.
```go
func (d *httpDataSourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
func (d *httpDataSource) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"url": {
Expand All @@ -162,8 +162,8 @@ The following examples show how to migrate portions of the
For a complete example, clone the
`terraform-provider-tls` repository and compare the `common_cert.go` file in
[v3.4.0](https://github.com/hashicorp/terraform-provider-tls/blob/v3.4.0/internal/provider/common_cert.go)
and the `resource_cert_request.go` file in
[v4.0.1](https://github.com/hashicorp/terraform-provider-tls/blob/v4.0.1/internal/provider/resource_cert_request.go).
and the `resource_cert_request.go` file
[after the migration](https://github.com/hashicorp/terraform-provider-tls/blob/4dafb105818e45a88532f917e7b170ee2a9bb092/internal/provider/resource_cert_request.go).
### SDKv2
Expand All @@ -190,7 +190,7 @@ The following example from the `data_source_http.go` file shows how the type of
source is defined with the Framework after the migration.
```go
func (rt *certRequestResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
func (r *certRequestResource) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"dns_names": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ The following examples show how to migrate portions of the
For a complete example, clone the
`terraform-provider-random` repository and compare the `resource_password.go` file in
[v3.3.2](https://github.com/hashicorp/terraform-provider-random/blob/v3.3.2/internal/provider/resource_password.go)
with [v3.4.1](https://github.com/hashicorp/terraform-provider-random/blob/v3.4.1/internal/provider/resource_password.go).
with the file [after the migration](https://github.com/hashicorp/terraform-provider-random/blob/04292d3e31f71ff16b82512082baed037bb1069c/internal/provider/resource_password.go).
### SDKv2
Expand All @@ -94,7 +94,7 @@ This code validates that the `random_password`'s `length` attribute is greater t
validator.
```go
func (r *passwordResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
func (r *passwordResource) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"length": {
Expand Down
73 changes: 38 additions & 35 deletions website/docs/plugin/framework/migrating/data-sources/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,48 +45,49 @@ schema.Resource {
## Framework
In the Framework, you define data sources by adding them to the map returned by your provider's `GetDataSources` function.
In the Framework, you define data sources by adding them to the map returned by your provider's `DataSources` method.
The `GetDataSources` function on your `provider.Provider` returns a map from the data source name (string) to a type
that implements the `DataSourceType` interface for each data source your provider supports.
The `DataSources` method on your `provider.Provider` returns a slice of functions that return types
that implement the `datasource.DataSource` interface for each data source your provider supports.
The following code shows how you add a data source to your provider with the Framework.
```go
func (p *provider) GetDataSources(ctx context.Context) (map[string]provider.DataSourceType, diag.Diagnostics) {
return map[string]provider.DataSourceType{
func (p *provider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
/* ... */
}, nil
}
}
```
Like the `provider.ResourceType` interface, `provider.DataSourceType` requires `GetSchema` and `NewResource` functions.
These functions work the same way for data sources as they do for resources.
Like the `resource.Resource` interface, `datasource.DataSource` requires `GetSchema` and `Metadata` methods.
These methods work the same way for data sources as they do for resources. The `Read` method is also required.
The `GetSchema` function returns a `tfsdk.Schema` struct which defines your data source's attributes. This is the same
The `GetSchema` method returns a `tfsdk.Schema` struct which defines your data source's attributes. This is the same
struct you use to define provider and resource attributes.
The `NewResource` function returns a type that you define. The type implements the `resource.Resource` interface,
including the CRUD functions for your resource.
The `Metadata` method returns a type name that you define.
The `Read` method implements the logic for writing into the Terraform state.
The following code shows how you define a `provider.DataSourceType` which implements these two functions with the
The following code shows how you define a `datasource.DataSource` which implements these methods with the
Framework.
```go
type dataSourceTypeExample struct{}
type dataSourceExample struct{}

func (r *dataSourceTypeExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
func (d *dataSourceExample) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
/* ... */
}

func (r *dataSourceTypeExample) NewDataSource(ctx context.Context, p provider.Provider) (datasource.DataSource, diag.Diagnostics) {
func (d *dataSourceExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
/* ... */
}
```

Unlike resources, you only need to implement a read function for your data sources. Refer to the
[Resources - CRUD functions](/plugin/framework/migrating/resources/crud) page in this guide to learn how to define this
function on your `datasource.DataSource` type.
func (d *dataSourceExample) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
/* ... */
}
```
## Migration Notes
Expand All @@ -103,8 +104,8 @@ provider.
For a complete example, clone the
`terraform-provider-http` repository and compare the `data_source.go` file in
[v2.2.0](https://github.com/hashicorp/terraform-provider-http/blob/v2.2.0/internal/provider/data_source.go)
and the `data_source_http.go` file in
[v3.0.1](https://github.com/hashicorp/terraform-provider-http/blob/v3.0.1/internal/provider/data_source_http.go).
and the `data_source_http.go` file
[after the migration](https://github.com/hashicorp/terraform-provider-http/blob/8527d5b4546b54cdef246a13befc5745dbbbf740/internal/provider/data_source_http.go).
### SDKv2
Expand Down Expand Up @@ -145,18 +146,27 @@ The following example from the `provider.go` file shows how the `http` data sour
the migration.
```go
func (p *provider) GetDataSources(context.Context) (map[string]provider.DataSourceType, diag.Diagnostics) {
return map[string]provider.DataSourceType{
"http": &httpDataSourceType{},
}, nil
func (p *provider) DataSources(context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
func() datasource.DataSource {
return &httpDataSource{}
},
}
}
```
This code from the `data_source_http.go` file defines the `GetSchema` function for the `http` data source with the
This code from the `data_source_http.go` file defines the methods for the `http` data source with the
Framework.
```go
func (d *httpDataSourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
func (d *httpDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
// This is unconventional in that the data source name matches the provider name.
// Typically these should have the provider name, an underscore, then the type name.
// e.g. http_request
resp.TypeName = "http"
}

func (d *httpDataSource) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"url": {
Expand All @@ -166,14 +176,7 @@ func (d *httpDataSourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diag
},
/* ... */

func (d *httpDataSourceType) NewDataSource(context.Context, provider.Provider) (datasource.DataSource, diag.Diagnostics) {
return &httpDataSource{}, nil
}
```
This code from the `data_source_http.go` file defines the `Read` function for the `http` data source with the Framework.
```go
func (d *httpDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
/* ... */
}
```
Loading

0 comments on commit 6dd6fbe

Please sign in to comment.