Skip to content

Commit 613d445

Browse files
committed
Add strict field for strict validation mode
* Defaults to false
1 parent 576998d commit 613d445

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

CHANGES.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ Notable changes between releases.
44

55
## Latest
66

7-
* Allow Fedora CoreOS Config `content`
7+
* Support Fedora CoreOS Config `content` ([#52](https://github.com/poseidon/terraform-provider-ct/pull/52))
88
* Render Container Linux Config `content` in Ignition v2.2 format
9-
* Render Fedora CoreOS `content` in Ignition v3.x format (determined by content)
10-
* Fedora CoreOS Configs specify their [own version](https://github.com/coreos/fcct/blob/master/docs/configuration-v1_0.md)
9+
* Render Fedora CoreOS `content` in Ignition v3.x format (determined by their own [version](https://github.com/coreos/fcct/blob/master/docs/configuration-v1_0.md))
10+
* Add `strict` field for strict validation (defaults to false) ([#53](https://github.com/poseidon/terraform-provider-ct/pull/53))
1111

1212
## v0.3.2
1313

ct/datasource_ct_config.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ func dataSourceCTConfig() *schema.Resource {
4343
Optional: true,
4444
Default: false,
4545
},
46+
"strict": &schema.Schema{
47+
Type: schema.TypeBool,
48+
Optional: true,
49+
Default: false,
50+
},
4651
"rendered": &schema.Schema{
4752
Type: schema.TypeString,
4853
Computed: true,
@@ -70,37 +75,39 @@ func renderConfig(d *schema.ResourceData) (string, error) {
7075
platform := d.Get("platform").(string)
7176
snippetsIface := d.Get("snippets").([]interface{})
7277
pretty := d.Get("pretty_print").(bool)
78+
strict := d.Get("strict").(bool)
7379

7480
snippets := make([]string, len(snippetsIface))
7581
for i := range snippetsIface {
7682
snippets[i] = snippetsIface[i].(string)
7783
}
7884

7985
// Fedora CoreOS Config
80-
ign, err := fccToIgnition([]byte(content), pretty)
86+
ign, err := fccToIgnition([]byte(content), pretty, strict)
8187
if err == fcct.ErrNoVariant {
8288
// consider as Container Linux Config
83-
ign, err = renderCLC([]byte(content), platform, pretty, snippets)
89+
ign, err = renderCLC([]byte(content), platform, pretty, strict, snippets)
8490
}
8591
return string(ign), err
8692
}
8793

8894
// Translate Fedora CoreOS config to Ignition v3.X.Y
89-
func fccToIgnition(data []byte, pretty bool) ([]byte, error) {
95+
func fccToIgnition(data []byte, pretty, strict bool) ([]byte, error) {
9096
return fcct.Translate(data, common.TranslateOptions{
9197
Pretty: pretty,
98+
Strict: strict,
9299
})
93100
}
94101

95102
// Translate Container Linux Config as Ignition JSON.
96-
func renderCLC(data []byte, platform string, pretty bool, snippets []string) ([]byte, error) {
97-
ign, err := clcToIgnition(data, platform)
103+
func renderCLC(data []byte, platform string, pretty, strict bool, snippets []string) ([]byte, error) {
104+
ign, err := clcToIgnition(data, platform, strict)
98105
if err != nil {
99106
return nil, err
100107
}
101108

102109
for _, snippet := range snippets {
103-
ignext, err := clcToIgnition([]byte(snippet), platform)
110+
ignext, err := clcToIgnition([]byte(snippet), platform, strict)
104111
if err != nil {
105112
return nil, err
106113
}
@@ -114,9 +121,14 @@ func renderCLC(data []byte, platform string, pretty bool, snippets []string) ([]
114121
}
115122

116123
// Parse Container Linux config and convert to Ignition v2.2.0 format.
117-
func clcToIgnition(data []byte, platform string) (ignitionTypesV2_2.Config, error) {
124+
func clcToIgnition(data []byte, platform string, strict bool) (ignitionTypesV2_2.Config, error) {
118125
// parse bytes into a Container Linux Config
119126
clc, ast, report := clct.Parse([]byte(data))
127+
128+
if strict && len(report.Entries) > 0 {
129+
return ignitionTypesV2_2.Config{}, fmt.Errorf("error strict parsing Container Linux Config: %v", report.String())
130+
}
131+
120132
if report.IsFatal() {
121133
return ignitionTypesV2_2.Config{}, fmt.Errorf("error parsing Container Linux Config: %v", report.String())
122134
}

ct/datasource_ct_config_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ct
22

33
import (
4+
"regexp"
45
"testing"
56

67
r "github.com/hashicorp/terraform/helper/resource"
@@ -234,6 +235,14 @@ const fedoraCoreOSExpected = `{
234235
"systemd": {}
235236
}`
236237

238+
const invalidResource = `
239+
data "ct_config" "container-linux-strict" {
240+
content = "foo"
241+
strict = true
242+
some_invalid_field = "strict-mode-will-reject"
243+
}
244+
`
245+
237246
func TestRender(t *testing.T) {
238247
r.UnitTest(t, r.TestCase{
239248
Providers: testProviders,
@@ -265,3 +274,15 @@ func TestRender(t *testing.T) {
265274
},
266275
})
267276
}
277+
278+
func TestRenderErrors(t *testing.T) {
279+
r.UnitTest(t, r.TestCase{
280+
Providers: testProviders,
281+
Steps: []r.TestStep{
282+
r.TestStep{
283+
Config: invalidResource,
284+
ExpectError: regexp.MustCompile("^.*An argument named \"some_invalid_field\" is not expected here"),
285+
},
286+
},
287+
})
288+
}

0 commit comments

Comments
 (0)