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

     1  package quota
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"code.cloudfoundry.org/cli/cf/flags"
     8  	. "code.cloudfoundry.org/cli/cf/i18n"
     9  
    10  	"code.cloudfoundry.org/cli/cf/api/quotas"
    11  	"code.cloudfoundry.org/cli/cf/api/resources"
    12  	"code.cloudfoundry.org/cli/cf/commandregistry"
    13  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    14  	"code.cloudfoundry.org/cli/cf/formatters"
    15  	"code.cloudfoundry.org/cli/cf/requirements"
    16  	"code.cloudfoundry.org/cli/cf/terminal"
    17  )
    18  
    19  type ListQuotas struct {
    20  	ui        terminal.UI
    21  	config    coreconfig.Reader
    22  	quotaRepo quotas.QuotaRepository
    23  }
    24  
    25  func init() {
    26  	commandregistry.Register(&ListQuotas{})
    27  }
    28  
    29  func (cmd *ListQuotas) MetaData() commandregistry.CommandMetadata {
    30  	return commandregistry.CommandMetadata{
    31  		Name:        "quotas",
    32  		Description: T("List available usage quotas"),
    33  		Usage: []string{
    34  			T("CF_NAME quotas"),
    35  		},
    36  	}
    37  }
    38  
    39  func (cmd *ListQuotas) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    40  	usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd),
    41  		T("No argument required"),
    42  		func() bool {
    43  			return len(fc.Args()) != 0
    44  		},
    45  	)
    46  
    47  	reqs := []requirements.Requirement{
    48  		usageReq,
    49  		requirementsFactory.NewLoginRequirement(),
    50  	}
    51  
    52  	return reqs, nil
    53  }
    54  
    55  func (cmd *ListQuotas) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    56  	cmd.ui = deps.UI
    57  	cmd.config = deps.Config
    58  	cmd.quotaRepo = deps.RepoLocator.GetQuotaRepository()
    59  	return cmd
    60  }
    61  
    62  func (cmd *ListQuotas) Execute(c flags.FlagContext) error {
    63  	cmd.ui.Say(T("Getting quotas as {{.Username}}...", map[string]interface{}{"Username": terminal.EntityNameColor(cmd.config.Username())}))
    64  
    65  	quotas, err := cmd.quotaRepo.FindAll()
    66  
    67  	if err != nil {
    68  		return err
    69  	}
    70  	cmd.ui.Ok()
    71  	cmd.ui.Say("")
    72  
    73  	table := cmd.ui.Table([]string{
    74  		T("name"),
    75  		T("total memory"),
    76  		T("instance memory"),
    77  		T("routes"),
    78  		T("service instances"),
    79  		T("paid plans"),
    80  		T("app instances"),
    81  		T("route ports"),
    82  	})
    83  
    84  	var megabytes string
    85  	for _, quota := range quotas {
    86  		if quota.InstanceMemoryLimit == -1 {
    87  			megabytes = T("unlimited")
    88  		} else {
    89  			megabytes = formatters.ByteSize(quota.InstanceMemoryLimit * formatters.MEGABYTE)
    90  		}
    91  
    92  		servicesLimit := strconv.Itoa(quota.ServicesLimit)
    93  		if quota.ServicesLimit == -1 {
    94  			servicesLimit = T("unlimited")
    95  		}
    96  
    97  		appInstanceLimit := strconv.Itoa(quota.AppInstanceLimit)
    98  		if quota.AppInstanceLimit == resources.UnlimitedAppInstances {
    99  			appInstanceLimit = T("unlimited")
   100  		}
   101  
   102  		reservedRoutePorts := string(quota.ReservedRoutePorts)
   103  		if reservedRoutePorts == resources.UnlimitedReservedRoutePorts {
   104  			reservedRoutePorts = T("unlimited")
   105  		}
   106  
   107  		routesLimit := fmt.Sprintf("%d", quota.RoutesLimit)
   108  		if routesLimit == resources.UnlimitedRoutes {
   109  			routesLimit = T("unlimited")
   110  		}
   111  
   112  		table.Add(
   113  			quota.Name,
   114  			formatters.ByteSize(quota.MemoryLimit*formatters.MEGABYTE),
   115  			megabytes,
   116  			routesLimit,
   117  			fmt.Sprint(servicesLimit),
   118  			formatters.Allowed(quota.NonBasicServicesAllowed),
   119  			fmt.Sprint(appInstanceLimit),
   120  			fmt.Sprint(reservedRoutePorts),
   121  		)
   122  	}
   123  
   124  	err = table.Print()
   125  	if err != nil {
   126  		return err
   127  	}
   128  	return nil
   129  }