Skip to content

Commit

Permalink
awsproviderlint: Add AWSV001 check, switch fmtsprintfcallexpr pass to…
Browse files Browse the repository at this point in the history
  • Loading branch information
bflad authored Aug 20, 2020
1 parent 12a8f92 commit 9228598
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 71 deletions.
6 changes: 6 additions & 0 deletions awsproviderlint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ The `awsproviderlint` tool extends the `tfproviderlint` tool and its checks. See
| [AWSR001](passes/AWSR001/README.md) | check for `fmt.Sprintf()` calls using `.amazonaws.com` domain suffix |
| [AWSR002](passes/AWSR002/README.md) | check for `d.Set()` of `tags` attribute that should include `IgnoreConfig()` |

### AWS Validation Checks

| Check | Description |
|---|---|
| [AWSV001](passes/AWSV001) | check for `validation.StringInSlice()` calls using `[]string` parameter |

## Development and Testing

This project is built on the [`tfproviderlint`](https://github.com/bflad/tfproviderlint) project and the [`go/analysis`](https://godoc.org/golang.org/x/tools/go/analysis) framework.
Expand Down
2 changes: 1 addition & 1 deletion awsproviderlint/passes/AWSR001/AWSR001.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/bflad/tfproviderlint/helper/astutils"
"github.com/bflad/tfproviderlint/passes/commentignore"
"github.com/terraform-providers/terraform-provider-aws/awsproviderlint/passes/fmtsprintfcallexpr"
"github.com/bflad/tfproviderlint/passes/stdlib/fmtsprintfcallexpr"
"golang.org/x/tools/go/analysis"
)

Expand Down
69 changes: 69 additions & 0 deletions awsproviderlint/passes/AWSV001/AWSV001.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package AWSV001

import (
"go/ast"

"github.com/bflad/tfproviderlint/helper/astutils"
"github.com/bflad/tfproviderlint/passes/commentignore"
"github.com/bflad/tfproviderlint/passes/helper/validation/stringinslicecallexpr"
"golang.org/x/tools/go/analysis"
)

const Doc = `check for validation.StringInSlice() using []string parameter
The AWSV001 analyzer reports when a validation.StringInSlice() call has the
first parameter of a []string, which suggests either that AWS API model
constants are not available or that the usage is prior to the AWS Go SDK adding
functions that return all values for the enumeration type.
If the API model constants are not available, this check can be ignored but it
is recommended to submit an AWS Support case to the AWS service team for adding
the constants.
If the hardcoded strings are AWS Go SDK constants, this check reports when the
first parameter should be switched to the newer ENUM_Values() function.
`

const analyzerName = "AWSV001"

var Analyzer = &analysis.Analyzer{
Name: analyzerName,
Doc: Doc,
Requires: []*analysis.Analyzer{
commentignore.Analyzer,
stringinslicecallexpr.Analyzer,
},
Run: run,
}

func run(pass *analysis.Pass) (interface{}, error) {
callExprs := pass.ResultOf[stringinslicecallexpr.Analyzer].([]*ast.CallExpr)
commentIgnorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer)

for _, callExpr := range callExprs {
if commentIgnorer.ShouldIgnore(analyzerName, callExpr) {
continue
}

if len(callExpr.Args) < 2 {
continue
}

ast.Inspect(callExpr.Args[0], func(n ast.Node) bool {
compositeLit, ok := n.(*ast.CompositeLit)

if !ok {
return true
}

if astutils.IsExprTypeArrayString(compositeLit.Type) {
pass.Reportf(callExpr.Args[0].Pos(), "%s: prefer AWS Go SDK ENUM_Values() function (ignore if not applicable)", analyzerName)
return false
}

return true
})
}

return nil, nil
}
12 changes: 12 additions & 0 deletions awsproviderlint/passes/AWSV001/AWSV001_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package AWSV001

import (
"testing"

"golang.org/x/tools/go/analysis/analysistest"
)

func TestAWSV001(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, Analyzer, "a")
}
38 changes: 38 additions & 0 deletions awsproviderlint/passes/AWSV001/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# AWSV001

The `AWSV001` analyzer reports when a `validation.StringInSlice()` call has the first parameter of a `[]string`, which suggests either that AWS API model constants are not available or that the usage is prior to the AWS Go SDK adding functions that return all values for the enumeration type.

If the API model constants are not available, this check can be ignored but it is recommended to submit an AWS Support case to the AWS service team for adding the constants.

If the elements of the string slice are AWS Go SDK constants, this check reports when the parameter should be switched to the newer AWS Go SDK `ENUM_Values()` function.

## Flagged Code

```go
&schema.Schema{
ValidateFunc: validation.StringInSlice([]string{
service.EnumTypeExample1,
service.EnumTypeExample2,
}, false),
}
```

## Passing Code

```go
&schema.Schema{
ValidateFunc: validation.StringInSlice(service.EnumType_Values(), false),
}
```

## Ignoring Check

The check can be ignored for a certain line via a `//lintignore:AWSV001` comment on the previous line or at the end of the offending line, e.g.

```go
//lintignore:AWSV001
ValidateFunc: validation.StringInSlice([]string{
service.EnumTypeExample1,
service.EnumTypeExample2,
}, false),
```
32 changes: 32 additions & 0 deletions awsproviderlint/passes/AWSV001/testdata/src/a/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package a

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func f() {
testSlice := []string{"test"}

/* Passing cases */

_ = validation.StringInSlice(testSlice, false)

_ = validation.StringInSlice(testSlice, true)

_ = validation.StringInSlice(testFunc(), false)

/* Comment ignored cases */

//lintignore:AWSV001
_ = validation.StringInSlice([]string{"test"}, false)

_ = validation.StringInSlice([]string{"test"}, false) //lintignore:AWSV001

/* Failing cases */

_ = validation.StringInSlice([]string{"test"}, false) // want "prefer AWS Go SDK ENUM_Values\\(\\) function"
}

func testFunc() []string {
return []string{"test"}
}
1 change: 1 addition & 0 deletions awsproviderlint/passes/AWSV001/testdata/src/a/vendor
2 changes: 2 additions & 0 deletions awsproviderlint/passes/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/terraform-providers/terraform-provider-aws/awsproviderlint/passes/AWSAT006"
"github.com/terraform-providers/terraform-provider-aws/awsproviderlint/passes/AWSR001"
"github.com/terraform-providers/terraform-provider-aws/awsproviderlint/passes/AWSR002"
"github.com/terraform-providers/terraform-provider-aws/awsproviderlint/passes/AWSV001"
"golang.org/x/tools/go/analysis"
)

Expand All @@ -21,4 +22,5 @@ var AllChecks = []*analysis.Analyzer{
AWSAT006.Analyzer,
AWSR001.Analyzer,
AWSR002.Analyzer,
AWSV001.Analyzer,
}
55 changes: 0 additions & 55 deletions awsproviderlint/passes/fmtsprintfcallexpr/fmtsprintfcallexpr.go

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,12 @@ github.com/bflad/tfproviderlint/passes/helper/validation/iprangecallexpr
github.com/bflad/tfproviderlint/passes/helper/validation/iprangeselectorexpr
github.com/bflad/tfproviderlint/passes/helper/validation/singleipcallexpr
github.com/bflad/tfproviderlint/passes/helper/validation/singleipselectorexpr
github.com/bflad/tfproviderlint/passes/helper/validation/stringinslicecallexpr
github.com/bflad/tfproviderlint/passes/helper/validation/validatejsonstringselectorexpr
github.com/bflad/tfproviderlint/passes/helper/validation/validatelistuniquestringsselectorexpr
github.com/bflad/tfproviderlint/passes/helper/validation/validateregexpselectorexpr
github.com/bflad/tfproviderlint/passes/helper/validation/validaterfc3339timestringselectorexpr
github.com/bflad/tfproviderlint/passes/stdlib/fmtsprintfcallexpr
github.com/bflad/tfproviderlint/passes/testaccfuncdecl
github.com/bflad/tfproviderlint/passes/testfuncdecl
github.com/bflad/tfproviderlint/version
Expand Down

0 comments on commit 9228598

Please sign in to comment.