unmark object ID or Name for formatting

This commit is contained in:
James Bardin 2021-08-16 16:42:09 -04:00
parent 8407ce73db
commit a48b024c0a
2 changed files with 47 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package format
import (
"github.com/hashicorp/terraform/internal/lang/marks"
"github.com/zclconf/go-cty/cty"
)
@ -30,6 +31,11 @@ func ObjectValueID(obj cty.Value) (k, v string) {
case atys["id"] == cty.String:
v := obj.GetAttr("id")
if v.HasMark(marks.Sensitive) {
break
}
v, _ = v.Unmark()
if v.IsKnown() && !v.IsNull() {
return "id", v.AsString()
}
@ -38,6 +44,11 @@ func ObjectValueID(obj cty.Value) (k, v string) {
// "name" isn't always globally unique, but if there isn't also an
// "id" then it _often_ is, in practice.
v := obj.GetAttr("name")
if v.HasMark(marks.Sensitive) {
break
}
v, _ = v.Unmark()
if v.IsKnown() && !v.IsNull() {
return "name", v.AsString()
}
@ -77,25 +88,41 @@ func ObjectValueName(obj cty.Value) (k, v string) {
case atys["name"] == cty.String:
v := obj.GetAttr("name")
if v.HasMark(marks.Sensitive) {
break
}
v, _ = v.Unmark()
if v.IsKnown() && !v.IsNull() {
return "name", v.AsString()
}
case atys["tags"].IsMapType() && atys["tags"].ElementType() == cty.String:
tags := obj.GetAttr("tags")
if tags.IsNull() || !tags.IsWhollyKnown() {
if tags.IsNull() || !tags.IsWhollyKnown() || tags.HasMark(marks.Sensitive) {
break
}
tags, _ = tags.Unmark()
switch {
case tags.HasIndex(cty.StringVal("name")).RawEquals(cty.True):
v := tags.Index(cty.StringVal("name"))
if v.HasMark(marks.Sensitive) {
break
}
v, _ = v.Unmark()
if v.IsKnown() && !v.IsNull() {
return "tags.name", v.AsString()
}
case tags.HasIndex(cty.StringVal("Name")).RawEquals(cty.True):
// AWS-style naming convention
v := tags.Index(cty.StringVal("Name"))
if v.HasMark(marks.Sensitive) {
break
}
v, _ = v.Unmark()
if v.IsKnown() && !v.IsNull() {
return "tags.Name", v.AsString()
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"testing"
"github.com/hashicorp/terraform/internal/lang/marks"
"github.com/zclconf/go-cty/cty"
)
@ -57,6 +58,14 @@ func TestObjectValueIDOrName(t *testing.T) {
[...]string{"name", "awesome-foo"},
[...]string{"name", "awesome-foo"},
},
{
cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal("awesome-foo").Mark(marks.Sensitive),
}),
[...]string{"", ""},
[...]string{"", ""},
[...]string{"", ""},
},
{
cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal("awesome-foo"),
@ -161,6 +170,16 @@ func TestObjectValueIDOrName(t *testing.T) {
[...]string{"", ""},
[...]string{"", ""},
},
{
cty.ObjectVal(map[string]cty.Value{
"tags": cty.MapVal(map[string]cty.Value{
"Name": cty.UnknownVal(cty.String).Mark(marks.Sensitive),
}),
}),
[...]string{"", ""},
[...]string{"", ""},
[...]string{"", ""},
},
{
cty.ObjectVal(map[string]cty.Value{
"tags": cty.MapVal(map[string]cty.Value{