forked from cloudfoundry/bosh-bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_generator.go
59 lines (48 loc) · 1.43 KB
/
input_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
package gcp
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/cloudfoundry/bosh-bootloader/storage"
)
var tempDir func(dir, prefix string) (string, error) = ioutil.TempDir
var writeFile func(file string, data []byte, perm os.FileMode) error = ioutil.WriteFile
type InputGenerator struct {
}
func NewInputGenerator() InputGenerator {
return InputGenerator{}
}
func (i InputGenerator) Generate(state storage.State) (map[string]string, error) {
dir, err := tempDir("", "")
if err != nil {
return map[string]string{}, err
}
credentialsPath := filepath.Join(dir, "credentials.json")
err = writeFile(credentialsPath, []byte(state.GCP.ServiceAccountKey), os.ModePerm)
if err != nil {
return map[string]string{}, err
}
input := map[string]string{
"env_id": state.EnvID,
"project_id": state.GCP.ProjectID,
"region": state.GCP.Region,
"zone": state.GCP.Zone,
"credentials": credentialsPath,
"system_domain": state.LB.Domain,
}
if state.LB.Cert != "" && state.LB.Key != "" {
certPath := filepath.Join(dir, "cert")
err = writeFile(certPath, []byte(state.LB.Cert), os.ModePerm)
if err != nil {
return map[string]string{}, err
}
input["ssl_certificate"] = certPath
keyPath := filepath.Join(dir, "key")
err = writeFile(keyPath, []byte(state.LB.Key), os.ModePerm)
if err != nil {
return map[string]string{}, err
}
input["ssl_certificate_private_key"] = keyPath
}
return input, nil
}