Skip to content

Commit

Permalink
feat(blocks): adds validation_warnings data source
Browse files Browse the repository at this point in the history
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
tlkamp committed Jan 1, 2024
1 parent 3a425d2 commit ae58469
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 3 deletions.
36 changes: 36 additions & 0 deletions docs/data-sources/warnings.md
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.
31 changes: 31 additions & 0 deletions validation/data_source_warnings.go
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)
}
54 changes: 54 additions & 0 deletions validation/data_source_warnings_test.go
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()
}
7 changes: 4 additions & 3 deletions validation/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ func Provider() *schema.Provider {
"validation_warnings": resourceWarnings(),
},
DataSourcesMap: map[string]*schema.Resource{
"validation_error": dataSourceError(),
"validation_errors": dataSourceErrors(),
"validation_warning": dataSourceWarning(),
"validation_error": dataSourceError(),
"validation_errors": dataSourceErrors(),
"validation_warning": dataSourceWarning(),
"validation_warnings": dataSourceWarnings(),
},
}
}
Expand Down

0 comments on commit ae58469

Please sign in to comment.