Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added write-only support to generate #434

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ example resource
- `single_nested_block` (Block, Optional) example single nested block (see [below for nested schema](#nestedblock--single_nested_block))
- `single_nested_block_sensitive_nested_attribute` (Block, Optional) example sensitive single nested block (see [below for nested schema](#nestedblock--single_nested_block_sensitive_nested_attribute))
- `string_attribute` (String) example string attribute
- `write_only_string_attribute` (String, Write-only) example write-only string attribute

### Read-Only

Expand Down Expand Up @@ -418,6 +419,13 @@ scaffolding(stringInput string, boolInput bool, float64Input number, int64Input
"description": "example string attribute",
"description_kind": "markdown",
"optional": true
},
"write_only_string_attribute": {
"type": "string",
"description": "example write-only string attribute",
"description_kind": "markdown",
"optional": true,
"write_only": true
}
},
"block_types": {
Expand Down
2 changes: 1 addition & 1 deletion internal/schemamd/behaviors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func childAttributeIsOptional(att *tfjson.SchemaAttribute) bool {
return att.Optional
}

// childBlockIsOptional returns true for blocks with with min items 0
// childBlockIsOptional returns true for blocks with min items 0
// which are either empty or have any required or optional children.
func childBlockIsOptional(block *tfjson.SchemaBlockType) bool {
if block.MinItems > 0 {
Expand Down
2 changes: 1 addition & 1 deletion internal/schemamd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type groupFilter struct {
}

var (
// Attributes and Blocks are in one of 3 characteristic groups:
// Attributes and Blocks are in one of 4 characteristic groups:
// * Required
// * Optional
// * Read-Only
Expand Down
5 changes: 5 additions & 0 deletions internal/schemamd/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func TestRender(t *testing.T) {
"testdata/deep_nested_attributes.schema.json",
"testdata/deep_nested_attributes.md",
},
{
"deep_nested_write_only_attributes",
"testdata/deep_nested_write_only_attributes.schema.json",
"testdata/deep_nested_write_only_attributes.md",
},
} {
c := c
t.Run(c.name, func(t *testing.T) {
Expand Down
46 changes: 46 additions & 0 deletions internal/schemamd/testdata/deep_nested_write_only_attributes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## Schema

### Required

- `level_one` (Attributes) (see [below for nested schema](#nestedatt--level_one))

### Read-Only

- `id` (String) Example identifier

<a id="nestedatt--level_one"></a>
### Nested Schema for `level_one`

Optional:

- `level_two` (Attributes, Write-only) (see [below for nested schema](#nestedatt--level_one--level_two))

<a id="nestedatt--level_one--level_two"></a>
### Nested Schema for `level_one.level_two`

Optional:

- `level_three` (Attributes, Write-only) (see [below for nested schema](#nestedatt--level_one--level_two--level_three))

<a id="nestedatt--level_one--level_two--level_three"></a>
### Nested Schema for `level_one.level_two.level_three`

Optional:

- `level_four_primary` (Attributes, Write-only) (see [below for nested schema](#nestedatt--level_one--level_two--level_three--level_four_primary))
- `level_four_secondary` (String, Write-only)

<a id="nestedatt--level_one--level_two--level_three--level_four_primary"></a>
### Nested Schema for `level_one.level_two.level_three.level_four_primary`

Optional:

- `level_five` (Attributes, Write-only) Parent should be level_one.level_two.level_three.level_four_primary. (see [below for nested schema](#nestedatt--level_one--level_two--level_three--level_four_primary--level_five))
- `level_four_primary_string` (String, Write-only) Parent should be level_one.level_two.level_three.level_four_primary.

<a id="nestedatt--level_one--level_two--level_three--level_four_primary--level_five"></a>
### Nested Schema for `level_one.level_two.level_three.level_four_primary.level_five`

Optional:

- `level_five_string` (String, Write-only) Parent should be level_one.level_two.level_three.level_four_primary.level_five.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"version": 0,
"block": {
"attributes": {
"id": {
"type": "string",
"description": "Example identifier",
"description_kind": "markdown",
"computed": true
},
"level_one": {
"nested_type": {
"attributes": {
"level_two": {
"nested_type": {
"attributes": {
"level_three": {
"nested_type": {
"attributes": {
"level_four_primary": {
"nested_type": {
"attributes": {
"level_five": {
"nested_type": {
"attributes": {
"level_five_string": {
"type": "string",
"description": "Parent should be level_one.level_two.level_three.level_four_primary.level_five.",
"description_kind": "plain",
"optional": true,
"write_only": true
}
},
"nesting_mode": "single"
},
"description": "Parent should be level_one.level_two.level_three.level_four_primary.",
"description_kind": "plain",
"optional": true,
"write_only": true
},
"level_four_primary_string": {
"type": "string",
"description": "Parent should be level_one.level_two.level_three.level_four_primary.",
"description_kind": "plain",
"optional": true,
"write_only": true
}
},
"nesting_mode": "single"
},
"description_kind": "plain",
"optional": true,
"write_only": true
},
"level_four_secondary": {
"type": "string",
"description_kind": "plain",
"optional": true,
"write_only": true
}
},
"nesting_mode": "single"
},
"description_kind": "plain",
"optional": true,
"write_only": true
}
},
"nesting_mode": "single"
},
"description_kind": "plain",
"optional": true,
"write_only": true
}
},
"nesting_mode": "single"
},
"description_kind": "plain",
"required": true
}
},
"description": "Example resource",
"description_kind": "markdown"
}
}
5 changes: 4 additions & 1 deletion internal/schemamd/testdata/framework_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- `single_nested_block` (Block, Optional) example single nested block (see [below for nested schema](#nestedblock--single_nested_block))
- `single_nested_block_sensitive_nested_attribute` (Block, Optional) example sensitive single nested block (see [below for nested schema](#nestedblock--single_nested_block_sensitive_nested_attribute))
- `string_attribute` (String) example string attribute
- `write_only_string_attribute` (String, Write-only) example write only string attribute

### Read-Only

Expand All @@ -39,6 +40,7 @@ Optional:

- `list_nested_block_attribute` (String) example list nested block attribute
- `list_nested_block_attribute_with_default` (String) example list nested block attribute with default
- `list_nested_block_write_only_attribute` (String, Write-only) example list nested block write-only attribute
- `nested_list_block` (Block List) (see [below for nested schema](#nestedblock--list_nested_block--nested_list_block))

<a id="nestedblock--list_nested_block--nested_list_block"></a>
Expand Down Expand Up @@ -98,6 +100,7 @@ Optional:
Optional:

- `set_nested_block_attribute` (String) example set nested block attribute
- `set_nested_block_write_only_attribute` (String, Write-only) example set nested block write-only attribute


<a id="nestedblock--single_nested_block"></a>
Expand All @@ -123,4 +126,4 @@ Optional:
Read-Only:

- `set_nested_block_attribute` (String) example set nested block attribute
- `set_nested_block_sensitive_attribute` (String, Sensitive) example sensitive set nested block attribute
- `set_nested_block_sensitive_attribute` (String, Sensitive) example sensitive set nested block attribute
21 changes: 21 additions & 0 deletions internal/schemamd/testdata/framework_types.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@
"description": "example string attribute",
"description_kind": "markdown",
"optional": true
},
"write_only_string_attribute": {
"type": "string",
"description": "example write only string attribute",
"description_kind": "markdown",
"optional": true,
"write_only": true
}
},
"block_types": {
Expand All @@ -181,6 +188,13 @@
"description_kind": "markdown",
"optional": true
},
"list_nested_block_write_only_attribute": {
"type": "string",
"description": "example list nested block write-only attribute",
"description_kind": "markdown",
"optional": true,
"write_only": true
},
"list_nested_block_attribute_with_default": {
"type": "string",
"description": "example list nested block attribute with default",
Expand Down Expand Up @@ -239,6 +253,13 @@
"description": "example set nested block attribute",
"description_kind": "markdown",
"optional": true
},
"set_nested_block_write_only_attribute": {
"type": "string",
"description": "example set nested block write-only attribute",
"description_kind": "markdown",
"optional": true,
"write_only": true
}
},
"description": "example set nested block",
Expand Down
7 changes: 7 additions & 0 deletions internal/schemamd/write_attribute_description.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ func WriteAttributeDescription(w io.Writer, att *tfjson.SchemaAttribute, include
}
}

if att.WriteOnly {
_, err := io.WriteString(w, ", Write-only")
if err != nil {
return err
}
}

_, err = io.WriteString(w, ")")
if err != nil {
return err
Expand Down
9 changes: 9 additions & 0 deletions internal/schemamd/write_attribute_description_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ func TestWriteAttributeDescription(t *testing.T) {
Description: "This is an attribute.",
},
},
{
"(String, Required, Write-only) This is an attribute.",
&tfjson.SchemaAttribute{
AttributeType: cty.String,
Required: true,
Description: "This is an attribute.",
WriteOnly: true,
},
},
{
"(String, Required, Deprecated) This is an attribute.",
&tfjson.SchemaAttribute{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ func WriteNestedAttributeTypeDescription(w io.Writer, att *tfjson.SchemaAttribut
}
}

if att.WriteOnly {
_, err := io.WriteString(w, ", Write-only")
if err != nil {
return err
}
}

_, err = io.WriteString(w, ")")
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ func TestWriteNestedAttributeTypeDescription(t *testing.T) {
Optional: true,
},
},
{
"(Attributes, Optional, Write-only) This is an attribute.",
&tfjson.SchemaAttribute{
Description: "This is an attribute.",
AttributeNestedType: &tfjson.SchemaNestedAttributeType{
NestingMode: tfjson.SchemaNestingModeSingle,
Attributes: map[string]*tfjson.SchemaAttribute{
"foo": {
AttributeType: cty.String,
Required: true,
Description: "This is a nested attribute.",
},
},
},
Optional: true,
WriteOnly: true,
},
},
{
"(Attributes List, Min: 2, Max: 3) This is an attribute.",
&tfjson.SchemaAttribute{
Expand Down
Loading