-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Check: S038 for invalid attribute reference semantically (AtLeast…
…OneOf)
- Loading branch information
Showing
13 changed files
with
363 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# S038 | ||
|
||
The S038 analyzer reports cases of Schemas which include `ConflictsWith` and have invalid schema attribute references. | ||
|
||
NOTE: This pass only works with Terraform resources that are fully defined in a single function. | ||
|
||
## Flagged Code | ||
|
||
```go | ||
// Attribute reference in multi nested block is not supported | ||
&schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"x": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"foo": { | ||
AtLeastOneOf: []string{"x.0.bar"}, | ||
}, | ||
"bar": { | ||
AtLeastOneOf: []string{"x.0.foo"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
``` | ||
|
||
or | ||
|
||
```go | ||
// Non-existed attribute reference | ||
&schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"x": { | ||
Type: schema.TypeList, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"foo": { | ||
AtLeastOneOf: []string{"x.1.bar"}, | ||
}, | ||
"bar": { | ||
AtLeastOneOf: []string{"x.1.foo"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
``` | ||
|
||
## Passing Code | ||
|
||
```go | ||
&schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"x": { | ||
Type: schema.TypeList, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"foo": { | ||
AtLeastOneOf: []string{"x.0.bar"}, | ||
}, | ||
"bar": { | ||
AtLeastOneOf: []string{"x.0.foo"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
``` | ||
|
||
## Ignoring Reports | ||
|
||
Singular reports can be ignored by adding the a `//lintignore:S038` Go code comment at the end of the offending line or on the line immediately proceding, e.g. | ||
|
||
```go | ||
//lintignore:S038 | ||
&schema.Resource{ | ||
// ... | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package S038 | ||
|
||
import ( | ||
"github.com/bflad/tfproviderlint/helper/analysisutils" | ||
"github.com/bflad/tfproviderlint/helper/terraformtype/helper/schema" | ||
) | ||
|
||
var Analyzer = analysisutils.SchemaAttributeReferencesSemanticsAnalyzer("S038", schema.SchemaFieldAtLeastOneOf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package S038 | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
func TestS038(t *testing.T) { | ||
testdata := analysistest.TestData() | ||
analysistest.Run(t, testdata, Analyzer, "a") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package a | ||
|
||
import ( | ||
s "github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func falias() { | ||
_ = &s.Resource{ | ||
Read: func(*s.ResourceData, interface{}) error { return nil }, | ||
Schema: map[string]*s.Schema{ | ||
"x": { | ||
Type: s.TypeList, | ||
Elem: &s.Resource{ | ||
Schema: map[string]*s.Schema{ | ||
"foo": { | ||
AtLeastOneOf: []string{"x.0.bar"}, // want `S038: invalid AtLeastOneOf attribute reference semantics: "x.0" configuration block attribute references are only valid for TypeList and MaxItems: 1 attributes` | ||
}, | ||
"bar": { | ||
AtLeastOneOf: []string{"x.0.foo"}, // want `S038: invalid AtLeastOneOf attribute reference semantics: "x.0" configuration block attribute references are only valid for TypeList and MaxItems: 1 attributes` | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
Oops, something went wrong.