example_resource.notgo 6.0 KB

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