aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/terraform/config/loader_hcl2.go
blob: 4f9f129f2e78853cb07bdceeeb9b30a156c37ca7 (plain) (blame)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package config

import (
	"fmt"
	"sort"
	"strings"

	gohcl2 "github.com/hashicorp/hcl2/gohcl"
	hcl2 "github.com/hashicorp/hcl2/hcl"
	hcl2parse "github.com/hashicorp/hcl2/hclparse"
	"github.com/hashicorp/terraform/config/hcl2shim"
	"github.com/zclconf/go-cty/cty"
)

// hcl2Configurable is an implementation of configurable that knows
// how to turn a HCL Body into a *Config object.
type hcl2Configurable struct {
	SourceFilename string
	Body           hcl2.Body
}

// hcl2Loader is a wrapper around a HCL parser that provides a fileLoaderFunc.
type hcl2Loader struct {
	Parser *hcl2parse.Parser
}

// For the moment we'll just have a global loader since we don't have anywhere
// better to stash this.
// TODO: refactor the loader API so that it uses some sort of object we can
// stash the parser inside.
var globalHCL2Loader = newHCL2Loader()

// newHCL2Loader creates a new hcl2Loader containing a new HCL Parser.
//
// HCL parsers retain information about files that are loaded to aid in
// producing diagnostic messages, so all files within a single configuration
// should be loaded with the same parser to ensure the availability of
// full diagnostic information.
func newHCL2Loader() hcl2Loader {
	return hcl2Loader{
		Parser: hcl2parse.NewParser(),
	}
}

// loadFile is a fileLoaderFunc that knows how to read a HCL2 file and turn it
// into a hcl2Configurable.
func (l hcl2Loader) loadFile(filename string) (configurable, []string, error) {
	var f *hcl2.File
	var diags hcl2.Diagnostics
	if strings.HasSuffix(filename, ".json") {
		f, diags = l.Parser.ParseJSONFile(filename)
	} else {
		f, diags = l.Parser.ParseHCLFile(filename)
	}
	if diags.HasErrors() {
		// Return diagnostics as an error; callers may type-assert this to
		// recover the original diagnostics, if it doesn't end up wrapped
		// in another error.
		return nil, nil, diags
	}

	return &hcl2Configurable{
		SourceFilename: filename,
		Body:           f.Body,
	}, nil, nil
}

func (t *hcl2Configurable) Config() (*Config, error) {
	config := &Config{}

	// these structs are used only for the initial shallow decoding; we'll
	// expand this into the main, public-facing config structs afterwards.
	type atlas struct {
		Name    string    `hcl:"name"`
		Include *[]string `hcl:"include"`
		Exclude *[]string `hcl:"exclude"`
	}
	type provider struct {
		Name    string    `hcl:"name,label"`
		Alias   *string   `hcl:"alias,attr"`
		Version *string   `hcl:"version,attr"`
		Config  hcl2.Body `hcl:",remain"`
	}
	type module struct {
		Name      string             `hcl:"name,label"`
		Source    string             `hcl:"source,attr"`
		Version   *string            `hcl:"version,attr"`
		Providers *map[string]string `hcl:"providers,attr"`
		Config    hcl2.Body          `hcl:",remain"`
	}
	type resourceLifecycle struct {
		CreateBeforeDestroy *bool     `hcl:"create_before_destroy,attr"`
		PreventDestroy      *bool     `hcl:"prevent_destroy,attr"`
		IgnoreChanges       *[]string `hcl:"ignore_changes,attr"`
	}
	type connection struct {
		Config hcl2.Body `hcl:",remain"`
	}
	type provisioner struct {
		Type string `hcl:"type,label"`

		When      *string `hcl:"when,attr"`
		OnFailure *string `hcl:"on_failure,attr"`

		Connection *connection `hcl:"connection,block"`
		Config     hcl2.Body   `hcl:",remain"`
	}
	type managedResource struct {
		Type string `hcl:"type,label"`
		Name string `hcl:"name,label"`

		CountExpr hcl2.Expression `hcl:"count,attr"`
		Provider  *string         `hcl:"provider,attr"`
		DependsOn *[]string       `hcl:"depends_on,attr"`

		Lifecycle    *resourceLifecycle `hcl:"lifecycle,block"`
		Provisioners []provisioner      `hcl:"provisioner,block"`
		Connection   *connection        `hcl:"connection,block"`

		Config hcl2.Body `hcl:",remain"`
	}
	type dataResource struct {
		Type string `hcl:"type,label"`
		Name string `hcl:"name,label"`

		CountExpr hcl2.Expression `hcl:"count,attr"`
		Provider  *string         `hcl:"provider,attr"`
		DependsOn *[]string       `hcl:"depends_on,attr"`

		Config hcl2.Body `hcl:",remain"`
	}
	type variable struct {
		Name string `hcl:"name,label"`

		DeclaredType *string    `hcl:"type,attr"`
		Default      *cty.Value `hcl:"default,attr"`
		Description  *string    `hcl:"description,attr"`
		Sensitive    *bool      `hcl:"sensitive,attr"`
	}
	type output struct {
		Name string `hcl:"name,label"`

		ValueExpr   hcl2.Expression `hcl:"value,attr"`
		DependsOn   *[]string       `hcl:"depends_on,attr"`
		Description *string         `hcl:"description,attr"`
		Sensitive   *bool           `hcl:"sensitive,attr"`
	}
	type locals struct {
		Definitions hcl2.Attributes `hcl:",remain"`
	}
	type backend struct {
		Type   string    `hcl:"type,label"`
		Config hcl2.Body `hcl:",remain"`
	}
	type terraform struct {
		RequiredVersion *string  `hcl:"required_version,attr"`
		Backend         *backend `hcl:"backend,block"`
	}
	type topLevel struct {
		Atlas     *atlas            `hcl:"atlas,block"`
		Datas     []dataResource    `hcl:"data,block"`
		Modules   []module          `hcl:"module,block"`
		Outputs   []output          `hcl:"output,block"`
		Providers []provider        `hcl:"provider,block"`
		Resources []managedResource `hcl:"resource,block"`
		Terraform *terraform        `hcl:"terraform,block"`
		Variables []variable        `hcl:"variable,block"`
		Locals    []*locals         `hcl:"locals,block"`
	}

	var raw topLevel
	diags := gohcl2.DecodeBody(t.Body, nil, &raw)
	if diags.HasErrors() {
		// Do some minimal decoding to see if we can at least get the
		// required Terraform version, which might help explain why we
		// couldn't parse the rest.
		if raw.Terraform != nil && raw.Terraform.RequiredVersion != nil {
			config.Terraform = &Terraform{
				RequiredVersion: *raw.Terraform.RequiredVersion,
			}
		}

		// We return the diags as an implementation of error, which the
		// caller than then type-assert if desired to recover the individual
		// diagnostics.
		// FIXME: The current API gives us no way to return warnings in the
		// absense of any errors.
		return config, diags
	}

	if raw.Terraform != nil {
		var reqdVersion string
		var backend *Backend

		if raw.Terraform.RequiredVersion != nil {
			reqdVersion = *raw.Terraform.RequiredVersion
		}
		if raw.Terraform.Backend != nil {
			backend = new(Backend)
			backend.Type = raw.Terraform.Backend.Type

			// We don't permit interpolations or nested blocks inside the
			// backend config, so we can decode the config early here and
			// get direct access to the values, which is important for the
			// config hashing to work as expected.
			var config map[string]string
			configDiags := gohcl2.DecodeBody(raw.Terraform.Backend.Config, nil, &config)
			diags = append(diags, configDiags...)

			raw := make(map[string]interface{}, len(config))
			for k, v := range config {
				raw[k] = v
			}

			var err error
			backend.RawConfig, err = NewRawConfig(raw)
			if err != nil {
				diags = append(diags, &hcl2.Diagnostic{
					Severity: hcl2.DiagError,
					Summary:  "Invalid backend configuration",
					Detail:   fmt.Sprintf("Error in backend configuration: %s", err),
				})
			}
		}

		config.Terraform = &Terraform{
			RequiredVersion: reqdVersion,
			Backend:         backend,
		}
	}

	if raw.Atlas != nil {
		var include, exclude []string
		if raw.Atlas.Include != nil {
			include = *raw.Atlas.Include
		}
		if raw.Atlas.Exclude != nil {
			exclude = *raw.Atlas.Exclude
		}
		config.Atlas = &AtlasConfig{
			Name:    raw.Atlas.Name,
			Include: include,
			Exclude: exclude,
		}
	}

	for _, rawM := range raw.Modules {
		m := &Module{
			Name:      rawM.Name,
			Source:    rawM.Source,
			RawConfig: NewRawConfigHCL2(rawM.Config),
		}

		if rawM.Version != nil {
			m.Version = *rawM.Version
		}

		if rawM.Providers != nil {
			m.Providers = *rawM.Providers
		}

		config.Modules = append(config.Modules, m)
	}

	for _, rawV := range raw.Variables {
		v := &Variable{
			Name: rawV.Name,
		}
		if rawV.DeclaredType != nil {
			v.DeclaredType = *rawV.DeclaredType
		}
		if rawV.Default != nil {
			v.Default = hcl2shim.ConfigValueFromHCL2(*rawV.Default)
		}
		if rawV.Description != nil {
			v.Description = *rawV.Description
		}

		config.Variables = append(config.Variables, v)
	}

	for _, rawO := range raw.Outputs {
		o := &Output{
			Name: rawO.Name,
		}

		if rawO.Description != nil {
			o.Description = *rawO.Description
		}
		if rawO.DependsOn != nil {
			o.DependsOn = *rawO.DependsOn
		}
		if rawO.Sensitive != nil {
			o.Sensitive = *rawO.Sensitive
		}

		// The result is expected to be a map like map[string]interface{}{"value": something},
		// so we'll fake that with our hcl2shim.SingleAttrBody shim.
		o.RawConfig = NewRawConfigHCL2(hcl2shim.SingleAttrBody{
			Name: "value",
			Expr: rawO.ValueExpr,
		})

		config.Outputs = append(config.Outputs, o)
	}

	for _, rawR := range raw.Resources {
		r := &Resource{
			Mode: ManagedResourceMode,
			Type: rawR.Type,
			Name: rawR.Name,
		}
		if rawR.Lifecycle != nil {
			var l ResourceLifecycle
			if rawR.Lifecycle.CreateBeforeDestroy != nil {
				l.CreateBeforeDestroy = *rawR.Lifecycle.CreateBeforeDestroy
			}
			if rawR.Lifecycle.PreventDestroy != nil {
				l.PreventDestroy = *rawR.Lifecycle.PreventDestroy
			}
			if rawR.Lifecycle.IgnoreChanges != nil {
				l.IgnoreChanges = *rawR.Lifecycle.IgnoreChanges
			}
			r.Lifecycle = l
		}
		if rawR.Provider != nil {
			r.Provider = *rawR.Provider
		}
		if rawR.DependsOn != nil {
			r.DependsOn = *rawR.DependsOn
		}

		var defaultConnInfo *RawConfig
		if rawR.Connection != nil {
			defaultConnInfo = NewRawConfigHCL2(rawR.Connection.Config)
		}

		for _, rawP := range rawR.Provisioners {
			p := &Provisioner{
				Type: rawP.Type,
			}

			switch {
			case rawP.When == nil:
				p.When = ProvisionerWhenCreate
			case *rawP.When == "create":
				p.When = ProvisionerWhenCreate
			case *rawP.When == "destroy":
				p.When = ProvisionerWhenDestroy
			default:
				p.When = ProvisionerWhenInvalid
			}

			switch {
			case rawP.OnFailure == nil:
				p.OnFailure = ProvisionerOnFailureFail
			case *rawP.When == "fail":
				p.OnFailure = ProvisionerOnFailureFail
			case *rawP.When == "continue":
				p.OnFailure = ProvisionerOnFailureContinue
			default:
				p.OnFailure = ProvisionerOnFailureInvalid
			}

			if rawP.Connection != nil {
				p.ConnInfo = NewRawConfigHCL2(rawP.Connection.Config)
			} else {
				p.ConnInfo = defaultConnInfo
			}

			p.RawConfig = NewRawConfigHCL2(rawP.Config)

			r.Provisioners = append(r.Provisioners, p)
		}

		// The old loader records the count expression as a weird RawConfig with
		// a single-element map inside. Since the rest of the world is assuming
		// that, we'll mimic it here.
		{
			countBody := hcl2shim.SingleAttrBody{
				Name: "count",
				Expr: rawR.CountExpr,
			}

			r.RawCount = NewRawConfigHCL2(countBody)
			r.RawCount.Key = "count"
		}

		r.RawConfig = NewRawConfigHCL2(rawR.Config)

		config.Resources = append(config.Resources, r)

	}

	for _, rawR := range raw.Datas {
		r := &Resource{
			Mode: DataResourceMode,
			Type: rawR.Type,
			Name: rawR.Name,
		}

		if rawR.Provider != nil {
			r.Provider = *rawR.Provider
		}
		if rawR.DependsOn != nil {
			r.DependsOn = *rawR.DependsOn
		}

		// The old loader records the count expression as a weird RawConfig with
		// a single-element map inside. Since the rest of the world is assuming
		// that, we'll mimic it here.
		{
			countBody := hcl2shim.SingleAttrBody{
				Name: "count",
				Expr: rawR.CountExpr,
			}

			r.RawCount = NewRawConfigHCL2(countBody)
			r.RawCount.Key = "count"
		}

		r.RawConfig = NewRawConfigHCL2(rawR.Config)

		config.Resources = append(config.Resources, r)
	}

	for _, rawP := range raw.Providers {
		p := &ProviderConfig{
			Name: rawP.Name,
		}

		if rawP.Alias != nil {
			p.Alias = *rawP.Alias
		}
		if rawP.Version != nil {
			p.Version = *rawP.Version
		}

		// The result is expected to be a map like map[string]interface{}{"value": something},
		// so we'll fake that with our hcl2shim.SingleAttrBody shim.
		p.RawConfig = NewRawConfigHCL2(rawP.Config)

		config.ProviderConfigs = append(config.ProviderConfigs, p)
	}

	for _, rawL := range raw.Locals {
		names := make([]string, 0, len(rawL.Definitions))
		for n := range rawL.Definitions {
			names = append(names, n)
		}
		sort.Strings(names)
		for _, n := range names {
			attr := rawL.Definitions[n]
			l := &Local{
				Name: n,
				RawConfig: NewRawConfigHCL2(hcl2shim.SingleAttrBody{
					Name: "value",
					Expr: attr.Expr,
				}),
			}
			config.Locals = append(config.Locals, l)
		}
	}

	// FIXME: The current API gives us no way to return warnings in the
	// absense of any errors.
	var err error
	if diags.HasErrors() {
		err = diags
	}

	return config, err
}