More on terraform receita provider

This commit is contained in:
Bitor Tonixa Balenca 2023-12-19 23:31:44 +00:00
parent 9324ddd4b1
commit 5050270cc4
No known key found for this signature in database
GPG key ID: AF4454DAA92FA1F4
3 changed files with 66 additions and 8 deletions

View file

@ -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"
}

View file

@ -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

View file

@ -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)
}