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