From b42a37f38b77f783dd19e3cd18e469de58bcd7fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uro=C5=A1=20Simovi=C4=87?= Date: Mon, 11 Mar 2024 10:21:26 +0100 Subject: [PATCH] feat: add error and warning data sources --- .gitignore | 2 +- misc/error_data_source.go | 75 +++++++++++++++++++++++++++++++++++++ misc/provider.go | 3 +- misc/warning_data_source.go | 74 ++++++++++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 misc/error_data_source.go create mode 100644 misc/warning_data_source.go diff --git a/.gitignore b/.gitignore index 4229c61..f26ef6c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ .DS_Store vendor -terraform-provider-kiwi +terraform-provider-misc bin .idea diff --git a/misc/error_data_source.go b/misc/error_data_source.go new file mode 100644 index 0000000..271335f --- /dev/null +++ b/misc/error_data_source.go @@ -0,0 +1,75 @@ +package misc + +import ( + "context" + "time" + + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" +) + +var _ datasource.DataSource = &ErrorDataSource{} + +func NewErrorDataSource() datasource.DataSource { + return &ErrorDataSource{} +} + +// ErrorDataSource defines the data source implementation. +type ErrorDataSource struct{} + +// ErrorDataSourceModel describes the data source data model. +type ErrorDataSourceModel struct { + Id types.String `tfsdk:"id"` + Condition types.Bool `tfsdk:"condition"` + Summary types.String `tfsdk:"summary"` + Details types.String `tfsdk:"details"` +} + +func (d *ErrorDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_error" +} + +func (d *ErrorDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + // This description is used by the documentation generator and the language server. + MarkdownDescription: "Error data source", + + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + MarkdownDescription: "Error identifier", + Computed: true, + }, + "condition": schema.BoolAttribute{ + MarkdownDescription: "Error condition", + Required: true, + }, + "summary": schema.StringAttribute{ + MarkdownDescription: "Error message summary", + Required: true, + }, + "details": schema.StringAttribute{ + MarkdownDescription: "Error message details", + Optional: true, + }, + }, + } +} + +func (d *ErrorDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var data ErrorDataSourceModel + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) + if resp.Diagnostics.HasError() { + return + } + + if !data.Condition.ValueBool() { + var details string + if !data.Details.IsNull() { + details = data.Details.String() + } + resp.Diagnostics.AddError(data.Summary.String(), details) + } + + types.StringValue(time.Now().Format(time.RFC3339Nano)) +} diff --git a/misc/provider.go b/misc/provider.go index 964d0e2..53c2f33 100644 --- a/misc/provider.go +++ b/misc/provider.go @@ -43,7 +43,8 @@ func (p *kiwiProvider) Configure(ctx context.Context, req provider.ConfigureRequ // DataSources defines the data sources implemented in the provider. func (p *kiwiProvider) DataSources(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ - //NewCoffeesDataSource, + NewErrorDataSource, + NewWarningDataSource, } } diff --git a/misc/warning_data_source.go b/misc/warning_data_source.go new file mode 100644 index 0000000..519ae16 --- /dev/null +++ b/misc/warning_data_source.go @@ -0,0 +1,74 @@ +package misc + +import ( + "context" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" +) + +var _ datasource.DataSource = &WarningDataSource{} + +func NewWarningDataSource() datasource.DataSource { + return &WarningDataSource{} +} + +// WarningDataSource defines the data source implementation. +type WarningDataSource struct{} + +// WarningDataSourceModel describes the data source data model. +type WarningDataSourceModel struct { + Id types.String `tfsdk:"id"` + Condition types.Bool `tfsdk:"condition"` + Summary types.String `tfsdk:"summary"` + Details types.String `tfsdk:"details"` +} + +func (d *WarningDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_warning" +} + +func (d *WarningDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + // This description is used by the documentation generator and the language server. + MarkdownDescription: "Error data source", + + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + MarkdownDescription: "Error identifier", + Computed: true, + }, + "condition": schema.BoolAttribute{ + MarkdownDescription: "Error condition", + Required: true, + }, + "summary": schema.StringAttribute{ + MarkdownDescription: "Error message summary", + Required: true, + }, + "details": schema.StringAttribute{ + MarkdownDescription: "Error message details", + Optional: true, + }, + }, + } +} + +func (d *WarningDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var data ErrorDataSourceModel + + // Read Terraform configuration data into the model + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) + + if resp.Diagnostics.HasError() { + return + } + + if !data.Condition.ValueBool() { + var details string + if !data.Details.IsNull() { + details = data.Details.String() + } + resp.Diagnostics.AddWarning(data.Summary.String(), details) + } +}