forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
space_command.go
198 lines (164 loc) · 6.05 KB
/
space_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
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
package v6
import (
"fmt"
"strings"
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v2action"
"code.cloudfoundry.org/cli/actor/v3action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/command/v6/shared"
sharedV3 "code.cloudfoundry.org/cli/command/v6/shared"
"code.cloudfoundry.org/cli/util/ui"
)
//go:generate counterfeiter . SpaceActor
type SpaceActor interface {
GetSpaceByOrganizationAndName(orgGUID string, spaceName string) (v2action.Space, v2action.Warnings, error)
GetSpaceSummaryByOrganizationAndName(orgGUID string, spaceName string) (v2action.SpaceSummary, v2action.Warnings, error)
}
//go:generate counterfeiter . SpaceActorV3
type SpaceActorV3 interface {
GetEffectiveIsolationSegmentBySpace(spaceGUID string, orgDefaultIsolationSegmentGUID string) (v3action.IsolationSegment, v3action.Warnings, error)
}
type SpaceCommand struct {
RequiredArgs flag.Space `positional-args:"yes"`
GUID bool `long:"guid" description:"Retrieve and display the given space's guid. All other output for the space is suppressed."`
SecurityGroupRules bool `long:"security-group-rules" description:"Retrieve the rules for all the security groups associated with the space."`
usage interface{} `usage:"CF_NAME space SPACE [--guid] [--security-group-rules]"`
relatedCommands interface{} `related_commands:"set-space-isolation-segment, space-quota, space-users"`
UI command.UI
Config command.Config
SharedActor command.SharedActor
Actor SpaceActor
ActorV3 SpaceActorV3
}
func (cmd *SpaceCommand) Setup(config command.Config, ui command.UI) error {
cmd.Config = config
cmd.UI = ui
cmd.SharedActor = sharedaction.NewActor(config)
ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui)
if err != nil {
return err
}
cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
ccClientV3, _, err := sharedV3.NewV3BasedClients(config, ui, true)
if err != nil {
if _, ok := err.(translatableerror.V3APIDoesNotExistError); !ok {
return err
}
} else {
cmd.ActorV3 = v3action.NewActor(ccClientV3, config, nil, nil)
}
return nil
}
func (cmd SpaceCommand) Execute(args []string) error {
err := cmd.SharedActor.CheckTarget(true, false)
if err == nil {
if cmd.GUID {
err = cmd.displaySpaceGUID()
} else {
err = cmd.displaySpaceSummary(cmd.SecurityGroupRules)
}
}
return err
}
func (cmd SpaceCommand) displaySpaceGUID() error {
org, warnings, err := cmd.Actor.GetSpaceByOrganizationAndName(cmd.Config.TargetedOrganization().GUID, cmd.RequiredArgs.Space)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayText(org.GUID)
return nil
}
func (cmd SpaceCommand) displaySpaceSummary(displaySecurityGroupRules bool) error {
user, err := cmd.Config.CurrentUser()
if err != nil {
return err
}
cmd.UI.DisplayTextWithFlavor("Getting info for space {{.TargetSpace}} in org {{.OrgName}} as {{.CurrentUser}}...", map[string]interface{}{
"TargetSpace": cmd.RequiredArgs.Space,
"OrgName": cmd.Config.TargetedOrganization().Name,
"CurrentUser": user.Name,
})
cmd.UI.DisplayNewline()
spaceSummary, warnings, err := cmd.Actor.GetSpaceSummaryByOrganizationAndName(cmd.Config.TargetedOrganization().GUID, cmd.RequiredArgs.Space)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
table := [][]string{
{cmd.UI.TranslateText("name:"), spaceSummary.Name},
{cmd.UI.TranslateText("org:"), spaceSummary.OrgName},
{cmd.UI.TranslateText("apps:"), strings.Join(spaceSummary.AppNames, ", ")},
{cmd.UI.TranslateText("services:"), strings.Join(spaceSummary.ServiceInstanceNames, ", ")},
}
isolationSegmentRow, err := cmd.isolationSegmentRow(spaceSummary)
if err != nil {
return err
}
if isolationSegmentRow != nil {
table = append(table, isolationSegmentRow)
}
table = append(table,
[]string{cmd.UI.TranslateText("space quota:"), spaceSummary.SpaceQuotaName})
table = append(table,
[]string{cmd.UI.TranslateText("running security groups:"), strings.Join(spaceSummary.RunningSecurityGroupNames, ", ")})
table = append(table,
[]string{cmd.UI.TranslateText("staging security groups:"), strings.Join(spaceSummary.StagingSecurityGroupNames, ", ")})
cmd.UI.DisplayKeyValueTable("", table, 3)
if displaySecurityGroupRules {
table := [][]string{
{
cmd.UI.TranslateText(""),
cmd.UI.TranslateText("security group"),
cmd.UI.TranslateText("destination"),
cmd.UI.TranslateText("ports"),
cmd.UI.TranslateText("protocol"),
cmd.UI.TranslateText("lifecycle"),
cmd.UI.TranslateText("description"),
},
}
currentGroupIndex := -1
var currentGroupName string
for _, securityGroupRule := range spaceSummary.SecurityGroupRules {
var currentGroupIndexString string
if securityGroupRule.Name != currentGroupName {
currentGroupIndex++
currentGroupIndexString = fmt.Sprintf("#%d", currentGroupIndex)
currentGroupName = securityGroupRule.Name
}
table = append(table, []string{
currentGroupIndexString,
securityGroupRule.Name,
securityGroupRule.Destination,
securityGroupRule.Ports,
securityGroupRule.Protocol,
string(securityGroupRule.Lifecycle),
securityGroupRule.Description,
})
}
cmd.UI.DisplayNewline()
cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
}
return nil
}
func (cmd SpaceCommand) isolationSegmentRow(spaceSummary v2action.SpaceSummary) ([]string, error) {
if cmd.ActorV3 == nil {
return nil, nil
}
isolationSegmentName := ""
isolationSegment, v3Warnings, err := cmd.ActorV3.GetEffectiveIsolationSegmentBySpace(
spaceSummary.GUID, spaceSummary.OrgDefaultIsolationSegmentGUID)
cmd.UI.DisplayWarnings(v3Warnings)
if err == nil {
isolationSegmentName = isolationSegment.Name
} else {
if _, ok := err.(actionerror.NoRelationshipError); !ok {
return nil, err
}
}
return []string{cmd.UI.TranslateText("isolation segment:"), isolationSegmentName}, nil
}