2019-02-24 05:13:54 +08:00
|
|
|
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
|
|
|
|
// RAINBOND, Application Management Platform
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version. For any non-GPL usage of Rainbond,
|
|
|
|
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
|
|
|
|
// must be obtained first.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/goodrain/rainbond/api/handler"
|
|
|
|
"github.com/goodrain/rainbond/api/middleware"
|
|
|
|
"github.com/goodrain/rainbond/api/model"
|
|
|
|
httputil "github.com/goodrain/rainbond/util/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ThirdPartyServiceController implements ThirdPartyServicer
|
2019-03-01 13:27:27 +08:00
|
|
|
type ThirdPartyServiceController struct{}
|
2019-02-24 05:13:54 +08:00
|
|
|
|
|
|
|
// Endpoints POST->add endpoints, PUT->update endpoints, DELETE->delete endpoints
|
|
|
|
func (t *ThirdPartyServiceController) Endpoints(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "POST":
|
|
|
|
t.addEndpoints(w, r)
|
|
|
|
case "PUT":
|
|
|
|
t.updEndpoints(w, r)
|
|
|
|
case "DELETE":
|
|
|
|
t.delEndpoints(w, r)
|
2019-02-25 10:57:01 +08:00
|
|
|
case "GET":
|
|
|
|
t.listEndpoints(w, r)
|
2019-02-24 05:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ThirdPartyServiceController) addEndpoints(w http.ResponseWriter, r *http.Request) {
|
2019-02-25 10:57:01 +08:00
|
|
|
var data model.AddEndpiontsReq
|
2019-02-24 05:13:54 +08:00
|
|
|
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &data, nil) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sid := r.Context().Value(middleware.ContextKey("service_id")).(string)
|
2019-02-25 10:57:01 +08:00
|
|
|
if err := handler.Get3rdPartySvcHandler().AddEndpoints(sid, &data); err != nil {
|
2019-02-24 05:13:54 +08:00
|
|
|
httputil.ReturnError(r, w, 500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
httputil.ReturnSuccess(r, w, "success")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ThirdPartyServiceController) updEndpoints(w http.ResponseWriter, r *http.Request) {
|
2019-02-25 10:57:01 +08:00
|
|
|
var data model.UpdEndpiontsReq
|
2019-02-24 05:13:54 +08:00
|
|
|
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &data, nil) {
|
|
|
|
return
|
|
|
|
}
|
2019-03-01 14:36:20 +08:00
|
|
|
sid := r.Context().Value(middleware.ContextKey("service_id")).(string)
|
|
|
|
if err := handler.Get3rdPartySvcHandler().UpdEndpoints(&data, sid); err != nil {
|
2019-02-24 05:13:54 +08:00
|
|
|
httputil.ReturnError(r, w, 500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
httputil.ReturnSuccess(r, w, "success")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ThirdPartyServiceController) delEndpoints(w http.ResponseWriter, r *http.Request) {
|
2019-02-25 10:57:01 +08:00
|
|
|
var data model.DelEndpiontsReq
|
2019-02-24 05:13:54 +08:00
|
|
|
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &data, nil) {
|
|
|
|
return
|
|
|
|
}
|
2019-03-01 14:36:20 +08:00
|
|
|
sid := r.Context().Value(middleware.ContextKey("service_id")).(string)
|
|
|
|
if err := handler.Get3rdPartySvcHandler().DelEndpoints(sid, &data); err != nil {
|
2019-02-24 05:13:54 +08:00
|
|
|
httputil.ReturnError(r, w, 500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
httputil.ReturnSuccess(r, w, "success")
|
|
|
|
}
|
2019-02-24 21:10:13 +08:00
|
|
|
|
2019-02-25 10:57:01 +08:00
|
|
|
func (t *ThirdPartyServiceController) listEndpoints(w http.ResponseWriter, r *http.Request) {
|
|
|
|
sid := r.Context().Value(middleware.ContextKey("service_id")).(string)
|
|
|
|
res, err := handler.Get3rdPartySvcHandler().ListEndpoints(sid)
|
|
|
|
if err != nil {
|
|
|
|
httputil.ReturnError(r, w, 500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2019-02-26 16:39:27 +08:00
|
|
|
if res == nil || len(res) == 0 {
|
|
|
|
httputil.ReturnSuccess(r, w, []*model.EndpointResp{})
|
|
|
|
return
|
|
|
|
}
|
2019-02-25 10:57:01 +08:00
|
|
|
httputil.ReturnSuccess(r, w, res)
|
|
|
|
}
|