example_resource.go 6.1 KB

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