Skip to content

Commit

Permalink
Add support for write-only attributes (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
SarahFrench authored Dec 12, 2024
1 parent 1a30a75 commit 338fe14
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 46 deletions.
6 changes: 6 additions & 0 deletions schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ type SchemaAttribute struct {
// in logs. Future versions of Terraform may encrypt or otherwise
// treat these values with greater care than non-sensitive fields.
Sensitive bool `json:"sensitive,omitempty"`

// If true, this attribute is write only and its value will not be
// persisted in artifacts such as plan files or state.
WriteOnly bool `json:"write_only,omitempty"`
}

// jsonSchemaAttribute describes an attribute within a schema block
Expand All @@ -249,6 +253,7 @@ type jsonSchemaAttribute struct {
Optional bool `json:"optional,omitempty"`
Computed bool `json:"computed,omitempty"`
Sensitive bool `json:"sensitive,omitempty"`
WriteOnly bool `json:"write_only,omitempty"`
}

func (as *SchemaAttribute) MarshalJSON() ([]byte, error) {
Expand All @@ -261,6 +266,7 @@ func (as *SchemaAttribute) MarshalJSON() ([]byte, error) {
Optional: as.Optional,
Computed: as.Computed,
Sensitive: as.Sensitive,
WriteOnly: as.WriteOnly,
}
if as.AttributeType != cty.NilType {
attrTy, _ := as.AttributeType.MarshalJSON()
Expand Down
85 changes: 39 additions & 46 deletions schemas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,47 @@ import (
)

func TestProviderSchemasValidate(t *testing.T) {
f, err := os.Open("testdata/basic/schemas.json")
if err != nil {
t.Fatal(err)
cases := map[string]struct {
testDataPath string
}{
"a basic provider schema is validated": {
testDataPath: "testdata/basic/schemas.json",
},
"a provider schema including functions is validated": {
testDataPath: "testdata/functions/schemas.json",
},
"a provider schema including ephemeral resources is validated": {
testDataPath: "testdata/ephemeral_resources/schemas.json",
},
"a provider schema including a resource with write-only attribute(s) is validated": {
testDataPath: "testdata/write_only_attribute_on_resource/schemas.json",
},
}
defer f.Close()

var schemas *ProviderSchemas
if err := json.NewDecoder(f).Decode(&schemas); err != nil {
t.Fatal(err)
}

if err := schemas.Validate(); err != nil {
t.Fatal(err)
}
}
for tn, tc := range cases {
t.Run(tn, func(t *testing.T) {
f, err := os.Open(tc.testDataPath)
if err != nil {
t.Fatal(err)
}
defer f.Close()

func TestProviderSchemasValidate_functions(t *testing.T) {
f, err := os.Open("testdata/functions/schemas.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
var schemas *ProviderSchemas
if err := json.NewDecoder(f).Decode(&schemas); err != nil {
t.Fatal(err)
}

var schemas *ProviderSchemas
if err := json.NewDecoder(f).Decode(&schemas); err != nil {
t.Fatal(err)
}

if err := schemas.Validate(); err != nil {
t.Fatal(err)
if err := schemas.Validate(); err != nil {
t.Fatal(err)
}
})
}
}

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

if err := schemas.Validate(); err != nil {
t.Fatal(err)
}
}

func TestProviderSchemasValidate_nestedAttributes(t *testing.T) {
f, err := os.Open("testdata/nested_attributes/schemas.json")
if err != nil {
t.Fatal(err)
resourceSchema := schemas.Schemas["terraform.io/builtin/terraform"].ResourceSchemas["terraform_example"]
if resourceSchema.Block.Attributes["wo_attr"].WriteOnly != true {
t.Fatal("expected terraform_example.wo_attr to be marked as write-only")
}
defer f.Close()

var schemas *ProviderSchemas
if err := json.NewDecoder(f).Decode(&schemas); err != nil {
t.Fatal(err)
}

if err := schemas.Validate(); err != nil {
t.Fatal(err)
if resourceSchema.Block.Attributes["foo"].WriteOnly != false {
t.Fatal("expected terraform_example.foo to not be marked as write-only")
}
}
1 change: 1 addition & 0 deletions testdata/write_only_attribute_on_resource/schemas.json
Original file line number Diff line number Diff line change
@@ -0,0 +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 comments on commit 338fe14

Please sign in to comment.