-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathterraform_backend.go
102 lines (95 loc) · 2.58 KB
/
terraform_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
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
package main
import (
"fmt"
"io/ioutil"
"log"
"sync"
backendInit "github.com/hashicorp/terraform/backend/init"
"github.com/hashicorp/terraform/configs/hcl2shim"
"github.com/lyraproj/dgo/dgo"
"github.com/lyraproj/dgo/vf"
"github.com/lyraproj/dgocty"
"github.com/lyraproj/hierasdk/hiera"
"github.com/lyraproj/hierasdk/plugin"
"github.com/lyraproj/hierasdk/register"
)
func main() {
register.DataHash(`terraform_backend`, TerraformBackendData)
plugin.ServeAndExit()
}
var lookupLock sync.Mutex
// TerraformBackendData is a data hash function that returns values from a Terraform backend.
// The config can be any valid Terraform backend configuration.
func TerraformBackendData(ctx hiera.ProviderContext) dgo.Map {
// Hide Terraform's debug messages temporarily. A global mutex is required when doing
// since only one Go routine can hide and restore at any given time.
lookupLock.Lock()
stdOut := log.Writer()
log.SetOutput(ioutil.Discard)
defer func() {
log.SetOutput(stdOut)
lookupLock.Unlock()
}()
backend, ok := ctx.StringOption(`backend`)
if !ok {
panic(fmt.Errorf(`missing required provider option 'backend'`))
}
workspace, ok := ctx.StringOption(`workspace`)
if !ok {
workspace = "default"
}
configMap := ctx.Option(`config`)
if configMap == nil {
panic(fmt.Errorf(`missing required provider option 'config'`))
}
if _, ok := configMap.(dgo.Map); !ok {
panic(fmt.Errorf("%q must be a map", "config"))
}
config := dgocty.ToCty(configMap, false)
backendInit.Init(nil)
f := backendInit.Backend(backend)
if f == nil {
panic(fmt.Errorf("unknown backend type %q", backend))
}
b := f()
schema := b.ConfigSchema()
configVal, err := schema.CoerceValue(config)
if err != nil {
panic(fmt.Errorf("the given configuration is not valid for backend %q", backend))
}
newVal, diags := b.PrepareConfig(configVal)
if diags.HasErrors() {
panic(diags.Err())
}
configVal = newVal
diags = b.Configure(configVal)
if diags.HasErrors() {
panic(diags.Err())
}
state, err := b.StateMgr(workspace)
if err != nil {
panic(err)
}
err = state.RefreshState()
if err != nil {
panic(err)
}
remoteState := state.State()
output := vf.MutableMap()
if !remoteState.Empty() {
mod := remoteState.RootModule()
rootKey, ok := ctx.StringOption(`root_key`)
if ok {
nested := vf.MutableMap()
for k, os := range mod.OutputValues {
nested.Put(k, ctx.ToData(hcl2shim.ConfigValueFromHCL2(os.Value)))
}
output.Put(rootKey, nested)
} else {
for k, os := range mod.OutputValues {
output.Put(k, ctx.ToData(hcl2shim.ConfigValueFromHCL2(os.Value)))
}
}
}
return output
}