example_resource.go 4.8 KB

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