-
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.
passes: New Checks for common unstable d.SetId() values
- Loading branch information
Showing
45 changed files
with
734 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package R015 | ||
|
||
import ( | ||
"go/ast" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/bflad/tfproviderlint/helper/terraformtype/helper/resource" | ||
"github.com/bflad/tfproviderlint/passes/commentignore" | ||
"github.com/bflad/tfproviderlint/passes/helper/schema/resourcedatasetidcallexpr" | ||
) | ||
|
||
const Doc = `check for (*schema.ResourceData).SetId() usage with unstable resource.UniqueId() value | ||
Schema attributes should be stable across Terraform runs.` | ||
|
||
const analyzerName = "R015" | ||
|
||
var Analyzer = &analysis.Analyzer{ | ||
Name: analyzerName, | ||
Doc: Doc, | ||
Requires: []*analysis.Analyzer{ | ||
commentignore.Analyzer, | ||
resourcedatasetidcallexpr.Analyzer, | ||
}, | ||
Run: run, | ||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
ignorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer) | ||
callExprs := pass.ResultOf[resourcedatasetidcallexpr.Analyzer].([]*ast.CallExpr) | ||
for _, callExpr := range callExprs { | ||
if ignorer.ShouldIgnore(analyzerName, callExpr) { | ||
continue | ||
} | ||
|
||
if len(callExpr.Args) < 1 { | ||
continue | ||
} | ||
|
||
ast.Inspect(callExpr.Args[0], func(n ast.Node) bool { | ||
callExpr, ok := n.(*ast.CallExpr) | ||
|
||
if !ok { | ||
return true | ||
} | ||
|
||
if resource.IsFunc(callExpr.Fun, pass.TypesInfo, resource.FuncNameUniqueId) { | ||
pass.Reportf(callExpr.Pos(), "%s: schema attributes should be stable across Terraform runs", analyzerName) | ||
return false | ||
} | ||
|
||
return true | ||
}) | ||
} | ||
|
||
return nil, nil | ||
} |
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,14 @@ | ||
package R015_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
|
||
"github.com/bflad/tfproviderlint/passes/R015" | ||
) | ||
|
||
func TestR015(t *testing.T) { | ||
testdata := analysistest.TestData() | ||
analysistest.Run(t, testdata, R015.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,24 @@ | ||
# R015 | ||
|
||
The R015 analyzer reports [`(*schema.ResourceData).SetId()`](https://godoc.org/github.com/hashicorp/terraform-plugin-sdk/helper/schema#ResourceData.Set) usage with unstable `resource.UniqueId()` value. Schema attributes should be stable across Terraform runs. | ||
|
||
## Flagged Code | ||
|
||
```go | ||
d.SetId(resource.UniqueId()) | ||
``` | ||
|
||
## Passing Code | ||
|
||
```go | ||
d.SetId("stablestring") | ||
``` | ||
|
||
## Ignoring Reports | ||
|
||
Singular reports can be ignored by adding the a `//lintignore:R015` Go code comment at the end of the offending line or on the line immediately proceding, e.g. | ||
|
||
```go | ||
//lintignore:R015 | ||
d.SetId(resource.UniqueId()) | ||
``` |
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,14 @@ | ||
package a | ||
|
||
import ( | ||
r "github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func falias() { | ||
var d schema.ResourceData | ||
|
||
d.SetId(r.UniqueId()) // want "schema attributes should be stable across Terraform runs" | ||
|
||
d.SetId("test") | ||
} |
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,14 @@ | ||
package a | ||
|
||
import ( | ||
r "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func falias_v2() { | ||
var d schema.ResourceData | ||
|
||
d.SetId(r.UniqueId()) // want "schema attributes should be stable across Terraform runs" | ||
|
||
d.SetId("test") | ||
} |
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,15 @@ | ||
package a | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func fcommentignore() { | ||
var d schema.ResourceData | ||
|
||
d.SetId(resource.UniqueId()) //lintignore:R015 | ||
|
||
//lintignore:R015 | ||
d.SetId(resource.UniqueId()) | ||
} |
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,15 @@ | ||
package a | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func fcommentignore_v2() { | ||
var d schema.ResourceData | ||
|
||
d.SetId(resource.UniqueId()) //lintignore:R015 | ||
|
||
//lintignore:R015 | ||
d.SetId(resource.UniqueId()) | ||
} |
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,24 @@ | ||
package a | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func f() { | ||
var d schema.ResourceData | ||
|
||
d.SetId(resource.UniqueId()) // want "schema attributes should be stable across Terraform runs" | ||
|
||
d.SetId("test") | ||
|
||
fResourceFunc(&d, nil) | ||
} | ||
|
||
func fResourceFunc(d *schema.ResourceData, meta interface{}) error { | ||
d.SetId(resource.UniqueId()) // want "schema attributes should be stable across Terraform runs" | ||
|
||
d.SetId("test") | ||
|
||
return nil | ||
} |
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,24 @@ | ||
package a | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func f_v2() { | ||
var d schema.ResourceData | ||
|
||
d.SetId(resource.UniqueId()) // want "schema attributes should be stable across Terraform runs" | ||
|
||
d.SetId("test") | ||
|
||
fResourceFunc_v2(&d, nil) | ||
} | ||
|
||
func fResourceFunc_v2(d *schema.ResourceData, meta interface{}) error { | ||
d.SetId(resource.UniqueId()) // want "schema attributes should be stable across Terraform runs" | ||
|
||
d.SetId("test") | ||
|
||
return nil | ||
} |
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 a | ||
|
||
import ( | ||
"a/resource" | ||
"a/schema" | ||
) | ||
|
||
func foutside() { | ||
var d schema.ResourceData | ||
|
||
d.SetId(resource.UniqueId()) | ||
} |
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,5 @@ | ||
package resource | ||
|
||
func UniqueId() string { | ||
return "" | ||
} |
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,7 @@ | ||
package schema | ||
|
||
type ResourceData struct{} | ||
|
||
func (d *ResourceData) SetId(value interface{}) error { | ||
return nil | ||
} |
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 @@ | ||
../../../../../vendor |
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,58 @@ | ||
package R016 | ||
|
||
import ( | ||
"go/ast" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/bflad/tfproviderlint/helper/terraformtype/helper/resource" | ||
"github.com/bflad/tfproviderlint/passes/commentignore" | ||
"github.com/bflad/tfproviderlint/passes/helper/schema/resourcedatasetidcallexpr" | ||
) | ||
|
||
const Doc = `check for (*schema.ResourceData).SetId() usage with unstable resource.PrefixedUniqueId() value | ||
Schema attributes should be stable across Terraform runs.` | ||
|
||
const analyzerName = "R016" | ||
|
||
var Analyzer = &analysis.Analyzer{ | ||
Name: analyzerName, | ||
Doc: Doc, | ||
Requires: []*analysis.Analyzer{ | ||
commentignore.Analyzer, | ||
resourcedatasetidcallexpr.Analyzer, | ||
}, | ||
Run: run, | ||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
ignorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer) | ||
callExprs := pass.ResultOf[resourcedatasetidcallexpr.Analyzer].([]*ast.CallExpr) | ||
for _, callExpr := range callExprs { | ||
if ignorer.ShouldIgnore(analyzerName, callExpr) { | ||
continue | ||
} | ||
|
||
if len(callExpr.Args) < 1 { | ||
continue | ||
} | ||
|
||
ast.Inspect(callExpr.Args[0], func(n ast.Node) bool { | ||
callExpr, ok := n.(*ast.CallExpr) | ||
|
||
if !ok { | ||
return true | ||
} | ||
|
||
if resource.IsFunc(callExpr.Fun, pass.TypesInfo, resource.FuncNamePrefixedUniqueId) { | ||
pass.Reportf(callExpr.Pos(), "%s: schema attributes should be stable across Terraform runs", analyzerName) | ||
return false | ||
} | ||
|
||
return true | ||
}) | ||
} | ||
|
||
return nil, nil | ||
} |
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,14 @@ | ||
package R016_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
|
||
"github.com/bflad/tfproviderlint/passes/R016" | ||
) | ||
|
||
func TestR016(t *testing.T) { | ||
testdata := analysistest.TestData() | ||
analysistest.Run(t, testdata, R016.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,24 @@ | ||
# R016 | ||
|
||
The R016 analyzer reports [`(*schema.ResourceData).SetId()`](https://godoc.org/github.com/hashicorp/terraform-plugin-sdk/helper/schema#ResourceData.Set) usage with unstable `resource.PrefixedUniqueId()` value. Schema attributes should be stable across Terraform runs. | ||
|
||
## Flagged Code | ||
|
||
```go | ||
d.SetId(resource.PrefixedUniqueId("example")) | ||
``` | ||
|
||
## Passing Code | ||
|
||
```go | ||
d.SetId("stablestring") | ||
``` | ||
|
||
## Ignoring Reports | ||
|
||
Singular reports can be ignored by adding the a `//lintignore:R016` Go code comment at the end of the offending line or on the line immediately proceding, e.g. | ||
|
||
```go | ||
//lintignore:R016 | ||
d.SetId(resource.PrefixedUniqueId("example")) | ||
``` |
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,14 @@ | ||
package a | ||
|
||
import ( | ||
r "github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func falias() { | ||
var d schema.ResourceData | ||
|
||
d.SetId(r.PrefixedUniqueId("test")) // want "schema attributes should be stable across Terraform runs" | ||
|
||
d.SetId("test") | ||
} |
Oops, something went wrong.