forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-plugins.go
283 lines (246 loc) · 8.47 KB
/
generate-plugins.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Generate Plugins is a small program that updates the lists of plugins in
// command/internal_plugin_list.go so they will be compiled into the main
// terraform binary.
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strings"
)
const target = "command/internal_plugin_list.go"
func main() {
wd, _ := os.Getwd()
if filepath.Base(wd) != "terraform" {
log.Fatalf("This program must be invoked in the terraform project root; in %s", wd)
}
// Collect all of the data we need about plugins we have in the project
providers, err := discoverProviders()
if err != nil {
log.Fatalf("Failed to discover providers: %s", err)
}
provisioners, err := discoverProvisioners()
if err != nil {
log.Fatalf("Failed to discover provisioners: %s", err)
}
// Do some simple code generation and templating
output := source
output = strings.Replace(output, "IMPORTS", makeImports(providers, provisioners), 1)
output = strings.Replace(output, "PROVIDERS", makeProviderMap(providers), 1)
output = strings.Replace(output, "PROVISIONERS", makeProvisionerMap(provisioners), 1)
// TODO sort the lists of plugins so we are not subjected to random OS ordering of the plugin lists
// Write our generated code to the command/plugin.go file
file, err := os.Create(target)
defer file.Close()
if err != nil {
log.Fatalf("Failed to open %s for writing: %s", target, err)
}
_, err = file.WriteString(output)
if err != nil {
log.Fatalf("Failed writing to %s: %s", target, err)
}
log.Printf("Generated %s", target)
}
type plugin struct {
Package string // Package name from ast remoteexec
PluginName string // Path via deriveName() remote-exec
TypeName string // Type of plugin provisioner
Path string // Relative import path builtin/provisioners/remote-exec
ImportName string // See deriveImport() remoteexecprovisioner
}
// makeProviderMap creates a map of providers like this:
//
// var InternalProviders = map[string]plugin.ProviderFunc{
// "aws": aws.Provider,
// "azurerm": azurerm.Provider,
// "cloudflare": cloudflare.Provider,
func makeProviderMap(items []plugin) string {
output := ""
for _, item := range items {
output += fmt.Sprintf("\t\"%s\": %s.%s,\n", item.PluginName, item.ImportName, item.TypeName)
}
return output
}
// makeProvisionerMap creates a map of provisioners like this:
//
// "file": func() terraform.ResourceProvisioner { return new(file.ResourceProvisioner) },
// "local-exec": func() terraform.ResourceProvisioner { return new(localexec.ResourceProvisioner) },
// "remote-exec": func() terraform.ResourceProvisioner { return new(remoteexec.ResourceProvisioner) },
//
// This is more verbose than the Provider case because there is no corresponding
// Provisioner function.
func makeProvisionerMap(items []plugin) string {
output := ""
for _, item := range items {
output += fmt.Sprintf("\t\"%s\": func() terraform.ResourceProvisioner { return new(%s.%s) },\n", item.PluginName, item.ImportName, item.TypeName)
}
return output
}
func makeImports(providers, provisioners []plugin) string {
plugins := []string{}
for _, provider := range providers {
plugins = append(plugins, fmt.Sprintf("\t%s \"github.com/hashicorp/terraform/%s\"\n", provider.ImportName, filepath.ToSlash(provider.Path)))
}
for _, provisioner := range provisioners {
plugins = append(plugins, fmt.Sprintf("\t%s \"github.com/hashicorp/terraform/%s\"\n", provisioner.ImportName, filepath.ToSlash(provisioner.Path)))
}
// Make things pretty
sort.Strings(plugins)
return strings.Join(plugins, "")
}
// listDirectories recursively lists directories under the specified path
func listDirectories(path string) ([]string, error) {
names := []string{}
items, err := ioutil.ReadDir(path)
if err != nil {
return names, err
}
for _, item := range items {
// We only want directories
if item.IsDir() {
if item.Name() == "test-fixtures" {
continue
}
currentDir := filepath.Join(path, item.Name())
names = append(names, currentDir)
// Do some recursion
subNames, err := listDirectories(currentDir)
if err == nil {
names = append(names, subNames...)
}
}
}
return names, nil
}
// deriveName determines the name of the plugin relative to the specified root
// path.
func deriveName(root, full string) string {
short, _ := filepath.Rel(root, full)
bits := strings.Split(short, string(os.PathSeparator))
return strings.Join(bits, "-")
}
// deriveImport will build a unique import identifier based on packageName and
// the result of deriveName(). This is important for disambigutating between
// providers and provisioners that have the same name. This will be something
// like:
//
// remote-exec -> remoteexecprovisioner
//
// which is long, but is deterministic and unique.
func deriveImport(typeName, derivedName string) string {
return strings.Replace(derivedName, "-", "", -1) + strings.ToLower(typeName)
}
// discoverTypesInPath searches for types of typeID in path using go's ast and
// returns a list of plugins it finds.
func discoverTypesInPath(path, typeID, typeName string) ([]plugin, error) {
pluginTypes := []plugin{}
dirs, err := listDirectories(path)
if err != nil {
return pluginTypes, err
}
for _, dir := range dirs {
fset := token.NewFileSet()
goPackages, err := parser.ParseDir(fset, dir, nil, parser.AllErrors)
if err != nil {
return pluginTypes, fmt.Errorf("Failed parsing directory %s: %s", dir, err)
}
for _, goPackage := range goPackages {
ast.PackageExports(goPackage)
ast.Inspect(goPackage, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.FuncDecl:
// If we get a function then we will check the function name
// against typeName and the function return type (Results)
// against typeID.
//
// There may be more than one return type but in the target
// case there should only be one. Also the return type is a
// ast.SelectorExpr which means we have multiple nodes.
// We'll read all of them as ast.Ident (identifier), join
// them via . to get a string like terraform.ResourceProvider
// and see if it matches our expected typeID
//
// This is somewhat verbose but prevents us from identifying
// the wrong types if the function name is amiguous or if
// there are other subfolders added later.
if x.Name.Name == typeName && len(x.Type.Results.List) == 1 {
node := x.Type.Results.List[0].Type
typeIdentifiers := []string{}
ast.Inspect(node, func(m ast.Node) bool {
switch y := m.(type) {
case *ast.Ident:
typeIdentifiers = append(typeIdentifiers, y.Name)
}
// We need all of the identifiers to join so we
// can't break early here.
return true
})
if strings.Join(typeIdentifiers, ".") == typeID {
derivedName := deriveName(path, dir)
pluginTypes = append(pluginTypes, plugin{
Package: goPackage.Name,
PluginName: derivedName,
ImportName: deriveImport(x.Name.Name, derivedName),
TypeName: x.Name.Name,
Path: dir,
})
}
}
case *ast.TypeSpec:
// In the simpler case we will simply check whether the type
// declaration has the name we were looking for.
if x.Name.Name == typeID {
derivedName := deriveName(path, dir)
pluginTypes = append(pluginTypes, plugin{
Package: goPackage.Name,
PluginName: derivedName,
ImportName: deriveImport(x.Name.Name, derivedName),
TypeName: x.Name.Name,
Path: dir,
})
// The AST stops parsing when we return false. Once we
// find the symbol we want we can stop parsing.
return false
}
}
return true
})
}
}
return pluginTypes, nil
}
func discoverProviders() ([]plugin, error) {
path := "./builtin/providers"
typeID := "terraform.ResourceProvider"
typeName := "Provider"
return discoverTypesInPath(path, typeID, typeName)
}
func discoverProvisioners() ([]plugin, error) {
path := "./builtin/provisioners"
typeID := "ResourceProvisioner"
typeName := ""
return discoverTypesInPath(path, typeID, typeName)
}
const source = `// +build !core
//
// This file is automatically generated by scripts/generate-plugins.go -- Do not edit!
//
package command
import (
IMPORTS
"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/terraform"
)
var InternalProviders = map[string]plugin.ProviderFunc{
PROVIDERS
}
var InternalProvisioners = map[string]plugin.ProvisionerFunc{
PROVISIONERS
}
`