This repository has been archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mock.go
239 lines (216 loc) · 5.97 KB
/
mock.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package client
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/castai/cli/pkg/client/sdk"
)
func NewMock() Interface {
cred1, cred2, cred3 := "cred1", "cred2", "cred3"
c1 := "00000000-0000-0000-0000-000000000000"
n1 := "11111111-1111-1111-1111-111111111111"
now := time.Now()
c1Nodes := []sdk.Node{
{
Id: stringPointer(n1),
Cloud: "aws",
InstanceType: "t3a.large",
Name: stringPointer("node1"),
Role: "master",
Shape: "t3a.large",
SpotConfig: nil,
State: &sdk.NodeState{Phase: stringPointer("ready")},
Network: &sdk.NodeNetwork{
PrivateIp: "127.0.0.1",
PublicIp: "1.1.1.1",
},
CreatedAt: &now,
},
}
return &mockClient{
credentials: []sdk.CloudCredentials{
{
Cloud: "aws",
Id: cred1,
Name: "aws",
UsedBy: nil,
},
{
Cloud: "gcp",
Id: cred2,
Name: "gcp",
UsedBy: nil,
},
{
Cloud: "azure",
Id: cred3,
Name: "azure",
UsedBy: nil,
},
},
clusters: map[string]sdk.KubernetesCluster{
c1: {
Addons: nil,
CloudCredentialsIDs: []string{cred1},
Id: c1,
Name: "test-cluster-1",
Region: sdk.ClusterRegion{
Name: "eu-central",
DisplayName: " Europe Central (Frankfurt)",
},
Status: "ready",
Nodes: c1Nodes,
CreatedAt: &now,
},
},
nodes: map[string]map[string]sdk.Node{
c1: {
n1: c1Nodes[0],
},
},
regions: []sdk.CastRegion{
{
Clouds: []sdk.Cloud{
{
Name: "aws",
},
{
Name: "gcp",
},
{
Name: "azure",
},
{
Name: "do",
},
},
DisplayName: "Europe Central (Frankfurt)",
Name: "eu-central",
},
},
feedbackEvents: []sdk.KubernetesClusterFeedbackEvent{
{
CreatedAt: time.Date(2021, 1, 1, 12, 15, 5, 0, time.UTC),
Id: "",
Message: "[AWS] VPC Created",
Severity: "info",
},
},
}
}
func stringPointer(v string) *string {
return &v
}
type mockClient struct {
credentials []sdk.CloudCredentials
clusters map[string]sdk.KubernetesCluster
nodes map[string]map[string]sdk.Node
regions []sdk.CastRegion
tokens []sdk.AuthToken
feedbackEvents []sdk.KubernetesClusterFeedbackEvent
}
func (m *mockClient) GetClusterFeedbackEvents(ctx context.Context, clusterID sdk.ClusterId) ([]sdk.KubernetesClusterFeedbackEvent, error) {
return []sdk.KubernetesClusterFeedbackEvent{}, nil
}
func (m *mockClient) TriggerClusterReconcile(ctx context.Context, clusterID sdk.ClusterId) error {
return nil
}
func (m *mockClient) AddClusterNode(ctx context.Context, clusterID sdk.ClusterId, node sdk.Node) error {
nodes, ok := m.nodes[string(clusterID)]
if !ok {
return fmt.Errorf("cluster not found, id=%s", clusterID)
}
nodes[uuid.New().String()] = node
return nil
}
func (m *mockClient) DeleteClusterNode(ctx context.Context, clusterID sdk.ClusterId, nodeID string) error {
nodes, ok := m.nodes[string(clusterID)]
if !ok {
return fmt.Errorf("cluster not found, id=%s", clusterID)
}
delete(nodes, nodeID)
return nil
}
func (m *mockClient) CloseNodeSSH(ctx context.Context, clusterID sdk.ClusterId, nodeID string) error {
return nil
}
func (m *mockClient) GetClusterNode(ctx context.Context, clusterID sdk.ClusterId, nodeID string) (*sdk.Node, error) {
nodes, ok := m.nodes[string(clusterID)]
if !ok {
return nil, fmt.Errorf("cluster %s not found", clusterID)
}
node, ok := nodes[nodeID]
if !ok {
return nil, fmt.Errorf("node %s not found", nodeID)
}
return &node, nil
}
func (m *mockClient) SetupNodeSSH(ctx context.Context, clusterID sdk.ClusterId, nodeID string, req sdk.SetupNodeSshJSONRequestBody) error {
return nil
}
func (m *mockClient) ListClusterNodes(ctx context.Context, req sdk.ClusterId) ([]sdk.Node, error) {
var res []sdk.Node
for _, node := range m.nodes[string(req)] {
res = append(res, node)
}
return res, nil
}
func (m *mockClient) GetCluster(ctx context.Context, req sdk.ClusterId) (*sdk.KubernetesCluster, error) {
if c, ok := m.clusters[string(req)]; ok {
return &c, nil
}
return nil, fmt.Errorf("cluster %s not found", req)
}
func (m *mockClient) DeleteCluster(ctx context.Context, req sdk.ClusterId) error {
if _, ok := m.clusters[string(req)]; ok {
delete(m.clusters, string(req))
return nil
}
return fmt.Errorf("cluster %s not found", req)
}
func (m *mockClient) FeedbackEvents(ctx context.Context, req sdk.ClusterId) ([]sdk.KubernetesClusterFeedbackEvent, error) {
return m.feedbackEvents, nil
}
func (m *mockClient) CreateNewCluster(ctx context.Context, req sdk.CreateNewClusterJSONRequestBody) (*sdk.KubernetesCluster, error) {
newCluster := sdk.KubernetesCluster{
Addons: req.Addons,
CloudCredentialsIDs: req.CloudCredentialsIDs,
Id: uuid.New().String(),
Name: req.Name,
Network: req.Network,
Nodes: req.Nodes,
ReconcileMode: "ok",
Region: sdk.ClusterRegion{
Name: req.Region,
},
Status: "ready",
}
m.clusters[newCluster.Id] = newCluster
return &newCluster, nil
}
func (m *mockClient) ListRegions(ctx context.Context) ([]sdk.CastRegion, error) {
return m.regions, nil
}
func (m *mockClient) ListCloudCredentials(ctx context.Context) ([]sdk.CloudCredentials, error) {
return m.credentials, nil
}
func (m *mockClient) GetClusterKubeconfig(ctx context.Context, req sdk.ClusterId) ([]byte, error) {
config := `
apiVersion: v1
clusters:
- cluster:
server: https://server.local.onmulti.cloud:6443
name: test`
return []byte(config), nil
}
func (m *mockClient) ListKubernetesClusters(ctx context.Context, req *sdk.ListKubernetesClustersParams) ([]sdk.KubernetesCluster, error) {
list := make([]sdk.KubernetesCluster, 0, len(m.clusters))
for _, cluster := range m.clusters {
list = append(list, cluster)
}
return list, nil
}
func (m *mockClient) ListAuthTokens(ctx context.Context) ([]sdk.AuthToken, error) {
return m.tokens, nil
}