forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
221 lines (187 loc) · 7.19 KB
/
server.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
/*
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 server
import (
"context"
"crypto/tls"
"encoding/base64"
"fmt"
"net/http"
"time"
"github.com/golang/glog"
"github.com/kubernetes-incubator/service-catalog/contrib/pkg/broker/controller"
"github.com/kubernetes-incubator/service-catalog/contrib/pkg/brokerapi"
"github.com/kubernetes-incubator/service-catalog/pkg/util"
"github.com/gorilla/mux"
)
type server struct {
controller controller.Controller
}
// CreateHandler creates Broker HTTP handler based on an implementation
// of a controller.Controller interface.
func createHandler(c controller.Controller) http.Handler {
s := server{
controller: c,
}
var router = mux.NewRouter()
router.HandleFunc("/v2/catalog", s.catalog).Methods("GET")
router.HandleFunc("/v2/service_instances/{instance_id}/last_operation", s.getServiceInstanceLastOperation).Methods("GET")
router.HandleFunc("/v2/service_instances/{instance_id}", s.createServiceInstance).Methods("PUT")
router.HandleFunc("/v2/service_instances/{instance_id}", s.removeServiceInstance).Methods("DELETE")
router.HandleFunc("/v2/service_instances/{instance_id}/service_bindings/{binding_id}", s.bind).Methods("PUT")
router.HandleFunc("/v2/service_instances/{instance_id}/service_bindings/{binding_id}", s.unBind).Methods("DELETE")
return router
}
// Run creates the HTTP handler based on an implementation of a
// controller.Controller interface, and begins to listen on the specified address.
func Run(ctx context.Context, addr string, c controller.Controller) error {
listenAndServe := func(srv *http.Server) error {
return srv.ListenAndServe()
}
return run(ctx, addr, listenAndServe, c)
}
// RunTLS creates the HTTPS handler based on an implementation of a
// controller.Controller interface, and begins to listen on the specified address.
func RunTLS(ctx context.Context, addr string, cert string, key string, c controller.Controller) error {
var decodedCert, decodedKey []byte
var tlsCert tls.Certificate
var err error
decodedCert, err = base64.StdEncoding.DecodeString(cert)
if err != nil {
return err
}
decodedKey, err = base64.StdEncoding.DecodeString(key)
if err != nil {
return err
}
tlsCert, err = tls.X509KeyPair(decodedCert, decodedKey)
if err != nil {
return err
}
listenAndServe := func(srv *http.Server) error {
srv.TLSConfig = new(tls.Config)
srv.TLSConfig.Certificates = []tls.Certificate{tlsCert}
return srv.ListenAndServeTLS("", "")
}
return run(ctx, addr, listenAndServe, c)
}
func run(ctx context.Context, addr string, listenAndServe func(srv *http.Server) error, c controller.Controller) error {
glog.Infof("Starting server on %d\n", addr)
srv := &http.Server{
Addr: addr,
Handler: createHandler(c),
}
go func() {
<-ctx.Done()
c, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if srv.Shutdown(c) != nil {
srv.Close()
}
}()
return listenAndServe(srv)
}
func (s *server) catalog(w http.ResponseWriter, r *http.Request) {
glog.Infof("Get Service Broker Catalog...")
if result, err := s.controller.Catalog(); err == nil {
util.WriteResponse(w, http.StatusOK, result)
} else {
util.WriteErrorResponse(w, http.StatusBadRequest, err)
}
}
func (s *server) getServiceInstanceLastOperation(w http.ResponseWriter, r *http.Request) {
instanceID := mux.Vars(r)["instance_id"]
q := r.URL.Query()
serviceID := q.Get("service_id")
planID := q.Get("plan_id")
operation := q.Get("operation")
glog.Infof("GetServiceInstance ... %s\n", instanceID)
if result, err := s.controller.GetServiceInstanceLastOperation(instanceID, serviceID, planID, operation); err == nil {
util.WriteResponse(w, http.StatusOK, result)
} else {
util.WriteErrorResponse(w, http.StatusBadRequest, err)
}
}
func (s *server) createServiceInstance(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["instance_id"]
glog.Infof("CreateServiceInstance %s...\n", id)
var req brokerapi.CreateServiceInstanceRequest
if err := util.BodyToObject(r, &req); err != nil {
glog.Errorf("error unmarshalling: %v", err)
util.WriteErrorResponse(w, http.StatusBadRequest, err)
return
}
// TODO: Check if parameters are required, if not, this thing below is ok to leave in,
// if they are ,they should be checked. Because if no parameters are passed in, this will
// fail because req.Parameters is nil.
if req.Parameters == nil {
req.Parameters = make(map[string]interface{})
}
if result, err := s.controller.CreateServiceInstance(id, &req); err == nil {
util.WriteResponse(w, http.StatusCreated, result)
} else {
util.WriteErrorResponse(w, http.StatusBadRequest, err)
}
}
func (s *server) removeServiceInstance(w http.ResponseWriter, r *http.Request) {
instanceID := mux.Vars(r)["instance_id"]
q := r.URL.Query()
serviceID := q.Get("service_id")
planID := q.Get("plan_id")
acceptsIncomplete := q.Get("accepts_incomplete") == "true"
glog.Infof("RemoveServiceInstance %s...\n", instanceID)
if result, err := s.controller.RemoveServiceInstance(instanceID, serviceID, planID, acceptsIncomplete); err == nil {
util.WriteResponse(w, http.StatusOK, result)
} else {
util.WriteErrorResponse(w, http.StatusBadRequest, err)
}
}
func (s *server) bind(w http.ResponseWriter, r *http.Request) {
bindingID := mux.Vars(r)["binding_id"]
instanceID := mux.Vars(r)["instance_id"]
glog.Infof("Bind binding_id=%s, instance_id=%s\n", bindingID, instanceID)
var req brokerapi.BindingRequest
if err := util.BodyToObject(r, &req); err != nil {
glog.Errorf("Failed to unmarshall request: %v", err)
util.WriteErrorResponse(w, http.StatusBadRequest, err)
return
}
// TODO: Check if parameters are required, if not, this thing below is ok to leave in,
// if they are ,they should be checked. Because if no parameters are passed in, this will
// fail because req.Parameters is nil.
if req.Parameters == nil {
req.Parameters = make(map[string]interface{})
}
// Pass in the instanceId to the template.
req.Parameters["instanceId"] = instanceID
if result, err := s.controller.Bind(instanceID, bindingID, &req); err == nil {
util.WriteResponse(w, http.StatusOK, result)
} else {
util.WriteErrorResponse(w, http.StatusBadRequest, err)
}
}
func (s *server) unBind(w http.ResponseWriter, r *http.Request) {
instanceID := mux.Vars(r)["instance_id"]
bindingID := mux.Vars(r)["binding_id"]
q := r.URL.Query()
serviceID := q.Get("service_id")
planID := q.Get("plan_id")
glog.Infof("UnBind: Service instance guid: %s:%s", bindingID, instanceID)
if err := s.controller.UnBind(instanceID, bindingID, serviceID, planID); err == nil {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, "{}") //id)
} else {
util.WriteErrorResponse(w, http.StatusBadRequest, err)
}
}