Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 338fe14

Browse files
authored
Add support for write-only attributes (#146)
1 parent 1a30a75 commit 338fe14

File tree

3 files changed

+46
-46
lines changed

3 files changed

+46
-46
lines changed

schemas.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ type SchemaAttribute struct {
230230
// in logs. Future versions of Terraform may encrypt or otherwise
231231
// treat these values with greater care than non-sensitive fields.
232232
Sensitive bool `json:"sensitive,omitempty"`
233+
234+
// If true, this attribute is write only and its value will not be
235+
// persisted in artifacts such as plan files or state.
236+
WriteOnly bool `json:"write_only,omitempty"`
233237
}
234238

235239
// jsonSchemaAttribute describes an attribute within a schema block
@@ -249,6 +253,7 @@ type jsonSchemaAttribute struct {
249253
Optional bool `json:"optional,omitempty"`
250254
Computed bool `json:"computed,omitempty"`
251255
Sensitive bool `json:"sensitive,omitempty"`
256+
WriteOnly bool `json:"write_only,omitempty"`
252257
}
253258

254259
func (as *SchemaAttribute) MarshalJSON() ([]byte, error) {
@@ -261,6 +266,7 @@ func (as *SchemaAttribute) MarshalJSON() ([]byte, error) {
261266
Optional: as.Optional,
262267
Computed: as.Computed,
263268
Sensitive: as.Sensitive,
269+
WriteOnly: as.WriteOnly,
264270
}
265271
if as.AttributeType != cty.NilType {
266272
attrTy, _ := as.AttributeType.MarshalJSON()

schemas_test.go

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,47 @@ import (
1010
)
1111

1212
func TestProviderSchemasValidate(t *testing.T) {
13-
f, err := os.Open("testdata/basic/schemas.json")
14-
if err != nil {
15-
t.Fatal(err)
13+
cases := map[string]struct {
14+
testDataPath string
15+
}{
16+
"a basic provider schema is validated": {
17+
testDataPath: "testdata/basic/schemas.json",
18+
},
19+
"a provider schema including functions is validated": {
20+
testDataPath: "testdata/functions/schemas.json",
21+
},
22+
"a provider schema including ephemeral resources is validated": {
23+
testDataPath: "testdata/ephemeral_resources/schemas.json",
24+
},
25+
"a provider schema including a resource with write-only attribute(s) is validated": {
26+
testDataPath: "testdata/write_only_attribute_on_resource/schemas.json",
27+
},
1628
}
17-
defer f.Close()
1829

19-
var schemas *ProviderSchemas
20-
if err := json.NewDecoder(f).Decode(&schemas); err != nil {
21-
t.Fatal(err)
22-
}
23-
24-
if err := schemas.Validate(); err != nil {
25-
t.Fatal(err)
26-
}
27-
}
30+
for tn, tc := range cases {
31+
t.Run(tn, func(t *testing.T) {
32+
f, err := os.Open(tc.testDataPath)
33+
if err != nil {
34+
t.Fatal(err)
35+
}
36+
defer f.Close()
2837

29-
func TestProviderSchemasValidate_functions(t *testing.T) {
30-
f, err := os.Open("testdata/functions/schemas.json")
31-
if err != nil {
32-
t.Fatal(err)
33-
}
34-
defer f.Close()
38+
var schemas *ProviderSchemas
39+
if err := json.NewDecoder(f).Decode(&schemas); err != nil {
40+
t.Fatal(err)
41+
}
3542

36-
var schemas *ProviderSchemas
37-
if err := json.NewDecoder(f).Decode(&schemas); err != nil {
38-
t.Fatal(err)
39-
}
40-
41-
if err := schemas.Validate(); err != nil {
42-
t.Fatal(err)
43+
if err := schemas.Validate(); err != nil {
44+
t.Fatal(err)
45+
}
46+
})
4347
}
4448
}
4549

46-
func TestProviderSchemasValidate_ephemeralResources(t *testing.T) {
47-
f, err := os.Open("testdata/ephemeral_resources/schemas.json")
50+
// TestProviderSchemas_writeOnlyAttribute asserts that write-only attributes in a resource in a
51+
// provider schema JSON file are marked as WriteOnly once decoded into a ProviderSchemas struct
52+
func TestProviderSchemas_writeOnlyAttribute(t *testing.T) {
53+
f, err := os.Open("testdata/write_only_attribute_on_resource/schemas.json")
4854
if err != nil {
4955
t.Fatal(err)
5056
}
@@ -55,24 +61,11 @@ func TestProviderSchemasValidate_ephemeralResources(t *testing.T) {
5561
t.Fatal(err)
5662
}
5763

58-
if err := schemas.Validate(); err != nil {
59-
t.Fatal(err)
60-
}
61-
}
62-
63-
func TestProviderSchemasValidate_nestedAttributes(t *testing.T) {
64-
f, err := os.Open("testdata/nested_attributes/schemas.json")
65-
if err != nil {
66-
t.Fatal(err)
64+
resourceSchema := schemas.Schemas["terraform.io/builtin/terraform"].ResourceSchemas["terraform_example"]
65+
if resourceSchema.Block.Attributes["wo_attr"].WriteOnly != true {
66+
t.Fatal("expected terraform_example.wo_attr to be marked as write-only")
6767
}
68-
defer f.Close()
69-
70-
var schemas *ProviderSchemas
71-
if err := json.NewDecoder(f).Decode(&schemas); err != nil {
72-
t.Fatal(err)
73-
}
74-
75-
if err := schemas.Validate(); err != nil {
76-
t.Fatal(err)
68+
if resourceSchema.Block.Attributes["foo"].WriteOnly != false {
69+
t.Fatal("expected terraform_example.foo to not be marked as write-only")
7770
}
7871
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"format_version":"1.0","provider_schemas":{"terraform.io/builtin/terraform":{"provider":{"version":0},"resource_schemas":{"terraform_example":{"version":0,"block":{"attributes":{"foo":{"type":"string","description_kind":"plain","optional":true},"wo_attr":{"type":"string","description_kind":"plain","optional":true,"write_only":true}},"description_kind":"plain"}}}}}}

0 commit comments

Comments
 (0)