forked from Qarik-Group/eden
/
service_instances.go
237 lines (201 loc) · 6.51 KB
/
service_instances.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package config
import (
"encoding/json"
"os"
"time"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
"gopkg.in/yaml.v2"
)
type FSConfig struct {
path string
fs boshsys.FileSystem
schema FSServiceInstances
}
type FSServiceInstances struct {
ServiceInstances []*FSServiceInstance `yaml:"service_instances"`
}
type FSServiceInstance struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
ServiceID string `yaml:"service_id"`
ServiceName string `yaml:"service_name"`
PlanID string `yaml:"plan_id"`
PlanName string `yaml:"plan_name"`
BrokerURL string `yaml:"broker_url"`
Bindings []fsServiceBinding `yaml:"bindings"`
CreatedAt time.Time `yaml:"created_at"`
}
// ServiceBinding represents a binding with credentials
type fsServiceBinding struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
Credentials string `yaml:"credentials"`
CreatedAt time.Time `yaml:"created_at"`
}
func NewFSConfigFromPath(path string, fs boshsys.FileSystem) (FSConfig, error) {
var schema FSServiceInstances
absPath, err := fs.ExpandPath(path)
if err != nil {
return FSConfig{}, err
}
if fs.FileExists(absPath) {
bytes, err := fs.ReadFile(absPath)
if err != nil {
return FSConfig{}, bosherr.WrapErrorf(err, "Reading config '%s'", absPath)
}
err = yaml.Unmarshal(bytes, &schema)
if err != nil {
return FSConfig{}, bosherr.WrapError(err, "Unmarshalling config")
}
}
return FSConfig{path: absPath, fs: fs, schema: schema}, nil
}
// ProvisionNewServiceInstance initialize new FSServiceInstance
func (c FSConfig) ProvisionNewServiceInstance(id, name, serviceID, serviceName, planID, planName, brokerURL string) {
_, inst := c.findOrCreateServiceInstanceByIDOrName(id, name)
inst.ServiceID = serviceID
inst.ServiceName = serviceName
inst.PlanID = planID
inst.PlanName = planName
inst.BrokerURL = brokerURL
c.Save()
}
// FindServiceInstance returns a copy of a service instance record
func (c FSConfig) FindServiceInstance(idOrName string) FSServiceInstance {
_, inst := c.findOrCreateServiceInstance(idOrName)
return *inst
}
// RenameServiceInstance updates the .Name of a service instance
func (c FSConfig) RenameServiceInstance(idOrName, newName string) {
_, inst := c.findOrCreateServiceInstance(idOrName)
inst.Name = newName
c.Save()
}
// BindServiceInstance records a new bindingID
func (c FSConfig) BindServiceInstance(instanceID, bindingID, name string, rawCredentials interface{}) (err error) {
_, inst := c.findOrCreateServiceInstance(instanceID)
credentialsStr, err := json.Marshal(rawCredentials)
if err != nil {
return bosherr.WrapError(err, "Marshalling raw credentials")
}
binding := fsServiceBinding{
ID: bindingID,
Name: name,
Credentials: string(credentialsStr),
CreatedAt: time.Now(),
}
inst.Bindings = append(inst.Bindings, binding)
return c.Save()
}
// UnbindServiceInstance removes record of a binding
func (c FSConfig) UnbindServiceInstance(instanceID, bindingNameOrID string) {
_, inst := c.findOrCreateServiceInstance(instanceID)
bindings := []fsServiceBinding{}
for _, binding := range inst.Bindings {
if binding.ID != bindingNameOrID && binding.Name != bindingNameOrID {
bindings = append(bindings, binding)
}
}
inst.Bindings = bindings
c.Save()
}
// DeprovisionServiceInstance removes record of an instance
func (c FSConfig) DeprovisionServiceInstance(instanceNameOrID string) {
instances := []*FSServiceInstance{}
for _, instance := range c.schema.ServiceInstances {
if instance.ID != instanceNameOrID && instance.Name != instanceNameOrID {
instances = append(instances, instance)
}
}
c.schema.ServiceInstances = instances
c.Save()
}
// ServiceInstances returns the list of service instances created locally
func (c FSConfig) ServiceInstances() []*FSServiceInstance {
return c.schema.ServiceInstances
}
// Save configuration/data to file
func (c FSConfig) Save() error {
bytes, err := yaml.Marshal(c.schema)
if err != nil {
return bosherr.WrapError(err, "Marshalling config")
}
err = c.fs.WriteFile(c.path, bytes)
if err != nil {
return bosherr.WrapErrorf(err, "Writing config '%s'", c.path)
}
os.Chmod(c.path, 0600)
return nil
}
func (c *FSConfig) findOrCreateServiceInstance(idOrName string) (int, *FSServiceInstance) {
if idOrName != "" {
for i, instance := range c.schema.ServiceInstances {
if idOrName == instance.ID || idOrName == instance.Name {
return i, instance
}
}
}
return c.appendNewServiceInstanceWithID(idOrName)
}
func (c *FSConfig) findOrCreateServiceInstanceByIDOrName(id, name string) (int, *FSServiceInstance) {
for i, instance := range c.schema.ServiceInstances {
if id == instance.ID || (name != "" && name == instance.Name) {
return i, instance
}
}
i, instance := c.appendNewServiceInstanceWithID(id)
instance.Name = name
return i, instance
}
func (c *FSConfig) appendNewServiceInstanceWithID(id string) (int, *FSServiceInstance) {
instance := &FSServiceInstance{ID: id, CreatedAt: time.Now()}
c.schema.ServiceInstances = append(c.schema.ServiceInstances, instance)
index := len(c.schema.ServiceInstances) - 1
return index, instance
}
func (c FSConfig) deepCopy() FSConfig {
bytes, err := yaml.Marshal(c.schema)
if err != nil {
panic("serializing config schema")
}
var schema FSServiceInstances
err = yaml.Unmarshal(bytes, &schema)
if err != nil {
panic("deserializing config schema")
}
return FSConfig{path: c.path, fs: c.fs, schema: schema}
}
// Credentials fixes any map[interface{}]interface{} into map[string]interface{}
// as expected by JSON marshalling
func (b fsServiceBinding) CredentialsJSON() (out map[string]interface{}, err error) {
err = json.Unmarshal([]byte(b.Credentials), &out)
if err != nil {
return nil, bosherr.WrapError(err, "Unmarshalling raw credentials")
}
return
}
func deepCopy(rawInput interface{}) (output map[string]interface{}) {
output = map[string]interface{}{}
switch input := rawInput.(type) {
case map[string]interface{}:
for key, rawValue := range input {
switch value := rawValue.(type) {
case map[interface{}]interface{}:
output[key] = deepCopy(value)
default:
output[key] = value
}
}
case map[interface{}]interface{}:
for key, rawValue := range input {
switch value := rawValue.(type) {
case map[interface{}]interface{}:
output[key.(string)] = deepCopy(value)
default:
output[key.(string)] = value
}
}
}
return
}