Skip to content

Commit

Permalink
chore: Migrate from tfsdk to new go packages
Browse files Browse the repository at this point in the history
See [1] for more information on these changes.

[1]: hashicorp/terraform-plugin-framework#432
  • Loading branch information
gonzolino committed Aug 16, 2022
1 parent 8524bf0 commit 2577270
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 37 deletions.
24 changes: 13 additions & 11 deletions internal/provider/geofencing_resource.go
Expand Up @@ -7,14 +7,16 @@ 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"
)

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

type geofencingResourceType struct{}

Expand Down Expand Up @@ -43,7 +45,7 @@ func (geofencingResourceType) GetSchema(_ context.Context) (tfsdk.Schema, diag.D
}, nil
}

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

return geofencingResource{
Expand All @@ -58,10 +60,10 @@ type geofencingResourceData struct {
}

type geofencingResource struct {
provider provider
provider tadoProvider
}

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

diags := req.Config.Get(ctx, &data)
Expand Down Expand Up @@ -120,7 +122,7 @@ func (r geofencingResource) Create(ctx context.Context, req tfsdk.CreateResource
resp.Diagnostics.Append(diags...)
}

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

diags := req.State.Get(ctx, &data)
Expand Down Expand Up @@ -162,7 +164,7 @@ func (r geofencingResource) Read(ctx context.Context, req tfsdk.ReadResourceRequ
resp.Diagnostics.Append(diags...)
}

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

diags := req.Plan.Get(ctx, &data)
Expand Down Expand Up @@ -221,7 +223,7 @@ func (r geofencingResource) Update(ctx context.Context, req tfsdk.UpdateResource
resp.Diagnostics.Append(diags...)
}

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

diags := req.State.Get(ctx, &data)
Expand All @@ -234,6 +236,6 @@ func (geofencingResource) Delete(ctx context.Context, req tfsdk.DeleteResourceRe
// No deletion necesary on tado api.
}

func (geofencingResource) ImportState(ctx context.Context, req tfsdk.ImportResourceStateRequest, resp *tfsdk.ImportResourceStateResponse) {
tfsdk.ResourceImportStatePassthroughID(ctx, path.Root("home_name"), req, resp)
func (geofencingResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("home_name"), req, resp)
}
12 changes: 7 additions & 5 deletions internal/provider/home_data_source.go
Expand Up @@ -4,14 +4,16 @@ import (
"context"
"fmt"

"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"
)

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

type homeDataSourceType struct{}

Expand Down Expand Up @@ -95,7 +97,7 @@ func (homeDataSourceType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagn
}, nil
}

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

return homeDataSource{
Expand All @@ -121,10 +123,10 @@ type homeDataSourceData struct {
}

type homeDataSource struct {
provider provider
provider tadoProvider
}

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

diags := req.Config.Get(ctx, &data)
Expand Down
33 changes: 17 additions & 16 deletions internal/provider/provider.go
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/gonzolino/gotado/v2"
"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"
)
Expand All @@ -18,11 +19,11 @@ const (
)

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

// provider satisfies the tfsdk.Provider interface and usually is included
// tadoProvider satisfies the provider.Provider interface and usually is included
// with all Resource and DataSource implementations.
type provider struct {
type tadoProvider 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 @@ -48,7 +49,7 @@ type providerData struct {
Password types.String `tfsdk:"password"`
}

func (p *provider) Configure(ctx context.Context, req tfsdk.ConfigureProviderRequest, resp *tfsdk.ConfigureProviderResponse) {
func (p *tadoProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var data providerData
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
Expand Down Expand Up @@ -94,20 +95,20 @@ 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 *tadoProvider) GetResources(_ context.Context) (map[string]provider.ResourceType, diag.Diagnostics) {
return map[string]provider.ResourceType{
"tado_geofencing": geofencingResourceType{},
}, nil
}

func (p *provider) GetDataSources(ctx context.Context) (map[string]tfsdk.DataSourceType, diag.Diagnostics) {
return map[string]tfsdk.DataSourceType{
func (p *tadoProvider) GetDataSources(_ context.Context) (map[string]provider.DataSourceType, diag.Diagnostics) {
return map[string]provider.DataSourceType{
"tado_home": homeDataSourceType{},
"tado_zone": zoneDataSourceType{},
}, nil
}

func (p *provider) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
func (p *tadoProvider) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"username": {
Expand All @@ -125,9 +126,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 &tadoProvider{
version: version,
}
}
Expand All @@ -138,25 +139,25 @@ func New(version string) func() tfsdk.Provider {
// this helper can be skipped and the provider type can be directly type
// asserted (e.g. provider: in.(*provider)), however using this can prevent
// potential panics.
func convertProviderType(in tfsdk.Provider) (provider, diag.Diagnostics) {
func convertProviderType(in provider.Provider) (tadoProvider, diag.Diagnostics) {
var diags diag.Diagnostics

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

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 tadoProvider{}, 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 tadoProvider{}, diags
}

return *p, diags
Expand Down
12 changes: 7 additions & 5 deletions internal/provider/zone_data_source.go
Expand Up @@ -4,14 +4,16 @@ import (
"context"
"fmt"

"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"
)

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

type zoneDataSourceType struct{}

Expand Down Expand Up @@ -60,7 +62,7 @@ func (zoneDataSourceType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagn
}, nil
}

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

return zoneDataSource{
Expand All @@ -79,10 +81,10 @@ type zoneDataSourceData struct {
}

type zoneDataSource struct {
provider provider
provider tadoProvider
}

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

diags := req.Config.Get(ctx, &data)
Expand Down

0 comments on commit 2577270

Please sign in to comment.