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