forked from openshift/origin
/
controller.go
164 lines (144 loc) · 4.35 KB
/
controller.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
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"encoding/json"
"errors"
"fmt"
"sync"
"github.com/golang/glog"
"github.com/kubernetes-incubator/service-catalog/contrib/pkg/broker/controller"
"github.com/kubernetes-incubator/service-catalog/contrib/pkg/brokerapi"
)
type errNoSuchInstance struct {
instanceID string
}
func (e errNoSuchInstance) Error() string {
return fmt.Sprintf("no such instance with ID %s", e.instanceID)
}
type userProvidedServiceInstance struct {
Name string
Credential *brokerapi.Credential
}
type userProvidedController struct {
rwMutex sync.RWMutex
instanceMap map[string]*userProvidedServiceInstance
}
// CreateController creates an instance of a User Provided service broker controller.
func CreateController() controller.Controller {
var instanceMap = make(map[string]*userProvidedServiceInstance)
return &userProvidedController{
instanceMap: instanceMap,
}
}
func (c *userProvidedController) Catalog() (*brokerapi.Catalog, error) {
glog.Info("Catalog()")
return &brokerapi.Catalog{
Services: []*brokerapi.Service{
{
Name: "user-provided-service",
ID: "4f6e6cf6-ffdd-425f-a2c7-3c9258ad2468",
Description: "A user provided service",
Plans: []brokerapi.ServicePlan{{
Name: "default",
ID: "86064792-7ea2-467b-af93-ac9694d96d52",
Description: "Sample plan description",
Free: true,
},
},
Bindable: true,
},
},
}, nil
}
func (c *userProvidedController) CreateServiceInstance(
id string,
req *brokerapi.CreateServiceInstanceRequest,
) (*brokerapi.CreateServiceInstanceResponse, error) {
glog.Info("CreateServiceInstance()")
credString, ok := req.Parameters["credentials"]
c.rwMutex.Lock()
defer c.rwMutex.Unlock()
if ok {
jsonCred, err := json.Marshal(credString)
if err != nil {
glog.Errorf("Failed to marshal credentials: %v", err)
return nil, err
}
var cred brokerapi.Credential
err = json.Unmarshal(jsonCred, &cred)
if err != nil {
glog.Errorf("Failed to unmarshal credentials: %v", err)
return nil, err
}
c.instanceMap[id] = &userProvidedServiceInstance{
Name: id,
Credential: &cred,
}
} else {
c.instanceMap[id] = &userProvidedServiceInstance{
Name: id,
Credential: &brokerapi.Credential{
"special-key-1": "special-value-1",
"special-key-2": "special-value-2",
},
}
}
glog.Infof("Created User Provided Service Instance:\n%v\n", c.instanceMap[id])
return &brokerapi.CreateServiceInstanceResponse{}, nil
}
func (c *userProvidedController) GetServiceInstanceLastOperation(
instanceID,
serviceID,
planID,
operation string,
) (*brokerapi.LastOperationResponse, error) {
glog.Info("GetServiceInstanceLastOperation()")
return nil, errors.New("Unimplemented")
}
func (c *userProvidedController) RemoveServiceInstance(
instanceID,
serviceID,
planID string,
acceptsIncomplete bool,
) (*brokerapi.DeleteServiceInstanceResponse, error) {
glog.Info("RemoveServiceInstance()")
c.rwMutex.Lock()
defer c.rwMutex.Unlock()
_, ok := c.instanceMap[instanceID]
if ok {
delete(c.instanceMap, instanceID)
return &brokerapi.DeleteServiceInstanceResponse{}, nil
}
return &brokerapi.DeleteServiceInstanceResponse{}, nil
}
func (c *userProvidedController) Bind(
instanceID,
bindingID string,
req *brokerapi.BindingRequest,
) (*brokerapi.CreateServiceBindingResponse, error) {
glog.Info("Bind()")
c.rwMutex.RLock()
defer c.rwMutex.RUnlock()
instance, ok := c.instanceMap[instanceID]
if !ok {
return nil, errNoSuchInstance{instanceID: instanceID}
}
cred := instance.Credential
return &brokerapi.CreateServiceBindingResponse{Credentials: *cred}, nil
}
func (c *userProvidedController) UnBind(instanceID, bindingID, serviceID, planID string) error {
glog.Info("UnBind()")
// Since we don't persist the binding, there's nothing to do here.
return nil
}