/
config_helper.go
45 lines (35 loc) · 1004 Bytes
/
config_helper.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
package helpers
import (
"encoding/json"
"io/ioutil"
"github.com/alphagov/paas-elasticache-broker/broker"
)
func WriteCustomConfig(
config broker.Config,
brokerName string,
elastiCacheSubnetGroupName string,
ec2SecurityGroupID string,
) (broker.Config, string, error) {
newConfig := &config
newConfig.BrokerName = brokerName
newConfig.CacheSubnetGroupName = elastiCacheSubnetGroupName
newConfig.VpcSecurityGroupIds = []string{ec2SecurityGroupID}
if err := newConfig.Validate(); err != nil {
return broker.Config{}, "", err
}
configFile, err := ioutil.TempFile("", "elasticache-broker")
if err != nil {
return broker.Config{}, "", err
}
configJSON, err := json.Marshal(newConfig)
if err != nil {
return broker.Config{}, "", err
}
if err = ioutil.WriteFile(configFile.Name(), configJSON, 0644); err != nil {
return broker.Config{}, "", err
}
if err = configFile.Close(); err != nil {
return broker.Config{}, "", err
}
return *newConfig, configFile.Name(), err
}