-
Notifications
You must be signed in to change notification settings - Fork 25
/
generator.go
121 lines (97 loc) · 2.64 KB
/
generator.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package config
import (
"html/template"
"io"
"os"
"github.com/aserto-dev/topaz/pkg/cli/cc"
)
type Generator struct {
templateParams
ConfigName string
}
func NewGenerator(configName string) *Generator {
return &Generator{ConfigName: configName}
}
func (g *Generator) WithVersion(version int) *Generator {
g.Version = version
return g
}
func (g *Generator) WithLocalPolicyImage(image string) *Generator {
g.LocalPolicyImage = image
return g
}
func (g *Generator) WithPolicyName(policyName string) *Generator {
g.PolicyName = policyName
return g
}
func (g *Generator) WithResource(resource string) *Generator {
g.Resource = resource
return g
}
func (g *Generator) WithEdgeDirectory(enabled bool) *Generator {
g.EdgeDirectory = enabled
return g
}
func (g *Generator) WithEnableDirectoryV2(enabled bool) *Generator {
g.EnableDirectoryV2 = false
return g
}
func (g *Generator) WithTenantID(tenantID string) *Generator {
g.TenantID = tenantID
return g
}
func (g *Generator) WithDiscovery(url, key string) *Generator {
g.DiscoveryURL = url
g.TenantKey = key
return g
}
func (g *Generator) WithController(url, clientCertPath, clientKeyPath string) *Generator {
g.ControlPlane.Enabled = true
g.ControlPlane.Address = url
g.ControlPlane.ClientCertPath = clientCertPath
g.ControlPlane.ClientKeyPath = clientKeyPath
return g
}
func (g *Generator) WithSelfDecisionLogger(emsURL, clientCertPath, clientKeyPath, storePath string) *Generator {
g.DecisionLogging = true
g.DecisionLogger.EMSAddress = emsURL
g.DecisionLogger.ClientCertPath = clientCertPath
g.DecisionLogger.ClientKeyPath = clientKeyPath
g.DecisionLogger.StorePath = storePath
return g
}
func (g *Generator) GenerateConfig(w io.Writer, templateData string) error {
return g.writeConfig(w, templateData)
}
func (g *Generator) CreateConfigDir() (string, error) {
configDir := cc.GetTopazCfgDir()
if fi, err := os.Stat(configDir); err == nil && fi.IsDir() {
return configDir, nil
}
return configDir, os.MkdirAll(configDir, 0o700)
}
func (g *Generator) CreateCertsDir() (string, error) {
certsDir := cc.GetTopazCertsDir()
if fi, err := os.Stat(certsDir); err == nil && fi.IsDir() {
return certsDir, nil
}
return certsDir, os.MkdirAll(certsDir, 0o700)
}
func (g *Generator) CreateDataDir() (string, error) {
dataDir := cc.GetTopazDataDir()
if fi, err := os.Stat(dataDir); err == nil && fi.IsDir() {
return dataDir, nil
}
return dataDir, os.MkdirAll(dataDir, 0o700)
}
func (g *Generator) writeConfig(w io.Writer, templ string) error {
t, err := template.New("config").Parse(templ)
if err != nil {
return err
}
err = t.Execute(w, g)
if err != nil {
return err
}
return nil
}