example_data_source.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package provider
  2. import (
  3. "context"
  4. "log"
  5. "github.com/hashicorp/terraform-plugin-framework/diag"
  6. "github.com/hashicorp/terraform-plugin-framework/tfsdk"
  7. "github.com/hashicorp/terraform-plugin-framework/types"
  8. )
  9. type exampleDataSourceType struct{}
  10. func (t exampleDataSourceType) 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 data source",
  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. MarkdownDescription: "Example identifier",
  22. Type: types.StringType,
  23. Computed: true,
  24. },
  25. },
  26. }, nil
  27. }
  28. func (t exampleDataSourceType) NewDataSource(ctx context.Context, in tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
  29. provider, diags := convertProviderType(in)
  30. return exampleDataSource{
  31. provider: provider,
  32. }, diags
  33. }
  34. type exampleDataSourceData struct {
  35. ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
  36. Id types.String `tfsdk:"id"`
  37. }
  38. type exampleDataSource struct {
  39. provider provider
  40. }
  41. func (d exampleDataSource) Read(ctx context.Context, req tfsdk.ReadDataSourceRequest, resp *tfsdk.ReadDataSourceResponse) {
  42. var data exampleDataSourceData
  43. diags := req.Config.Get(ctx, &data)
  44. resp.Diagnostics.Append(diags...)
  45. log.Printf("got here")
  46. if resp.Diagnostics.HasError() {
  47. return
  48. }
  49. log.Printf("got here")
  50. // If applicable, this is a great opportunity to initialize any necessary
  51. // provider client data and make a call using it.
  52. // example, err := d.provider.client.ReadExample(...)
  53. // if err != nil {
  54. // resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read example, got error: %s", err))
  55. // return
  56. // }
  57. // For the purposes of this example code, hardcoding a response value to
  58. // save into the Terraform state.
  59. data.Id = types.String{Value: "example-id"}
  60. diags = resp.State.Set(ctx, &data)
  61. resp.Diagnostics.Append(diags...)
  62. }