-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
97 lines (85 loc) · 2.31 KB
/
routes.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
/*
* *******************************************************************************
* * Copyright (c) 2023 Datasance Teknoloji A.S.
* *
* * This program and the accompanying materials are made available under the
* * terms of the Eclipse Public License v. 2.0 which is available at
* * http://www.eclipse.org/legal/epl-2.0
* *
* * SPDX-License-Identifier: EPL-2.0
* *******************************************************************************
*
*/
package get
import (
"github.com/datasance/potctl/internal/config"
rsc "github.com/datasance/potctl/internal/resource"
clientutil "github.com/datasance/potctl/internal/util/client"
"github.com/datasance/iofog-go-sdk/v3/pkg/client"
)
type routeExecutor struct {
namespace string
}
func newRouteExecutor(namespace string) *routeExecutor {
return &routeExecutor{
namespace: namespace,
}
}
func (exe *routeExecutor) GetName() string {
return ""
}
func (exe *routeExecutor) Execute() error {
printNamespace(exe.namespace)
table, err := generateRouteOutput(exe.namespace)
if err != nil {
return err
}
return print(table)
}
func generateRouteOutput(namespace string) (table [][]string, err error) {
_, err = config.GetNamespace(namespace)
if err != nil {
return
}
// Connect to Controller
clt, err := clientutil.NewControllerClient(namespace)
if err != nil && !rsc.IsNoControlPlaneError(err) {
return
}
routes := []client.Route{}
if err == nil {
// Populate table
listResponse, err := clt.ListRoutes()
if err != nil {
return table, err
}
routes = listResponse.Routes
}
return tabulateRoutes(namespace, routes)
}
func tabulateRoutes(namespace string, routes []client.Route) (table [][]string, err error) {
// Generate table and headers
table = make([][]string, len(routes)+1)
headers := []string{"ROUTE", "SOURCE MSVC", "DEST MSVC"}
table[0] = append(table[0], headers...)
// Populate rows
for idx, route := range routes {
// Convert route details
from, err := clientutil.GetMicroserviceName(namespace, route.SourceMicroserviceUUID)
if err != nil {
return table, err
}
to, err := clientutil.GetMicroserviceName(namespace, route.DestMicroserviceUUID)
if err != nil {
return table, err
}
// Store values
row := []string{
route.Name,
from,
to,
}
table[idx+1] = append(table[idx+1], row...)
}
return
}