Skip to content

Commit 34323bf

Browse files
authored
Remove hashicorp/terraform-provider-aws build dependency (#144)
* Remove hashicorp/terraform-provider-aws build dependency * Add go:generate for rules/api * Add README to tools/provider-schema
1 parent 2d95a80 commit 34323bf

File tree

14 files changed

+144
-416
lines changed

14 files changed

+144
-416
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tools/provider-schema/.terraform/

go.mod

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,34 @@ module github.com/terraform-linters/tflint-ruleset-aws
33
go 1.16
44

55
require (
6+
github.com/agext/levenshtein v1.2.2 // indirect
67
github.com/aws/aws-sdk-go v1.39.0
78
github.com/dave/dst v0.26.2
9+
github.com/fatih/color v1.9.0 // indirect
810
github.com/golang/mock v1.6.0
911
github.com/google/go-cmp v0.5.6
1012
github.com/hashicorp/aws-sdk-go-base v0.7.1
13+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
14+
github.com/hashicorp/go-multierror v1.1.1 // indirect
1115
github.com/hashicorp/hcl/v2 v2.10.0
12-
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.0
16+
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
17+
github.com/kr/text v0.2.0 // indirect
18+
github.com/kylelemons/godebug v1.1.0 // indirect
19+
github.com/mattn/go-colorable v0.1.7 // indirect
1320
github.com/mitchellh/go-homedir v1.1.0
21+
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
22+
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
1423
github.com/onsi/ginkgo v1.14.2 // indirect
1524
github.com/onsi/gomega v1.10.4 // indirect
1625
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e
26+
github.com/sergi/go-diff v1.2.0 // indirect
27+
github.com/stretchr/testify v1.7.0 // indirect
1728
github.com/terraform-linters/tflint-plugin-sdk v0.9.0
18-
github.com/terraform-providers/terraform-provider-aws v1.60.1-0.20210702165135-63f852f1a2b6
1929
github.com/zclconf/go-cty v1.8.4
30+
google.golang.org/appengine v1.6.6 // indirect
31+
google.golang.org/genproto v0.0.0-20200711021454-869866162049 // indirect
32+
google.golang.org/grpc v1.32.0 // indirect
33+
google.golang.org/protobuf v1.25.0 // indirect
34+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
35+
gopkg.in/yaml.v2 v2.4.0 // indirect
2036
)

go.sum

Lines changed: 1 addition & 391 deletions
Large diffs are not rendered by default.

rules/api/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//go:generate go run -tags generators ./generator/main.go
2+
3+
package api

rules/api/generator/main.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import (
99

1010
"github.com/hashicorp/hcl/v2/gohcl"
1111
"github.com/hashicorp/hcl/v2/hclparse"
12-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1312
utils "github.com/terraform-linters/tflint-ruleset-aws/rules/generator-utils"
14-
"github.com/terraform-providers/terraform-provider-aws/aws"
1513
)
1614

1715
type definition struct {
@@ -40,7 +38,7 @@ type providerMeta struct {
4038
RuleNameCCList []string
4139
}
4240

43-
var awsProvider = aws.Provider()
41+
var awsProvider = utils.LoadProviderSchema("../../tools/provider-schema/schema.json")
4442

4543
func main() {
4644
files, err := filepath.Glob("./definitions/*.hcl")
@@ -92,19 +90,25 @@ func main() {
9290
}
9391

9492
func dataType(resource, attribute string) string {
95-
resourceSchema, ok := awsProvider.ResourcesMap[resource]
93+
resourceSchema, ok := awsProvider.ResourceSchemas[resource]
9694
if !ok {
9795
panic(fmt.Sprintf("resource `%s` not found in the Terraform schema", resource))
9896
}
99-
attrSchema, ok := resourceSchema.Schema[attribute]
97+
attrSchema, ok := resourceSchema.Block.Attributes[attribute]
10098
if !ok {
10199
panic(fmt.Sprintf("`%s.%s` not found in the Terraform schema", resource, attribute))
102100
}
103101

104-
switch attrSchema.Type {
105-
case schema.TypeString:
102+
switch ty := attrSchema.Type.(type) {
103+
case string:
104+
if ty != "string" {
105+
panic(fmt.Errorf("Unexpected data type: %#v", attrSchema.Type))
106+
}
106107
return "string"
107-
case schema.TypeSet:
108+
case []interface{}:
109+
if len(ty) != 2 || !(ty[0] == "set" && ty[1] == "string") {
110+
panic(fmt.Errorf("Unexpected data type: %#v", attrSchema.Type))
111+
}
108112
return "list"
109113
default:
110114
panic(fmt.Errorf("Unexpected data type: %#v", attrSchema.Type))

rules/generator-utils/schema.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package utils
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
)
7+
8+
type Schema struct {
9+
ProviderSchemas ProviderSchemas `json:"provider_schemas"`
10+
}
11+
12+
type ProviderSchemas struct {
13+
AWS ProviderSchema `json:"registry.terraform.io/hashicorp/aws"`
14+
}
15+
16+
type ProviderSchema struct {
17+
ResourceSchemas map[string]ResourceSchema `json:"resource_schemas"`
18+
}
19+
20+
type ResourceSchema struct {
21+
Block BlockSchema `json:"block"`
22+
}
23+
24+
type BlockSchema struct {
25+
Attributes map[string]AttributeSchema `json:"attributes"`
26+
BlockTypes map[string]ResourceSchema `json:"block_types"`
27+
}
28+
29+
type AttributeSchema struct {
30+
Type interface{} `json:"type"`
31+
Sensitive bool `json:"sensitive"`
32+
}
33+
34+
func LoadProviderSchema(path string) ProviderSchema {
35+
src, err := ioutil.ReadFile(path)
36+
if err != nil {
37+
panic(err)
38+
}
39+
40+
var schema Schema
41+
if err := json.Unmarshal(src, &schema); err != nil {
42+
panic(err)
43+
}
44+
return schema.ProviderSchemas.AWS
45+
}

rules/models/generator/main.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import (
1212
hcl "github.com/hashicorp/hcl/v2"
1313
"github.com/hashicorp/hcl/v2/gohcl"
1414
"github.com/hashicorp/hcl/v2/hclparse"
15-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
16-
"github.com/terraform-providers/terraform-provider-aws/aws"
15+
utils "github.com/terraform-linters/tflint-ruleset-aws/rules/generator-utils"
1716
)
1817

1918
type mappingFile struct {
@@ -56,7 +55,7 @@ func main() {
5655
mappingFiles = append(mappingFiles, mf)
5756
}
5857

59-
awsProvider := aws.Provider()
58+
awsProvider := utils.LoadProviderSchema("../../tools/provider-schema/schema.json")
6059

6160
generatedRules := []string{}
6261
for _, mappingFile := range mappingFiles {
@@ -99,20 +98,26 @@ func main() {
9998
generateDocFile(generatedRules)
10099
}
101100

102-
func fetchSchema(resource, attribute string, model map[string]interface{}, provider *schema.Provider) *schema.Schema {
103-
resourceSchema, ok := provider.ResourcesMap[resource]
101+
func fetchSchema(resource, attribute string, model map[string]interface{}, provider utils.ProviderSchema) utils.AttributeSchema {
102+
resourceSchema, ok := provider.ResourceSchemas[resource]
104103
if !ok {
105104
panic(fmt.Sprintf("resource `%s` not found in the Terraform schema", resource))
106105
}
107-
attrSchema, ok := resourceSchema.Schema[attribute]
106+
attrSchema, ok := resourceSchema.Block.Attributes[attribute]
108107
if !ok {
109-
panic(fmt.Sprintf("`%s.%s` not found in the Terraform schema", resource, attribute))
108+
if _, ok := resourceSchema.Block.BlockTypes[attribute]; !ok {
109+
panic(fmt.Sprintf("`%s.%s` not found in the Terraform schema", resource, attribute))
110+
}
110111
}
111112

112113
switch model["type"].(string) {
113114
case "string":
114-
if attrSchema.Type != schema.TypeString {
115-
panic(fmt.Sprintf("`%s.%s` is expected as string, but not", resource, attribute))
115+
ty, ok := attrSchema.Type.(string)
116+
if !ok {
117+
panic(fmt.Sprintf("`%s.%s` is expected as string, but not (%s)", resource, attribute, attrSchema.Type))
118+
}
119+
if ty != "string" && ty != "number" {
120+
panic(fmt.Sprintf("`%s.%s` is expected as string, but not (%s)", resource, attribute, attrSchema.Type))
116121
}
117122
default:
118123
// noop

rules/models/generator/rule.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"regexp"
88
"strings"
99

10-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1110
utils "github.com/terraform-linters/tflint-ruleset-aws/rules/generator-utils"
1211
)
1312

@@ -25,7 +24,7 @@ type ruleMeta struct {
2524
TestNG string
2625
}
2726

28-
func generateRuleFile(resource, attribute string, model map[string]interface{}, schema *schema.Schema) {
27+
func generateRuleFile(resource, attribute string, model map[string]interface{}, schema utils.AttributeSchema) {
2928
ruleName := makeRuleName(resource, attribute)
3029

3130
meta := &ruleMeta{

rules/tags/generator/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"sort"
1111
"text/template"
1212

13-
"github.com/terraform-providers/terraform-provider-aws/aws"
13+
utils "github.com/terraform-linters/tflint-ruleset-aws/rules/generator-utils"
1414
)
1515

1616
const filename = "resources.go"
@@ -20,11 +20,11 @@ type TemplateData struct {
2020
}
2121

2222
func main() {
23-
provider := aws.Provider()
23+
provider := utils.LoadProviderSchema("../../tools/provider-schema/schema.json")
2424
resources := make([]string, 0)
2525

26-
for name, resource := range provider.ResourcesMap {
27-
if _, ok := resource.Schema["tags"]; ok {
26+
for name, resource := range provider.ResourceSchemas {
27+
if _, ok := resource.Block.Attributes["tags"]; ok {
2828
resources = append(resources, name)
2929
}
3030
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.3

0 commit comments

Comments
 (0)