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

     1  package v7action
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     6  	"code.cloudfoundry.org/cli/resources"
     7  )
     8  
     9  func (actor Actor) ApplySpaceQuotaByName(quotaName, spaceGUID, orgGUID string) (Warnings, error) {
    10  	var allWarnings Warnings
    11  
    12  	spaceQuota, warnings, err := actor.GetSpaceQuotaByName(quotaName, orgGUID)
    13  	allWarnings = append(allWarnings, warnings...)
    14  	if err != nil {
    15  		return allWarnings, err
    16  	}
    17  
    18  	_, ccWarnings, err := actor.CloudControllerClient.ApplySpaceQuota(spaceQuota.GUID, spaceGUID)
    19  	allWarnings = append(allWarnings, ccWarnings...)
    20  
    21  	return allWarnings, err
    22  }
    23  
    24  func (actor Actor) CreateSpaceQuota(spaceQuotaName string, orgGuid string, limits QuotaLimits) (Warnings, error) {
    25  	allWarnings := Warnings{}
    26  
    27  	spaceQuota := resources.SpaceQuota{
    28  		Quota: resources.Quota{
    29  			Name: spaceQuotaName,
    30  			Apps: resources.AppLimit{
    31  				TotalMemory:       limits.TotalMemoryInMB,
    32  				InstanceMemory:    limits.PerProcessMemoryInMB,
    33  				TotalAppInstances: limits.TotalInstances,
    34  			},
    35  			Services: resources.ServiceLimit{
    36  				TotalServiceInstances: limits.TotalServiceInstances,
    37  				PaidServicePlans:      limits.PaidServicesAllowed,
    38  			},
    39  			Routes: resources.RouteLimit{
    40  				TotalRoutes:        limits.TotalRoutes,
    41  				TotalReservedPorts: limits.TotalReservedPorts,
    42  			},
    43  		},
    44  		OrgGUID:    orgGuid,
    45  		SpaceGUIDs: nil,
    46  	}
    47  
    48  	setZeroDefaultsForQuotaCreation(&spaceQuota.Apps, &spaceQuota.Routes, &spaceQuota.Services)
    49  	convertUnlimitedToNil(&spaceQuota.Apps, &spaceQuota.Routes, &spaceQuota.Services)
    50  
    51  	_, warnings, err := actor.CloudControllerClient.CreateSpaceQuota(spaceQuota)
    52  	allWarnings = append(allWarnings, warnings...)
    53  
    54  	return allWarnings, err
    55  }
    56  
    57  func (actor Actor) DeleteSpaceQuotaByName(quotaName string, orgGUID string) (Warnings, error) {
    58  	var allWarnings Warnings
    59  
    60  	quota, warnings, err := actor.GetSpaceQuotaByName(quotaName, orgGUID)
    61  	allWarnings = append(allWarnings, warnings...)
    62  	if err != nil {
    63  		return allWarnings, err
    64  	}
    65  
    66  	jobURL, ccv3Warnings, err := actor.CloudControllerClient.DeleteSpaceQuota(quota.GUID)
    67  	allWarnings = append(allWarnings, Warnings(ccv3Warnings)...)
    68  	if err != nil {
    69  		return allWarnings, err
    70  	}
    71  
    72  	ccv3Warnings, err = actor.CloudControllerClient.PollJob(jobURL)
    73  	allWarnings = append(allWarnings, Warnings(ccv3Warnings)...)
    74  	if err != nil {
    75  		return allWarnings, err
    76  	}
    77  
    78  	return allWarnings, nil
    79  }
    80  
    81  func (actor Actor) GetSpaceQuotaByName(spaceQuotaName string, orgGUID string) (resources.SpaceQuota, Warnings, error) {
    82  	ccv3Quotas, warnings, err := actor.CloudControllerClient.GetSpaceQuotas(
    83  		ccv3.Query{
    84  			Key:    ccv3.OrganizationGUIDFilter,
    85  			Values: []string{orgGUID},
    86  		},
    87  		ccv3.Query{
    88  			Key:    ccv3.NameFilter,
    89  			Values: []string{spaceQuotaName},
    90  		},
    91  	)
    92  
    93  	if err != nil {
    94  		return resources.SpaceQuota{}, Warnings(warnings), err
    95  	}
    96  
    97  	if len(ccv3Quotas) == 0 {
    98  		return resources.SpaceQuota{}, Warnings(warnings), actionerror.SpaceQuotaNotFoundForNameError{Name: spaceQuotaName}
    99  	}
   100  
   101  	return ccv3Quotas[0], Warnings(warnings), nil
   102  }
   103  
   104  func (actor Actor) GetSpaceQuotasByOrgGUID(orgGUID string) ([]resources.SpaceQuota, Warnings, error) {
   105  	spaceQuotas, warnings, err := actor.CloudControllerClient.GetSpaceQuotas(
   106  		ccv3.Query{
   107  			Key:    ccv3.OrganizationGUIDFilter,
   108  			Values: []string{orgGUID},
   109  		},
   110  	)
   111  
   112  	if err != nil {
   113  		return []resources.SpaceQuota{}, Warnings(warnings), err
   114  	}
   115  
   116  	return spaceQuotas, Warnings(warnings), nil
   117  }
   118  
   119  func (actor Actor) UpdateSpaceQuota(currentName, orgGUID, newName string, limits QuotaLimits) (Warnings, error) {
   120  	var allWarnings Warnings
   121  
   122  	oldSpaceQuota, warnings, err := actor.GetSpaceQuotaByName(currentName, orgGUID)
   123  	allWarnings = append(allWarnings, warnings...)
   124  	if err != nil {
   125  		return allWarnings, err
   126  	}
   127  
   128  	if newName == "" {
   129  		newName = currentName
   130  	}
   131  
   132  	newSpaceQuota := resources.SpaceQuota{
   133  		Quota: resources.Quota{
   134  			GUID: oldSpaceQuota.GUID,
   135  			Name: newName,
   136  			Apps: resources.AppLimit{
   137  				TotalMemory:       limits.TotalMemoryInMB,
   138  				InstanceMemory:    limits.PerProcessMemoryInMB,
   139  				TotalAppInstances: limits.TotalInstances,
   140  			},
   141  			Services: resources.ServiceLimit{
   142  				TotalServiceInstances: limits.TotalServiceInstances,
   143  				PaidServicePlans:      limits.PaidServicesAllowed,
   144  			},
   145  			Routes: resources.RouteLimit{
   146  				TotalRoutes:        limits.TotalRoutes,
   147  				TotalReservedPorts: limits.TotalReservedPorts,
   148  			},
   149  		},
   150  	}
   151  
   152  	convertUnlimitedToNil(&newSpaceQuota.Apps, &newSpaceQuota.Routes, &newSpaceQuota.Services)
   153  
   154  	_, ccWarnings, err := actor.CloudControllerClient.UpdateSpaceQuota(newSpaceQuota)
   155  	allWarnings = append(allWarnings, ccWarnings...)
   156  
   157  	return allWarnings, err
   158  }
   159  
   160  func (actor Actor) UnsetSpaceQuota(spaceQuotaName, spaceName, orgGUID string) (Warnings, error) {
   161  	var allWarnings Warnings
   162  	space, warnings, err := actor.GetSpaceByNameAndOrganization(spaceName, orgGUID)
   163  	allWarnings = append(allWarnings, warnings...)
   164  	if err != nil {
   165  		return allWarnings, err
   166  	}
   167  
   168  	spaceQuota, warnings, err := actor.GetSpaceQuotaByName(spaceQuotaName, orgGUID)
   169  	allWarnings = append(allWarnings, warnings...)
   170  	if err != nil {
   171  		return allWarnings, err
   172  	}
   173  
   174  	ccWarnings, err := actor.CloudControllerClient.UnsetSpaceQuota(spaceQuota.GUID, space.GUID)
   175  	allWarnings = append(allWarnings, ccWarnings...)
   176  	if err != nil {
   177  		return allWarnings, err
   178  	}
   179  
   180  	return allWarnings, nil
   181  }