-
Notifications
You must be signed in to change notification settings - Fork 235
/
kind_registry.go
135 lines (108 loc) · 3.07 KB
/
kind_registry.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
package kind_registry
import (
"fmt"
"sort"
"strings"
"github.com/srl-labs/containerlab/nodes"
)
var KindRegistryInstance = newKindRegistry()
type Initializer func() nodes.Node
type KindRegistry struct {
// the nodeindex is a helping struct to speedup kind lookups.
nodeIndex map[string]*nodeRegistryEntry
}
// newKindRegistry constructs a new Registry.
func newKindRegistry() *KindRegistry {
r := &KindRegistry{
nodeIndex: map[string]*nodeRegistryEntry{},
}
return r
}
// Register registers the node' init function for all provided names.
func (r *KindRegistry) Register(names []string, initf Initializer, credentials *Credentials) error {
newEntry := newRegistryEntry(names, initf, credentials)
return r.addEntry(newEntry)
}
// addEntry adds the node entry to the registry.
func (r *KindRegistry) addEntry(entry *nodeRegistryEntry) error {
for _, name := range entry.nodeKindNames {
if _, exists := r.nodeIndex[name]; exists {
return fmt.Errorf("node kind %q already registered in Node Registry", name)
}
r.nodeIndex[name] = entry
}
return nil
}
// NewNodeOfKind return a new Node of the given Node Kind.
func (r *KindRegistry) NewNodeOfKind(nodeKindName string) (nodes.Node, error) {
nodeKindEntry, ok := r.nodeIndex[nodeKindName]
if !ok {
registeredKinds := strings.Join(r.GetRegisteredNodeKindNames(), ", ")
return nil, fmt.Errorf("kind %q is not supported. Supported kinds are %q", nodeKindName, registeredKinds)
}
// return a new instance of the requested node
return nodeKindEntry.initFunction(), nil
}
// GetRegisteredNodeKindNames returns a sorted slice of all the registered node kind names in the registry.
func (r *KindRegistry) GetRegisteredNodeKindNames() []string {
var result []string
for k := range r.nodeIndex {
result = append(result, k)
}
// sort and return
sort.Strings(result)
return result
}
func (r *KindRegistry) Kind(kind string) *nodeRegistryEntry {
return r.nodeIndex[kind]
}
type nodeRegistryEntry struct {
nodeKindNames []string
initFunction Initializer
credentials *Credentials
}
// Credentials returns entry's credentials.
func (e *nodeRegistryEntry) Credentials() *Credentials {
if e == nil {
return nil
}
return e.credentials
}
func newRegistryEntry(nodeKindNames []string, initFunction Initializer, credentials *Credentials) *nodeRegistryEntry {
return &nodeRegistryEntry{
nodeKindNames: nodeKindNames,
initFunction: initFunction,
credentials: credentials,
}
}
// Credentials defines NOS SSH credentials.
type Credentials struct {
username string
password string
}
// NewCredentials constructor for the Credentials struct.
func NewCredentials(username, password string) *Credentials {
return &Credentials{
username: username,
password: password,
}
}
func (c *Credentials) GetUsername() string {
if c == nil {
return ""
}
return c.username
}
func (c *Credentials) GetPassword() string {
if c == nil {
return ""
}
return c.password
}
// Slice returns credentials as a slice.
func (c *Credentials) Slice() []string {
if c == nil {
return nil
}
return []string{c.GetUsername(), c.GetPassword()}
}