-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
89 lines (77 loc) · 2.01 KB
/
connection.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
package sdk
import (
"fmt"
cloutpkg "github.com/tliron/puccini/clout"
cloututil "github.com/tliron/puccini/clout/util"
)
//
// Connection
//
type Connection struct {
Relationship
Name string
IP string
Port int64
Source *OCIContainer
Target *OCIContainer
}
func GetConnection(vertex *cloutpkg.Vertex, edgesOutIndex int, edge *cloutpkg.Edge) (*Connection, error) {
var relationship struct {
Name string `ard:"name"`
Attributes struct {
IP string `ard:"ip"`
Port int64 `ard:"port"`
} `ard:"attributes"`
}
if err := ardReflector.Pack(edge.Properties, &relationship); err != nil {
return nil, err
}
connection := Connection{
Name: fmt.Sprintf("%s:%d", relationship.Name, edgesOutIndex),
IP: relationship.Attributes.IP,
Port: relationship.Attributes.Port,
Relationship: Relationship{
vertexID: vertex.ID,
edgesOutIndex: edgesOutIndex,
},
}
if sources, err := GetVertexOCIContainers(vertex); err == nil {
if len(sources) > 0 {
connection.Source = sources[0]
} else {
return nil, err
}
}
if edge.Target != nil {
if targets, err := GetVertexOCIContainers(edge.Target); err == nil {
if len(targets) > 0 {
connection.Target = targets[0]
}
} else {
return nil, err
}
}
return &connection, nil
}
func GetVertexConnections(vertex *cloutpkg.Vertex) ([]*Connection, error) {
var connections []*Connection
for index, edge := range cloututil.GetToscaRelationships(vertex, "cloud.puccini.khutulun::IPPort") {
if connection, err := GetConnection(vertex, index, edge); err == nil {
connections = append(connections, connection)
} else {
return nil, err
}
}
return connections, nil
}
func GetCloutConnections(clout *cloutpkg.Clout) ([]*Connection, error) {
var connections []*Connection
for _, vertex := range cloututil.GetToscaNodeTemplates(clout, "") {
if connections_, err := GetVertexConnections(vertex); err == nil {
connections = append(connections, connections_...)
} else {
return nil, err
}
}
return connections, nil
}