This repository was archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathterraform.go
More file actions
58 lines (53 loc) · 1.32 KB
/
terraform.go
File metadata and controls
58 lines (53 loc) · 1.32 KB
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
package workspacehelper
import (
"bytes"
"text/template"
"github.com/hashicorp/terraform-k8s/api/v1alpha1"
)
const (
// TerraformConfigMap names the Terraform template ConfigMap
TerraformConfigMap = "terraform"
// TerraformOperator is for labelling
TerraformOperator = "terraform-k8s"
)
// CreateTerraformTemplate creates a template for the Terraform configuration
func CreateTerraformTemplate(workspace *v1alpha1.Workspace) ([]byte, error) {
tfTemplate, err := template.New("main.tf").Parse(`terraform {
backend "remote" {
organization = "{{.Spec.Organization}}"
workspaces {
name = "{{.ObjectMeta.Namespace}}-{{.ObjectMeta.Name}}"
}
}
}
{{- range .Spec.Variables}}
{{- if not .EnvironmentVariable }}
variable "{{.Key}}" {}
{{- end}}
{{- end}}
{{- range .Spec.Outputs}}
output "{{.Key}}" {
value = module.operator.{{.ModuleOutputName}}
sensitive = {{.Sensitive}}
}
{{- end}}
module "operator" {
source = "{{.Spec.Module.Source}}"
{{- if .Spec.Module.Version }}
version = "{{.Spec.Module.Version}}"
{{- end}}
{{- range .Spec.Variables}}
{{- if not .EnvironmentVariable }}
{{.Key}} = var.{{.Key}}
{{- end}}
{{- end}}
}`)
if err != nil {
return nil, err
}
var tpl bytes.Buffer
if err := tfTemplate.Execute(&tpl, workspace); err != nil {
return nil, err
}
return tpl.Bytes(), nil
}