github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/v7action/organization_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  	"code.cloudfoundry.org/cli/types"
     8  )
     9  
    10  type QuotaLimits struct {
    11  	TotalMemoryInMB       *types.NullInt
    12  	PerProcessMemoryInMB  *types.NullInt
    13  	TotalInstances        *types.NullInt
    14  	PaidServicesAllowed   *bool
    15  	TotalServiceInstances *types.NullInt
    16  	TotalRoutes           *types.NullInt
    17  	TotalReservedPorts    *types.NullInt
    18  }
    19  
    20  func (actor Actor) ApplyOrganizationQuotaByName(quotaName string, orgGUID string) (Warnings, error) {
    21  	var allWarnings Warnings
    22  
    23  	quota, warnings, err := actor.GetOrganizationQuotaByName(quotaName)
    24  	allWarnings = append(allWarnings, warnings...)
    25  	if err != nil {
    26  		return allWarnings, err
    27  	}
    28  
    29  	_, ccWarnings, err := actor.CloudControllerClient.ApplyOrganizationQuota(quota.GUID, orgGUID)
    30  	allWarnings = append(allWarnings, ccWarnings...)
    31  
    32  	return allWarnings, err
    33  }
    34  
    35  // CreateOrganization creates a new organization with the given name
    36  func (actor Actor) CreateOrganizationQuota(name string, limits QuotaLimits) (Warnings, error) {
    37  	orgQuota := createQuotaStruct(name, limits)
    38  	setZeroDefaultsForQuotaCreation(&orgQuota.Apps, &orgQuota.Routes, &orgQuota.Services)
    39  	convertUnlimitedToNil(&orgQuota.Apps, &orgQuota.Routes, &orgQuota.Services)
    40  
    41  	_, apiWarnings, err := actor.CloudControllerClient.CreateOrganizationQuota(orgQuota)
    42  
    43  	return Warnings(apiWarnings), err
    44  }
    45  
    46  func (actor Actor) DeleteOrganizationQuota(quotaName string) (Warnings, error) {
    47  	var allWarnings Warnings
    48  
    49  	quota, warnings, err := actor.GetOrganizationQuotaByName(quotaName)
    50  	allWarnings = append(allWarnings, warnings...)
    51  	if err != nil {
    52  		return allWarnings, err
    53  	}
    54  
    55  	jobURL, ccWarnings, err := actor.CloudControllerClient.DeleteOrganizationQuota(quota.GUID)
    56  	allWarnings = append(allWarnings, ccWarnings...)
    57  	if err != nil {
    58  		return allWarnings, err
    59  	}
    60  
    61  	ccWarnings, err = actor.CloudControllerClient.PollJob(jobURL)
    62  	allWarnings = append(allWarnings, ccWarnings...)
    63  
    64  	return allWarnings, err
    65  }
    66  
    67  func (actor Actor) GetOrganizationQuotas() ([]resources.OrganizationQuota, Warnings, error) {
    68  	orgQuotas, warnings, err := actor.CloudControllerClient.GetOrganizationQuotas()
    69  	if err != nil {
    70  		return []resources.OrganizationQuota{}, Warnings(warnings), err
    71  	}
    72  
    73  	return orgQuotas, Warnings(warnings), nil
    74  }
    75  
    76  func (actor Actor) GetOrganizationQuotaByName(orgQuotaName string) (resources.OrganizationQuota, Warnings, error) {
    77  	ccv3OrgQuotas, warnings, err := actor.CloudControllerClient.GetOrganizationQuotas(
    78  		ccv3.Query{
    79  			Key:    ccv3.NameFilter,
    80  			Values: []string{orgQuotaName},
    81  		},
    82  	)
    83  	if err != nil {
    84  		return resources.OrganizationQuota{}, Warnings(warnings), err
    85  
    86  	}
    87  
    88  	if len(ccv3OrgQuotas) == 0 {
    89  		return resources.OrganizationQuota{}, Warnings(warnings), actionerror.OrganizationQuotaNotFoundForNameError{Name: orgQuotaName}
    90  	}
    91  
    92  	return ccv3OrgQuotas[0], Warnings(warnings), nil
    93  }
    94  
    95  func (actor Actor) UpdateOrganizationQuota(quotaName string, newName string, limits QuotaLimits) (Warnings, error) {
    96  	var allWarnings Warnings
    97  
    98  	quota, warnings, err := actor.GetOrganizationQuotaByName(quotaName)
    99  	allWarnings = append(allWarnings, warnings...)
   100  	if err != nil {
   101  		return allWarnings, err
   102  	}
   103  
   104  	if newName == "" {
   105  		newName = quotaName
   106  	}
   107  	newQuota := createQuotaStruct(newName, limits)
   108  	newQuota.GUID = quota.GUID
   109  	convertUnlimitedToNil(&newQuota.Apps, &newQuota.Routes, &newQuota.Services)
   110  
   111  	_, ccWarnings, err := actor.CloudControllerClient.UpdateOrganizationQuota(newQuota)
   112  	allWarnings = append(allWarnings, ccWarnings...)
   113  
   114  	return allWarnings, err
   115  }
   116  
   117  func createQuotaStruct(name string, limits QuotaLimits) resources.OrganizationQuota {
   118  	AppLimit := resources.AppLimit{
   119  		TotalMemory:       limits.TotalMemoryInMB,
   120  		InstanceMemory:    limits.PerProcessMemoryInMB,
   121  		TotalAppInstances: limits.TotalInstances,
   122  	}
   123  	ServiceLimit := resources.ServiceLimit{
   124  		TotalServiceInstances: limits.TotalServiceInstances,
   125  		PaidServicePlans:      limits.PaidServicesAllowed,
   126  	}
   127  	RouteLimit := resources.RouteLimit{
   128  		TotalRoutes:        limits.TotalRoutes,
   129  		TotalReservedPorts: limits.TotalReservedPorts,
   130  	}
   131  
   132  	quota := resources.OrganizationQuota{
   133  		Quota: resources.Quota{
   134  			Name:     name,
   135  			Apps:     AppLimit,
   136  			Services: ServiceLimit,
   137  			Routes:   RouteLimit,
   138  		},
   139  	}
   140  
   141  	return quota
   142  }
   143  
   144  func setZeroDefaultsForQuotaCreation(apps *resources.AppLimit, routes *resources.RouteLimit, services *resources.ServiceLimit) {
   145  	if apps.TotalMemory == nil {
   146  		apps.TotalMemory = &types.NullInt{IsSet: true, Value: 0}
   147  	}
   148  
   149  	if routes.TotalRoutes == nil {
   150  		routes.TotalRoutes = &types.NullInt{IsSet: true, Value: 0}
   151  	}
   152  
   153  	if routes.TotalReservedPorts == nil {
   154  		routes.TotalReservedPorts = &types.NullInt{IsSet: true, Value: 0}
   155  	}
   156  
   157  	if services.TotalServiceInstances == nil {
   158  		services.TotalServiceInstances = &types.NullInt{IsSet: true, Value: 0}
   159  	}
   160  }
   161  
   162  func convertUnlimitedToNil(apps *resources.AppLimit, routes *resources.RouteLimit, services *resources.ServiceLimit) {
   163  	flags := []*types.NullInt{
   164  		apps.TotalMemory,
   165  		apps.InstanceMemory,
   166  		apps.TotalAppInstances,
   167  		services.TotalServiceInstances,
   168  		routes.TotalRoutes,
   169  		routes.TotalReservedPorts,
   170  	}
   171  
   172  	for i := 0; i < len(flags); i++ {
   173  		if flags[i] != nil {
   174  			if flags[i].Value == -1 {
   175  				flags[i].IsSet = false
   176  				flags[i].Value = 0
   177  			}
   178  		}
   179  	}
   180  }