example_data_source.go 3.1 KB

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