From 5050270cc48209c980b241f7279ab2d1dc0d5323 Mon Sep 17 00:00:00 2001 From: Bitor Tonixa Balenca Date: Tue, 19 Dec 2023 23:31:44 +0000 Subject: [PATCH] More on terraform receita provider --- example/receitas.tf | 4 +- internal/provider/provider.go | 10 +++-- internal/provider/receita_resource.go | 60 ++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/example/receitas.tf b/example/receitas.tf index b42e5da..b7bd8e2 100644 --- a/example/receitas.tf +++ b/example/receitas.tf @@ -1,12 +1,12 @@ terraform { required_providers { receita = { - source = "terraform.local/local/receita" + source = "terraform.local/balhau/receita" } } } -provider "receitas" { +provider "receita" { endpoint = "localhost:9999" } diff --git a/internal/provider/provider.go b/internal/provider/provider.go index df0149d..5c76c60 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -19,7 +19,7 @@ type ReceitaProvider struct { // Datamodel for our provider type ReceitaProviderModel struct { - Folder types.String `tfsdk:"folder"` + Endpoint types.String `tfsdk:"endpoint"` } // Now we need to define the contract defined by provider.Provider from terraform @@ -41,7 +41,7 @@ func (p *ReceitaProvider) Schema(ctx context.Context, req provider.SchemaRequest Attributes: map[string]schema.Attribute{ "endpoint": schema.StringAttribute{ MarkdownDescription: "Endpoint under which the receitas will be called upon", - Optional: false, + Required: true, }, }, } @@ -66,11 +66,13 @@ func (p *ReceitaProvider) Configure(ctx context.Context, req provider.ConfigureR // Set the resources enabled by this provider func (p *ReceitaProvider) Resources(ctx context.Context) []func() resource.Resource { - return []func() resource.Resource{} + return []func() resource.Resource{ + NewReceitaResource, + } } func (p *ReceitaProvider) DataSources(ctx context.Context) []func() datasource.DataSource { - return nil + return []func() datasource.DataSource{} } // This is the builder method for our ReceitaProvider instances diff --git a/internal/provider/receita_resource.go b/internal/provider/receita_resource.go index 2e8e526..45e7c05 100644 --- a/internal/provider/receita_resource.go +++ b/internal/provider/receita_resource.go @@ -6,6 +6,7 @@ import ( "net/http" "github.com/google/uuid" + "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" @@ -50,11 +51,11 @@ func (r *ReceitaResource) Schema(ctx context.Context, req resource.SchemaRequest Attributes: map[string]schema.Attribute{ "name": schema.StringAttribute{ MarkdownDescription: "Name of the receita", - Optional: false, + Required: true, }, "author": schema.StringAttribute{ MarkdownDescription: "Name of the author", - Optional: false, + Required: true, }, "id": schema.StringAttribute{ Computed: true, @@ -101,6 +102,8 @@ func (r *ReceitaResource) Create(ctx context.Context, req resource.CreateRequest return } + // Do specific stuff + // Here just generates a random uuid data.Id = basetypes.NewStringValue(uuid.Must(uuid.NewRandom()).String()) @@ -109,3 +112,56 @@ func (r *ReceitaResource) Create(ctx context.Context, req resource.CreateRequest resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } + +func (r *ReceitaResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var data ReceitaResourceModel + + // Read Terraform prior state data into the model + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + + if resp.Diagnostics.HasError() { + return + } + + // Do read specific operation + + // Save updated data into Terraform state + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) + +} + +func (r *ReceitaResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var data ReceitaResourceModel + + //Read terraform state into model + //Append is a varadic method and Get returns an array we need to use ... to transform an array into varadic representation + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) + + // Return in case of error + if resp.Diagnostics.HasError() { + return + } + + //Do specific update stuff + + //Finally update the terraform state + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} + +func (r *ReceitaResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var data ReceitaResourceModel + + // Load tf state into the model + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + + if resp.Diagnostics.HasError() { + return + } + + // Do specific delete logic + +} + +func (r *ReceitaResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) +}