forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
third_party_service_handler.go
184 lines (164 loc) · 5.12 KB
/
third_party_service_handler.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
// 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 (
"fmt"
"strconv"
"strings"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/api/model"
"github.com/goodrain/rainbond/db"
dbmodel "github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/util"
"github.com/goodrain/rainbond/worker/client"
)
// ThirdPartyServiceHanlder handles business logic for all third-party services
type ThirdPartyServiceHanlder struct {
dbmanager db.Manager
statusCli *client.AppRuntimeSyncClient
}
// Create3rdPartySvcHandler creates a new *ThirdPartyServiceHanlder.
func Create3rdPartySvcHandler(dbmanager db.Manager, statusCli *client.AppRuntimeSyncClient) *ThirdPartyServiceHanlder {
return &ThirdPartyServiceHanlder{
dbmanager: dbmanager,
statusCli: statusCli,
}
}
// AddEndpoints adds endpoints for third-party service.
func (t *ThirdPartyServiceHanlder) AddEndpoints(sid string, d *model.AddEndpiontsReq) error {
address, port := convertAddressPort(d.Address)
if port == 0 {
//set default port by service port
ports, _ := t.dbmanager.TenantServicesPortDao().GetPortsByServiceID(sid)
if len(ports) > 0 {
port = ports[0].ContainerPort
}
}
ep := &dbmodel.Endpoint{
UUID: util.NewUUID(),
ServiceID: sid,
IP: address,
Port: port,
IsOnline: &d.IsOnline,
}
if err := t.dbmanager.EndpointsDao().AddModel(ep); err != nil {
return err
}
logrus.Debugf("add new endpoint[address: %s, port: %d]", address, port)
t.statusCli.AddThirdPartyEndpoint(ep)
return nil
}
// UpdEndpoints updates endpoints for third-party service.
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
}
if d.Address != "" {
address, port := convertAddressPort(d.Address)
ep.IP = address
ep.Port = port
}
ep.IsOnline = &d.IsOnline
if err := t.dbmanager.EndpointsDao().UpdateModel(ep); err != nil {
return err
}
t.statusCli.UpdThirdPartyEndpoint(ep)
return nil
}
func convertAddressPort(s string) (address string, port int) {
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://"
}
if strings.Contains(s, ":") {
sp := strings.Split(s, ":")
address = prefix + sp[0]
port, _ = strconv.Atoi(sp[1])
} else {
address = prefix + s
}
return address, port
}
// DelEndpoints deletes endpoints for third-party service.
func (t *ThirdPartyServiceHanlder) DelEndpoints(epid, sid string) error {
ep, err := t.dbmanager.EndpointsDao().GetByUUID(epid)
if err != nil {
logrus.Warningf("EpID: %s; error getting endpoints: %v", epid, err)
return err
}
if err := t.dbmanager.EndpointsDao().DelByUUID(epid); err != nil {
return err
}
t.statusCli.DelThirdPartyEndpoint(ep)
return nil
}
// ListEndpoints lists third-party service endpoints.
func (t *ThirdPartyServiceHanlder) ListEndpoints(sid string) ([]*model.EndpointResp, error) {
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 {
ep := &model.EndpointResp{
EpID: item.UUID,
Address: func(ip string, p int) string {
if p != 0 {
return fmt.Sprintf("%s:%d", ip, p)
}
return ip
}(item.IP, item.Port),
Status: "-",
IsOnline: false,
IsStatic: true,
}
m[ep.Address] = ep
}
thirdPartyEndpoints, err := t.statusCli.ListThirdPartyEndpoints(sid)
if err != nil {
logrus.Warningf("ServiceID: %s; grpc; error listing third-party endpoints: %v", sid, err)
return nil, err
}
if thirdPartyEndpoints != nil && thirdPartyEndpoints.Obj != nil {
for _, item := range thirdPartyEndpoints.Obj {
ep := m[fmt.Sprintf("%s:%d", item.Ip, item.Port)]
if ep != nil {
ep.IsOnline = true
ep.Status = item.Status
continue
}
rep := &model.EndpointResp{
EpID: item.Uuid,
Address: item.Ip,
Status: item.Status,
IsOnline: true,
IsStatic: false,
}
m[rep.Address] = rep
}
}
var res []*model.EndpointResp
for _, item := range m {
res = append(res, item)
}
return res, nil
}