-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Checks for common unstable d.SetId() values and introduce standar…
…d library package helpers (#193) * Introduce standard library helpers and pass for fmt.Sprintf() calls Functionality to replace current Terraform AWS Provider handling and generally will be useful in the future. * passes: New Checks for common unstable d.SetId() values Reference: bflad/tfproviderlint#191 Reference: bflad/tfproviderlint#192
- Loading branch information
1 parent
08e5d52
commit e1d8979
Showing
50 changed files
with
912 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package analysisutils | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"reflect" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/passes/inspect" | ||
) | ||
|
||
// StdlibFunctionCallExprAnalyzer returns an Analyzer for standard library function *ast.CallExpr | ||
func StdlibFunctionCallExprAnalyzer(analyzerName string, packagePath string, functionName string) *analysis.Analyzer { | ||
return &analysis.Analyzer{ | ||
Name: analyzerName, | ||
Doc: fmt.Sprintf("find %s.%s() calls for later passes", packagePath, functionName), | ||
Requires: []*analysis.Analyzer{ | ||
inspect.Analyzer, | ||
}, | ||
Run: StdlibFunctionCallExprRunner(packagePath, functionName), | ||
ResultType: reflect.TypeOf([]*ast.CallExpr{}), | ||
} | ||
} |
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,33 @@ | ||
package analysisutils | ||
|
||
import ( | ||
"go/ast" | ||
|
||
"github.com/bflad/tfproviderlint/helper/astutils" | ||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/passes/inspect" | ||
"golang.org/x/tools/go/ast/inspector" | ||
) | ||
|
||
// StdlibFunctionCallExprRunner returns an Analyzer runner for function *ast.CallExpr | ||
func StdlibFunctionCallExprRunner(packagePath string, functionName string) func(*analysis.Pass) (interface{}, error) { | ||
return func(pass *analysis.Pass) (interface{}, error) { | ||
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) | ||
nodeFilter := []ast.Node{ | ||
(*ast.CallExpr)(nil), | ||
} | ||
var result []*ast.CallExpr | ||
|
||
inspect.Preorder(nodeFilter, func(n ast.Node) { | ||
callExpr := n.(*ast.CallExpr) | ||
|
||
if !astutils.IsStdlibPackageFunc(callExpr.Fun, pass.TypesInfo, packagePath, functionName) { | ||
return | ||
} | ||
|
||
result = append(result, callExpr) | ||
}) | ||
|
||
return result, 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
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 | ||
} |
Oops, something went wrong.