github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/v7action/space.go (about)

     1  package v7action
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  
     7  	"code.cloudfoundry.org/cli/actor/actionerror"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    11  	"code.cloudfoundry.org/cli/resources"
    12  )
    13  
    14  type SpaceSummary struct {
    15  	Space                 resources.Space
    16  	Name                  string
    17  	OrgName               string
    18  	AppNames              []string
    19  	ServiceInstanceNames  []string
    20  	IsolationSegmentName  string
    21  	QuotaName             string
    22  	RunningSecurityGroups []resources.SecurityGroup
    23  	StagingSecurityGroups []resources.SecurityGroup
    24  }
    25  
    26  func (actor Actor) CreateSpace(spaceName, orgGUID string) (resources.Space, Warnings, error) {
    27  	allWarnings := Warnings{}
    28  
    29  	space, apiWarnings, err := actor.CloudControllerClient.CreateSpace(resources.Space{
    30  		Name: spaceName,
    31  		Relationships: resources.Relationships{
    32  			constant.RelationshipTypeOrganization: resources.Relationship{GUID: orgGUID},
    33  		},
    34  	})
    35  
    36  	actorWarnings := Warnings(apiWarnings)
    37  	allWarnings = append(allWarnings, actorWarnings...)
    38  
    39  	if _, ok := err.(ccerror.NameNotUniqueInOrgError); ok {
    40  		return resources.Space{}, allWarnings, actionerror.SpaceAlreadyExistsError{Space: spaceName}
    41  	}
    42  	return resources.Space{
    43  		GUID: space.GUID,
    44  		Name: spaceName,
    45  	}, allWarnings, err
    46  }
    47  
    48  // ResetSpaceIsolationSegment disassociates a space from an isolation segment.
    49  //
    50  // If the space's organization has a default isolation segment, return its
    51  // name. Otherwise return the empty string.
    52  func (actor Actor) ResetSpaceIsolationSegment(orgGUID string, spaceGUID string) (string, Warnings, error) {
    53  	var allWarnings Warnings
    54  
    55  	_, apiWarnings, err := actor.CloudControllerClient.UpdateSpaceIsolationSegmentRelationship(spaceGUID, "")
    56  	allWarnings = append(allWarnings, apiWarnings...)
    57  	if err != nil {
    58  		return "", allWarnings, err
    59  	}
    60  
    61  	isoSegRelationship, apiWarnings, err := actor.CloudControllerClient.GetOrganizationDefaultIsolationSegment(orgGUID)
    62  	allWarnings = append(allWarnings, apiWarnings...)
    63  	if err != nil {
    64  		return "", allWarnings, err
    65  	}
    66  
    67  	var isoSegName string
    68  	if isoSegRelationship.GUID != "" {
    69  		isolationSegment, apiWarnings, err := actor.CloudControllerClient.GetIsolationSegment(isoSegRelationship.GUID)
    70  		allWarnings = append(allWarnings, apiWarnings...)
    71  		if err != nil {
    72  			return "", allWarnings, err
    73  		}
    74  		isoSegName = isolationSegment.Name
    75  	}
    76  
    77  	return isoSegName, allWarnings, nil
    78  }
    79  
    80  func (actor Actor) GetSpaceByNameAndOrganization(spaceName string, orgGUID string) (resources.Space, Warnings, error) {
    81  	ccv3Spaces, _, warnings, err := actor.CloudControllerClient.GetSpaces(
    82  		ccv3.Query{Key: ccv3.NameFilter, Values: []string{spaceName}},
    83  		ccv3.Query{Key: ccv3.OrganizationGUIDFilter, Values: []string{orgGUID}},
    84  	)
    85  
    86  	if err != nil {
    87  		return resources.Space{}, Warnings(warnings), err
    88  	}
    89  
    90  	if len(ccv3Spaces) == 0 {
    91  		return resources.Space{}, Warnings(warnings), actionerror.SpaceNotFoundError{Name: spaceName}
    92  	}
    93  
    94  	return resources.Space(ccv3Spaces[0]), Warnings(warnings), nil
    95  }
    96  
    97  func (actor Actor) GetSpaceSummaryByNameAndOrganization(spaceName string, orgGUID string) (SpaceSummary, Warnings, error) {
    98  	var allWarnings Warnings
    99  
   100  	org, warnings, err := actor.GetOrganizationByGUID(orgGUID)
   101  	allWarnings = append(allWarnings, warnings...)
   102  	if err != nil {
   103  		return SpaceSummary{}, allWarnings, err
   104  	}
   105  
   106  	space, warnings, err := actor.GetSpaceByNameAndOrganization(spaceName, org.GUID)
   107  	allWarnings = append(allWarnings, warnings...)
   108  	if err != nil {
   109  		return SpaceSummary{}, allWarnings, err
   110  	}
   111  
   112  	apps, warnings, err := actor.GetApplicationsBySpace(space.GUID)
   113  	allWarnings = append(allWarnings, warnings...)
   114  	if err != nil {
   115  		return SpaceSummary{}, allWarnings, err
   116  	}
   117  
   118  	appNames := make([]string, len(apps))
   119  	for i, app := range apps {
   120  		appNames[i] = app.Name
   121  	}
   122  	sort.Strings(appNames)
   123  
   124  	serviceInstances, ccv3Warnings, err := actor.CloudControllerClient.GetServiceInstances(
   125  		ccv3.Query{
   126  			Key:    ccv3.SpaceGUIDFilter,
   127  			Values: []string{space.GUID},
   128  		})
   129  	allWarnings = append(allWarnings, Warnings(ccv3Warnings)...)
   130  	if err != nil {
   131  		return SpaceSummary{}, allWarnings, err
   132  	}
   133  
   134  	serviceInstanceNames := make([]string, len(serviceInstances))
   135  	for i, instance := range serviceInstances {
   136  		serviceInstanceNames[i] = instance.Name
   137  	}
   138  	sort.Strings(serviceInstanceNames)
   139  
   140  	isoSegRelationship, ccv3Warnings, err := actor.CloudControllerClient.GetSpaceIsolationSegment(space.GUID)
   141  	allWarnings = append(allWarnings, Warnings(ccv3Warnings)...)
   142  	if err != nil {
   143  		return SpaceSummary{}, allWarnings, err
   144  	}
   145  
   146  	isoSegName := ""
   147  	isoSegGUID := isoSegRelationship.GUID
   148  	isDefaultIsoSeg := false
   149  
   150  	if isoSegGUID == "" {
   151  		defaultIsoSeg, ccv3Warnings, err := actor.CloudControllerClient.GetOrganizationDefaultIsolationSegment(org.GUID)
   152  		allWarnings = append(allWarnings, Warnings(ccv3Warnings)...)
   153  		if err != nil {
   154  			return SpaceSummary{}, allWarnings, err
   155  		}
   156  		isoSegGUID = defaultIsoSeg.GUID
   157  		if isoSegGUID != "" {
   158  			isDefaultIsoSeg = true
   159  		}
   160  	}
   161  
   162  	if isoSegGUID != "" {
   163  		isoSeg, ccv3warnings, err := actor.CloudControllerClient.GetIsolationSegment(isoSegGUID)
   164  		allWarnings = append(allWarnings, Warnings(ccv3warnings)...)
   165  		if err != nil {
   166  			return SpaceSummary{}, allWarnings, err
   167  		}
   168  		if isDefaultIsoSeg {
   169  			isoSegName = fmt.Sprintf("%s (org default)", isoSeg.Name)
   170  		} else {
   171  			isoSegName = isoSeg.Name
   172  		}
   173  	}
   174  
   175  	appliedQuotaRelationshipGUID := space.Relationships[constant.RelationshipTypeQuota].GUID
   176  
   177  	var spaceQuota resources.SpaceQuota
   178  	if appliedQuotaRelationshipGUID != "" {
   179  		spaceQuota, ccv3Warnings, err = actor.CloudControllerClient.GetSpaceQuota(space.Relationships[constant.RelationshipTypeQuota].GUID)
   180  		allWarnings = append(allWarnings, Warnings(ccv3Warnings)...)
   181  
   182  		if err != nil {
   183  			return SpaceSummary{}, allWarnings, err
   184  		}
   185  	}
   186  
   187  	runningSecurityGroups, ccv3Warnings, err := actor.CloudControllerClient.GetRunningSecurityGroups(space.GUID)
   188  	allWarnings = append(allWarnings, ccv3Warnings...)
   189  	if err != nil {
   190  		return SpaceSummary{}, allWarnings, err
   191  	}
   192  
   193  	stagingSecurityGroups, ccv3Warnings, err := actor.CloudControllerClient.GetStagingSecurityGroups(space.GUID)
   194  	allWarnings = append(allWarnings, ccv3Warnings...)
   195  	if err != nil {
   196  		return SpaceSummary{}, allWarnings, err
   197  	}
   198  
   199  	spaceSummary := SpaceSummary{
   200  		OrgName:               org.Name,
   201  		Name:                  space.Name,
   202  		Space:                 space,
   203  		AppNames:              appNames,
   204  		ServiceInstanceNames:  serviceInstanceNames,
   205  		IsolationSegmentName:  isoSegName,
   206  		QuotaName:             spaceQuota.Name,
   207  		RunningSecurityGroups: runningSecurityGroups,
   208  		StagingSecurityGroups: stagingSecurityGroups,
   209  	}
   210  
   211  	return spaceSummary, allWarnings, nil
   212  }
   213  
   214  // GetOrganizationSpacesWithLabelSelector returns a list of spaces in the specified org
   215  func (actor Actor) GetOrganizationSpacesWithLabelSelector(orgGUID string, labelSelector string) ([]resources.Space, Warnings, error) {
   216  
   217  	queries := []ccv3.Query{
   218  		ccv3.Query{Key: ccv3.OrganizationGUIDFilter, Values: []string{orgGUID}},
   219  		ccv3.Query{Key: ccv3.OrderBy, Values: []string{ccv3.NameOrder}},
   220  	}
   221  	if len(labelSelector) > 0 {
   222  		queries = append(queries, ccv3.Query{Key: ccv3.LabelSelectorFilter, Values: []string{labelSelector}})
   223  	}
   224  
   225  	ccv3Spaces, _, warnings, err := actor.CloudControllerClient.GetSpaces(queries...)
   226  	if err != nil {
   227  		return []resources.Space{}, Warnings(warnings), err
   228  	}
   229  
   230  	spaces := make([]resources.Space, len(ccv3Spaces))
   231  	for i, ccv3Space := range ccv3Spaces {
   232  		spaces[i] = resources.Space(ccv3Space)
   233  	}
   234  
   235  	return spaces, Warnings(warnings), nil
   236  }
   237  
   238  // GetOrganizationSpaces returns a list of spaces in the specified org
   239  func (actor Actor) GetOrganizationSpaces(orgGUID string) ([]resources.Space, Warnings, error) {
   240  	return actor.GetOrganizationSpacesWithLabelSelector(orgGUID, "")
   241  }
   242  
   243  func (actor Actor) DeleteSpaceByNameAndOrganizationName(spaceName string, orgName string) (Warnings, error) {
   244  	var allWarnings Warnings
   245  
   246  	org, actorWarnings, err := actor.GetOrganizationByName(orgName)
   247  	allWarnings = append(allWarnings, actorWarnings...)
   248  	if err != nil {
   249  		return allWarnings, err
   250  	}
   251  
   252  	space, warnings, err := actor.GetSpaceByNameAndOrganization(spaceName, org.GUID)
   253  	allWarnings = append(allWarnings, warnings...)
   254  	if err != nil {
   255  		return allWarnings, err
   256  	}
   257  
   258  	jobURL, deleteWarnings, err := actor.CloudControllerClient.DeleteSpace(space.GUID)
   259  	allWarnings = append(allWarnings, Warnings(deleteWarnings)...)
   260  	if err != nil {
   261  		return allWarnings, err
   262  	}
   263  
   264  	ccWarnings, err := actor.CloudControllerClient.PollJob(jobURL)
   265  	allWarnings = append(allWarnings, Warnings(ccWarnings)...)
   266  
   267  	return allWarnings, err
   268  }
   269  
   270  func (actor Actor) RenameSpaceByNameAndOrganizationGUID(oldSpaceName, newSpaceName, orgGUID string) (resources.Space, Warnings, error) {
   271  	var allWarnings Warnings
   272  
   273  	space, getWarnings, err := actor.GetSpaceByNameAndOrganization(oldSpaceName, orgGUID)
   274  	allWarnings = append(allWarnings, getWarnings...)
   275  	if err != nil {
   276  		return resources.Space{}, allWarnings, err
   277  	}
   278  
   279  	ccSpace, updateWarnings, err := actor.CloudControllerClient.UpdateSpace(resources.Space{GUID: space.GUID, Name: newSpaceName})
   280  	allWarnings = append(allWarnings, Warnings(updateWarnings)...)
   281  
   282  	return resources.Space(ccSpace), allWarnings, err
   283  }