-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes.go
67 lines (62 loc) · 2.65 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
package main
import (
"code.cloudfoundry.org/cli/cf/terminal"
"fmt"
"github.com/cloudfoundry/go-cfclient/v3/client"
"github.com/integrii/flaggy"
"github/metskem/panzer-plugin/conf"
"os"
"os/exec"
)
var colNames = []string{"hostname", "domain", "org", "space", "bound apps"}
/** listRoutes - The main function to produce the response to list routes. */
func listRoutes() {
flaggy.DefaultParser.ShowHelpOnUnexpected = false
flaggy.DefaultParser.ShowVersionWithVersionFlag = false
flaggy.Bool(&conf.FlagSwitchToSpace, "t", "target", "cf target the space where the route is found")
flaggy.String(&conf.FlagRoute, "r", "route", "the route to lookup (specify only hostname, without the domain name)")
flaggy.Parse()
if conf.FlagRoute == "" {
fmt.Println("Please use the -r flag to specify the route name")
os.Exit(1)
}
fmt.Printf("Getting routes for hostname %s as %s...\n\n", terminal.EntityNameColor(conf.FlagRoute), terminal.EntityNameColor(conf.CurrentUser))
routeListOptions := client.RouteListOptions{ListOptions: &client.ListOptions{}, Hosts: client.Filter{Values: []string{conf.FlagRoute}}}
if routes, err := conf.CfClient.Routes.ListAll(conf.CfCtx, &routeListOptions); err != nil {
fmt.Println(terminal.FailureColor(fmt.Sprintf("failed to get routes: %s", err)))
} else {
if len(routes) == 0 {
fmt.Printf("no routes found for hostname %s\n", conf.FlagRoute)
} else {
table := terminal.NewTable(colNames)
var orgName, spaceName string
for _, route := range routes {
var colValues [5]string
colValues[0] = conf.FlagRoute
domain, _ := conf.CfClient.Domains.Get(conf.CfCtx, route.Relationships.Domain.Data.GUID)
colValues[1] = domain.Name
space, _ := conf.CfClient.Spaces.Get(conf.CfCtx, route.Relationships.Space.Data.GUID)
org, _ := conf.CfClient.Organizations.Get(conf.CfCtx, space.Relationships.Organization.Data.GUID)
colValues[2] = org.Name
colValues[3] = space.Name
table.Add(colValues[:]...)
orgName = colValues[2]
spaceName = colValues[3]
var destList string
for _, dest := range route.Destinations {
app, _ := conf.CfClient.Applications.Get(conf.CfCtx, *dest.App.GUID)
destList = fmt.Sprintf("%s%s ", destList, app.Name)
}
colValues[4] = destList
}
_ = table.PrintTo(os.Stdout)
if conf.FlagSwitchToSpace {
// You normally would use cliConnection.CliCommand, but that screws up my "NetworkPolicyV1Endpoint" in my cf config.json. So instead issue os command:
cmd := exec.Command("cf", "target", "-o", orgName, "-s", spaceName)
if err = cmd.Run(); err != nil {
fmt.Printf("failed to set target to org %s and space %s: %s", orgName, spaceName, err)
}
}
}
}
}