github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/spacequota/create_quota.go (about)

     1  package spacequota
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/api/organizations"
     5  	"github.com/cloudfoundry/cli/cf/api/space_quotas"
     6  	"github.com/cloudfoundry/cli/cf/command_registry"
     7  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     8  	"github.com/cloudfoundry/cli/cf/errors"
     9  	"github.com/cloudfoundry/cli/cf/formatters"
    10  	. "github.com/cloudfoundry/cli/cf/i18n"
    11  	"github.com/cloudfoundry/cli/cf/models"
    12  	"github.com/cloudfoundry/cli/cf/requirements"
    13  	"github.com/cloudfoundry/cli/cf/terminal"
    14  	"github.com/cloudfoundry/cli/flags"
    15  	"github.com/cloudfoundry/cli/flags/flag"
    16  )
    17  
    18  type CreateSpaceQuota struct {
    19  	ui        terminal.UI
    20  	config    core_config.Reader
    21  	quotaRepo space_quotas.SpaceQuotaRepository
    22  	orgRepo   organizations.OrganizationRepository
    23  }
    24  
    25  func init() {
    26  	command_registry.Register(&CreateSpaceQuota{})
    27  }
    28  
    29  func (cmd *CreateSpaceQuota) MetaData() command_registry.CommandMetadata {
    30  	fs := make(map[string]flags.FlagSet)
    31  	fs["allow-paid-service-plans"] = &cliFlags.BoolFlag{Name: "allow-paid-service-plans", Usage: T("Can provision instances of paid service plans (Default: disallowed)")}
    32  	fs["i"] = &cliFlags.StringFlag{Name: "i", Usage: T("Maximum amount of memory an application instance can have (e.g. 1024M, 1G, 10G). -1 represents an unlimited amount. (Default: unlimited)")}
    33  	fs["m"] = &cliFlags.StringFlag{Name: "m", Usage: T("Total amount of memory a space can have (e.g. 1024M, 1G, 10G)")}
    34  	fs["r"] = &cliFlags.IntFlag{Name: "r", Usage: T("Total number of routes")}
    35  	fs["s"] = &cliFlags.IntFlag{Name: "s", Usage: T("Total number of service instances")}
    36  
    37  	return command_registry.CommandMetadata{
    38  		Name:        "create-space-quota",
    39  		Description: T("Define a new space resource quota"),
    40  		Usage:       T("CF_NAME create-space-quota QUOTA [-i INSTANCE_MEMORY] [-m MEMORY] [-r ROUTES] [-s SERVICE_INSTANCES] [--allow-paid-service-plans]"),
    41  		Flags:       fs,
    42  	}
    43  }
    44  
    45  func (cmd *CreateSpaceQuota) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
    46  	if len(fc.Args()) != 1 {
    47  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + command_registry.Commands.CommandUsage("create-space-quota"))
    48  	}
    49  
    50  	return []requirements.Requirement{
    51  		requirementsFactory.NewLoginRequirement(),
    52  		requirementsFactory.NewTargetedOrgRequirement(),
    53  	}, nil
    54  }
    55  
    56  func (cmd *CreateSpaceQuota) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
    57  	cmd.ui = deps.Ui
    58  	cmd.config = deps.Config
    59  	cmd.quotaRepo = deps.RepoLocator.GetSpaceQuotaRepository()
    60  	cmd.orgRepo = deps.RepoLocator.GetOrganizationRepository()
    61  	return cmd
    62  }
    63  
    64  func (cmd *CreateSpaceQuota) Execute(context flags.FlagContext) {
    65  	name := context.Args()[0]
    66  	org := cmd.config.OrganizationFields()
    67  
    68  	cmd.ui.Say(T("Creating space quota {{.QuotaName}} for org {{.OrgName}} as {{.Username}}...", map[string]interface{}{
    69  		"QuotaName": terminal.EntityNameColor(name),
    70  		"OrgName":   terminal.EntityNameColor(org.Name),
    71  		"Username":  terminal.EntityNameColor(cmd.config.Username()),
    72  	}))
    73  
    74  	quota := models.SpaceQuota{
    75  		Name:    name,
    76  		OrgGuid: org.Guid,
    77  	}
    78  
    79  	memoryLimit := context.String("m")
    80  	if memoryLimit != "" {
    81  		parsedMemory, errr := formatters.ToMegabytes(memoryLimit)
    82  		if errr != nil {
    83  			cmd.ui.Failed(T("Invalid memory limit: {{.MemoryLimit}}\n{{.Err}}", map[string]interface{}{"MemoryLimit": memoryLimit, "Err": errr}))
    84  		}
    85  
    86  		quota.MemoryLimit = parsedMemory
    87  	}
    88  
    89  	instanceMemoryLimit := context.String("i")
    90  	var parsedMemory int64
    91  	var err error
    92  	if instanceMemoryLimit == "-1" || instanceMemoryLimit == "" {
    93  		parsedMemory = -1
    94  	} else {
    95  		parsedMemory, err = formatters.ToMegabytes(instanceMemoryLimit)
    96  		if err != nil {
    97  			cmd.ui.Failed(T("Invalid instance memory limit: {{.MemoryLimit}}\n{{.Err}}", map[string]interface{}{"MemoryLimit": instanceMemoryLimit, "Err": err}))
    98  		}
    99  	}
   100  
   101  	quota.InstanceMemoryLimit = parsedMemory
   102  
   103  	if context.IsSet("r") {
   104  		quota.RoutesLimit = context.Int("r")
   105  	}
   106  
   107  	if context.IsSet("s") {
   108  		quota.ServicesLimit = context.Int("s")
   109  	}
   110  
   111  	if context.IsSet("allow-paid-service-plans") {
   112  		quota.NonBasicServicesAllowed = true
   113  	}
   114  
   115  	err = cmd.quotaRepo.Create(quota)
   116  
   117  	httpErr, ok := err.(errors.HttpError)
   118  	if ok && httpErr.ErrorCode() == errors.QUOTA_EXISTS {
   119  		cmd.ui.Ok()
   120  		cmd.ui.Warn(T("Space Quota Definition {{.QuotaName}} already exists", map[string]interface{}{"QuotaName": quota.Name}))
   121  		return
   122  	}
   123  
   124  	if err != nil {
   125  		cmd.ui.Failed(err.Error())
   126  	}
   127  
   128  	cmd.ui.Ok()
   129  }