12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package provider
- import (
- "context"
- "github.com/hashicorp/terraform-plugin-framework/datasource"
- "github.com/hashicorp/terraform-plugin-framework/diag"
- "github.com/hashicorp/terraform-plugin-framework/provider"
- "github.com/hashicorp/terraform-plugin-framework/tfsdk"
- "github.com/hashicorp/terraform-plugin-framework/types"
- "github.com/hashicorp/terraform-plugin-log/tflog"
- )
- var _ provider.DataSourceType = exampleDataSourceType{}
- var _ datasource.DataSource = exampleDataSource{}
- type exampleDataSourceType struct{}
- func (t exampleDataSourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
- return tfsdk.Schema{
-
- MarkdownDescription: "Example data source",
- Attributes: map[string]tfsdk.Attribute{
- "configurable_attribute": {
- MarkdownDescription: "Example configurable attribute",
- Optional: true,
- Type: types.StringType,
- },
- "id": {
- MarkdownDescription: "Example identifier",
- Type: types.StringType,
- Computed: true,
- },
- },
- }, nil
- }
- func (t exampleDataSourceType) NewDataSource(ctx context.Context, in provider.Provider) (datasource.DataSource, diag.Diagnostics) {
- provider, diags := convertProviderType(in)
- return exampleDataSource{
- provider: provider,
- }, diags
- }
- type exampleDataSourceData struct {
- ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
- Id types.String `tfsdk:"id"`
- }
- type exampleDataSource struct {
- provider scaffoldingProvider
- }
- func (d exampleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
- var data exampleDataSourceData
- diags := req.Config.Get(ctx, &data)
- resp.Diagnostics.Append(diags...)
- if resp.Diagnostics.HasError() {
- return
- }
-
-
-
-
-
-
-
-
-
- data.Id = types.String{Value: "example-id"}
-
-
- tflog.Trace(ctx, "read a data source")
- diags = resp.State.Set(ctx, &data)
- resp.Diagnostics.Append(diags...)
- }
|