example_resource.go 4.6 KB

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