-
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.
- Loading branch information
Showing
17 changed files
with
562 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
20 changes: 20 additions & 0 deletions
20
helper/terraformtype/helper/schema/type_resourcetimeout.go
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,20 @@ | ||
package schema | ||
|
||
import "time" | ||
|
||
const ( | ||
ResourceTimeoutTypeCreateField = `Create` | ||
ResourceTimeoutTypeDefaultField = `Default` | ||
ResourceTimeoutTypeDeleteField = `Delete` | ||
ResourceTimeoutTypeReadField = `Read` | ||
ResourceTimeoutTypeUpdateField = `Update` | ||
) | ||
|
||
// resourceTimeoutType is an internal representation of the SDK helper/schema.ResourceTimeout type | ||
type resourceTimeoutType struct { | ||
Create *time.Duration | ||
Default *time.Duration | ||
Delete *time.Duration | ||
Read *time.Duration | ||
Update *time.Duration | ||
} |
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,49 @@ | ||
# XR006 | ||
|
||
The XR006 analyzer reports extraneous `Timeouts` fields in resources where the corresponding `Create`/`CreateContext`, `Delete`/`DeleteContext`, `Read`/`ReadContext`, or `Update`/`UpdateContext` implementation does not exist. | ||
|
||
## Flagged Code | ||
|
||
```go | ||
&schema.Resource{ | ||
/* ... no Create ... */ | ||
Read: /* ... */, | ||
Timeouts: schema.ResourceTimesout{ | ||
Create: schema.DefaultTimeout(10 * time.Minute), | ||
}, | ||
} | ||
``` | ||
|
||
## Passing Code | ||
|
||
```go | ||
// Fixed Timeouts field alignment | ||
&schema.Resource{ | ||
/* ... no Create ... */ | ||
Read: /* ... */, | ||
Timeouts: schema.ResourceTimesout{ | ||
Read: schema.DefaultTimeout(10 * time.Minute), | ||
}, | ||
} | ||
|
||
// Removed Timeouts | ||
&schema.Resource{ | ||
/* ... no Create ... */ | ||
Read: /* ... */, | ||
} | ||
``` | ||
|
||
## Ignoring Reports | ||
|
||
Singular reports can be ignored by adding the a `//lintignore:XR006` Go code comment at the end of the offending line or on the line immediately proceding, e.g. | ||
|
||
```go | ||
//lintignore:XR006 | ||
&schema.Resource{ | ||
/* ... no Create ... */ | ||
Read: /* ... */, | ||
Timeouts: schema.ResourceTimesout{ | ||
Create: schema.DefaultTimeout(10 * time.Minute), | ||
}, | ||
} | ||
``` |
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,53 @@ | ||
package XR006 | ||
|
||
import ( | ||
"github.com/bflad/tfproviderlint/helper/terraformtype/helper/schema" | ||
"github.com/bflad/tfproviderlint/passes/commentignore" | ||
"github.com/bflad/tfproviderlint/passes/helper/schema/resourceinfo" | ||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
const Doc = `check for Resource that implements Timeouts for missing Create, Delete, Read, or Update implementation | ||
The XR006 analyzer reports extraneous Timeouts fields in resources where the | ||
corresponding Create, Delete, Read, or Update implementation does not exist.` | ||
|
||
const analyzerName = "XR006" | ||
|
||
var Analyzer = &analysis.Analyzer{ | ||
Name: analyzerName, | ||
Doc: Doc, | ||
Requires: []*analysis.Analyzer{ | ||
commentignore.Analyzer, | ||
resourceinfo.Analyzer, | ||
}, | ||
Run: run, | ||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
ignorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer) | ||
resources := pass.ResultOf[resourceinfo.Analyzer].([]*schema.ResourceInfo) | ||
for _, resource := range resources { | ||
if ignorer.ShouldIgnore(analyzerName, resource.AstCompositeLit) { | ||
continue | ||
} | ||
|
||
if !resource.DeclaresField(schema.ResourceFieldCreate) && !resource.DeclaresField(schema.ResourceFieldCreateContext) && resource.Resource.Timeouts.Create != nil { | ||
pass.Reportf(resource.AstCompositeLit.Pos(), "%s: resource should not configure Timeouts.Create without Create implementation", analyzerName) | ||
} | ||
|
||
if !resource.DeclaresField(schema.ResourceFieldDelete) && !resource.DeclaresField(schema.ResourceFieldDeleteContext) && resource.Resource.Timeouts.Delete != nil { | ||
pass.Reportf(resource.AstCompositeLit.Pos(), "%s: resource should not configure Timeouts.Delete without Delete implementation", analyzerName) | ||
} | ||
|
||
if !resource.DeclaresField(schema.ResourceFieldRead) && !resource.DeclaresField(schema.ResourceFieldReadContext) && resource.Resource.Timeouts.Read != nil { | ||
pass.Reportf(resource.AstCompositeLit.Pos(), "%s: resource should not configure Timeouts.Read without Read implementation", analyzerName) | ||
} | ||
|
||
if !resource.DeclaresField(schema.ResourceFieldUpdate) && !resource.DeclaresField(schema.ResourceFieldUpdateContext) && resource.Resource.Timeouts.Update != nil { | ||
pass.Reportf(resource.AstCompositeLit.Pos(), "%s: resource should not configure Timeouts.Update without Update implementation", analyzerName) | ||
} | ||
} | ||
|
||
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,13 @@ | ||
package XR006_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/bflad/tfproviderlint/xpasses/XR006" | ||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
func TestXR006(t *testing.T) { | ||
testdata := analysistest.TestData() | ||
analysistest.Run(t, testdata, XR006.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,33 @@ | ||
package a | ||
|
||
import ( | ||
"time" | ||
|
||
s "github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func falias() { | ||
_ = s.Resource{ // want "resource should not configure Timeouts.Create without Create implementation" | ||
Timeouts: &s.ResourceTimeout{ | ||
Create: s.DefaultTimeout(time.Minute), | ||
}, | ||
} | ||
|
||
_ = s.Resource{ // want "resource should not configure Timeouts.Delete without Delete implementation" | ||
Timeouts: &s.ResourceTimeout{ | ||
Delete: s.DefaultTimeout(time.Minute), | ||
}, | ||
} | ||
|
||
_ = s.Resource{ // want "resource should not configure Timeouts.Read without Read implementation" | ||
Timeouts: &s.ResourceTimeout{ | ||
Read: s.DefaultTimeout(time.Minute), | ||
}, | ||
} | ||
|
||
_ = s.Resource{ // want "resource should not configure Timeouts.Update without Update implementation" | ||
Timeouts: &s.ResourceTimeout{ | ||
Update: s.DefaultTimeout(time.Minute), | ||
}, | ||
} | ||
} |
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 a | ||
|
||
import ( | ||
"time" | ||
|
||
s "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func falias_v2() { | ||
_ = s.Resource{ // want "resource should not configure Timeouts.Create without Create implementation" | ||
Timeouts: &s.ResourceTimeout{ | ||
Create: s.DefaultTimeout(time.Minute), | ||
}, | ||
} | ||
|
||
_ = s.Resource{ // want "resource should not configure Timeouts.Delete without Delete implementation" | ||
Timeouts: &s.ResourceTimeout{ | ||
Delete: s.DefaultTimeout(time.Minute), | ||
}, | ||
} | ||
|
||
_ = s.Resource{ // want "resource should not configure Timeouts.Read without Read implementation" | ||
Timeouts: &s.ResourceTimeout{ | ||
Read: s.DefaultTimeout(time.Minute), | ||
}, | ||
} | ||
|
||
_ = s.Resource{ // want "resource should not configure Timeouts.Update without Update implementation" | ||
Timeouts: &s.ResourceTimeout{ | ||
Update: s.DefaultTimeout(time.Minute), | ||
}, | ||
} | ||
} |
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,37 @@ | ||
package a | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func fcommentignore() { | ||
//lintignore:XR006 | ||
_ = schema.Resource{ | ||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(time.Minute), | ||
}, | ||
} | ||
|
||
//lintignore:XR006 | ||
_ = schema.Resource{ | ||
Timeouts: &schema.ResourceTimeout{ | ||
Delete: schema.DefaultTimeout(time.Minute), | ||
}, | ||
} | ||
|
||
//lintignore:XR006 | ||
_ = schema.Resource{ | ||
Timeouts: &schema.ResourceTimeout{ | ||
Read: schema.DefaultTimeout(time.Minute), | ||
}, | ||
} | ||
|
||
//lintignore:XR006 | ||
_ = schema.Resource{ | ||
Timeouts: &schema.ResourceTimeout{ | ||
Update: schema.DefaultTimeout(time.Minute), | ||
}, | ||
} | ||
} |
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,37 @@ | ||
package a | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func fcommentignore_v2() { | ||
//lintignore:XR006 | ||
_ = schema.Resource{ | ||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(time.Minute), | ||
}, | ||
} | ||
|
||
//lintignore:XR006 | ||
_ = schema.Resource{ | ||
Timeouts: &schema.ResourceTimeout{ | ||
Delete: schema.DefaultTimeout(time.Minute), | ||
}, | ||
} | ||
|
||
//lintignore:XR006 | ||
_ = schema.Resource{ | ||
Timeouts: &schema.ResourceTimeout{ | ||
Read: schema.DefaultTimeout(time.Minute), | ||
}, | ||
} | ||
|
||
//lintignore:XR006 | ||
_ = schema.Resource{ | ||
Timeouts: &schema.ResourceTimeout{ | ||
Update: schema.DefaultTimeout(time.Minute), | ||
}, | ||
} | ||
} |
Oops, something went wrong.