Merge pull request #29397 from hashicorp/jbardin/format-id-name-marks
unmark object ID or Name for formatting
This commit is contained in:
commit
a94155d0ca
|
@ -1,6 +1,7 @@
|
||||||
package format
|
package format
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/hashicorp/terraform/internal/lang/marks"
|
||||||
"github.com/zclconf/go-cty/cty"
|
"github.com/zclconf/go-cty/cty"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -30,6 +31,11 @@ func ObjectValueID(obj cty.Value) (k, v string) {
|
||||||
|
|
||||||
case atys["id"] == cty.String:
|
case atys["id"] == cty.String:
|
||||||
v := obj.GetAttr("id")
|
v := obj.GetAttr("id")
|
||||||
|
if v.HasMark(marks.Sensitive) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
v, _ = v.Unmark()
|
||||||
|
|
||||||
if v.IsKnown() && !v.IsNull() {
|
if v.IsKnown() && !v.IsNull() {
|
||||||
return "id", v.AsString()
|
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
|
// "name" isn't always globally unique, but if there isn't also an
|
||||||
// "id" then it _often_ is, in practice.
|
// "id" then it _often_ is, in practice.
|
||||||
v := obj.GetAttr("name")
|
v := obj.GetAttr("name")
|
||||||
|
if v.HasMark(marks.Sensitive) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
v, _ = v.Unmark()
|
||||||
|
|
||||||
if v.IsKnown() && !v.IsNull() {
|
if v.IsKnown() && !v.IsNull() {
|
||||||
return "name", v.AsString()
|
return "name", v.AsString()
|
||||||
}
|
}
|
||||||
|
@ -77,25 +88,41 @@ func ObjectValueName(obj cty.Value) (k, v string) {
|
||||||
|
|
||||||
case atys["name"] == cty.String:
|
case atys["name"] == cty.String:
|
||||||
v := obj.GetAttr("name")
|
v := obj.GetAttr("name")
|
||||||
|
if v.HasMark(marks.Sensitive) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
v, _ = v.Unmark()
|
||||||
|
|
||||||
if v.IsKnown() && !v.IsNull() {
|
if v.IsKnown() && !v.IsNull() {
|
||||||
return "name", v.AsString()
|
return "name", v.AsString()
|
||||||
}
|
}
|
||||||
|
|
||||||
case atys["tags"].IsMapType() && atys["tags"].ElementType() == cty.String:
|
case atys["tags"].IsMapType() && atys["tags"].ElementType() == cty.String:
|
||||||
tags := obj.GetAttr("tags")
|
tags := obj.GetAttr("tags")
|
||||||
if tags.IsNull() || !tags.IsWhollyKnown() {
|
if tags.IsNull() || !tags.IsWhollyKnown() || tags.HasMark(marks.Sensitive) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
tags, _ = tags.Unmark()
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case tags.HasIndex(cty.StringVal("name")).RawEquals(cty.True):
|
case tags.HasIndex(cty.StringVal("name")).RawEquals(cty.True):
|
||||||
v := tags.Index(cty.StringVal("name"))
|
v := tags.Index(cty.StringVal("name"))
|
||||||
|
if v.HasMark(marks.Sensitive) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
v, _ = v.Unmark()
|
||||||
|
|
||||||
if v.IsKnown() && !v.IsNull() {
|
if v.IsKnown() && !v.IsNull() {
|
||||||
return "tags.name", v.AsString()
|
return "tags.name", v.AsString()
|
||||||
}
|
}
|
||||||
case tags.HasIndex(cty.StringVal("Name")).RawEquals(cty.True):
|
case tags.HasIndex(cty.StringVal("Name")).RawEquals(cty.True):
|
||||||
// AWS-style naming convention
|
// AWS-style naming convention
|
||||||
v := tags.Index(cty.StringVal("Name"))
|
v := tags.Index(cty.StringVal("Name"))
|
||||||
|
if v.HasMark(marks.Sensitive) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
v, _ = v.Unmark()
|
||||||
|
|
||||||
if v.IsKnown() && !v.IsNull() {
|
if v.IsKnown() && !v.IsNull() {
|
||||||
return "tags.Name", v.AsString()
|
return "tags.Name", v.AsString()
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/internal/lang/marks"
|
||||||
"github.com/zclconf/go-cty/cty"
|
"github.com/zclconf/go-cty/cty"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -57,6 +58,14 @@ func TestObjectValueIDOrName(t *testing.T) {
|
||||||
[...]string{"name", "awesome-foo"},
|
[...]string{"name", "awesome-foo"},
|
||||||
[...]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{
|
cty.ObjectVal(map[string]cty.Value{
|
||||||
"name": cty.StringVal("awesome-foo"),
|
"name": cty.StringVal("awesome-foo"),
|
||||||
|
@ -161,6 +170,16 @@ func TestObjectValueIDOrName(t *testing.T) {
|
||||||
[...]string{"", ""},
|
[...]string{"", ""},
|
||||||
[...]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{
|
cty.ObjectVal(map[string]cty.Value{
|
||||||
"tags": cty.MapVal(map[string]cty.Value{
|
"tags": cty.MapVal(map[string]cty.Value{
|
||||||
|
|
Loading…
Reference in New Issue