2019-07-03 23:15:44 +02:00
|
|
|
package registry
|
2018-12-04 18:41:40 +02:00
|
|
|
|
2019-07-03 23:15:44 +02:00
|
|
|
func CopyServices(current []*Service) []*Service {
|
|
|
|
var services []*Service
|
2018-12-04 18:41:40 +02:00
|
|
|
|
|
|
|
for _, service := range current {
|
|
|
|
// copy service
|
2019-07-03 23:15:44 +02:00
|
|
|
s := new(Service)
|
2018-12-04 18:41:40 +02:00
|
|
|
*s = *service
|
|
|
|
|
|
|
|
// copy nodes
|
2019-07-03 23:15:44 +02:00
|
|
|
var nodes []*Node
|
2018-12-04 18:41:40 +02:00
|
|
|
for _, node := range service.Nodes {
|
2019-07-03 23:15:44 +02:00
|
|
|
n := new(Node)
|
2018-12-04 18:41:40 +02:00
|
|
|
*n = *node
|
|
|
|
nodes = append(nodes, n)
|
|
|
|
}
|
|
|
|
s.Nodes = nodes
|
|
|
|
|
|
|
|
// copy endpoints
|
2019-07-03 23:15:44 +02:00
|
|
|
var eps []*Endpoint
|
2018-12-04 18:41:40 +02:00
|
|
|
for _, ep := range service.Endpoints {
|
2019-07-03 23:15:44 +02:00
|
|
|
e := new(Endpoint)
|
2018-12-04 18:41:40 +02:00
|
|
|
*e = *ep
|
|
|
|
eps = append(eps, e)
|
|
|
|
}
|
|
|
|
s.Endpoints = eps
|
|
|
|
|
|
|
|
// append service
|
|
|
|
services = append(services, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2019-07-03 23:15:44 +02:00
|
|
|
func addServiceNodes(old, neu []*Node) []*Node {
|
|
|
|
var nodes []*Node
|
2018-12-29 17:44:51 +02:00
|
|
|
|
|
|
|
// add all new nodes
|
2018-12-04 18:41:40 +02:00
|
|
|
for _, n := range neu {
|
2018-12-29 17:44:51 +02:00
|
|
|
node := *n
|
|
|
|
nodes = append(nodes, &node)
|
|
|
|
}
|
|
|
|
|
|
|
|
// look at old nodes
|
|
|
|
for _, o := range old {
|
|
|
|
var exists bool
|
|
|
|
|
|
|
|
// check against new nodes
|
|
|
|
for _, n := range nodes {
|
|
|
|
// ids match then skip
|
2018-12-04 18:41:40 +02:00
|
|
|
if o.Id == n.Id {
|
2018-12-29 17:44:51 +02:00
|
|
|
exists = true
|
2018-12-04 18:41:40 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 17:44:51 +02:00
|
|
|
|
|
|
|
// keep old node
|
|
|
|
if !exists {
|
|
|
|
node := *o
|
|
|
|
nodes = append(nodes, &node)
|
2018-12-04 18:41:40 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-29 17:44:51 +02:00
|
|
|
|
|
|
|
return nodes
|
2018-12-04 18:41:40 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 23:15:44 +02:00
|
|
|
func AddServices(olist []*Service, nlist []*Service) []*Service {
|
|
|
|
var srv []*Service
|
2019-02-01 11:05:03 +02:00
|
|
|
|
2019-07-03 23:15:44 +02:00
|
|
|
for _, n := range nlist {
|
2018-12-04 18:41:40 +02:00
|
|
|
var seen bool
|
2019-07-03 23:15:44 +02:00
|
|
|
for _, o := range olist {
|
|
|
|
if o.Version == n.Version {
|
|
|
|
sp := new(Service)
|
2019-02-01 11:05:03 +02:00
|
|
|
// make copy
|
|
|
|
*sp = *o
|
|
|
|
// set nodes
|
2019-07-03 23:15:44 +02:00
|
|
|
sp.Nodes = addServiceNodes(o.Nodes, n.Nodes)
|
2019-02-01 11:05:03 +02:00
|
|
|
|
|
|
|
// mark as seen
|
2018-12-04 18:41:40 +02:00
|
|
|
seen = true
|
2019-02-01 11:05:03 +02:00
|
|
|
srv = append(srv, sp)
|
2018-12-04 18:41:40 +02:00
|
|
|
break
|
2019-07-03 23:15:44 +02:00
|
|
|
} else {
|
|
|
|
sp := new(Service)
|
|
|
|
// make copy
|
|
|
|
*sp = *o
|
|
|
|
srv = append(srv, sp)
|
2018-12-04 18:41:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !seen {
|
2019-07-03 23:15:44 +02:00
|
|
|
srv = append(srv, CopyServices([]*Service{n})...)
|
2018-12-04 18:41:40 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-01 11:05:03 +02:00
|
|
|
return srv
|
2018-12-04 18:41:40 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 23:15:44 +02:00
|
|
|
func delServiceNodes(old, del []*Node) []*Node {
|
|
|
|
var nodes []*Node
|
2018-12-04 18:41:40 +02:00
|
|
|
for _, o := range old {
|
|
|
|
var rem bool
|
|
|
|
for _, n := range del {
|
|
|
|
if o.Id == n.Id {
|
|
|
|
rem = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !rem {
|
|
|
|
nodes = append(nodes, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2019-07-03 23:15:44 +02:00
|
|
|
func DelServices(old, del []*Service) []*Service {
|
|
|
|
var services []*Service
|
2019-06-22 19:58:12 +02:00
|
|
|
|
2018-12-29 17:44:51 +02:00
|
|
|
for _, o := range old {
|
2019-07-03 23:15:44 +02:00
|
|
|
srv := new(Service)
|
2019-06-22 19:58:12 +02:00
|
|
|
*srv = *o
|
|
|
|
|
2018-12-04 18:41:40 +02:00
|
|
|
var rem bool
|
2019-06-22 19:58:12 +02:00
|
|
|
|
2018-12-04 18:41:40 +02:00
|
|
|
for _, s := range del {
|
2019-06-22 19:58:12 +02:00
|
|
|
if srv.Version == s.Version {
|
2019-07-03 23:15:44 +02:00
|
|
|
srv.Nodes = delServiceNodes(srv.Nodes, s.Nodes)
|
2019-06-22 19:58:12 +02:00
|
|
|
|
|
|
|
if len(srv.Nodes) == 0 {
|
2018-12-04 18:41:40 +02:00
|
|
|
rem = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-22 19:58:12 +02:00
|
|
|
|
2018-12-04 18:41:40 +02:00
|
|
|
if !rem {
|
2019-06-22 19:58:12 +02:00
|
|
|
services = append(services, srv)
|
2018-12-04 18:41:40 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-22 19:58:12 +02:00
|
|
|
|
2018-12-04 18:41:40 +02:00
|
|
|
return services
|
|
|
|
}
|