-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from kiwicom/uros/error
feat: add error and warning data sources
- Loading branch information
Showing
4 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |