-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfvars.go
85 lines (70 loc) · 2.09 KB
/
tfvars.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
package cluster
import (
"os"
"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/asset/ignition/bootstrap"
"github.com/openshift/installer/pkg/asset/ignition/machine"
"github.com/openshift/installer/pkg/asset/installconfig"
"github.com/openshift/installer/pkg/tfvars"
"github.com/pkg/errors"
)
const (
// TfVarsFileName is the filename for Terraform variables.
TfVarsFileName = "terraform.tfvars"
tfvarsAssetName = "Terraform Variables"
)
// TerraformVariables depends on InstallConfig and
// Ignition to generate the terrafor.tfvars.
type TerraformVariables struct {
File *asset.File
}
var _ asset.WritableAsset = (*TerraformVariables)(nil)
// Name returns the human-friendly name of the asset.
func (t *TerraformVariables) Name() string {
return tfvarsAssetName
}
// Dependencies returns the dependency of the TerraformVariable
func (t *TerraformVariables) Dependencies() []asset.Asset {
return []asset.Asset{
&installconfig.InstallConfig{},
&bootstrap.Bootstrap{},
&machine.Master{},
}
}
// Generate generates the terraform.tfvars file.
func (t *TerraformVariables) Generate(parents asset.Parents) error {
installConfig := &installconfig.InstallConfig{}
bootstrap := &bootstrap.Bootstrap{}
master := &machine.Master{}
parents.Get(installConfig, bootstrap, master)
bootstrapIgn := string(bootstrap.Files()[0].Data)
masterIgn := string(master.Files()[0].Data)
data, err := tfvars.TFVars(installConfig.Config, bootstrapIgn, masterIgn)
if err != nil {
return errors.Wrap(err, "failed to get Tfvars")
}
t.File = &asset.File{
Filename: TfVarsFileName,
Data: data,
}
return nil
}
// Files returns the files generated by the asset.
func (t *TerraformVariables) Files() []*asset.File {
if t.File != nil {
return []*asset.File{t.File}
}
return []*asset.File{}
}
// Load reads the terraform.tfvars from disk.
func (t *TerraformVariables) Load(f asset.FileFetcher) (found bool, err error) {
file, err := f.FetchByName(TfVarsFileName)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
t.File = file
return true, nil
}