example_resource.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package provider
  2. import (
  3. "context"
  4. "github.com/hashicorp/terraform-plugin-framework/diag"
  5. "github.com/hashicorp/terraform-plugin-framework/path"
  6. "github.com/hashicorp/terraform-plugin-framework/provider"
  7. "github.com/hashicorp/terraform-plugin-framework/resource"
  8. "github.com/hashicorp/terraform-plugin-framework/tfsdk"
  9. "github.com/hashicorp/terraform-plugin-framework/types"
  10. "github.com/hashicorp/terraform-plugin-log/tflog"
  11. )
  12. // Ensure provider defined types fully satisfy framework interfaces
  13. var _ provider.ResourceType = exampleResourceType{}
  14. var _ resource.Resource = exampleResource{}
  15. var _ resource.ResourceWithImportState = exampleResource{}
  16. type exampleResourceType struct{}
  17. func (t exampleResourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
  18. return tfsdk.Schema{
  19. // This description is used by the documentation generator and the language server.
  20. MarkdownDescription: "Example resource",
  21. Attributes: map[string]tfsdk.Attribute{
  22. "configurable_attribute": {
  23. MarkdownDescription: "Example configurable attribute",
  24. Optional: true,
  25. Type: types.StringType,
  26. },
  27. "id": {
  28. Computed: true,
  29. MarkdownDescription: "Example identifier",
  30. PlanModifiers: tfsdk.AttributePlanModifiers{
  31. resource.UseStateForUnknown(),
  32. },
  33. Type: types.StringType,
  34. },
  35. },
  36. }, nil
  37. }
  38. func (t exampleResourceType) NewResource(ctx context.Context, in provider.Provider) (resource.Resource, diag.Diagnostics) {
  39. provider, diags := convertProviderType(in)
  40. return exampleResource{
  41. provider: provider,
  42. }, diags
  43. }
  44. type exampleResourceData struct {
  45. ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
  46. Id types.String `tfsdk:"id"`
  47. }
  48. type exampleResource struct {
  49. provider scaffoldingProvider
  50. }
  51. func (r exampleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
  52. var data exampleResourceData
  53. diags := req.Config.Get(ctx, &data)
  54. resp.Diagnostics.Append(diags...)
  55. if resp.Diagnostics.HasError() {
  56. return
  57. }
  58. // If applicable, this is a great opportunity to initialize any necessary
  59. // provider client data and make a call using it.
  60. // example, err := d.provider.client.CreateExample(...)
  61. // if err != nil {
  62. // resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create example, got error: %s", err))
  63. // return
  64. // }
  65. // For the purposes of this example code, hardcoding a response value to
  66. // save into the Terraform state.
  67. data.Id = types.String{Value: "example-id"}
  68. // Write logs using the tflog package
  69. // Documentation: https://terraform.io/plugin/log
  70. tflog.Trace(ctx, "created a resource")
  71. diags = resp.State.Set(ctx, &data)
  72. resp.Diagnostics.Append(diags...)
  73. }
  74. func (r exampleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
  75. var data exampleResourceData
  76. diags := req.State.Get(ctx, &data)
  77. resp.Diagnostics.Append(diags...)
  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. // example, err := d.provider.client.ReadExample(...)
  84. // if err != nil {
  85. // resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read example, got error: %s", err))
  86. // return
  87. // }
  88. diags = resp.State.Set(ctx, &data)
  89. resp.Diagnostics.Append(diags...)
  90. }
  91. func (r exampleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
  92. var data exampleResourceData
  93. diags := req.Plan.Get(ctx, &data)
  94. resp.Diagnostics.Append(diags...)
  95. if resp.Diagnostics.HasError() {
  96. return
  97. }
  98. // If applicable, this is a great opportunity to initialize any necessary
  99. // provider client data and make a call using it.
  100. // example, err := d.provider.client.UpdateExample(...)
  101. // if err != nil {
  102. // resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update example, got error: %s", err))
  103. // return
  104. // }
  105. diags = resp.State.Set(ctx, &data)
  106. resp.Diagnostics.Append(diags...)
  107. }
  108. func (r exampleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
  109. var data exampleResourceData
  110. diags := req.State.Get(ctx, &data)
  111. resp.Diagnostics.Append(diags...)
  112. if resp.Diagnostics.HasError() {
  113. return
  114. }
  115. // If applicable, this is a great opportunity to initialize any necessary
  116. // provider client data and make a call using it.
  117. // example, err := d.provider.client.DeleteExample(...)
  118. // if err != nil {
  119. // resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete example, got error: %s", err))
  120. // return
  121. // }
  122. }
  123. func (r exampleResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
  124. resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
  125. }