forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
security_groups_command.go
114 lines (93 loc) · 3.07 KB
/
security_groups_command.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
package v6
import (
"fmt"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v2action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/v6/shared"
"code.cloudfoundry.org/cli/util/ui"
)
//go:generate counterfeiter . SecurityGroupsActor
type SecurityGroupsActor interface {
CloudControllerAPIVersion() string
GetSecurityGroupsWithOrganizationSpaceAndLifecycle(includeStaging bool) ([]v2action.SecurityGroupWithOrganizationSpaceAndLifecycle, v2action.Warnings, error)
}
type SecurityGroupsCommand struct {
usage interface{} `usage:"CF_NAME security-groups"`
relatedCommands interface{} `related_commands:"bind-security-group, bind-running-security-group, bind-staging-security-group, security-group"`
SharedActor command.SharedActor
Config command.Config
UI command.UI
Actor SecurityGroupsActor
}
func (cmd *SecurityGroupsCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
cmd.SharedActor = sharedaction.NewActor(config)
ccClient, uaaClient, err := shared.NewClients(config, ui, true)
if err != nil {
return err
}
cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
return nil
}
func (cmd SecurityGroupsCommand) Execute(args []string) error {
user, err := cmd.Config.CurrentUser()
if err != nil {
return err
}
err = cmd.SharedActor.CheckTarget(false, false)
if err != nil {
return err
}
includeStaging := true
cmd.UI.DisplayTextWithFlavor("Getting security groups as {{.UserName}}...",
map[string]interface{}{"UserName": user.Name})
secGroupOrgSpaces, warnings, err := cmd.Actor.GetSecurityGroupsWithOrganizationSpaceAndLifecycle(includeStaging)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayOK()
table := [][]string{
{
cmd.UI.TranslateText(""),
cmd.UI.TranslateText("name"),
cmd.UI.TranslateText("organization"),
cmd.UI.TranslateText("space"),
cmd.UI.TranslateText("lifecycle"),
},
}
currentGroupIndex := -1
var currentGroupName string
for _, secGroupOrgSpace := range secGroupOrgSpaces {
var currentGroupIndexString string
if secGroupOrgSpace.SecurityGroup.Name != currentGroupName {
currentGroupIndex++
currentGroupIndexString = fmt.Sprintf("#%d", currentGroupIndex)
currentGroupName = secGroupOrgSpace.SecurityGroup.Name
}
switch {
case secGroupOrgSpace.Organization.Name == "" && secGroupOrgSpace.Space.Name == "" &&
(secGroupOrgSpace.SecurityGroup.RunningDefault ||
secGroupOrgSpace.SecurityGroup.StagingDefault):
table = append(table, []string{
currentGroupIndexString,
secGroupOrgSpace.SecurityGroup.Name,
cmd.UI.TranslateText("<all>"),
cmd.UI.TranslateText("<all>"),
string(secGroupOrgSpace.Lifecycle),
})
default:
table = append(table, []string{
currentGroupIndexString,
secGroupOrgSpace.SecurityGroup.Name,
secGroupOrgSpace.Organization.Name,
secGroupOrgSpace.Space.Name,
string(secGroupOrgSpace.Lifecycle),
})
}
}
cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
return nil
}