Skip to content

Commit

Permalink
ephemeral: add test case for block rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Jan 6, 2025
1 parent 173ed01 commit b867f54
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/command/jsonformat/differ/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func ComputeDiffForBlock(change structured.Change, block *jsonprovider.Block) co
childValue.AfterExplicit = false

childChange := ComputeDiffForAttribute(childValue, attr)
if childChange.Action == plans.NoOp && childValue.Before == nil && childValue.After == nil && !attr.WriteOnly {
if childChange.Action == plans.NoOp && childValue.Before == nil && childValue.After == nil {
// Don't record nil values at all in blocks except if they are write-only.
continue
}
Expand Down
93 changes: 93 additions & 0 deletions internal/command/jsonformat/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7464,6 +7464,10 @@ func TestResourceChange_writeOnlyAttributes(t *testing.T) {
"user": cty.String,
"write_only_set_password": cty.String,
}),
"block": cty.List(cty.Object(map[string]cty.Type{
"user": cty.String,
"block_set_password": cty.String,
})),
})),
After: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("i-02ae66f368e8518a9"),
Expand All @@ -7472,6 +7476,10 @@ func TestResourceChange_writeOnlyAttributes(t *testing.T) {
"user": cty.StringVal("not-secret"),
"write_only_set_password": cty.NullVal(cty.String),
}),
"block": cty.ListVal([]cty.Value{cty.ObjectVal(map[string]cty.Value{
"user": cty.StringVal("this-is-not-secret"),
"block_set_password": cty.NullVal(cty.String),
})}),
}),
Schema: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
Expand All @@ -7487,6 +7495,17 @@ func TestResourceChange_writeOnlyAttributes(t *testing.T) {
},
},
},
BlockTypes: map[string]*configschema.NestedBlock{
"block": {
Nesting: configschema.NestingList,
Block: configschema.Block{
Attributes: map[string]*configschema.Attribute{
"user": {Type: cty.String, Optional: true},
"block_set_password": {Type: cty.String, Optional: true, Sensitive: true, WriteOnly: true},
},
},
},
},
},
ExpectedOutput: ` # test_instance.example will be created
+ resource "test_instance" "example" {
Expand All @@ -7496,6 +7515,11 @@ func TestResourceChange_writeOnlyAttributes(t *testing.T) {
}
+ id = "i-02ae66f368e8518a9"
+ write_only = (write-only attribute)
+ block {
+ block_set_password = (write-only attribute)
+ user = "this-is-not-secret"
}
}`,
},
"update attribute": {
Expand All @@ -7508,6 +7532,10 @@ func TestResourceChange_writeOnlyAttributes(t *testing.T) {
"user": cty.StringVal("not-secret"),
"write_only_set_password": cty.NullVal(cty.String),
}),
"block": cty.ListVal([]cty.Value{cty.ObjectVal(map[string]cty.Value{
"user": cty.StringVal("not-so-secret"),
"block_set_password": cty.NullVal(cty.String),
})}),
}),
After: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("i-02ae66f368e8518a10"),
Expand All @@ -7516,6 +7544,10 @@ func TestResourceChange_writeOnlyAttributes(t *testing.T) {
"user": cty.StringVal("not-secret"),
"write_only_set_password": cty.NullVal(cty.String),
}),
"block": cty.ListVal([]cty.Value{cty.ObjectVal(map[string]cty.Value{
"user": cty.StringVal("not-so-secret"),
"block_set_password": cty.NullVal(cty.String),
})}),
}),
Schema: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
Expand All @@ -7531,11 +7563,24 @@ func TestResourceChange_writeOnlyAttributes(t *testing.T) {
},
},
},
BlockTypes: map[string]*configschema.NestedBlock{
"block": {
Nesting: configschema.NestingList,
Block: configschema.Block{
Attributes: map[string]*configschema.Attribute{
"user": {Type: cty.String, Optional: true},
"block_set_password": {Type: cty.String, Optional: true, Sensitive: true, WriteOnly: true},
},
},
},
},
},
ExpectedOutput: ` # test_instance.example will be updated in-place
~ resource "test_instance" "example" {
~ id = "i-02ae66f368e8518a9" -> "i-02ae66f368e8518a10"
# (2 unchanged attributes hidden)
# (1 unchanged block hidden)
}`,
},
"update - delete block": {
Expand All @@ -7548,6 +7593,10 @@ func TestResourceChange_writeOnlyAttributes(t *testing.T) {
"user": cty.StringVal("not-secret"),
"write_only_set_password": cty.NullVal(cty.String),
}),
"block": cty.ListVal([]cty.Value{cty.ObjectVal(map[string]cty.Value{
"user": cty.StringVal("not-secret"),
"block_set_password": cty.NullVal(cty.String),
})}),
}),
After: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("i-02ae66f368e8518a9"),
Expand All @@ -7556,6 +7605,10 @@ func TestResourceChange_writeOnlyAttributes(t *testing.T) {
"user": cty.String,
"write_only_set_password": cty.String,
})),
"block": cty.NullVal(cty.List(cty.Object(map[string]cty.Type{
"user": cty.String,
"block_set_password": cty.String,
}))),
}),
Schema: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
Expand All @@ -7571,6 +7624,17 @@ func TestResourceChange_writeOnlyAttributes(t *testing.T) {
},
},
},
BlockTypes: map[string]*configschema.NestedBlock{
"block": {
Nesting: configschema.NestingList,
Block: configschema.Block{
Attributes: map[string]*configschema.Attribute{
"user": {Type: cty.String, Optional: true},
"block_set_password": {Type: cty.String, Optional: true, Sensitive: true, WriteOnly: true},
},
},
},
},
},
ExpectedOutput: ` # test_instance.example will be updated in-place
~ resource "test_instance" "example" {
Expand All @@ -7580,6 +7644,11 @@ func TestResourceChange_writeOnlyAttributes(t *testing.T) {
} -> null
id = "i-02ae66f368e8518a9"
# (1 unchanged attribute hidden)
- block {
- block_set_password = (write-only attribute) -> null
- user = "not-secret" -> null
}
}`,
},
"delete": {
Expand All @@ -7592,6 +7661,10 @@ func TestResourceChange_writeOnlyAttributes(t *testing.T) {
"user": cty.StringVal("not-secret"),
"write_only_set_password": cty.NullVal(cty.String),
}),
"block": cty.ListVal([]cty.Value{cty.ObjectVal(map[string]cty.Value{
"user": cty.StringVal("not-secret"),
"block_set_password": cty.NullVal(cty.String),
})}),
}),
After: cty.NullVal(cty.Object(map[string]cty.Type{
"id": cty.String,
Expand All @@ -7600,6 +7673,10 @@ func TestResourceChange_writeOnlyAttributes(t *testing.T) {
"user": cty.String,
"write_only_set_password": cty.String,
}),
"block": cty.List(cty.Object(map[string]cty.Type{
"user": cty.String,
"block_set_password": cty.String,
})),
})),
Schema: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
Expand All @@ -7615,6 +7692,17 @@ func TestResourceChange_writeOnlyAttributes(t *testing.T) {
},
},
},
BlockTypes: map[string]*configschema.NestedBlock{
"block": {
Nesting: configschema.NestingList,
Block: configschema.Block{
Attributes: map[string]*configschema.Attribute{
"user": {Type: cty.String, Optional: true},
"block_set_password": {Type: cty.String, Optional: true, Sensitive: true, WriteOnly: true},
},
},
},
},
},
ExpectedOutput: ` # test_instance.example will be destroyed
- resource "test_instance" "example" {
Expand All @@ -7624,6 +7712,11 @@ func TestResourceChange_writeOnlyAttributes(t *testing.T) {
} -> null
- id = "i-02ae66f368e8518a9" -> null
- write_only = (write-only attribute) -> null
- block {
- block_set_password = (write-only attribute) -> null
- user = "not-secret" -> null
}
}`,
},
}
Expand Down

0 comments on commit b867f54

Please sign in to comment.