github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/command/v7/create_org_quota_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/v7action"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     6  	"code.cloudfoundry.org/cli/command/flag"
     7  	"code.cloudfoundry.org/cli/types"
     8  )
     9  
    10  type CreateOrgQuotaCommand struct {
    11  	BaseCommand
    12  
    13  	RequiredArgs          flag.OrganizationQuota      `positional-args:"yes"`
    14  	NumAppInstances       flag.IntegerLimit           `short:"a" description:"Total number of application instances. (Default: unlimited)."`
    15  	PaidServicePlans      bool                        `long:"allow-paid-service-plans" description:"Allow provisioning instances of paid service plans. (Default: disallowed)."`
    16  	PerProcessMemory      flag.MegabytesWithUnlimited `short:"i" description:"Maximum amount of memory a process can have (e.g. 1024M, 1G, 10G). (Default: unlimited)."`
    17  	TotalMemory           flag.MegabytesWithUnlimited `short:"m" description:"Total amount of memory all processes can have (e.g. 1024M, 1G, 10G).  -1 represents an unlimited amount. (Default: 0)."`
    18  	TotalRoutes           flag.IntegerLimit           `short:"r" description:"Total number of routes. -1 represents an unlimited amount. (Default: 0)."`
    19  	TotalReservedPorts    flag.IntegerLimit           `long:"reserved-route-ports" description:"Maximum number of routes that may be created with ports. -1 represents an unlimited amount. (Default: 0)."`
    20  	TotalServiceInstances flag.IntegerLimit           `short:"s" description:"Total number of service instances. -1 represents an unlimited amount. (Default: 0)."`
    21  	TotalLogVolume        flag.BytesWithUnlimited     `short:"l" description:"Total log volume per second all processes can have, in bytes (e.g. 128B, 4K, 1M). -1 represents an unlimited amount. (Default: -1)."`
    22  
    23  	usage           interface{} `usage:"CF_NAME create-org-quota ORG_QUOTA [-m TOTAL_MEMORY] [-i INSTANCE_MEMORY] [-r ROUTES] [-s SERVICE_INSTANCES] [-a APP_INSTANCES] [--allow-paid-service-plans] [--reserved-route-ports RESERVED_ROUTE_PORTS] [-l LOG_VOLUME]"`
    24  	relatedCommands interface{} `related_commands:"create-org, org-quotas, set-org-quota"`
    25  }
    26  
    27  func (cmd CreateOrgQuotaCommand) Execute(args []string) error {
    28  	err := cmd.SharedActor.CheckTarget(false, false)
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	user, err := cmd.Actor.GetCurrentUser()
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	orgQuotaName := cmd.RequiredArgs.OrganizationQuotaName
    39  
    40  	cmd.UI.DisplayTextWithFlavor("Creating org quota {{.OrganizationQuotaName}} as {{.User}}...",
    41  		map[string]interface{}{
    42  			"User":                  user.Name,
    43  			"OrganizationQuotaName": orgQuotaName,
    44  		})
    45  
    46  	warnings, err := cmd.Actor.CreateOrganizationQuota(orgQuotaName, v7action.QuotaLimits{
    47  		TotalMemoryInMB:       convertMegabytesFlagToNullInt(cmd.TotalMemory),
    48  		PerProcessMemoryInMB:  convertMegabytesFlagToNullInt(cmd.PerProcessMemory),
    49  		TotalInstances:        convertIntegerLimitFlagToNullInt(cmd.NumAppInstances),
    50  		PaidServicesAllowed:   &cmd.PaidServicePlans,
    51  		TotalServiceInstances: convertIntegerLimitFlagToNullInt(cmd.TotalServiceInstances),
    52  		TotalRoutes:           convertIntegerLimitFlagToNullInt(cmd.TotalRoutes),
    53  		TotalReservedPorts:    convertIntegerLimitFlagToNullInt(cmd.TotalReservedPorts),
    54  		TotalLogVolume:        convertBytesFlagToNullInt(cmd.TotalLogVolume),
    55  	})
    56  	cmd.UI.DisplayWarnings(warnings)
    57  
    58  	if _, ok := err.(ccerror.QuotaAlreadyExists); ok {
    59  		cmd.UI.DisplayWarning(err.Error())
    60  		cmd.UI.DisplayOK()
    61  		return nil
    62  	}
    63  
    64  	if err != nil {
    65  		return err
    66  	}
    67  	cmd.UI.DisplayOK()
    68  
    69  	return nil
    70  }
    71  
    72  func convertBytesFlagToNullInt(flag flag.BytesWithUnlimited) *types.NullInt {
    73  	if !flag.IsSet {
    74  		return nil
    75  	}
    76  
    77  	return &types.NullInt{IsSet: true, Value: flag.Value}
    78  }
    79  
    80  func convertMegabytesFlagToNullInt(flag flag.MegabytesWithUnlimited) *types.NullInt {
    81  	if !flag.IsSet {
    82  		return nil
    83  	}
    84  
    85  	return &types.NullInt{IsSet: true, Value: flag.Value}
    86  }
    87  
    88  func convertIntegerLimitFlagToNullInt(flag flag.IntegerLimit) *types.NullInt {
    89  	if !flag.IsSet {
    90  		return nil
    91  	}
    92  
    93  	return &types.NullInt{IsSet: true, Value: flag.Value}
    94  }