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

     1  package spacequota
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/api/space_quotas"
     5  	"github.com/cloudfoundry/cli/cf/command_registry"
     6  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     7  	"github.com/cloudfoundry/cli/cf/formatters"
     8  	. "github.com/cloudfoundry/cli/cf/i18n"
     9  	"github.com/cloudfoundry/cli/cf/requirements"
    10  	"github.com/cloudfoundry/cli/cf/terminal"
    11  	"github.com/cloudfoundry/cli/flags"
    12  	"github.com/cloudfoundry/cli/flags/flag"
    13  )
    14  
    15  type UpdateSpaceQuota struct {
    16  	ui             terminal.UI
    17  	config         core_config.Reader
    18  	spaceQuotaRepo space_quotas.SpaceQuotaRepository
    19  }
    20  
    21  func init() {
    22  	command_registry.Register(&UpdateSpaceQuota{})
    23  }
    24  
    25  func (cmd *UpdateSpaceQuota) MetaData() command_registry.CommandMetadata {
    26  	fs := make(map[string]flags.FlagSet)
    27  	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.")}
    28  	fs["m"] = &cliFlags.StringFlag{Name: "m", Usage: T("Total amount of memory a space can have (e.g. 1024M, 1G, 10G)")}
    29  	fs["n"] = &cliFlags.StringFlag{Name: "n", Usage: T("New name")}
    30  	fs["r"] = &cliFlags.IntFlag{Name: "r", Usage: T("Total number of routes")}
    31  	fs["s"] = &cliFlags.IntFlag{Name: "s", Usage: T("Total number of service instances")}
    32  	fs["allow-paid-service-plans"] = &cliFlags.BoolFlag{Name: "allow-paid-service-plans", Usage: T("Can provision instances of paid service plans")}
    33  	fs["disallow-paid-service-plans"] = &cliFlags.BoolFlag{Name: "disallow-paid-service-plans", Usage: T("Can not provision instances of paid service plans")}
    34  
    35  	return command_registry.CommandMetadata{
    36  		Name:        "update-space-quota",
    37  		Description: T("update an existing space quota"),
    38  		Usage:       T("CF_NAME update-space-quota SPACE-QUOTA-NAME [-i MAX-INSTANCE-MEMORY] [-m MEMORY] [-n NEW_NAME] [-r ROUTES] [-s SERVICES] [--allow-paid-service-plans | --disallow-paid-service-plans]"),
    39  		Flags:       fs,
    40  	}
    41  }
    42  
    43  func (cmd *UpdateSpaceQuota) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
    44  	if len(fc.Args()) != 1 {
    45  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + command_registry.Commands.CommandUsage("update-space-quota"))
    46  	}
    47  
    48  	reqs = []requirements.Requirement{
    49  		requirementsFactory.NewLoginRequirement(),
    50  		requirementsFactory.NewTargetedOrgRequirement(),
    51  	}
    52  	return
    53  }
    54  
    55  func (cmd *UpdateSpaceQuota) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
    56  	cmd.ui = deps.Ui
    57  	cmd.config = deps.Config
    58  	cmd.spaceQuotaRepo = deps.RepoLocator.GetSpaceQuotaRepository()
    59  	return cmd
    60  }
    61  
    62  func (cmd *UpdateSpaceQuota) Execute(c flags.FlagContext) {
    63  	name := c.Args()[0]
    64  
    65  	spaceQuota, apiErr := cmd.spaceQuotaRepo.FindByName(name)
    66  	if apiErr != nil {
    67  		cmd.ui.Failed(apiErr.Error())
    68  		return
    69  	}
    70  
    71  	allowPaidServices := c.Bool("allow-paid-service-plans")
    72  	disallowPaidServices := c.Bool("disallow-paid-service-plans")
    73  	if allowPaidServices && disallowPaidServices {
    74  		cmd.ui.Failed(T("Please choose either allow or disallow. Both flags are not permitted to be passed in the same command."))
    75  	}
    76  
    77  	if allowPaidServices {
    78  		spaceQuota.NonBasicServicesAllowed = true
    79  	}
    80  
    81  	if disallowPaidServices {
    82  		spaceQuota.NonBasicServicesAllowed = false
    83  	}
    84  
    85  	if c.String("i") != "" {
    86  		var memory int64
    87  		var formatError error
    88  
    89  		memFlag := c.String("i")
    90  
    91  		if memFlag == "-1" {
    92  			memory = -1
    93  		} else {
    94  			memory, formatError = formatters.ToMegabytes(memFlag)
    95  			if formatError != nil {
    96  				cmd.ui.Failed(T("Incorrect Usage\n\n") + command_registry.Commands.CommandUsage("update-space-quota"))
    97  			}
    98  		}
    99  
   100  		spaceQuota.InstanceMemoryLimit = memory
   101  	}
   102  
   103  	if c.String("m") != "" {
   104  		memory, formatError := formatters.ToMegabytes(c.String("m"))
   105  
   106  		if formatError != nil {
   107  			cmd.ui.Failed(T("Incorrect Usage\n\n") + command_registry.Commands.CommandUsage("update-space-quota"))
   108  		}
   109  
   110  		spaceQuota.MemoryLimit = memory
   111  	}
   112  
   113  	if c.String("n") != "" {
   114  		spaceQuota.Name = c.String("n")
   115  	}
   116  
   117  	if c.IsSet("s") {
   118  		spaceQuota.ServicesLimit = c.Int("s")
   119  	}
   120  
   121  	if c.IsSet("r") {
   122  		spaceQuota.RoutesLimit = c.Int("r")
   123  	}
   124  
   125  	cmd.ui.Say(T("Updating space quota {{.Quota}} as {{.Username}}...",
   126  		map[string]interface{}{
   127  			"Quota":    terminal.EntityNameColor(name),
   128  			"Username": terminal.EntityNameColor(cmd.config.Username()),
   129  		}))
   130  
   131  	apiErr = cmd.spaceQuotaRepo.Update(spaceQuota)
   132  	if apiErr != nil {
   133  		cmd.ui.Failed(apiErr.Error())
   134  		return
   135  	}
   136  
   137  	cmd.ui.Ok()
   138  }