/
mock_external_registry.go
107 lines (89 loc) · 3.36 KB
/
mock_external_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
// Copyright 2016 IBM Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package store
import (
"github.com/amalgam8/amalgam8/pkg/auth"
)
type mockExternalRegistry struct {
mockServiceInstances map[string]*ServiceInstance
}
// NewMockExternalRegistry is a mock version of the external registry
func NewMockExternalRegistry(mockServiceInstances map[string]*ServiceInstance) ExternalRegistry {
db := &mockExternalRegistry{
mockServiceInstances: mockServiceInstances,
}
return db
}
func (mer *mockExternalRegistry) ReadKeys(namespace auth.Namespace) ([]string, error) {
var keys []string
for key := range mer.mockServiceInstances {
keys = append(keys, key)
}
return keys, nil
}
func (mer *mockExternalRegistry) ReadServiceInstanceByInstID(namespace auth.Namespace, instanceID string) (*ServiceInstance, error) {
for _, instance := range mer.mockServiceInstances {
if instanceID == instance.ID {
return instance.DeepClone(), nil
}
}
return &ServiceInstance{}, nil
}
func (mer *mockExternalRegistry) ListServiceInstancesByName(namespace auth.Namespace, name string) (map[string]*ServiceInstance, error) {
siMap := make(map[string]*ServiceInstance)
for _, instance := range mer.mockServiceInstances {
if name == instance.ServiceName {
dbKey := DBKey{InstanceID: instance.ID, Namespace: namespace.String()}
siMap[dbKey.String()] = instance.DeepClone()
continue
}
}
return siMap, nil
}
func (mer *mockExternalRegistry) ListAllServiceInstances(namespace auth.Namespace) (map[string]ServiceInstanceMap, error) {
// Sort the instances by service name
serviceList := make(map[string][]*ServiceInstance)
for _, instance := range mer.mockServiceInstances {
serviceList[instance.ServiceName] = append(serviceList[instance.ServiceName], instance)
}
serviceInstances := make(map[string]ServiceInstanceMap)
for sname, instances := range serviceList {
siMap := make(ServiceInstanceMap)
for _, inst := range instances {
dbKey := DBKey{InstanceID: inst.ID, Namespace: inst.ServiceName}
siMap[dbKey] = inst.DeepClone()
}
serviceInstances[sname] = siMap
}
return serviceInstances, nil
}
func (mer *mockExternalRegistry) InsertServiceInstance(namespace auth.Namespace, instance *ServiceInstance) error {
si := instance.DeepClone()
dbKey := DBKey{InstanceID: instance.ID, Namespace: namespace.String()}
mer.mockServiceInstances[dbKey.String()] = si
return nil
}
func (mer *mockExternalRegistry) DeleteServiceInstance(namespace auth.Namespace, instanceID string) (int, error) {
var count int
dbKey := DBKey{InstanceID: instanceID, Namespace: namespace.String()}
// Don't need to loop to delete, but need a count of instances deleted
for index := range mer.mockServiceInstances {
if index == dbKey.String() {
count++
// Remove the instance from the slice
delete(mer.mockServiceInstances, dbKey.String())
}
}
return count, nil
}