Skip to content

Commit

Permalink
Merge pull request #2 from kiwicom/uros/error
Browse files Browse the repository at this point in the history
feat: add error and warning data sources
  • Loading branch information
UrosSimovic authored Mar 11, 2024
2 parents f501bce + b42a37f commit 72e9c75
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.DS_Store
vendor

terraform-provider-kiwi
terraform-provider-misc
bin

.idea
Expand Down
75 changes: 75 additions & 0 deletions misc/error_data_source.go
Original file line number Diff line number Diff line change
@@ -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))
}
3 changes: 2 additions & 1 deletion misc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
74 changes: 74 additions & 0 deletions misc/warning_data_source.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 72e9c75

Please sign in to comment.