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