example_data_source.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package provider
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "github.com/hashicorp/terraform-plugin-framework/datasource"
  7. "github.com/hashicorp/terraform-plugin-framework/diag"
  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 _ datasource.DataSource = &ExampleDataSource{}
  14. func NewExampleDataSource() datasource.DataSource {
  15. return &ExampleDataSource{}
  16. }
  17. // ExampleDataSource defines the data source implementation.
  18. type ExampleDataSource struct {
  19. client *http.Client
  20. }
  21. // ExampleDataSourceModel describes the data source data model.
  22. type ExampleDataSourceModel struct {
  23. ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
  24. Id types.String `tfsdk:"id"`
  25. }
  26. func (d *ExampleDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
  27. resp.TypeName = req.ProviderTypeName + "_example"
  28. }
  29. func (d *ExampleDataSource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
  30. return tfsdk.Schema{
  31. // This description is used by the documentation generator and the language server.
  32. MarkdownDescription: "Example data source",
  33. Attributes: map[string]tfsdk.Attribute{
  34. "configurable_attribute": {
  35. MarkdownDescription: "Example configurable attribute",
  36. Optional: true,
  37. Type: types.StringType,
  38. },
  39. "id": {
  40. MarkdownDescription: "Example identifier",
  41. Type: types.StringType,
  42. Computed: true,
  43. },
  44. },
  45. }, nil
  46. }
  47. func (d *ExampleDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
  48. // Prevent panic if the provider has not been configured.
  49. if req.ProviderData == nil {
  50. return
  51. }
  52. client, ok := req.ProviderData.(*http.Client)
  53. if !ok {
  54. resp.Diagnostics.AddError(
  55. "Unexpected Data Source Configure Type",
  56. fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
  57. )
  58. return
  59. }
  60. d.client = client
  61. }
  62. func (d *ExampleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
  63. var data ExampleDataSourceModel
  64. // Read Terraform configuration data into the model
  65. resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
  66. if resp.Diagnostics.HasError() {
  67. return
  68. }
  69. // If applicable, this is a great opportunity to initialize any necessary
  70. // provider client data and make a call using it.
  71. // httpResp, err := d.client.Do(httpReq)
  72. // if err != nil {
  73. // resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read example, got error: %s", err))
  74. // return
  75. // }
  76. // For the purposes of this example code, hardcoding a response value to
  77. // save into the Terraform state.
  78. data.Id = types.String{Value: "example-id"}
  79. // Write logs using the tflog package
  80. // Documentation: https://terraform.io/plugin/log
  81. tflog.Trace(ctx, "read a data source")
  82. // Save data into Terraform state
  83. resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
  84. }