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 handler
|
|
|
|
|
|
|
|
import (
|
2019-03-14 11:12:17 +08:00
|
|
|
"fmt"
|
2019-05-17 10:40:46 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-02-24 05:13:54 +08:00
|
|
|
"github.com/goodrain/rainbond/api/model"
|
|
|
|
"github.com/goodrain/rainbond/db"
|
2019-03-08 14:46:11 +08:00
|
|
|
dbmodel "github.com/goodrain/rainbond/db/model"
|
2019-03-14 11:12:17 +08:00
|
|
|
"github.com/goodrain/rainbond/util"
|
2020-11-25 16:39:38 +08:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-09-19 21:08:01 +08:00
|
|
|
|
2019-03-01 14:36:20 +08:00
|
|
|
"github.com/goodrain/rainbond/worker/client"
|
2019-02-24 05:13:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// ThirdPartyServiceHanlder handles business logic for all third-party services
|
|
|
|
type ThirdPartyServiceHanlder struct {
|
|
|
|
dbmanager db.Manager
|
2019-03-01 13:27:27 +08:00
|
|
|
statusCli *client.AppRuntimeSyncClient
|
2019-02-24 05:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create3rdPartySvcHandler creates a new *ThirdPartyServiceHanlder.
|
2019-03-01 13:27:27 +08:00
|
|
|
func Create3rdPartySvcHandler(dbmanager db.Manager, statusCli *client.AppRuntimeSyncClient) *ThirdPartyServiceHanlder {
|
|
|
|
return &ThirdPartyServiceHanlder{
|
|
|
|
dbmanager: dbmanager,
|
|
|
|
statusCli: statusCli,
|
|
|
|
}
|
2019-02-24 05:13:54 +08:00
|
|
|
}
|
|
|
|
|
2019-03-01 14:36:20 +08:00
|
|
|
// AddEndpoints adds endpoints for third-party service.
|
2019-02-25 10:57:01 +08:00
|
|
|
func (t *ThirdPartyServiceHanlder) AddEndpoints(sid string, d *model.AddEndpiontsReq) error {
|
2019-05-17 10:40:46 +08:00
|
|
|
address, port := convertAddressPort(d.Address)
|
2020-01-07 21:31:26 +08:00
|
|
|
if port == 0 {
|
|
|
|
//set default port by service port
|
|
|
|
ports, _ := t.dbmanager.TenantServicesPortDao().GetPortsByServiceID(sid)
|
|
|
|
if len(ports) > 0 {
|
|
|
|
port = ports[0].ContainerPort
|
|
|
|
}
|
|
|
|
}
|
2019-03-08 14:46:11 +08:00
|
|
|
ep := &dbmodel.Endpoint{
|
|
|
|
UUID: util.NewUUID(),
|
|
|
|
ServiceID: sid,
|
2019-05-17 10:40:46 +08:00
|
|
|
IP: address,
|
|
|
|
Port: port,
|
2019-03-08 14:46:11 +08:00
|
|
|
IsOnline: &d.IsOnline,
|
|
|
|
}
|
|
|
|
if err := t.dbmanager.EndpointsDao().AddModel(ep); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-17 18:19:34 +08:00
|
|
|
logrus.Debugf("add new endpoint[address: %s, port: %d]", address, port)
|
2019-03-08 14:46:11 +08:00
|
|
|
t.statusCli.AddThirdPartyEndpoint(ep)
|
|
|
|
return nil
|
2019-02-24 05:13:54 +08:00
|
|
|
}
|
|
|
|
|
2019-03-01 14:36:20 +08:00
|
|
|
// UpdEndpoints updates endpoints for third-party service.
|
2019-03-08 14:46:11 +08:00
|
|
|
func (t *ThirdPartyServiceHanlder) UpdEndpoints(d *model.UpdEndpiontsReq) error {
|
|
|
|
ep, err := t.dbmanager.EndpointsDao().GetByUUID(d.EpID)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warningf("EpID: %s; error getting endpoints: %v", d.EpID, err)
|
|
|
|
return err
|
2019-02-25 10:57:01 +08:00
|
|
|
}
|
2019-05-17 10:40:46 +08:00
|
|
|
if d.Address != "" {
|
|
|
|
address, port := convertAddressPort(d.Address)
|
|
|
|
ep.IP = address
|
|
|
|
ep.Port = port
|
2019-03-08 14:46:11 +08:00
|
|
|
}
|
|
|
|
ep.IsOnline = &d.IsOnline
|
|
|
|
if err := t.dbmanager.EndpointsDao().UpdateModel(ep); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
t.statusCli.UpdThirdPartyEndpoint(ep)
|
2019-03-14 11:12:17 +08:00
|
|
|
|
2019-03-08 14:46:11 +08:00
|
|
|
return nil
|
2019-02-24 05:13:54 +08:00
|
|
|
}
|
|
|
|
|
2019-05-17 10:40:46 +08:00
|
|
|
func convertAddressPort(s string) (address string, port int) {
|
2019-09-19 21:08:01 +08:00
|
|
|
prefix := ""
|
|
|
|
if strings.HasPrefix(s, "https://") {
|
|
|
|
s = strings.Split(s, "https://")[1]
|
|
|
|
prefix = "https://"
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(s, "http://") {
|
|
|
|
s = strings.Split(s, "http://")[1]
|
|
|
|
prefix = "http://"
|
2019-09-17 18:19:34 +08:00
|
|
|
}
|
2019-09-19 21:08:01 +08:00
|
|
|
|
|
|
|
if strings.Contains(s, ":") {
|
|
|
|
sp := strings.Split(s, ":")
|
|
|
|
address = prefix + sp[0]
|
|
|
|
port, _ = strconv.Atoi(sp[1])
|
|
|
|
} else {
|
|
|
|
address = prefix + s
|
2019-05-17 10:40:46 +08:00
|
|
|
}
|
2019-09-19 21:08:01 +08:00
|
|
|
|
2019-05-17 10:40:46 +08:00
|
|
|
return address, port
|
|
|
|
}
|
|
|
|
|
2019-03-01 14:36:20 +08:00
|
|
|
// DelEndpoints deletes endpoints for third-party service.
|
2019-03-08 14:46:11 +08:00
|
|
|
func (t *ThirdPartyServiceHanlder) DelEndpoints(epid, sid string) error {
|
2020-02-05 23:43:07 +08:00
|
|
|
ep, err := t.dbmanager.EndpointsDao().GetByUUID(epid)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warningf("EpID: %s; error getting endpoints: %v", epid, err)
|
|
|
|
return err
|
|
|
|
}
|
2019-03-08 14:46:11 +08:00
|
|
|
if err := t.dbmanager.EndpointsDao().DelByUUID(epid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-05 23:43:07 +08:00
|
|
|
t.statusCli.DelThirdPartyEndpoint(ep)
|
2019-03-08 14:46:11 +08:00
|
|
|
|
|
|
|
return nil
|
2019-02-24 21:10:13 +08:00
|
|
|
}
|
|
|
|
|
2019-02-25 10:57:01 +08:00
|
|
|
// ListEndpoints lists third-party service endpoints.
|
|
|
|
func (t *ThirdPartyServiceHanlder) ListEndpoints(sid string) ([]*model.EndpointResp, error) {
|
2019-03-14 11:12:17 +08:00
|
|
|
endpoints, err := t.dbmanager.EndpointsDao().List(sid)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warningf("ServiceID: %s; error listing endpoints from db; %v", sid, err)
|
|
|
|
}
|
|
|
|
m := make(map[string]*model.EndpointResp)
|
|
|
|
for _, item := range endpoints {
|
2020-02-05 22:24:17 +08:00
|
|
|
ep := &model.EndpointResp{
|
2019-03-14 11:12:17 +08:00
|
|
|
EpID: item.UUID,
|
2019-05-17 10:40:46 +08:00
|
|
|
Address: func(ip string, p int) string {
|
2019-03-31 02:43:04 +08:00
|
|
|
if p != 0 {
|
|
|
|
return fmt.Sprintf("%s:%d", ip, p)
|
2019-03-14 11:12:17 +08:00
|
|
|
}
|
|
|
|
return ip
|
|
|
|
}(item.IP, item.Port),
|
|
|
|
Status: "-",
|
|
|
|
IsOnline: false,
|
|
|
|
IsStatic: true,
|
|
|
|
}
|
2020-02-05 22:24:17 +08:00
|
|
|
m[ep.Address] = ep
|
2019-03-14 11:12:17 +08:00
|
|
|
}
|
2019-03-08 14:46:11 +08:00
|
|
|
thirdPartyEndpoints, err := t.statusCli.ListThirdPartyEndpoints(sid)
|
2019-03-01 13:27:27 +08:00
|
|
|
if err != nil {
|
2019-03-08 14:46:11 +08:00
|
|
|
logrus.Warningf("ServiceID: %s; grpc; error listing third-party endpoints: %v", sid, err)
|
2019-02-25 10:57:01 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-14 11:12:17 +08:00
|
|
|
if thirdPartyEndpoints != nil && thirdPartyEndpoints.Obj != nil {
|
|
|
|
for _, item := range thirdPartyEndpoints.Obj {
|
2020-02-05 22:24:17 +08:00
|
|
|
ep := m[fmt.Sprintf("%s:%d", item.Ip, item.Port)]
|
2019-03-14 11:12:17 +08:00
|
|
|
if ep != nil {
|
|
|
|
ep.IsOnline = true
|
|
|
|
ep.Status = item.Status
|
|
|
|
continue
|
|
|
|
}
|
2020-02-05 22:24:17 +08:00
|
|
|
rep := &model.EndpointResp{
|
2019-03-31 02:43:04 +08:00
|
|
|
EpID: item.Uuid,
|
2019-05-17 10:40:46 +08:00
|
|
|
Address: item.Ip,
|
2019-03-14 11:12:17 +08:00
|
|
|
Status: item.Status,
|
|
|
|
IsOnline: true,
|
|
|
|
IsStatic: false,
|
|
|
|
}
|
2020-02-05 22:24:17 +08:00
|
|
|
m[rep.Address] = rep
|
2019-03-14 11:12:17 +08:00
|
|
|
}
|
2019-03-08 14:46:11 +08:00
|
|
|
}
|
2019-02-25 10:57:01 +08:00
|
|
|
var res []*model.EndpointResp
|
2019-03-14 11:12:17 +08:00
|
|
|
for _, item := range m {
|
|
|
|
res = append(res, item)
|
2019-02-25 10:57:01 +08:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|