-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtenant-backend.go
60 lines (50 loc) · 1.54 KB
/
tenant-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
package tenant
import (
"fmt"
"log"
"os"
"path/filepath"
"tenant-terraform-generator/duplosdk"
"tenant-terraform-generator/tf-generator/common"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
)
type TenantBackend struct {
}
func (tb *TenantBackend) Generate(config *common.Config, client *duplosdk.Client) (*common.TFContext, error) {
log.Println("[TRACE] <====== Tenant backend TF generation started. =====>")
// create new empty hcl file object
hclFile := hclwrite.NewEmptyFile()
// create new file on system
path := filepath.Join(config.TFCodePath, config.TenantProject, "backend.tf")
tfFile, err := os.Create(path)
if err != nil {
fmt.Println(err)
return nil, err
}
// initialize the body of the new file object
rootBody := hclFile.Body()
// Add duplo terraform block
tfBlock := rootBody.AppendNewBlock("terraform",
nil)
tfBlockBody := tfBlock.Body()
s3Backend := tfBlockBody.AppendNewBlock("backend",
[]string{"s3"})
s3BackendBody := s3Backend.Body()
s3BackendBody.SetAttributeValue("region",
cty.StringVal(config.DuploDefaultPlanRegion)) // TODO - Take region from ENV VAR
s3BackendBody.SetAttributeValue("key",
cty.StringVal("tenant"))
s3BackendBody.SetAttributeValue("workspace_key_prefix",
cty.StringVal("admin:"))
s3BackendBody.SetAttributeValue("encrypt",
cty.True)
fmt.Printf("%s", hclFile.Bytes())
_, err = tfFile.Write(hclFile.Bytes())
if err != nil {
fmt.Println(err)
return nil, err
}
log.Println("[TRACE] <====== Tenant backend TF generation done. =====>")
return nil, nil
}