1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package provider
- import (
- "context"
- "log"
- "github.com/hashicorp/terraform-plugin-framework/diag"
- "github.com/hashicorp/terraform-plugin-framework/tfsdk"
- "github.com/hashicorp/terraform-plugin-framework/types"
- )
- // Ensure provider defined types fully satisfy framework interfaces
- var _ tfsdk.DataSourceType = exampleDataSourceType{}
- var _ tfsdk.DataSource = exampleDataSource{}
- type exampleDataSourceType struct{}
- func (t exampleDataSourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
- return tfsdk.Schema{
- // This description is used by the documentation generator and the language server.
- 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 tfsdk.Provider) (tfsdk.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 provider
- }
- func (d exampleDataSource) Read(ctx context.Context, req tfsdk.ReadDataSourceRequest, resp *tfsdk.ReadDataSourceResponse) {
- var data exampleDataSourceData
- diags := req.Config.Get(ctx, &data)
- resp.Diagnostics.Append(diags...)
- log.Printf("got here")
- if resp.Diagnostics.HasError() {
- return
- }
- log.Printf("got here")
- // If applicable, this is a great opportunity to initialize any necessary
- // provider client data and make a call using it.
- // example, err := d.provider.client.ReadExample(...)
- // if err != nil {
- // resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read example, got error: %s", err))
- // return
- // }
- // For the purposes of this example code, hardcoding a response value to
- // save into the Terraform state.
- data.Id = types.String{Value: "example-id"}
- diags = resp.State.Set(ctx, &data)
- resp.Diagnostics.Append(diags...)
- }
|