example_data_source.go 3.2 KB

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