github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/actor/v3action/isolation_segment.go (about) 1 package v3action 2 3 import ( 4 "fmt" 5 "net/url" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 9 ) 10 11 type IsolationSegmentSummary struct { 12 Name string 13 EntitledOrgs []string 14 } 15 16 // IsolationSegment represents a V3 actor IsolationSegment. 17 type IsolationSegment ccv3.IsolationSegment 18 19 // IsolationSegmentNotFoundError represents the error that occurs when the 20 // isolation segment is not found. 21 type IsolationSegmentNotFoundError struct { 22 Name string 23 } 24 25 func (e IsolationSegmentNotFoundError) Error() string { 26 return fmt.Sprintf("Isolation Segment '%s' not found.", e.Name) 27 } 28 29 // IsolationSegmentAlreadyExistsError gets returned when an isolation segment 30 // already exists. 31 type IsolationSegmentAlreadyExistsError struct { 32 Name string 33 } 34 35 func (e IsolationSegmentAlreadyExistsError) Error() string { 36 return fmt.Sprintf("Isolation Segment '%s' already exists.", e.Name) 37 } 38 39 // GetEffectiveIsolationSegmentBySpace returns the space's effective isolation 40 // segment. 41 // 42 // If the space has its own isolation segment, that will be returned. 43 // 44 // If the space does not have one, the organization's default isolation segment 45 // (GUID passed in) will be returned. 46 // 47 // If the space does not have one and the passed in organization default 48 // isolation segment GUID is empty, a NoRelationshipError will be returned. 49 func (actor Actor) GetEffectiveIsolationSegmentBySpace(spaceGUID string, orgDefaultIsolationSegmentGUID string) (IsolationSegment, Warnings, error) { 50 relationship, warnings, err := actor.CloudControllerClient.GetSpaceIsolationSegment(spaceGUID) 51 allWarnings := append(Warnings{}, warnings...) 52 if err != nil { 53 return IsolationSegment{}, allWarnings, err 54 } 55 56 effectiveGUID := relationship.GUID 57 if effectiveGUID == "" { 58 if orgDefaultIsolationSegmentGUID != "" { 59 effectiveGUID = orgDefaultIsolationSegmentGUID 60 } else { 61 return IsolationSegment{}, allWarnings, NoRelationshipError{} 62 } 63 } 64 65 isolationSegment, warnings, err := actor.CloudControllerClient.GetIsolationSegment(effectiveGUID) 66 allWarnings = append(allWarnings, warnings...) 67 if err != nil { 68 return IsolationSegment{}, allWarnings, err 69 } 70 71 return IsolationSegment(isolationSegment), allWarnings, err 72 } 73 74 // CreateIsolationSegmentByName creates a given isolation segment. 75 func (actor Actor) CreateIsolationSegmentByName(isolationSegment IsolationSegment) (Warnings, error) { 76 _, warnings, err := actor.CloudControllerClient.CreateIsolationSegment(ccv3.IsolationSegment(isolationSegment)) 77 if _, ok := err.(ccerror.UnprocessableEntityError); ok { 78 return Warnings(warnings), IsolationSegmentAlreadyExistsError{Name: isolationSegment.Name} 79 } 80 return Warnings(warnings), err 81 } 82 83 // DeleteIsolationSegmentByName deletes the given isolation segment. 84 func (actor Actor) DeleteIsolationSegmentByName(name string) (Warnings, error) { 85 isolationSegment, warnings, err := actor.GetIsolationSegmentByName(name) 86 allWarnings := append(Warnings{}, warnings...) 87 if err != nil { 88 return allWarnings, err 89 } 90 91 apiWarnings, err := actor.CloudControllerClient.DeleteIsolationSegment(isolationSegment.GUID) 92 return append(allWarnings, apiWarnings...), err 93 } 94 95 // EntitleIsolationSegmentToOrganizationByName entitles the given organization 96 // to use the specified isolation segment 97 func (actor Actor) EntitleIsolationSegmentToOrganizationByName(isolationSegmentName string, orgName string) (Warnings, error) { 98 isolationSegment, warnings, err := actor.GetIsolationSegmentByName(isolationSegmentName) 99 allWarnings := append(Warnings{}, warnings...) 100 if err != nil { 101 return allWarnings, err 102 } 103 104 organization, warnings, err := actor.GetOrganizationByName(orgName) 105 allWarnings = append(allWarnings, warnings...) 106 if err != nil { 107 return allWarnings, err 108 } 109 110 _, apiWarnings, err := actor.CloudControllerClient.EntitleIsolationSegmentToOrganizations(isolationSegment.GUID, []string{organization.GUID}) 111 return append(allWarnings, apiWarnings...), err 112 } 113 114 func (actor Actor) AssignIsolationSegmentToSpaceByNameAndSpace(isolationSegmentName string, spaceGUID string) (Warnings, error) { 115 seg, warnings, err := actor.GetIsolationSegmentByName(isolationSegmentName) 116 if err != nil { 117 return warnings, err 118 } 119 120 _, apiWarnings, err := actor.CloudControllerClient.AssignSpaceToIsolationSegment(spaceGUID, seg.GUID) 121 return append(warnings, apiWarnings...), err 122 } 123 124 // GetIsolationSegmentByName returns the requested isolation segment. 125 func (actor Actor) GetIsolationSegmentByName(name string) (IsolationSegment, Warnings, error) { 126 isolationSegments, warnings, err := actor.CloudControllerClient.GetIsolationSegments(url.Values{ccv3.NameFilter: []string{name}}) 127 if err != nil { 128 return IsolationSegment{}, Warnings(warnings), err 129 } 130 131 if len(isolationSegments) == 0 { 132 return IsolationSegment{}, Warnings(warnings), IsolationSegmentNotFoundError{Name: name} 133 } 134 135 return IsolationSegment(isolationSegments[0]), Warnings(warnings), nil 136 } 137 138 // GetIsolationSegmentSummaries returns all isolation segments and their entitled orgs 139 func (actor Actor) GetIsolationSegmentSummaries() ([]IsolationSegmentSummary, Warnings, error) { 140 isolationSegments, warnings, err := actor.CloudControllerClient.GetIsolationSegments(nil) 141 allWarnings := append(Warnings{}, warnings...) 142 if err != nil { 143 return nil, allWarnings, err 144 } 145 146 var isolationSegmentSummaries []IsolationSegmentSummary 147 148 for _, isolationSegment := range isolationSegments { 149 isolationSegmentSummary := IsolationSegmentSummary{ 150 Name: isolationSegment.Name, 151 EntitledOrgs: []string{}, 152 } 153 154 orgs, warnings, err := actor.CloudControllerClient.GetIsolationSegmentOrganizationsByIsolationSegment(isolationSegment.GUID) 155 allWarnings = append(allWarnings, warnings...) 156 if err != nil { 157 return nil, allWarnings, err 158 } 159 160 for _, org := range orgs { 161 isolationSegmentSummary.EntitledOrgs = append(isolationSegmentSummary.EntitledOrgs, org.Name) 162 } 163 164 isolationSegmentSummaries = append(isolationSegmentSummaries, isolationSegmentSummary) 165 } 166 return isolationSegmentSummaries, allWarnings, nil 167 } 168 169 func (actor Actor) GetIsolationSegmentsByOrganization(orgGUID string) ([]IsolationSegment, Warnings, error) { 170 ccv3IsolationSegments, warnings, err := actor.CloudControllerClient.GetIsolationSegments(url.Values{ 171 ccv3.OrganizationGUIDFilter: []string{orgGUID}, 172 }) 173 if err != nil { 174 return []IsolationSegment{}, Warnings(warnings), err 175 } 176 177 isolationSegments := make([]IsolationSegment, len(ccv3IsolationSegments)) 178 179 for i, _ := range ccv3IsolationSegments { 180 isolationSegments[i] = IsolationSegment(ccv3IsolationSegments[i]) 181 } 182 183 return isolationSegments, Warnings(warnings), nil 184 } 185 186 func (actor Actor) RevokeIsolationSegmentFromOrganizationByName(isolationSegmentName string, orgName string) (Warnings, error) { 187 segment, warnings, err := actor.GetIsolationSegmentByName(isolationSegmentName) 188 allWarnings := append(Warnings{}, warnings...) 189 if err != nil { 190 return allWarnings, err 191 } 192 193 org, warnings, err := actor.GetOrganizationByName(orgName) 194 allWarnings = append(allWarnings, warnings...) 195 196 if err != nil { 197 return allWarnings, err 198 } 199 200 apiWarnings, err := actor.CloudControllerClient.RevokeIsolationSegmentFromOrganization(segment.GUID, org.GUID) 201 202 allWarnings = append(allWarnings, apiWarnings...) 203 return allWarnings, err 204 } 205 206 // SetOrganizationDefaultIsolationSegment sets a default isolation segment on 207 // an organization. 208 func (actor Actor) SetOrganizationDefaultIsolationSegment(orgGUID string, isoSegGUID string) (Warnings, error) { 209 apiWarnings, err := actor.CloudControllerClient.PatchOrganizationDefaultIsolationSegment(orgGUID, isoSegGUID) 210 return Warnings(apiWarnings), err 211 } 212 213 // ResetOrganizationDefaultIsolationSegment resets the default isolation segment fon 214 // an organization. 215 func (actor Actor) ResetOrganizationDefaultIsolationSegment(orgGUID string) (Warnings, error) { 216 apiWarnings, err := actor.CloudControllerClient.PatchOrganizationDefaultIsolationSegment(orgGUID, "") 217 return Warnings(apiWarnings), err 218 }