This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
/
generator.go
215 lines (190 loc) · 8.39 KB
/
generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2023 Volvo Car Corporation
// SPDX-License-Identifier: Apache-2.0
package terrajen
import (
"go/token"
"path/filepath"
"strings"
"github.com/veggiemonk/strcase"
tfjson "github.com/hashicorp/terraform-json"
)
// ProviderGenerator is created for each provider and is used to generate the schema
// for each resource and data object, and the provider configuration.
// The schemas are used by the generator to create the Go files and sub packages.
type ProviderGenerator struct {
// GoProviderPkgPath is the Go pkg path to the generated provider directory.
// E.g. github.com/volvo-cars/github.com/volvo-cars/lingon/gen/aws
GoProviderPkgPath string
// GeneratedPackageLocation is the directory on the filesystem where the generated
// Go files will be created.
// The GoProviderPkgPath path must match the location of the generated files
// so that they can be imported correctly.
// E.g. if we are in a Go module called "my-module" and we generate the files in a
// "gen" directory within the root of "my-module", then GoProviderPkgPath is "my-module/gen"
// and the GeneratedPackageLocation is "./gen" assuming we are running from the root of
// "my-module"
GeneratedPackageLocation string
// ProviderName is the local name of the provider.
// E.g. aws
// https://developer.hashicorp.com/terraform/language/providers/requirements#local-names
ProviderName string
// ProviderSource is the source address of the provider.
// E.g. registry.terraform.io/hashicorp/aws
// https://developer.hashicorp.com/terraform/language/providers/requirements#source-addresses
ProviderSource string
// ProviderVersion is the version of thr provider.
// E.g. 4.49.0
ProviderVersion string
}
type SchemaType string
const (
SchemaTypeProvider SchemaType = "provider"
SchemaTypeResource SchemaType = "resource"
SchemaTypeData SchemaType = "data"
)
// SchemaProvider creates a schema for the provider config block for the provider
// represented by ProviderGenerator
func (a *ProviderGenerator) SchemaProvider(sb *tfjson.SchemaBlock) *Schema {
return &Schema{
SchemaType: SchemaTypeProvider,
GoProviderPkgPath: a.GoProviderPkgPath, // github.com/volvo-cars/github.com/volvo-cars/lingon/gen/aws
GeneratedPackageLocation: a.GeneratedPackageLocation, // gen/aws
ProviderName: a.ProviderName, // aws
ProviderSource: a.ProviderSource, // registry.terraform.io/hashicorp/aws
ProviderVersion: a.ProviderVersion, // 4.49.0
PackageName: a.ProviderName, // aws
ShortName: "provider",
Type: "provider",
StructName: "Provider",
ArgumentStructName: "ProviderArgs",
Receiver: structReceiverFromName("provider"),
NewFuncName: "NewProvider",
SubPackageName: "provider",
FilePath: filepath.Join(
a.GeneratedPackageLocation,
"provider"+fileExtension,
),
graph: newGraph(sb),
}
}
// SchemaResource creates a schema for the given resource for the provider
// represented by ProviderGenerator
func (a *ProviderGenerator) SchemaResource(
name string,
sb *tfjson.SchemaBlock,
) *Schema {
shortName := providerShortName(name)
spn := strings.ReplaceAll(shortName, "_", "")
fp := filepath.Join(a.GeneratedPackageLocation, shortName+fileExtension)
rs := &Schema{
SchemaType: SchemaTypeResource,
GoProviderPkgPath: a.GoProviderPkgPath, // github.com/volvo-cars/github.com/volvo-cars/lingon/gen/aws
GeneratedPackageLocation: a.GeneratedPackageLocation, // gen/aws
ProviderName: a.ProviderName, // aws
ProviderSource: a.ProviderSource, // hashicorp/aws
ProviderVersion: a.ProviderVersion, // 4.49.0
ShortName: shortName, // aws_iam_role => iam_role
PackageName: a.ProviderName, // aws
Type: name, // aws
StructName: strcase.Pascal(shortName), // iam_role => IamRole
ArgumentStructName: strcase.Pascal(shortName) + suffixArgs, // iam_role => IamRoleArgs
AttributesStructName: strcase.Camel(shortName) + suffixAttributes, // iam_role => iamRoleAttributes
StateStructName: strcase.Camel(shortName) + suffixState, // iam_role => IamRoleOut
Receiver: structReceiverFromName(shortName), // iam_role => ir
NewFuncName: "New" + strcase.Pascal(shortName),
SubPackageName: spn, // iam_role => iamrole
FilePath: fp,
graph: newGraph(sb),
}
return rs
}
// SchemaData creates a schema for the given data object for the provider
// represented by ProviderGenerator
func (a *ProviderGenerator) SchemaData(
name string,
sb *tfjson.SchemaBlock,
) *Schema {
shortName := providerShortName(name)
spn := strings.ReplaceAll(shortName, "_", "")
dataName := "data_" + shortName
fp := filepath.Join(a.GeneratedPackageLocation, dataName+fileExtension)
pn := strcase.Pascal(shortName)
ds := &Schema{
SchemaType: SchemaTypeData,
GoProviderPkgPath: a.GoProviderPkgPath, // github.com/volvo-cars/github.com/volvo-cars/lingon/gen/aws
GeneratedPackageLocation: a.GeneratedPackageLocation, // gen/aws
ProviderName: a.ProviderName, // aws
ProviderSource: a.ProviderSource, // hashicorp/aws
ProviderVersion: a.ProviderVersion, // 4.49.0
ShortName: shortName, // aws_iam_role => iam_role
PackageName: a.ProviderName, // aws
Type: name, // aws_iam_role
StructName: "Data" + pn, // iam_role => DataIamRole
ArgumentStructName: "Data" + pn + suffixArgs, // iam_role => DataIamRoleArgs
AttributesStructName: "data" + pn + suffixAttributes, // iam_role => dataIamRoleAttributes
Receiver: structReceiverFromName(shortName), // iam_role => ir
NewFuncName: "NewData" + pn, // iam_role => NewDataIamRole
SubPackageName: "data" + spn, // iam_role => dataiamrole
FilePath: fp,
graph: newGraph(sb),
}
return ds
}
// providerShortName takes a name like "aws_iam_role" and returns the name without
// the leading provider prefix, i.e. it returns "iam_role"
func providerShortName(name string) string {
underscoreIndex := strings.Index(name, "_")
if underscoreIndex == -1 {
return name
}
return name[underscoreIndex+1:]
}
// structReceiverFromName calculates a suitable receiver from the name of the object.
// It gets the first character of each word separated by underscores, e.g. iam_role => ir
func structReceiverFromName(name string) string {
ss := strings.Split(name, "_")
var receiver strings.Builder
for _, s := range ss {
receiver.WriteString(s[0:1])
}
r := receiver.String()
// Avoid using keywords for the receiver!
if token.Lookup(r).IsKeyword() || r == "nil" {
r = "_" + r
}
return r
}
// Schema is used to store all the relevant information required for the Go
// code generator.
// A schema can represent a resource, a data object or the provider configuration.
type Schema struct {
SchemaType SchemaType // resource / provider / data
GoProviderPkgPath string // github.com/volvo-cars/github.com/volvo-cars/lingon/gen/providers
GeneratedPackageLocation string // gen/providers/aws
ProviderName string // aws
ProviderSource string // registry.terraform.io/hashicorp/aws
ProviderVersion string // 4.49.0
ShortName string // aws_iam_role => iam_role
PackageName string // aws
Type string // aws_iam_role
// Structs
StructName string // iam_role => IamRole
ArgumentStructName string // iam_role => IamRoleArgs
AttributesStructName string // iam_role => iamRoleAttributes
StateStructName string // iam_role => iamRoleState
Receiver string // iam_role => ir
NewFuncName string // iam_role => NewIamRole
SubPackageName string // iam_role => iamrole
FilePath string // gen/providers/aws/ xxx
graph *graph
}
func (s *Schema) SubPkgQualPath() string {
return s.GoProviderPkgPath + "/" + s.SubPackageName
}
func (s *Schema) SubPkgPath() string {
return filepath.Join(
s.GeneratedPackageLocation,
s.SubPackageName,
s.ShortName+fileExtension,
)
}