|
@@ -2,10 +2,11 @@ package provider
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "fmt"
|
|
|
+ "net/http"
|
|
|
|
|
|
"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"
|
|
@@ -13,13 +14,29 @@ import (
|
|
|
)
|
|
|
|
|
|
// Ensure provider defined types fully satisfy framework interfaces
|
|
|
-var _ provider.ResourceType = exampleResourceType{}
|
|
|
-var _ resource.Resource = exampleResource{}
|
|
|
-var _ resource.ResourceWithImportState = exampleResource{}
|
|
|
+var _ resource.Resource = &ExampleResource{}
|
|
|
+var _ resource.ResourceWithImportState = &ExampleResource{}
|
|
|
|
|
|
-type exampleResourceType struct{}
|
|
|
+func NewExampleResource() resource.Resource {
|
|
|
+ return &ExampleResource{}
|
|
|
+}
|
|
|
+
|
|
|
+// ExampleResource defines the resource implementation.
|
|
|
+type ExampleResource struct {
|
|
|
+ client *http.Client
|
|
|
+}
|
|
|
+
|
|
|
+// ExampleResourceModel describes the resource data model.
|
|
|
+type ExampleResourceModel struct {
|
|
|
+ ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
|
|
|
+ Id types.String `tfsdk:"id"`
|
|
|
+}
|
|
|
+
|
|
|
+func (r *ExampleResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
|
|
+ resp.TypeName = req.ProviderTypeName + "_example"
|
|
|
+}
|
|
|
|
|
|
-func (t exampleResourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
|
|
|
+func (r *ExampleResource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
|
|
|
return tfsdk.Schema{
|
|
|
// This description is used by the documentation generator and the language server.
|
|
|
MarkdownDescription: "Example resource",
|
|
@@ -42,28 +59,31 @@ func (t exampleResourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
-func (t exampleResourceType) NewResource(ctx context.Context, in provider.Provider) (resource.Resource, diag.Diagnostics) {
|
|
|
- provider, diags := convertProviderType(in)
|
|
|
+func (r *ExampleResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
|
|
+ // Prevent panic if the provider has not been configured.
|
|
|
+ if req.ProviderData == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- return exampleResource{
|
|
|
- provider: provider,
|
|
|
- }, diags
|
|
|
-}
|
|
|
+ client, ok := req.ProviderData.(*http.Client)
|
|
|
|
|
|
-type exampleResourceData struct {
|
|
|
- ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
|
|
|
- Id types.String `tfsdk:"id"`
|
|
|
-}
|
|
|
+ if !ok {
|
|
|
+ resp.Diagnostics.AddError(
|
|
|
+ "Unexpected Resource Configure Type",
|
|
|
+ fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
|
|
|
+ )
|
|
|
+
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
-type exampleResource struct {
|
|
|
- provider scaffoldingProvider
|
|
|
+ r.client = client
|
|
|
}
|
|
|
|
|
|
-func (r exampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
|
|
- var data exampleResourceData
|
|
|
+func (r *ExampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
|
|
+ var data *ExampleResourceModel
|
|
|
|
|
|
- diags := req.Config.Get(ctx, &data)
|
|
|
- resp.Diagnostics.Append(diags...)
|
|
|
+ // Read Terraform plan data into the model
|
|
|
+ resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
|
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
|
return
|
|
@@ -71,7 +91,7 @@ func (r exampleResource) Create(ctx context.Context, req resource.CreateRequest,
|
|
|
|
|
|
// 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.CreateExample(...)
|
|
|
+ // httpResp, err := d.client.Do(httpReq)
|
|
|
// if err != nil {
|
|
|
// resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create example, got error: %s", err))
|
|
|
// return
|
|
@@ -85,15 +105,15 @@ func (r exampleResource) Create(ctx context.Context, req resource.CreateRequest,
|
|
|
// Documentation: https://terraform.io/plugin/log
|
|
|
tflog.Trace(ctx, "created a resource")
|
|
|
|
|
|
- diags = resp.State.Set(ctx, &data)
|
|
|
- resp.Diagnostics.Append(diags...)
|
|
|
+ // Save data into Terraform state
|
|
|
+ resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
|
}
|
|
|
|
|
|
-func (r exampleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
|
|
- var data exampleResourceData
|
|
|
+func (r *ExampleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
|
|
+ var data *ExampleResourceModel
|
|
|
|
|
|
- diags := req.State.Get(ctx, &data)
|
|
|
- resp.Diagnostics.Append(diags...)
|
|
|
+ // Read Terraform prior state data into the model
|
|
|
+ resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
|
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
|
return
|
|
@@ -101,21 +121,21 @@ func (r exampleResource) Read(ctx context.Context, req resource.ReadRequest, res
|
|
|
|
|
|
// 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(...)
|
|
|
+ // httpResp, err := d.client.Do(httpReq)
|
|
|
// if err != nil {
|
|
|
// resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read example, got error: %s", err))
|
|
|
// return
|
|
|
// }
|
|
|
|
|
|
- diags = resp.State.Set(ctx, &data)
|
|
|
- resp.Diagnostics.Append(diags...)
|
|
|
+ // Save updated data into Terraform state
|
|
|
+ resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
|
}
|
|
|
|
|
|
-func (r exampleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
|
|
- var data exampleResourceData
|
|
|
+func (r *ExampleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
|
|
+ var data *ExampleResourceModel
|
|
|
|
|
|
- diags := req.Plan.Get(ctx, &data)
|
|
|
- resp.Diagnostics.Append(diags...)
|
|
|
+ // Read Terraform plan data into the model
|
|
|
+ resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
|
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
|
return
|
|
@@ -123,21 +143,21 @@ func (r exampleResource) Update(ctx context.Context, req resource.UpdateRequest,
|
|
|
|
|
|
// 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.UpdateExample(...)
|
|
|
+ // httpResp, err := d.client.Do(httpReq)
|
|
|
// if err != nil {
|
|
|
// resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update example, got error: %s", err))
|
|
|
// return
|
|
|
// }
|
|
|
|
|
|
- diags = resp.State.Set(ctx, &data)
|
|
|
- resp.Diagnostics.Append(diags...)
|
|
|
+ // Save updated data into Terraform state
|
|
|
+ resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
|
}
|
|
|
|
|
|
-func (r exampleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
|
|
- var data exampleResourceData
|
|
|
+func (r *ExampleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
|
|
+ var data *ExampleResourceModel
|
|
|
|
|
|
- diags := req.State.Get(ctx, &data)
|
|
|
- resp.Diagnostics.Append(diags...)
|
|
|
+ // Read Terraform prior state data into the model
|
|
|
+ resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
|
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
|
return
|
|
@@ -145,13 +165,13 @@ func (r exampleResource) Delete(ctx context.Context, req resource.DeleteRequest,
|
|
|
|
|
|
// 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.DeleteExample(...)
|
|
|
+ // httpResp, err := d.client.Do(httpReq)
|
|
|
// if err != nil {
|
|
|
// resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete example, got error: %s", err))
|
|
|
// return
|
|
|
// }
|
|
|
}
|
|
|
|
|
|
-func (r exampleResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
|
|
+func (r *ExampleResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
|
|
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
|
|
|
}
|