example_resource.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package provider
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "github.com/hashicorp/terraform-plugin-framework/path"
  7. "github.com/hashicorp/terraform-plugin-framework/resource"
  8. "github.com/hashicorp/terraform-plugin-framework/resource/schema"
  9. "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
  10. "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
  11. "github.com/hashicorp/terraform-plugin-framework/types"
  12. "github.com/hashicorp/terraform-plugin-log/tflog"
  13. )
  14. // Ensure provider defined types fully satisfy framework interfaces.
  15. var _ resource.Resource = &ExampleResource{}
  16. var _ resource.ResourceWithImportState = &ExampleResource{}
  17. func NewExampleResource() resource.Resource {
  18. return &ExampleResource{}
  19. }
  20. // ExampleResource defines the resource implementation.
  21. type ExampleResource struct {
  22. client *http.Client
  23. }
  24. // ExampleResourceModel describes the resource data model.
  25. type ExampleResourceModel struct {
  26. ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
  27. Id types.String `tfsdk:"id"`
  28. }
  29. func (r *ExampleResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
  30. resp.TypeName = req.ProviderTypeName + "_example"
  31. }
  32. func (r *ExampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
  33. resp.Schema = schema.Schema{
  34. // This description is used by the documentation generator and the language server.
  35. MarkdownDescription: "Example resource",
  36. Attributes: map[string]schema.Attribute{
  37. "configurable_attribute": schema.StringAttribute{
  38. MarkdownDescription: "Example configurable attribute",
  39. Optional: true,
  40. },
  41. "id": schema.StringAttribute{
  42. Computed: true,
  43. MarkdownDescription: "Example identifier",
  44. PlanModifiers: []planmodifier.String{
  45. stringplanmodifier.UseStateForUnknown(),
  46. },
  47. },
  48. },
  49. }
  50. }
  51. func (r *ExampleResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
  52. // Prevent panic if the provider has not been configured.
  53. if req.ProviderData == nil {
  54. return
  55. }
  56. client, ok := req.ProviderData.(*http.Client)
  57. if !ok {
  58. resp.Diagnostics.AddError(
  59. "Unexpected Resource Configure Type",
  60. fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
  61. )
  62. return
  63. }
  64. r.client = client
  65. }
  66. func (r *ExampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
  67. var data *ExampleResourceModel
  68. // Read Terraform plan data into the model
  69. resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
  70. if resp.Diagnostics.HasError() {
  71. return
  72. }
  73. // If applicable, this is a great opportunity to initialize any necessary
  74. // provider client data and make a call using it.
  75. // httpResp, err := r.client.Do(httpReq)
  76. // if err != nil {
  77. // resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create example, got error: %s", err))
  78. // return
  79. // }
  80. // For the purposes of this example code, hardcoding a response value to
  81. // save into the Terraform state.
  82. data.Id = types.StringValue("example-id")
  83. // Write logs using the tflog package
  84. // Documentation: https://terraform.io/plugin/log
  85. tflog.Trace(ctx, "created a resource")
  86. // Save data into Terraform state
  87. resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
  88. }
  89. func (r *ExampleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
  90. var data *ExampleResourceModel
  91. // Read Terraform prior state data into the model
  92. resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
  93. if resp.Diagnostics.HasError() {
  94. return
  95. }
  96. // If applicable, this is a great opportunity to initialize any necessary
  97. // provider client data and make a call using it.
  98. // httpResp, err := r.client.Do(httpReq)
  99. // if err != nil {
  100. // resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read example, got error: %s", err))
  101. // return
  102. // }
  103. // Save updated data into Terraform state
  104. resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
  105. }
  106. func (r *ExampleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
  107. var data *ExampleResourceModel
  108. // Read Terraform plan data into the model
  109. resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
  110. if resp.Diagnostics.HasError() {
  111. return
  112. }
  113. // If applicable, this is a great opportunity to initialize any necessary
  114. // provider client data and make a call using it.
  115. // httpResp, err := r.client.Do(httpReq)
  116. // if err != nil {
  117. // resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update example, got error: %s", err))
  118. // return
  119. // }
  120. // Save updated data into Terraform state
  121. resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
  122. }
  123. func (r *ExampleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
  124. var data *ExampleResourceModel
  125. // Read Terraform prior state data into the model
  126. resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
  127. if resp.Diagnostics.HasError() {
  128. return
  129. }
  130. // If applicable, this is a great opportunity to initialize any necessary
  131. // provider client data and make a call using it.
  132. // httpResp, err := r.client.Do(httpReq)
  133. // if err != nil {
  134. // resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete example, got error: %s", err))
  135. // return
  136. // }
  137. }
  138. func (r *ExampleResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
  139. resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
  140. }