-
Notifications
You must be signed in to change notification settings - Fork 929
/
space_summary.go
140 lines (117 loc) · 4.24 KB
/
space_summary.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
package v2action
import (
"sort"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
)
type SecurityGroupRule struct {
Name string
Description string
Destination string
Lifecycle ccv2.SecurityGroupLifecycle
Ports string
Protocol string
}
type SpaceSummary struct {
Space
OrgName string
OrgDefaultIsolationSegmentGUID string
AppNames []string
ServiceInstanceNames []string
SpaceQuotaName string
RunningSecurityGroupNames []string
StagingSecurityGroupNames []string
SecurityGroupRules []SecurityGroupRule
}
func (actor Actor) GetSpaceSummaryByOrganizationAndName(orgGUID string, name string, includeStagingSecurityGroupsRules bool) (SpaceSummary, Warnings, error) {
var allWarnings Warnings
org, warnings, err := actor.GetOrganization(orgGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return SpaceSummary{}, allWarnings, err
}
space, warnings, err := actor.GetSpaceByOrganizationAndName(org.GUID, name)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return SpaceSummary{}, allWarnings, err
}
apps, warnings, err := actor.GetApplicationsBySpace(space.GUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return SpaceSummary{}, allWarnings, err
}
appNames := make([]string, len(apps))
for i, app := range apps {
appNames[i] = app.Name
}
sort.Strings(appNames)
serviceInstances, warnings, err := actor.GetServiceInstancesBySpace(space.GUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return SpaceSummary{}, allWarnings, err
}
serviceInstanceNames := make([]string, len(serviceInstances))
for i, serviceInstance := range serviceInstances {
serviceInstanceNames[i] = serviceInstance.Name
}
sort.Strings(serviceInstanceNames)
var spaceQuota SpaceQuota
if space.SpaceQuotaDefinitionGUID != "" {
spaceQuota, warnings, err = actor.GetSpaceQuota(space.SpaceQuotaDefinitionGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return SpaceSummary{}, allWarnings, err
}
}
securityGroups, warnings, err := actor.GetSpaceRunningSecurityGroupsBySpace(space.GUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return SpaceSummary{}, allWarnings, err
}
var runningSecurityGroupNames []string
var stagingSecurityGroupNames []string
var securityGroupRules []SecurityGroupRule
for _, securityGroup := range securityGroups {
runningSecurityGroupNames = append(runningSecurityGroupNames, securityGroup.Name)
securityGroupRules = append(securityGroupRules, extractSecurityGroupRules(securityGroup, ccv2.SecurityGroupLifecycleRunning)...)
}
sort.Strings(runningSecurityGroupNames)
if includeStagingSecurityGroupsRules {
securityGroups, warnings, err = actor.GetSpaceStagingSecurityGroupsBySpace(space.GUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return SpaceSummary{}, allWarnings, err
}
for _, securityGroup := range securityGroups {
stagingSecurityGroupNames = append(stagingSecurityGroupNames, securityGroup.Name)
securityGroupRules = append(securityGroupRules, extractSecurityGroupRules(securityGroup, ccv2.SecurityGroupLifecycleStaging)...)
}
sort.Strings(stagingSecurityGroupNames)
}
sort.Slice(securityGroupRules, func(i int, j int) bool {
if securityGroupRules[i].Name < securityGroupRules[j].Name {
return true
}
if securityGroupRules[i].Name > securityGroupRules[j].Name {
return false
}
if securityGroupRules[i].Destination < securityGroupRules[j].Destination {
return true
}
if securityGroupRules[i].Destination > securityGroupRules[j].Destination {
return false
}
return securityGroupRules[i].Lifecycle < securityGroupRules[j].Lifecycle
})
spaceSummary := SpaceSummary{
Space: space,
OrgName: org.Name,
OrgDefaultIsolationSegmentGUID: org.DefaultIsolationSegmentGUID,
AppNames: appNames,
ServiceInstanceNames: serviceInstanceNames,
SpaceQuotaName: spaceQuota.Name,
RunningSecurityGroupNames: runningSecurityGroupNames,
StagingSecurityGroupNames: stagingSecurityGroupNames,
SecurityGroupRules: securityGroupRules,
}
return spaceSummary, allWarnings, nil
}