example_data_source.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package provider
  2. import (
  3. "context"
  4. "github.com/hashicorp/terraform-plugin-framework/datasource"
  5. "github.com/hashicorp/terraform-plugin-framework/diag"
  6. "github.com/hashicorp/terraform-plugin-framework/provider"
  7. "github.com/hashicorp/terraform-plugin-framework/tfsdk"
  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 _ provider.DataSourceType = exampleDataSourceType{}
  13. var _ datasource.DataSource = exampleDataSource{}
  14. type exampleDataSourceType struct{}
  15. func (t exampleDataSourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
  16. return tfsdk.Schema{
  17. // This description is used by the documentation generator and the language server.
  18. MarkdownDescription: "Example data source",
  19. Attributes: map[string]tfsdk.Attribute{
  20. "configurable_attribute": {
  21. MarkdownDescription: "Example configurable attribute",
  22. Optional: true,
  23. Type: types.StringType,
  24. },
  25. "id": {
  26. MarkdownDescription: "Example identifier",
  27. Type: types.StringType,
  28. Computed: true,
  29. },
  30. },
  31. }, nil
  32. }
  33. func (t exampleDataSourceType) NewDataSource(ctx context.Context, in provider.Provider) (datasource.DataSource, diag.Diagnostics) {
  34. provider, diags := convertProviderType(in)
  35. return exampleDataSource{
  36. provider: provider,
  37. }, diags
  38. }
  39. type exampleDataSourceData struct {
  40. ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
  41. Id types.String `tfsdk:"id"`
  42. }
  43. type exampleDataSource struct {
  44. provider scaffoldingProvider
  45. }
  46. func (d exampleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
  47. var data exampleDataSourceData
  48. diags := req.Config.Get(ctx, &data)
  49. resp.Diagnostics.Append(diags...)
  50. if resp.Diagnostics.HasError() {
  51. return
  52. }
  53. // If applicable, this is a great opportunity to initialize any necessary
  54. // provider client data and make a call using it.
  55. // example, err := d.provider.client.ReadExample(...)
  56. // if err != nil {
  57. // resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read example, got error: %s", err))
  58. // return
  59. // }
  60. // For the purposes of this example code, hardcoding a response value to
  61. // save into the Terraform state.
  62. data.Id = types.String{Value: "example-id"}
  63. // Write logs using the tflog package
  64. // Documentation: https://terraform.io/plugin/log
  65. tflog.Trace(ctx, "read a data source")
  66. diags = resp.State.Set(ctx, &data)
  67. resp.Diagnostics.Append(diags...)
  68. }