123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- 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/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 _ resource.Resource = &ExampleResource{}
- var _ resource.ResourceWithImportState = &ExampleResource{}
- 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 (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",
- Attributes: map[string]tfsdk.Attribute{
- "configurable_attribute": {
- MarkdownDescription: "Example configurable attribute",
- Optional: true,
- Type: types.StringType,
- },
- "id": {
- Computed: true,
- MarkdownDescription: "Example identifier",
- PlanModifiers: tfsdk.AttributePlanModifiers{
- resource.UseStateForUnknown(),
- },
- Type: types.StringType,
- },
- },
- }, nil
- }
- 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
- }
- client, ok := req.ProviderData.(*http.Client)
- 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
- }
- r.client = client
- }
- func (r *ExampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
- var data *ExampleResourceModel
- // Read Terraform plan data into the model
- resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
- if resp.Diagnostics.HasError() {
- return
- }
- // If applicable, this is a great opportunity to initialize any necessary
- // provider client data and make a call using it.
- // 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
- // }
- // For the purposes of this example code, hardcoding a response value to
- // 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, "created a resource")
- // 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 *ExampleResourceModel
- // Read Terraform prior state data into the model
- resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
- if resp.Diagnostics.HasError() {
- return
- }
- // If applicable, this is a great opportunity to initialize any necessary
- // provider client data and make a call using it.
- // 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
- // }
- // 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 *ExampleResourceModel
- // Read Terraform plan data into the model
- resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
- if resp.Diagnostics.HasError() {
- return
- }
- // If applicable, this is a great opportunity to initialize any necessary
- // provider client data and make a call using it.
- // 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
- // }
- // 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 *ExampleResourceModel
- // Read Terraform prior state data into the model
- resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
- if resp.Diagnostics.HasError() {
- return
- }
- // If applicable, this is a great opportunity to initialize any necessary
- // provider client data and make a call using it.
- // 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) {
- resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
- }
|