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