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