example_resource.go 4.4 KB

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