Brian Flad a64a312802 Initial commit
Most of the content is inspired by existing content in https://github.com/hashicorp/terraform-provider-scaffolding.
2021-12-01 05:37:16 -05:00

80 lines
2.2 KiB
Go

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"
)
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...)
}