Rainbond/api/controller/gateway.go

383 lines
11 KiB
Go
Raw Normal View History

2018-11-21 15:04:03 +08:00
// RAINBOND, Application Management Platform
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
// 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 (
"fmt"
2018-12-04 13:43:15 +08:00
"net/http"
"net/url"
"strings"
2018-11-21 15:04:03 +08:00
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/api/handler"
api_model "github.com/goodrain/rainbond/api/model"
2018-11-28 17:01:58 +08:00
"github.com/goodrain/rainbond/cmd/api/option"
2018-12-04 13:43:15 +08:00
"github.com/goodrain/rainbond/mq/client"
2018-11-21 15:04:03 +08:00
httputil "github.com/goodrain/rainbond/util/http"
)
// GatewayStruct -
2018-11-21 15:04:03 +08:00
type GatewayStruct struct {
2018-12-04 13:43:15 +08:00
MQClient client.MQClient
cfg *option.Config
2018-11-21 15:04:03 +08:00
}
2018-11-23 20:39:34 +08:00
// HTTPRule is used to add, update or delete http rule which enables
2018-11-21 15:04:03 +08:00
// external traffic to access applications through the gateway
func (g *GatewayStruct) HTTPRule(w http.ResponseWriter, r *http.Request) {
2018-11-21 15:04:03 +08:00
switch r.Method {
case "POST":
g.addHTTPRule(w, r)
2018-11-21 15:04:03 +08:00
case "PUT":
g.updateHTTPRule(w, r)
2018-11-21 17:57:48 +08:00
case "DELETE":
g.deleteHTTPRule(w, r)
2018-11-21 15:04:03 +08:00
}
}
func (g *GatewayStruct) addHTTPRule(w http.ResponseWriter, r *http.Request) {
var req api_model.AddHTTPRuleStruct
2018-11-21 15:04:03 +08:00
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
if !ok {
return
}
// verify request
values := url.Values{}
if req.ContainerPort == 0 {
values["container_port"] = []string{"The container_port field is required"}
}
if strings.Replace(req.CertificateID, " ", "", -1) != "" {
if req.Certificate == "" {
values["certificate"] = []string{"The certificate field is required"}
}
if req.PrivateKey == "" {
values["private_key"] = []string{"The private_key field is required"}
}
}
if len(values) != 0 {
httputil.ReturnValidationError(r, w, values)
return
}
2018-11-21 15:04:03 +08:00
h := handler.GetGatewayHandler()
sid, err := h.AddHTTPRule(&req)
if err != nil {
2018-11-22 09:34:00 +08:00
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while adding http rule: %v", err))
2018-11-21 15:04:03 +08:00
return
}
2019-03-12 09:59:09 +08:00
if err := handler.GetGatewayHandler().SendTask(map[string]interface{}{
"service_id": sid,
"action": "add-http-rule",
}); err != nil {
2018-12-04 13:43:15 +08:00
logrus.Errorf("send runtime message about gateway failure %s", err.Error())
}
2018-11-26 18:08:16 +08:00
2018-11-21 15:04:03 +08:00
httputil.ReturnSuccess(r, w, "success")
}
func (g *GatewayStruct) updateHTTPRule(w http.ResponseWriter, r *http.Request) {
var req api_model.UpdateHTTPRuleStruct
2018-11-21 17:15:40 +08:00
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
if !ok {
return
}
// verify request
values := url.Values{}
if strings.Replace(req.CertificateID, " ", "", -1) != "" {
if req.Certificate == "" {
values["certificate"] = []string{"The certificate field is required"}
}
if req.PrivateKey == "" {
values["private_key"] = []string{"The private_key field is required"}
}
}
if len(req.RuleExtensions) > 0 {
for _, re := range req.RuleExtensions {
if re.Key == "" {
values["key"] = []string{"The key field is required"}
break
}
if re.Value == "" {
values["value"] = []string{"The value field is required"}
break
}
}
}
if len(values) != 0 {
httputil.ReturnValidationError(r, w, values)
return
}
2018-11-21 17:15:40 +08:00
h := handler.GetGatewayHandler()
sid, err := h.UpdateHTTPRule(&req)
if err != nil {
2018-11-21 17:57:48 +08:00
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
2018-11-21 17:15:40 +08:00
"updating http rule: %v", err))
return
}
2018-11-21 15:04:03 +08:00
2019-03-12 09:59:09 +08:00
if err := handler.GetGatewayHandler().SendTask(map[string]interface{}{
"service_id": sid,
"action": "update-http-rule",
}); err != nil {
2018-12-04 13:43:15 +08:00
logrus.Errorf("send runtime message about gateway failure %s", err.Error())
}
2018-11-26 18:08:16 +08:00
2018-11-21 17:15:40 +08:00
httputil.ReturnSuccess(r, w, "success")
2018-11-21 15:04:03 +08:00
}
func (g *GatewayStruct) deleteHTTPRule(w http.ResponseWriter, r *http.Request) {
var req api_model.DeleteHTTPRuleStruct
2018-11-21 17:57:48 +08:00
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
if !ok {
return
}
2018-11-21 15:04:03 +08:00
2018-11-21 17:57:48 +08:00
h := handler.GetGatewayHandler()
serviceID, err := h.DeleteHTTPRule(&req)
2018-11-21 17:57:48 +08:00
if err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while delete http rule: %v", err))
return
}
2019-03-12 09:59:09 +08:00
if err := handler.GetGatewayHandler().SendTask(map[string]interface{}{
"service_id": serviceID,
"action": "delete-http-rule",
}); err != nil {
2018-12-04 13:43:15 +08:00
logrus.Errorf("send runtime message about gateway failure %s", err.Error())
}
2018-11-26 18:08:16 +08:00
2018-11-21 17:57:48 +08:00
httputil.ReturnSuccess(r, w, "success")
2018-11-21 15:04:03 +08:00
}
2018-11-21 19:51:40 +08:00
2018-11-23 20:39:34 +08:00
// TCPRule is used to add, update or delete tcp rule which enables
2018-11-21 19:51:40 +08:00
// external traffic to access applications through the gateway
func (g *GatewayStruct) TCPRule(w http.ResponseWriter, r *http.Request) {
2018-11-21 19:51:40 +08:00
switch r.Method {
case "POST":
2018-11-26 02:19:08 +08:00
g.AddTCPRule(w, r)
2018-11-21 19:51:40 +08:00
case "PUT":
g.updateTCPRule(w, r)
2018-11-21 19:51:40 +08:00
case "DELETE":
g.deleteTCPRule(w, r)
2018-11-21 19:51:40 +08:00
}
}
2018-11-26 02:19:08 +08:00
// AddTCPRule adds a tcp rule
func (g *GatewayStruct) AddTCPRule(w http.ResponseWriter, r *http.Request) {
var req api_model.AddTCPRuleStruct
2018-11-21 19:51:40 +08:00
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
if !ok {
return
}
h := handler.GetGatewayHandler()
// verify request
values := url.Values{}
if req.ContainerPort == 0 {
values["container_port"] = []string{"The container_port field is required"}
}
if req.Port == 0 {
values["port"] = []string{"The port field is required"}
2018-11-28 17:01:58 +08:00
} else if req.Port <= g.cfg.MinExtPort {
values["port"] = []string{fmt.Sprintf("The port field should be greater than %d", g.cfg.MinExtPort)}
2018-12-19 13:53:11 +08:00
} else {
// check if the port exists
if h.PortExists(req.Port) {
values["port"] = []string{fmt.Sprintf("The port(%v) already exists", req.Port)}
}
}
if len(req.RuleExtensions) > 0 {
for _, re := range req.RuleExtensions {
if re.Key == "" {
values["key"] = []string{"The key field is required"}
break
}
if re.Value == "" {
values["value"] = []string{"The value field is required"}
break
}
}
}
if len(values) != 0 {
httputil.ReturnValidationError(r, w, values)
return
}
sid, err := h.AddTCPRule(&req)
if err != nil {
2018-11-22 09:34:00 +08:00
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
2018-11-21 19:51:40 +08:00
"adding tcp rule: %v", err))
return
}
2019-03-12 09:59:09 +08:00
if err := handler.GetGatewayHandler().SendTask(map[string]interface{}{
"service_id": sid,
"action": "add-tcp-rule",
}); err != nil {
2018-12-04 13:43:15 +08:00
logrus.Errorf("send runtime message about gateway failure %s", err.Error())
}
2018-11-26 18:08:16 +08:00
2018-11-21 19:51:40 +08:00
httputil.ReturnSuccess(r, w, "success")
}
func (g *GatewayStruct) updateTCPRule(w http.ResponseWriter, r *http.Request) {
var req api_model.UpdateTCPRuleStruct
2018-11-21 20:14:13 +08:00
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
if !ok {
return
}
2018-11-21 19:51:40 +08:00
2018-11-21 20:14:13 +08:00
h := handler.GetGatewayHandler()
// verify reqeust
values := url.Values{}
2018-12-19 13:53:11 +08:00
if req.Port != 0 && req.Port <= g.cfg.MinExtPort {
2018-11-28 17:01:58 +08:00
values["port"] = []string{fmt.Sprintf("The port field should be greater than %d", g.cfg.MinExtPort)}
2018-12-19 13:53:11 +08:00
} else {
// check if the port exists
if h.PortExists(req.Port) {
values["port"] = []string{fmt.Sprintf("The port(%v) already exists", req.Port)}
}
}
if len(req.RuleExtensions) > 0 {
for _, re := range req.RuleExtensions {
if re.Key == "" {
values["key"] = []string{"The key field is required"}
break
}
if re.Value == "" {
values["value"] = []string{"The value field is required"}
break
}
}
}
if len(values) != 0 {
httputil.ReturnValidationError(r, w, values)
return
}
2018-12-18 20:55:12 +08:00
sid, err := h.UpdateTCPRule(&req, g.cfg.MinExtPort)
if err != nil {
2018-11-22 09:34:00 +08:00
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
2018-11-21 20:14:13 +08:00
"updating tcp rule: %v", err))
return
}
2019-03-12 09:59:09 +08:00
if err := handler.GetGatewayHandler().SendTask(map[string]interface{}{
"service_id": sid,
"action": "update-tcp-rule",
}); err != nil {
2018-12-04 13:43:15 +08:00
logrus.Errorf("send runtime message about gateway failure %s", err.Error())
}
2018-11-26 18:08:16 +08:00
2018-11-21 20:14:13 +08:00
httputil.ReturnSuccess(r, w, "success")
2018-11-21 19:51:40 +08:00
}
func (g *GatewayStruct) deleteTCPRule(w http.ResponseWriter, r *http.Request) {
var req api_model.DeleteTCPRuleStruct
2018-11-21 20:33:37 +08:00
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
if !ok {
return
}
2018-11-21 19:51:40 +08:00
2018-11-21 20:33:37 +08:00
h := handler.GetGatewayHandler()
sid, err := h.DeleteTCPRule(&req)
2018-12-04 13:43:15 +08:00
if err != nil {
2018-11-22 09:34:00 +08:00
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
2018-11-21 20:33:37 +08:00
"deleting tcp rule: %v", err))
return
}
2019-03-12 09:59:09 +08:00
if err := handler.GetGatewayHandler().SendTask(map[string]interface{}{
"service_id": sid,
"action": "delete-tcp-rule",
}); err != nil {
2018-12-04 13:43:15 +08:00
logrus.Errorf("send runtime message about gateway failure %s", err.Error())
}
2018-11-21 20:33:37 +08:00
httputil.ReturnSuccess(r, w, "success")
2018-11-21 19:51:40 +08:00
}
2018-11-26 02:19:08 +08:00
// GetAvailablePort returns a available port
func (g *GatewayStruct) GetAvailablePort(w http.ResponseWriter, r *http.Request) {
h := handler.GetGatewayHandler()
res, err := h.GetAvailablePort()
if err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
"getting available port: %v", err))
return
}
httputil.ReturnSuccess(r, w, res)
2019-02-12 15:40:42 +08:00
}
2019-03-11 01:28:37 +08:00
// RuleConfig is used to add, update or delete rule config.
func (g *GatewayStruct) RuleConfig(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
g.addRuleConfig(w, r)
case "PUT":
g.updRuleConfig(w, r)
case "DELETE":
g.delRuleConfig(w, r)
}
}
func (g *GatewayStruct) addRuleConfig(w http.ResponseWriter, r *http.Request) {
var req api_model.AddRuleConfigReq
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
if !ok {
return
}
if err := handler.GetGatewayHandler().AddRuleConfig(&req); err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
"adding rule config: %v", err))
return
}
httputil.ReturnSuccess(r, w, "success")
}
func (g *GatewayStruct) updRuleConfig(w http.ResponseWriter, r *http.Request) {
var req api_model.UpdRuleConfigReq
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
if !ok {
return
}
if err := handler.GetGatewayHandler().UpdRuleConfig(&req); err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
"updating rule config: %v", err))
return
}
httputil.ReturnSuccess(r, w, "success")
}
func (g *GatewayStruct) delRuleConfig(w http.ResponseWriter, r *http.Request) {
var req api_model.AddRuleConfigReq
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
if !ok {
return
}
if err := handler.GetGatewayHandler().DelRuleConfig(req.ConfigID); err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
"deleting rule config: %v", err))
return
}
httputil.ReturnSuccess(r, w, "success")
}