Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/provider: Updates for framework data source, provider, and resource refactoring #72

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.17

require (
github.com/hashicorp/terraform-plugin-docs v0.13.0
github.com/hashicorp/terraform-plugin-framework v0.10.0
github.com/hashicorp/terraform-plugin-go v0.12.0
github.com/hashicorp/terraform-plugin-framework v0.10.1-0.20220729031038-a5443541a405
github.com/hashicorp/terraform-plugin-go v0.13.0
github.com/hashicorp/terraform-plugin-log v0.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.19.0
)
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,11 @@ github.com/hashicorp/terraform-json v0.14.0 h1:sh9iZ1Y8IFJLx+xQiKHGud6/TSUCM0N8e
github.com/hashicorp/terraform-json v0.14.0/go.mod h1:5A9HIWPkk4e5aeeXIBbkcOvaZbIYnAIkEyqP2pNSckM=
github.com/hashicorp/terraform-plugin-docs v0.13.0 h1:6e+VIWsVGb6jYJewfzq2ok2smPzZrt1Wlm9koLeKazY=
github.com/hashicorp/terraform-plugin-docs v0.13.0/go.mod h1:W0oCmHAjIlTHBbvtppWHe8fLfZ2BznQbuv8+UD8OucQ=
github.com/hashicorp/terraform-plugin-framework v0.10.0 h1:LGYcnvNdVaZA1ZHe53BHLVjaaGs7HTiq6+9Js29stL4=
github.com/hashicorp/terraform-plugin-framework v0.10.0/go.mod h1:CK7Opzukfu/2CPJs+HzUdfHrFlp+ZIQeSxjF0x8k464=
github.com/hashicorp/terraform-plugin-go v0.12.0 h1:6wW9mT1dSs0Xq4LR6HXj1heQ5ovr5GxXNJwkErZzpJw=
github.com/hashicorp/terraform-plugin-framework v0.10.1-0.20220729031038-a5443541a405 h1:7O/YSoffZ219wRGB1lqcUL6CiBAYxWa09kB/eSOZ9zQ=
github.com/hashicorp/terraform-plugin-framework v0.10.1-0.20220729031038-a5443541a405/go.mod h1:mfVAo3qMZ6LmK0y02rhDJNLUhP957OmwGuumNKz5qDQ=
github.com/hashicorp/terraform-plugin-go v0.12.0/go.mod h1:kwhmaWHNDvT1B3QiSJdAtrB/D4RaKSY/v3r2BuoWK4M=
github.com/hashicorp/terraform-plugin-go v0.13.0 h1:Zm+o91HUOcTLotaEu3X2jV/6wNi6f09gkZwGg/MDvCk=
github.com/hashicorp/terraform-plugin-go v0.13.0/go.mod h1:NYGFEM9GeRdSl52txue3RcBDFt2tufaqS22iURP8Bxs=
github.com/hashicorp/terraform-plugin-log v0.6.0/go.mod h1:p4R1jWBXRTvL4odmEkFfDdhUjHf9zcs/BCoNHAc7IK4=
github.com/hashicorp/terraform-plugin-log v0.7.0 h1:SDxJUyT8TwN4l5b5/VkiTIaQgY6R+Y2BQ0sRZftGKQs=
github.com/hashicorp/terraform-plugin-log v0.7.0/go.mod h1:p4R1jWBXRTvL4odmEkFfDdhUjHf9zcs/BCoNHAc7IK4=
Expand Down
22 changes: 12 additions & 10 deletions internal/provider/example_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package provider

import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

// Ensure provider defined types fully satisfy framework interfaces
var _ tfsdk.DataSourceType = exampleDataSourceType{}
var _ tfsdk.DataSource = exampleDataSource{}
var _ provider.DataSourceType = exampleDataSourceType{}
var _ datasource.DataSource = exampleDataSource{}

type exampleDataSourceType struct{}

Expand All @@ -35,7 +37,7 @@ func (t exampleDataSourceType) GetSchema(ctx context.Context) (tfsdk.Schema, dia
}, nil
}

func (t exampleDataSourceType) NewDataSource(ctx context.Context, in tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
func (t exampleDataSourceType) NewDataSource(ctx context.Context, in provider.Provider) (datasource.DataSource, diag.Diagnostics) {
provider, diags := convertProviderType(in)

return exampleDataSource{
Expand All @@ -49,23 +51,19 @@ type exampleDataSourceData struct {
}

type exampleDataSource struct {
provider provider
provider scaffoldingProvider
}

func (d exampleDataSource) Read(ctx context.Context, req tfsdk.ReadDataSourceRequest, resp *tfsdk.ReadDataSourceResponse) {
func (d exampleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data exampleDataSourceData

diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)

log.Printf("got here")

if resp.Diagnostics.HasError() {
return
}

log.Printf("got here")

// If applicable, this is a great opportunity to initialize any necessary
// provider client data and make a call using it.
// example, err := d.provider.client.ReadExample(...)
Expand All @@ -78,6 +76,10 @@ func (d exampleDataSource) Read(ctx context.Context, req tfsdk.ReadDataSourceReq
// save into the Terraform state.
data.Id = types.String{Value: "example-id"}

// Write logs using the tflog package
// Documentation: https://terraform.io/plugin/log
tflog.Trace(ctx, "read a data source")

diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
29 changes: 15 additions & 14 deletions internal/provider/example_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

// Ensure provider defined types fully satisfy framework interfaces
var _ tfsdk.ResourceType = exampleResourceType{}
var _ tfsdk.Resource = exampleResource{}
var _ tfsdk.ResourceWithImportState = exampleResource{}
var _ provider.ResourceType = exampleResourceType{}
var _ resource.Resource = exampleResource{}
var _ resource.ResourceWithImportState = exampleResource{}

type exampleResourceType struct{}

Expand All @@ -40,7 +42,7 @@ func (t exampleResourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.
}, nil
}

func (t exampleResourceType) NewResource(ctx context.Context, in tfsdk.Provider) (tfsdk.Resource, diag.Diagnostics) {
func (t exampleResourceType) NewResource(ctx context.Context, in provider.Provider) (resource.Resource, diag.Diagnostics) {
provider, diags := convertProviderType(in)

return exampleResource{
Expand All @@ -54,10 +56,10 @@ type exampleResourceData struct {
}

type exampleResource struct {
provider provider
provider scaffoldingProvider
}

func (r exampleResource) Create(ctx context.Context, req tfsdk.CreateResourceRequest, resp *tfsdk.CreateResourceResponse) {
func (r exampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data exampleResourceData

diags := req.Config.Get(ctx, &data)
Expand All @@ -79,16 +81,15 @@ func (r exampleResource) Create(ctx context.Context, req tfsdk.CreateResourceReq
// save into the Terraform state.
data.Id = types.String{Value: "example-id"}

// write logs using the tflog package
// see https://pkg.go.dev/github.com/hashicorp/terraform-plugin-log/tflog
// for more information
// Write logs using the tflog package
// Documentation: https://terraform.io/plugin/log
tflog.Trace(ctx, "created a resource")

diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}

func (r exampleResource) Read(ctx context.Context, req tfsdk.ReadResourceRequest, resp *tfsdk.ReadResourceResponse) {
func (r exampleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data exampleResourceData

diags := req.State.Get(ctx, &data)
Expand All @@ -110,7 +111,7 @@ func (r exampleResource) Read(ctx context.Context, req tfsdk.ReadResourceRequest
resp.Diagnostics.Append(diags...)
}

func (r exampleResource) Update(ctx context.Context, req tfsdk.UpdateResourceRequest, resp *tfsdk.UpdateResourceResponse) {
func (r exampleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data exampleResourceData

diags := req.Plan.Get(ctx, &data)
Expand All @@ -132,7 +133,7 @@ func (r exampleResource) Update(ctx context.Context, req tfsdk.UpdateResourceReq
resp.Diagnostics.Append(diags...)
}

func (r exampleResource) Delete(ctx context.Context, req tfsdk.DeleteResourceRequest, resp *tfsdk.DeleteResourceResponse) {
func (r exampleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data exampleResourceData

diags := req.State.Get(ctx, &data)
Expand All @@ -151,6 +152,6 @@ func (r exampleResource) Delete(ctx context.Context, req tfsdk.DeleteResourceReq
// }
}

func (r exampleResource) ImportState(ctx context.Context, req tfsdk.ImportResourceStateRequest, resp *tfsdk.ImportResourceStateResponse) {
tfsdk.ResourceImportStatePassthroughID(ctx, path.Root("id"), req, resp)
func (r exampleResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
33 changes: 17 additions & 16 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import (
"fmt"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// Ensure provider defined types fully satisfy framework interfaces
var _ tfsdk.Provider = &provider{}
var _ provider.Provider = &scaffoldingProvider{}

// provider satisfies the tfsdk.Provider interface and usually is included
// with all Resource and DataSource implementations.
type provider struct {
type scaffoldingProvider struct {
// client can contain the upstream provider SDK or HTTP client used to
// communicate with the upstream service. Resource and DataSource
// implementations can then make calls using this client.
Expand All @@ -38,7 +39,7 @@ type providerData struct {
Example types.String `tfsdk:"example"`
}

func (p *provider) Configure(ctx context.Context, req tfsdk.ConfigureProviderRequest, resp *tfsdk.ConfigureProviderResponse) {
func (p *scaffoldingProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var data providerData
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
Expand All @@ -56,19 +57,19 @@ func (p *provider) Configure(ctx context.Context, req tfsdk.ConfigureProviderReq
p.configured = true
}

func (p *provider) GetResources(ctx context.Context) (map[string]tfsdk.ResourceType, diag.Diagnostics) {
return map[string]tfsdk.ResourceType{
func (p *scaffoldingProvider) GetResources(ctx context.Context) (map[string]provider.ResourceType, diag.Diagnostics) {
return map[string]provider.ResourceType{
"scaffolding_example": exampleResourceType{},
}, nil
}

func (p *provider) GetDataSources(ctx context.Context) (map[string]tfsdk.DataSourceType, diag.Diagnostics) {
return map[string]tfsdk.DataSourceType{
func (p *scaffoldingProvider) GetDataSources(ctx context.Context) (map[string]provider.DataSourceType, diag.Diagnostics) {
return map[string]provider.DataSourceType{
"scaffolding_example": exampleDataSourceType{},
}, nil
}

func (p *provider) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
func (p *scaffoldingProvider) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"example": {
Expand All @@ -80,9 +81,9 @@ func (p *provider) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostic
}, nil
}

func New(version string) func() tfsdk.Provider {
return func() tfsdk.Provider {
return &provider{
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &scaffoldingProvider{
version: version,
}
}
Expand All @@ -91,27 +92,27 @@ func New(version string) func() tfsdk.Provider {
// convertProviderType is a helper function for NewResource and NewDataSource
// implementations to associate the concrete provider type. Alternatively,
// this helper can be skipped and the provider type can be directly type
// asserted (e.g. provider: in.(*provider)), however using this can prevent
// asserted (e.g. provider: in.(*scaffoldingProvider)), however using this can prevent
// potential panics.
func convertProviderType(in tfsdk.Provider) (provider, diag.Diagnostics) {
func convertProviderType(in provider.Provider) (scaffoldingProvider, diag.Diagnostics) {
var diags diag.Diagnostics

p, ok := in.(*provider)
p, ok := in.(*scaffoldingProvider)

if !ok {
diags.AddError(
"Unexpected Provider Instance Type",
fmt.Sprintf("While creating the data source or resource, an unexpected provider type (%T) was received. This is always a bug in the provider code and should be reported to the provider developers.", p),
)
return provider{}, diags
return scaffoldingProvider{}, diags
}

if p == nil {
diags.AddError(
"Unexpected Provider Instance Type",
"While creating the data source or resource, an unexpected empty provider instance was received. This is always a bug in the provider code and should be reported to the provider developers.",
)
return provider{}, diags
return scaffoldingProvider{}, diags
}

return *p, diags
Expand Down