Skip to content

Commit ddd8fa5

Browse files
kmoepaddycarver
andauthored
Provider, Resource, and DataSource types (#32)
* improve request godoc * define provider and resource types Co-authored-by: Paddy <[email protected]> * clarify godoc for resource types * use our schema package Co-authored-by: Paddy <[email protected]>
1 parent 72273ad commit ddd8fa5

File tree

4 files changed

+119
-8
lines changed

4 files changed

+119
-8
lines changed

data_source.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package tfsdk
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/schema"
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
)
9+
10+
// A DataSourceType is a type of data source. For each type of data source this
11+
// provider supports, it should define a type implementing DataSourceType and
12+
// return an instance of it in the map returned by Provider.GetDataSources.
13+
type DataSourceType interface {
14+
// GetSchema returns the schema for this data source.
15+
GetSchema(context.Context) (schema.Schema, []*tfprotov6.Diagnostic)
16+
17+
// NewDataSource instantiates a new DataSource of this DataSourceType.
18+
NewDataSource(Provider) (DataSource, []*tfprotov6.Diagnostic)
19+
}
20+
21+
// DataSource implements a data source instance.
22+
type DataSource interface {
23+
// Read is called when the provider must read data source values in
24+
// order to update state. Planned state values should be read from the
25+
// ReadResourceRequest and new state values set on the
26+
// ReadResourceResponse.
27+
Read(context.Context, *ReadResourceRequest, *ReadResourceResponse)
28+
}

provider.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package tfsdk
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/schema"
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
)
9+
10+
// Provider is the core interface that all Terraform providers must implement.
11+
type Provider interface {
12+
// GetSchema returns the schema for this provider's configuration. If
13+
// this provider has no configuration, return nil.
14+
GetSchema(context.Context) (schema.Schema, []*tfprotov6.Diagnostic)
15+
16+
// Configure is called at the beginning of the provider lifecycle, when
17+
// Terraform sends to the provider the values the user specified in the
18+
// provider configuration block. These are supplied in the
19+
// ConfigureProviderRequest argument.
20+
// Values from provider configuration are often used to initialise an
21+
// API client, which should be stored on the struct implementing the
22+
// Provider interface.
23+
Configure(context.Context, *ConfigureProviderRequest, *ConfigureProviderResponse)
24+
25+
// GetResources returns a map of the resource types this provider
26+
// supports.
27+
GetResources(context.Context) (map[string]ResourceType, []*tfprotov6.Diagnostic)
28+
29+
// GetDataSources returns a map of the data source types this provider
30+
// supports.
31+
GetDataSources(context.Context) (map[string]DataSourceType, []*tfprotov6.Diagnostic)
32+
}
33+
34+
// ProviderWithProviderMeta is a provider with a provider meta schema.
35+
// This functionality is currently experimental and subject to change or break
36+
// without warning; it should only be used by providers that are collaborating
37+
// on its use with the Terraform team.
38+
type ProviderWithProviderMeta interface {
39+
Provider
40+
// GetMetaSchema returns the provider meta schema.
41+
GetMetaSchema(context.Context) (*tfprotov6.Schema, []*tfprotov6.Diagnostic)
42+
}

request.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import (
44
"github.com/hashicorp/terraform-plugin-go/tftypes"
55
)
66

7-
// ConfigureProviderRequest represents a request supplying the provider with
8-
// the values the user supplied for the provider configuration block, along with
9-
// other runtime information supplied by Terraform or the Plugin SDK. An
10-
// instance of this request struct is supplied as an argument to the provider's
11-
// Configure function.
7+
// ConfigureProviderRequest represents a request containing the values the user
8+
// specified for the provider configuration block, along with other runtime
9+
// information from Terraform or the Plugin SDK. An instance of this request
10+
// struct is supplied as an argument to the provider's Configure function.
1211
type ConfigureProviderRequest struct {
1312
// TerraformVersion is the version of Terraform executing the request.
1413
// This is supplied for logging, analytics, and User-Agent purposes
@@ -45,9 +44,6 @@ type CreateResourceRequest struct {
4544
// resource. An instance of this request struct is supplied as an argument to
4645
// the resource's Read function.
4746
type ReadResourceRequest struct {
48-
// tfprotov6.ReadResourceRequest has no Config field
49-
// Config Config
50-
5147
// State is the current state of the resource prior to the Read
5248
// operation.
5349
// TODO uncomment when implemented

resource.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package tfsdk
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/schema"
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
)
9+
10+
// A ResourceType is a type of resource. For each type of resource this provider
11+
// supports, it should define a type implementing ResourceType and return an
12+
// instance of it in the map returned by Provider.GeResources.
13+
type ResourceType interface {
14+
// GetSchema returns the schema for this resource.
15+
GetSchema(context.Context) (schema.Schema, []*tfprotov6.Diagnostic)
16+
17+
// NewResource instantiates a new Resource of this ResourceType.
18+
NewResource(Provider) (Resource, []*tfprotov6.Diagnostic)
19+
}
20+
21+
// Resource represents a resource instance. This is the core interface that all
22+
// resources must implement.
23+
type Resource interface {
24+
// Create is called when the provider must create a new resource. Config
25+
// and planned state values should be read from the
26+
// CreateResourceRequest and new state values set on the
27+
// CreateResourceResponse.
28+
Create(context.Context, *CreateResourceRequest, *CreateResourceResponse)
29+
30+
// Read is called when the provider must read resource values in order
31+
// to update state. Planned state values should be read from the
32+
// ReadResourceRequest and new state values set on the
33+
// ReadResourceResponse.
34+
Read(context.Context, *ReadResourceRequest, *ReadResourceResponse)
35+
36+
// Update is called to update the state of the resource. Config, planned
37+
// state, and prior state values should be read from the
38+
// UpdateResourceRequest and new state values set on the
39+
// UpdateResourceResponse.
40+
Update(context.Context, *UpdateResourceRequest, *UpdateResourceResponse)
41+
42+
// Delete is called when the provider must delete the resource. Config
43+
// values may be read from the DeleteResourceRequest.
44+
Delete(context.Context, *DeleteResourceRequest, *DeleteResourceResponse)
45+
}

0 commit comments

Comments
 (0)