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

Add IsSensitive to AttributeSchema #42

Merged
merged 1 commit into from
Jun 2, 2021
Merged
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
19 changes: 10 additions & 9 deletions decoder/attribute_candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,24 @@ func attributeSchemaToCandidate(name string, attr *schema.AttributeSchema, rng h
}

func detailForAttribute(attr *schema.AttributeSchema) string {
var detail string
details := []string{}

if attr.IsRequired {
detail = "Required"
details = append(details, "required")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a reasonable change, but just FYI there will be some existing test data in the need of update potentially in terraform-schema and in terraform-ls 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds exciting 🙈

} else if attr.IsOptional {
detail = "Optional"
details = append(details, "optional")
}

if attr.IsSensitive {
radeksimko marked this conversation as resolved.
Show resolved Hide resolved
details = append(details, "sensitive")
}

friendlyName := attr.Expr.FriendlyName()
if friendlyName != "" {
if detail != "" {
detail = strings.Join([]string{detail, friendlyName}, ", ")
} else {
detail = friendlyName
}
details = append(details, friendlyName)
}

return detail
return strings.Join(details[:], ", ")
}

func snippetForAttribute(name string, attr *schema.AttributeSchema) string {
Expand Down
2 changes: 1 addition & 1 deletion decoder/body_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestDecoder_CandidateAtPos_computedAttributes(t *testing.T) {
List: []lang.Candidate{
{
Label: "attr2",
Detail: "Optional, number",
Detail: "optional, number",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand Down
79 changes: 70 additions & 9 deletions decoder/candidates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,16 @@ func TestDecoder_CandidatesAtPos_basic(t *testing.T) {
"five": {Expr: schema.LiteralTypeOnly(cty.DynamicPseudoType)},
},
},
schema.NewSchemaKey(schema.DependencyKeys{
Labels: []schema.LabelDependent{
{Index: 0, Value: "sensitive_resource"},
},
}): {
Attributes: map[string]*schema.AttributeSchema{
"six": {Expr: schema.LiteralTypeOnly(cty.Number), IsSensitive: true},
"seven": {Expr: schema.LiteralTypeOnly(cty.Number), IsRequired: true, IsSensitive: true},
},
},
},
}

Expand All @@ -783,6 +793,10 @@ func TestDecoder_CandidatesAtPos_basic(t *testing.T) {
count = 3
}

resource "sensitive_resource" "t" {
count = 2
}

resource "random_resource" "test" {
arg = ""
}
Expand Down Expand Up @@ -877,6 +891,19 @@ resource "random_resource" "test" {
},
Kind: lang.LabelCandidateKind,
},
{
Label: "sensitive_resource",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 11, Byte: 10},
End: hcl.Pos{Line: 1, Column: 25, Byte: 24},
},
NewText: "sensitive_resource",
Snippet: "sensitive_resource",
},
Kind: lang.LabelCandidateKind,
},
}),
},
{
Expand All @@ -885,7 +912,7 @@ resource "random_resource" "test" {
lang.CompleteCandidates([]lang.Candidate{
{
Label: "one",
Detail: "Required, string",
Detail: "required, string",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand All @@ -899,7 +926,7 @@ resource "random_resource" "test" {
},
{
Label: "three",
Detail: "Optional, bool",
Detail: "optional, bool",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand All @@ -914,7 +941,7 @@ resource "random_resource" "test" {
},
{
Label: "two",
Detail: "Optional, number",
Detail: "optional, number",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand All @@ -934,7 +961,7 @@ resource "random_resource" "test" {
lang.CompleteCandidates([]lang.Candidate{
{
Label: "one",
Detail: "Required, string",
Detail: "required, string",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand All @@ -948,7 +975,7 @@ resource "random_resource" "test" {
},
{
Label: "three",
Detail: "Optional, bool",
Detail: "optional, bool",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand All @@ -963,7 +990,7 @@ resource "random_resource" "test" {
},
{
Label: "two",
Detail: "Optional, number",
Detail: "optional, number",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand All @@ -977,6 +1004,40 @@ resource "random_resource" "test" {
},
}),
},
{
"second block attribute",
hcl.Pos{Line: 6, Column: 1, Byte: 89},
lang.CompleteCandidates([]lang.Candidate{
{
Label: "seven",
Detail: "required, sensitive, number",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 6, Column: 1, Byte: 89},
End: hcl.Pos{Line: 6, Column: 1, Byte: 89},
},
NewText: "seven",
Snippet: "seven = ${1:1}",
},
Kind: lang.AttributeCandidateKind,
},
{
Label: "six",
Detail: "sensitive, number",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 6, Column: 1, Byte: 89},
End: hcl.Pos{Line: 6, Column: 1, Byte: 89},
},
NewText: "six",
Snippet: "six = ${1:1}",
},
Kind: lang.AttributeCandidateKind,
},
}),
},
}

for i, tc := range testCases {
Expand Down Expand Up @@ -1110,7 +1171,7 @@ func TestDecoder_CandidatesAtPos_multipleTypes(t *testing.T) {
expectedCandidates := lang.CompleteCandidates([]lang.Candidate{
{
Label: "for_each",
Detail: "Optional, set of any single type or map of any single type",
Detail: "optional, set of any single type or map of any single type",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand Down Expand Up @@ -1194,7 +1255,7 @@ resource "any" "ref" {
lang.CompleteCandidates([]lang.Candidate{
{
Label: "count",
Detail: "Optional, number",
Detail: "optional, number",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand Down Expand Up @@ -1309,7 +1370,7 @@ resource "any" "ref" {
lang.CompleteCandidates([]lang.Candidate{
{
Label: "count",
Detail: "Optional, number",
Detail: "optional, number",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand Down
22 changes: 20 additions & 2 deletions decoder/hover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ func TestDecoder_HoverAtPos_basic(t *testing.T) {
IsOptional: true,
Description: lang.PlainText("Special attribute"),
},
"bool_attr": {
Expr: schema.LiteralTypeOnly(cty.Bool),
IsSensitive: true,
Description: lang.PlainText("Flag attribute"),
},
},
},
DependentBody: map[schema.SchemaKey]*schema.BodySchema{
Expand Down Expand Up @@ -314,6 +319,7 @@ func TestDecoder_HoverAtPos_basic(t *testing.T) {
testConfig := []byte(`myblock "sushi" "salmon" {
str_attr = "test"
num_attr = 4
bool_attr = true
}
`)
d := NewDecoder()
Expand All @@ -331,17 +337,29 @@ func TestDecoder_HoverAtPos_basic(t *testing.T) {
expectedData *lang.HoverData
}{
{
"attribute name",
"optional attribute name",
hcl.Pos{Line: 2, Column: 6, Byte: 32},
&lang.HoverData{
Content: lang.Markdown("**str_attr** _Optional, string_\n\nSpecial attribute"),
Content: lang.Markdown("**str_attr** _optional, string_\n\nSpecial attribute"),
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 2, Column: 3, Byte: 29},
End: hcl.Pos{Line: 2, Column: 20, Byte: 46},
},
},
},
{
"sensitive attribute name",
hcl.Pos{Line: 4, Column: 6, Byte: 68},
&lang.HoverData{
Content: lang.Markdown("**bool_attr** _sensitive, bool_\n\nFlag attribute"),
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 4, Column: 3, Byte: 64},
End: hcl.Pos{Line: 4, Column: 19, Byte: 80},
},
},
},
{
"block type",
hcl.Pos{Line: 1, Column: 3, Byte: 2},
Expand Down
2 changes: 2 additions & 0 deletions schema/attribute_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type AttributeSchema struct {
IsOptional bool
IsDeprecated bool
IsComputed bool
IsSensitive bool

Expr ExprConstraints

Expand Down Expand Up @@ -85,6 +86,7 @@ func (as *AttributeSchema) Copy() *AttributeSchema {
IsOptional: as.IsOptional,
IsDeprecated: as.IsDeprecated,
IsComputed: as.IsComputed,
IsSensitive: as.IsSensitive,
IsDepKey: as.IsDepKey,
Description: as.Description,
Expr: as.Expr.Copy(),
Expand Down
10 changes: 10 additions & 0 deletions schema/attribute_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ func TestAttributeSchema_Validate(t *testing.T) {
},
nil,
},
{
&AttributeSchema{
Expr: ExprConstraints{
TraversalExpr{OfType: cty.Number, OfScopeId: lang.ScopeId("blah")},
},
IsRequired: true,
IsSensitive: true,
},
nil,
},
}

for i, tc := range testCases {
Expand Down