-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(blocks): adds validation_warnings data source
Adds a validation_warnings data source, which accepts a list of at least one warning{} block. The warning schema is identical to the validation_warning resource and data source schema. See #1
- Loading branch information
Showing
4 changed files
with
125 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "validation_warnings Data Source - terraform-provider-validation" | ||
subcategory: "" | ||
description: |- | ||
Causes one or more warnings to be shown if the condition is true. | ||
--- | ||
|
||
# validation_warnings (Data Source) | ||
|
||
Causes one or more warnings to be shown if the condition is true. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `warning` (Block List, Min: 1) (see [below for nested schema](#nestedblock--warning)) | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
|
||
<a id="nestedblock--warning"></a> | ||
### Nested Schema for `warning` | ||
|
||
Required: | ||
|
||
- `condition` (Boolean) The condition which, if true, causes a warning message to be printed. | ||
- `summary` (String) The message displayed to the user if the condition is true. | ||
|
||
Optional: | ||
|
||
- `details` (String) More details about the message being displayed to the user, if any. |
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,31 @@ | ||
package validation | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceWarnings() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Causes one or more warnings to be shown if the condition is true.", | ||
ReadContext: dataSourceWarningsRead, | ||
Schema: map[string]*schema.Schema{ | ||
"warning": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Resource{ | ||
Schema: warnDataSchema, | ||
}, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceWarningsRead(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
data.SetId("none") | ||
warnings := data.Get("warning") | ||
warnList := warnings.([]interface{}) | ||
return parseWarnings(warnList) | ||
} |
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,54 @@ | ||
package validation | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
"text/template" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
const dataSrcWarns = "data.validation_warnings.warns" | ||
|
||
func TestDataSourceWarningsBasic(t *testing.T) { | ||
vs := []testValidation{ | ||
{Condition: true, Summary: "summary"}, | ||
{Condition: true, Summary: "Summary2", Details: "with details"}, | ||
{}, | ||
} | ||
|
||
resource.Test(t, resource.TestCase{ | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataValidationWarningsConfig(t, vs), | ||
Check: resource.ComposeTestCheckFunc(testAccCheckResourceExists(dataSrcWarns)), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataValidationWarningsConfig(t *testing.T, v []testValidation) string { | ||
t.Helper() | ||
|
||
rsc := `data "validation_warnings" "warns" { | ||
{{- range . }} | ||
warning { | ||
condition = {{ .Condition }} | ||
summary = "{{ .Summary }}" | ||
{{ if .Details }}details = "{{ .Details }}"{{ end }} | ||
} | ||
{{ end }} | ||
} | ||
` | ||
|
||
tmpl := template.New("test") | ||
out := &bytes.Buffer{} | ||
parsed, _ := tmpl.Parse(rsc) | ||
|
||
if err := parsed.Execute(out, v); err != nil { | ||
t.Error(err) | ||
} | ||
|
||
return out.String() | ||
} |
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