/
backend.go
51 lines (42 loc) · 1.32 KB
/
backend.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package earlydecoder
import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform-schema/backend"
"github.com/zclconf/go-cty/cty"
)
func decodeBackendsBlock(block *hcl.Block) (backend.BackendData, hcl.Diagnostics) {
bType := block.Labels[0]
attrs, diags := block.Body.JustAttributes()
switch bType {
case "remote":
if attr, ok := attrs["hostname"]; ok {
val, vDiags := attr.Expr.Value(nil)
diags = append(diags, vDiags...)
if val.IsWhollyKnown() && val.Type() == cty.String {
return &backend.Remote{
Hostname: val.AsString(),
}, nil
}
}
return &backend.Remote{}, nil
}
return &backend.UnknownBackendData{}, diags
}
func decodeCloudBlock(block *hcl.Block) (*backend.Cloud, hcl.Diagnostics) {
attrs, _ := block.Body.JustAttributes()
// Ignore diagnostics which may complain about unknown blocks
// https://developer.hashicorp.com/terraform/language/settings/terraform-cloud#usage-example
// Required for Terraform Enterprise
// Defaults to app.terraform.io for HCP Terraform
if attr, ok := attrs["hostname"]; ok {
val, vDiags := attr.Expr.Value(nil)
if val.IsWhollyKnown() && val.Type() == cty.String {
return &backend.Cloud{
Hostname: val.AsString(),
}, vDiags
}
}
return &backend.Cloud{}, nil
}