forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isolation_segment.go
198 lines (163 loc) · 7.36 KB
/
isolation_segment.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 v3action
import (
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
)
type IsolationSegmentSummary struct {
Name string
EntitledOrgs []string
}
// IsolationSegment represents a V3 actor IsolationSegment.
type IsolationSegment ccv3.IsolationSegment
// GetEffectiveIsolationSegmentBySpace returns the space's effective isolation
// segment.
//
// If the space has its own isolation segment, that will be returned.
//
// If the space does not have one, the organization's default isolation segment
// (GUID passed in) will be returned.
//
// If the space does not have one and the passed in organization default
// isolation segment GUID is empty, a NoRelationshipError will be returned.
func (actor Actor) GetEffectiveIsolationSegmentBySpace(spaceGUID string, orgDefaultIsolationSegmentGUID string) (IsolationSegment, Warnings, error) {
relationship, warnings, err := actor.CloudControllerClient.GetSpaceIsolationSegment(spaceGUID)
allWarnings := append(Warnings{}, warnings...)
if err != nil {
return IsolationSegment{}, allWarnings, err
}
effectiveGUID := relationship.GUID
if effectiveGUID == "" {
if orgDefaultIsolationSegmentGUID != "" {
effectiveGUID = orgDefaultIsolationSegmentGUID
} else {
return IsolationSegment{}, allWarnings, actionerror.NoRelationshipError{}
}
}
isolationSegment, warnings, err := actor.CloudControllerClient.GetIsolationSegment(effectiveGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return IsolationSegment{}, allWarnings, err
}
return IsolationSegment(isolationSegment), allWarnings, err
}
// CreateIsolationSegmentByName creates a given isolation segment.
func (actor Actor) CreateIsolationSegmentByName(isolationSegment IsolationSegment) (Warnings, error) {
_, warnings, err := actor.CloudControllerClient.CreateIsolationSegment(ccv3.IsolationSegment(isolationSegment))
if _, ok := err.(ccerror.UnprocessableEntityError); ok {
return Warnings(warnings), actionerror.IsolationSegmentAlreadyExistsError{Name: isolationSegment.Name}
}
return Warnings(warnings), err
}
// DeleteIsolationSegmentByName deletes the given isolation segment.
func (actor Actor) DeleteIsolationSegmentByName(name string) (Warnings, error) {
isolationSegment, warnings, err := actor.GetIsolationSegmentByName(name)
allWarnings := append(Warnings{}, warnings...)
if err != nil {
return allWarnings, err
}
apiWarnings, err := actor.CloudControllerClient.DeleteIsolationSegment(isolationSegment.GUID)
return append(allWarnings, apiWarnings...), err
}
// EntitleIsolationSegmentToOrganizationByName entitles the given organization
// to use the specified isolation segment
func (actor Actor) EntitleIsolationSegmentToOrganizationByName(isolationSegmentName string, orgName string) (Warnings, error) {
isolationSegment, warnings, err := actor.GetIsolationSegmentByName(isolationSegmentName)
allWarnings := append(Warnings{}, warnings...)
if err != nil {
return allWarnings, err
}
organization, warnings, err := actor.GetOrganizationByName(orgName)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return allWarnings, err
}
_, apiWarnings, err := actor.CloudControllerClient.EntitleIsolationSegmentToOrganizations(isolationSegment.GUID, []string{organization.GUID})
return append(allWarnings, apiWarnings...), err
}
func (actor Actor) AssignIsolationSegmentToSpaceByNameAndSpace(isolationSegmentName string, spaceGUID string) (Warnings, error) {
seg, warnings, err := actor.GetIsolationSegmentByName(isolationSegmentName)
if err != nil {
return warnings, err
}
_, apiWarnings, err := actor.CloudControllerClient.AssignSpaceToIsolationSegment(spaceGUID, seg.GUID)
return append(warnings, apiWarnings...), err
}
// GetIsolationSegmentByName returns the requested isolation segment.
func (actor Actor) GetIsolationSegmentByName(name string) (IsolationSegment, Warnings, error) {
isolationSegments, warnings, err := actor.CloudControllerClient.GetIsolationSegments(
ccv3.Query{Key: ccv3.NameFilter, Values: []string{name}},
)
if err != nil {
return IsolationSegment{}, Warnings(warnings), err
}
if len(isolationSegments) == 0 {
return IsolationSegment{}, Warnings(warnings), actionerror.IsolationSegmentNotFoundError{Name: name}
}
return IsolationSegment(isolationSegments[0]), Warnings(warnings), nil
}
// GetIsolationSegmentSummaries returns all isolation segments and their entitled orgs
func (actor Actor) GetIsolationSegmentSummaries() ([]IsolationSegmentSummary, Warnings, error) {
isolationSegments, warnings, err := actor.CloudControllerClient.GetIsolationSegments()
allWarnings := append(Warnings{}, warnings...)
if err != nil {
return nil, allWarnings, err
}
var isolationSegmentSummaries []IsolationSegmentSummary
for _, isolationSegment := range isolationSegments {
isolationSegmentSummary := IsolationSegmentSummary{
Name: isolationSegment.Name,
EntitledOrgs: []string{},
}
orgs, warnings, err := actor.CloudControllerClient.GetIsolationSegmentOrganizationsByIsolationSegment(isolationSegment.GUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return nil, allWarnings, err
}
for _, org := range orgs {
isolationSegmentSummary.EntitledOrgs = append(isolationSegmentSummary.EntitledOrgs, org.Name)
}
isolationSegmentSummaries = append(isolationSegmentSummaries, isolationSegmentSummary)
}
return isolationSegmentSummaries, allWarnings, nil
}
func (actor Actor) GetIsolationSegmentsByOrganization(orgGUID string) ([]IsolationSegment, Warnings, error) {
ccv3IsolationSegments, warnings, err := actor.CloudControllerClient.GetIsolationSegments(
ccv3.Query{Key: ccv3.OrganizationGUIDFilter, Values: []string{orgGUID}},
)
if err != nil {
return []IsolationSegment{}, Warnings(warnings), err
}
isolationSegments := make([]IsolationSegment, len(ccv3IsolationSegments))
for i := range ccv3IsolationSegments {
isolationSegments[i] = IsolationSegment(ccv3IsolationSegments[i])
}
return isolationSegments, Warnings(warnings), nil
}
func (actor Actor) RevokeIsolationSegmentFromOrganizationByName(isolationSegmentName string, orgName string) (Warnings, error) {
segment, warnings, err := actor.GetIsolationSegmentByName(isolationSegmentName)
allWarnings := append(Warnings{}, warnings...)
if err != nil {
return allWarnings, err
}
org, warnings, err := actor.GetOrganizationByName(orgName)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return allWarnings, err
}
apiWarnings, err := actor.CloudControllerClient.RevokeIsolationSegmentFromOrganization(segment.GUID, org.GUID)
allWarnings = append(allWarnings, apiWarnings...)
return allWarnings, err
}
// SetOrganizationDefaultIsolationSegment sets a default isolation segment on
// an organization.
func (actor Actor) SetOrganizationDefaultIsolationSegment(orgGUID string, isoSegGUID string) (Warnings, error) {
_, apiWarnings, err := actor.CloudControllerClient.PatchOrganizationDefaultIsolationSegment(orgGUID, isoSegGUID)
return Warnings(apiWarnings), err
}
// ResetOrganizationDefaultIsolationSegment resets the default isolation segment fon
// an organization.
func (actor Actor) ResetOrganizationDefaultIsolationSegment(orgGUID string) (Warnings, error) {
_, apiWarnings, err := actor.CloudControllerClient.PatchOrganizationDefaultIsolationSegment(orgGUID, "")
return Warnings(apiWarnings), err
}