github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/quota/update_quota.go (about)

     1  package quota
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"encoding/json"
     8  
     9  	"code.cloudfoundry.org/cli/cf"
    10  	"code.cloudfoundry.org/cli/cf/api/quotas"
    11  	"code.cloudfoundry.org/cli/cf/commandregistry"
    12  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    13  	"code.cloudfoundry.org/cli/cf/flags"
    14  	"code.cloudfoundry.org/cli/cf/formatters"
    15  	. "code.cloudfoundry.org/cli/cf/i18n"
    16  	"code.cloudfoundry.org/cli/cf/requirements"
    17  	"code.cloudfoundry.org/cli/cf/terminal"
    18  )
    19  
    20  type UpdateQuota struct {
    21  	ui        terminal.UI
    22  	config    coreconfig.Reader
    23  	quotaRepo quotas.QuotaRepository
    24  }
    25  
    26  func init() {
    27  	commandregistry.Register(&UpdateQuota{})
    28  }
    29  
    30  func (cmd *UpdateQuota) MetaData() commandregistry.CommandMetadata {
    31  	fs := make(map[string]flags.FlagSet)
    32  	fs["allow-paid-service-plans"] = &flags.BoolFlag{Name: "allow-paid-service-plans", Usage: T("Can provision instances of paid service plans")}
    33  	fs["disallow-paid-service-plans"] = &flags.BoolFlag{Name: "disallow-paid-service-plans", Usage: T("Cannot provision instances of paid service plans")}
    34  	fs["i"] = &flags.StringFlag{ShortName: "i", Usage: T("Maximum amount of memory an application instance can have (e.g. 1024M, 1G, 10G)")}
    35  	fs["m"] = &flags.StringFlag{ShortName: "m", Usage: T("Total amount of memory (e.g. 1024M, 1G, 10G)")}
    36  	fs["n"] = &flags.StringFlag{ShortName: "n", Usage: T("New name")}
    37  	fs["r"] = &flags.IntFlag{ShortName: "r", Usage: T("Total number of routes")}
    38  	fs["s"] = &flags.IntFlag{ShortName: "s", Usage: T("Total number of service instances")}
    39  	fs["a"] = &flags.IntFlag{ShortName: "a", Usage: T("Total number of application instances. -1 represents an unlimited amount.")}
    40  	fs["reserved-route-ports"] = &flags.StringFlag{Name: "reserved-route-ports", Usage: T("Maximum number of routes that may be created with reserved ports")}
    41  
    42  	return commandregistry.CommandMetadata{
    43  		Name:        "update-quota",
    44  		Description: T("Update an existing resource quota"),
    45  		Usage: []string{
    46  			"CF_NAME update-quota ",
    47  			T("QUOTA"),
    48  			" ",
    49  			fmt.Sprintf("[-m %s] ", T("TOTAL_MEMORY")),
    50  			fmt.Sprintf("[-i %s] ", T("INSTANCE_MEMORY")),
    51  			fmt.Sprintf("[-n %s] ", T("NEW_NAME")),
    52  			fmt.Sprintf("[-r %s] ", T("ROUTES")),
    53  			fmt.Sprintf("[-s %s] ", T("SERVICE_INSTANCES")),
    54  			fmt.Sprintf("[-a %s] ", T("APP_INSTANCES")),
    55  			"[--allow-paid-service-plans | --disallow-paid-service-plans] ",
    56  			fmt.Sprintf("[--reserved-route-ports %s] ", T("RESERVED_ROUTE_PORTS")),
    57  		},
    58  		Flags: fs,
    59  	}
    60  }
    61  
    62  func (cmd *UpdateQuota) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    63  	if len(fc.Args()) != 1 {
    64  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("update-quota"))
    65  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    66  	}
    67  
    68  	reqs := []requirements.Requirement{
    69  		requirementsFactory.NewLoginRequirement(),
    70  	}
    71  
    72  	if fc.IsSet("a") {
    73  		reqs = append(reqs, requirementsFactory.NewMinAPIVersionRequirement("Option '-a'", cf.OrgAppInstanceLimitMinimumAPIVersion))
    74  	}
    75  
    76  	if fc.IsSet("reserved-route-ports") {
    77  		reqs = append(reqs, requirementsFactory.NewMinAPIVersionRequirement("Option '--reserved-route-ports'", cf.ReservedRoutePortsMinimumAPIVersion))
    78  	}
    79  
    80  	return reqs, nil
    81  }
    82  
    83  func (cmd *UpdateQuota) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    84  	cmd.ui = deps.UI
    85  	cmd.config = deps.Config
    86  	cmd.quotaRepo = deps.RepoLocator.GetQuotaRepository()
    87  	return cmd
    88  }
    89  
    90  func (cmd *UpdateQuota) Execute(c flags.FlagContext) error {
    91  	oldQuotaName := c.Args()[0]
    92  	quota, err := cmd.quotaRepo.FindByName(oldQuotaName)
    93  
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	allowPaidServices := c.Bool("allow-paid-service-plans")
    99  	disallowPaidServices := c.Bool("disallow-paid-service-plans")
   100  	if allowPaidServices && disallowPaidServices {
   101  		return errors.New(T("Please choose either allow or disallow. Both flags are not permitted to be passed in the same command."))
   102  	}
   103  
   104  	if allowPaidServices {
   105  		quota.NonBasicServicesAllowed = true
   106  	}
   107  
   108  	if disallowPaidServices {
   109  		quota.NonBasicServicesAllowed = false
   110  	}
   111  
   112  	if c.IsSet("i") {
   113  		var memory int64
   114  
   115  		if c.String("i") == "-1" {
   116  			memory = -1
   117  		} else {
   118  			var formatError error
   119  
   120  			memory, formatError = formatters.ToMegabytes(c.String("i"))
   121  
   122  			if formatError != nil {
   123  				return errors.New(T("Incorrect Usage") + "\n\n" + commandregistry.Commands.CommandUsage("update-quota"))
   124  			}
   125  		}
   126  
   127  		quota.InstanceMemoryLimit = memory
   128  	}
   129  
   130  	if c.IsSet("a") {
   131  		quota.AppInstanceLimit = c.Int("a")
   132  	}
   133  
   134  	if c.IsSet("m") {
   135  		memory, formatError := formatters.ToMegabytes(c.String("m"))
   136  
   137  		if formatError != nil {
   138  			return errors.New(T("Incorrect Usage") + "\n\n" + commandregistry.Commands.CommandUsage("update-quota"))
   139  		}
   140  
   141  		quota.MemoryLimit = memory
   142  	}
   143  
   144  	if c.IsSet("n") {
   145  		quota.Name = c.String("n")
   146  	}
   147  
   148  	if c.IsSet("s") {
   149  		quota.ServicesLimit = c.Int("s")
   150  	}
   151  
   152  	if c.IsSet("r") {
   153  		quota.RoutesLimit = c.Int("r")
   154  	}
   155  
   156  	if c.IsSet("reserved-route-ports") {
   157  		quota.ReservedRoutePorts = json.Number(c.String("reserved-route-ports"))
   158  	}
   159  
   160  	cmd.ui.Say(T("Updating quota {{.QuotaName}} as {{.Username}}...", map[string]interface{}{
   161  		"QuotaName": terminal.EntityNameColor(oldQuotaName),
   162  		"Username":  terminal.EntityNameColor(cmd.config.Username()),
   163  	}))
   164  
   165  	err = cmd.quotaRepo.Update(quota)
   166  	if err != nil {
   167  		return err
   168  	}
   169  	cmd.ui.Ok()
   170  	return nil
   171  }