example_resource.go 5.5 KB

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