Skip to content

Commit e6bfe0e

Browse files
authored
generator: use terraform-json types for provider schemas (#589)
1 parent 72203f1 commit e6bfe0e

File tree

9 files changed

+24
-54
lines changed

9 files changed

+24
-54
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ require (
2828
)
2929

3030
require (
31+
github.com/hashicorp/terraform-json v0.20.0
3132
github.com/stretchr/testify v1.8.4
3233
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53
3334
golang.org/x/net v0.19.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mO
4444
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
4545
github.com/hashicorp/hcl/v2 v2.19.1 h1://i05Jqznmb2EXqa39Nsvyan2o5XyMowW5fnCKW5RPI=
4646
github.com/hashicorp/hcl/v2 v2.19.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE=
47+
github.com/hashicorp/terraform-json v0.20.0 h1:cJcvn4gIOTi0SD7pIy+xiofV1zFA3hza+6K+fo52IX8=
48+
github.com/hashicorp/terraform-json v0.20.0/go.mod h1:qdeBs11ovMzo5puhrRibdD6d2Dq6TyE/28JiU4tIQxk=
4749
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
4850
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
4951
github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE=

rules/api/generator/main.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build generators
1+
//go:build generators
22

33
package main
44

@@ -99,18 +99,10 @@ func dataType(resource, attribute string) string {
9999
panic(fmt.Sprintf("`%s.%s` not found in the Terraform schema", resource, attribute))
100100
}
101101

102-
switch ty := attrSchema.Type.(type) {
103-
case string:
104-
if ty != "string" {
105-
panic(fmt.Errorf("Unexpected data type: %#v", attrSchema.Type))
106-
}
102+
if ty := attrSchema.AttributeType.FriendlyName(); ty == "string" {
107103
return "string"
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-
}
104+
} else if attrSchema.AttributeType.IsSetType() || attrSchema.AttributeType.IsListType() {
112105
return "list"
113-
default:
114-
panic(fmt.Errorf("Unexpected data type: %#v", attrSchema.Type))
115106
}
107+
panic(fmt.Errorf("Unexpected data type: %#v", attrSchema.AttributeType))
116108
}

rules/generator-utils/schema.go

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,19 @@ package utils
33
import (
44
"encoding/json"
55
"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-
}
196

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-
}
7+
tfjson "github.com/hashicorp/terraform-json"
8+
)
339

34-
func LoadProviderSchema(path string) ProviderSchema {
10+
func LoadProviderSchema(path string) *tfjson.ProviderSchema {
3511
src, err := ioutil.ReadFile(path)
3612
if err != nil {
3713
panic(err)
3814
}
3915

40-
var schema Schema
16+
var schema tfjson.ProviderSchemas
4117
if err := json.Unmarshal(src, &schema); err != nil {
4218
panic(err)
4319
}
44-
return schema.ProviderSchemas.AWS
20+
return schema.Schemas["registry.terraform.io/hashicorp/aws"]
4521
}

rules/models/generator/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build generators
1+
//go:build generators
22

33
package main
44

rules/models/generator/main.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build generators
1+
//go:build generators
22

33
package main
44

@@ -12,6 +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+
tfjson "github.com/hashicorp/terraform-json"
1516
utils "github.com/terraform-linters/tflint-ruleset-aws/rules/generator-utils"
1617
)
1718

@@ -99,26 +100,23 @@ func main() {
99100
generateDocFile(generatedRules)
100101
}
101102

102-
func fetchSchema(resource, attribute string, model map[string]interface{}, provider utils.ProviderSchema) utils.AttributeSchema {
103+
func fetchSchema(resource, attribute string, model map[string]interface{}, provider *tfjson.ProviderSchema) *tfjson.SchemaAttribute {
103104
resourceSchema, ok := provider.ResourceSchemas[resource]
104105
if !ok {
105106
panic(fmt.Sprintf("resource `%s` not found in the Terraform schema", resource))
106107
}
107108
attrSchema, ok := resourceSchema.Block.Attributes[attribute]
108109
if !ok {
109-
if _, ok := resourceSchema.Block.BlockTypes[attribute]; !ok {
110+
if _, ok := resourceSchema.Block.NestedBlocks[attribute]; !ok {
110111
panic(fmt.Sprintf("`%s.%s` not found in the Terraform schema", resource, attribute))
111112
}
112113
}
113114

114115
switch model["type"].(string) {
115116
case "string":
116-
ty, ok := attrSchema.Type.(string)
117-
if !ok {
118-
panic(fmt.Sprintf("`%s.%s` is expected as string, but not (%s)", resource, attribute, attrSchema.Type))
119-
}
117+
ty := attrSchema.AttributeType.FriendlyName()
120118
if ty != "string" && ty != "number" {
121-
panic(fmt.Sprintf("`%s.%s` is expected as string, but not (%s)", resource, attribute, attrSchema.Type))
119+
panic(fmt.Sprintf("`%s.%s` is expected as string, but not (%s)", resource, attribute, ty))
122120
}
123121
default:
124122
// noop

rules/models/generator/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build generators
1+
//go:build generators
22

33
package main
44

rules/models/generator/rule.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build generators
1+
//go:build generators
22

33
package main
44

@@ -7,6 +7,7 @@ import (
77
"regexp"
88
"strings"
99

10+
tfjson "github.com/hashicorp/terraform-json"
1011
utils "github.com/terraform-linters/tflint-ruleset-aws/rules/generator-utils"
1112
)
1213

@@ -24,7 +25,7 @@ type ruleMeta struct {
2425
TestNG string
2526
}
2627

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

3031
meta := &ruleMeta{

rules/tags/generator/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build generators
1+
//go:build generators
22

33
package main
44

0 commit comments

Comments
 (0)