github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/space/space.go (about) 1 package space 2 3 import ( 4 "fmt" 5 "strings" 6 7 "code.cloudfoundry.org/cli/cf/commandregistry" 8 "code.cloudfoundry.org/cli/cf/flags" 9 . "code.cloudfoundry.org/cli/cf/i18n" 10 "code.cloudfoundry.org/cli/cf/models" 11 "code.cloudfoundry.org/cli/plugin/models" 12 13 "code.cloudfoundry.org/cli/cf/api/spacequotas" 14 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 15 "code.cloudfoundry.org/cli/cf/formatters" 16 "code.cloudfoundry.org/cli/cf/requirements" 17 "code.cloudfoundry.org/cli/cf/terminal" 18 ) 19 20 type ShowSpace struct { 21 ui terminal.UI 22 config coreconfig.Reader 23 spaceReq requirements.SpaceRequirement 24 quotaRepo spacequotas.SpaceQuotaRepository 25 pluginModel *plugin_models.GetSpace_Model 26 pluginCall bool 27 } 28 29 func init() { 30 commandregistry.Register(&ShowSpace{}) 31 } 32 33 func (cmd *ShowSpace) MetaData() commandregistry.CommandMetadata { 34 fs := make(map[string]flags.FlagSet) 35 fs["guid"] = &flags.BoolFlag{Name: "guid", Usage: T("Retrieve and display the given space's guid. All other output for the space is suppressed.")} 36 fs["security-group-rules"] = &flags.BoolFlag{Name: "security-group-rules", Usage: T("Retrieve the rules for all the security groups associated with the space")} 37 return commandregistry.CommandMetadata{ 38 Name: "space", 39 Description: T("Show space info"), 40 Usage: []string{ 41 T("CF_NAME space SPACE"), 42 }, 43 Flags: fs, 44 } 45 } 46 47 func (cmd *ShowSpace) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 48 if len(fc.Args()) != 1 { 49 cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("space")) 50 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1) 51 } 52 53 cmd.spaceReq = requirementsFactory.NewSpaceRequirement(fc.Args()[0]) 54 55 reqs := []requirements.Requirement{ 56 requirementsFactory.NewLoginRequirement(), 57 requirementsFactory.NewTargetedOrgRequirement(), 58 cmd.spaceReq, 59 } 60 61 return reqs, nil 62 } 63 64 func (cmd *ShowSpace) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 65 cmd.ui = deps.UI 66 cmd.config = deps.Config 67 cmd.quotaRepo = deps.RepoLocator.GetSpaceQuotaRepository() 68 cmd.pluginCall = pluginCall 69 cmd.pluginModel = deps.PluginModels.Space 70 return cmd 71 } 72 73 func (cmd *ShowSpace) Execute(c flags.FlagContext) error { 74 space := cmd.spaceReq.GetSpace() 75 if cmd.pluginCall { 76 cmd.populatePluginModel(space) 77 return nil 78 } 79 if c.Bool("guid") { 80 cmd.ui.Say(space.GUID) 81 } else { 82 cmd.ui.Say(T("Getting info for space {{.TargetSpace}} in org {{.OrgName}} as {{.CurrentUser}}...", 83 map[string]interface{}{ 84 "TargetSpace": terminal.EntityNameColor(space.Name), 85 "OrgName": terminal.EntityNameColor(space.Organization.Name), 86 "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), 87 })) 88 89 quotaString, err := cmd.quotaString(space) 90 if err != nil { 91 return err 92 } 93 94 cmd.ui.Ok() 95 cmd.ui.Say("") 96 table := cmd.ui.Table([]string{terminal.EntityNameColor(space.Name), "", ""}) 97 table.Add("", T("Org:"), terminal.EntityNameColor(space.Organization.Name)) 98 99 apps := []string{} 100 for _, app := range space.Applications { 101 apps = append(apps, terminal.EntityNameColor(app.Name)) 102 } 103 table.Add("", T("Apps:"), strings.Join(apps, ", ")) 104 105 domains := []string{} 106 for _, domain := range space.Domains { 107 domains = append(domains, terminal.EntityNameColor(domain.Name)) 108 } 109 table.Add("", T("Domains:"), strings.Join(domains, ", ")) 110 111 services := []string{} 112 for _, service := range space.ServiceInstances { 113 services = append(services, terminal.EntityNameColor(service.Name)) 114 } 115 table.Add("", T("Services:"), strings.Join(services, ", ")) 116 117 securityGroups := []string{} 118 for _, group := range space.SecurityGroups { 119 securityGroups = append(securityGroups, terminal.EntityNameColor(group.Name)) 120 } 121 table.Add("", T("Security Groups:"), strings.Join(securityGroups, ", ")) 122 123 table.Add("", T("Space Quota:"), terminal.EntityNameColor(quotaString)) 124 125 err = table.Print() 126 if err != nil { 127 return err 128 } 129 } 130 if c.Bool("security-group-rules") { 131 cmd.ui.Say("") 132 for _, group := range space.SecurityGroups { 133 cmd.ui.Say(T("Getting rules for the security group : {{.SecurityGroupName}}...", 134 map[string]interface{}{"SecurityGroupName": terminal.EntityNameColor(group.Name)})) 135 table := cmd.ui.Table([]string{"", "", "", ""}) 136 for _, rules := range group.Rules { 137 for ruleName, ruleValue := range rules { 138 table.Add("", ruleName, ":", fmt.Sprintf("%v", ruleValue)) 139 } 140 table.Add("", "", "", "") 141 } 142 err := table.Print() 143 if err != nil { 144 return err 145 } 146 } 147 } 148 149 return nil 150 } 151 152 func (cmd *ShowSpace) quotaString(space models.Space) (string, error) { 153 if space.SpaceQuotaGUID == "" { 154 return "", nil 155 } 156 157 quota, err := cmd.quotaRepo.FindByGUID(space.SpaceQuotaGUID) 158 if err != nil { 159 return "", err 160 } 161 162 spaceQuotaFields := []string{} 163 spaceQuotaFields = append(spaceQuotaFields, T("{{.MemoryLimit}} memory limit", map[string]interface{}{"MemoryLimit": quota.FormattedMemoryLimit()})) 164 spaceQuotaFields = append(spaceQuotaFields, T("{{.InstanceMemoryLimit}} instance memory limit", map[string]interface{}{"InstanceMemoryLimit": quota.FormattedInstanceMemoryLimit()})) 165 spaceQuotaFields = append(spaceQuotaFields, T("{{.RoutesLimit}} routes", map[string]interface{}{"RoutesLimit": quota.RoutesLimit})) 166 spaceQuotaFields = append(spaceQuotaFields, T("{{.ServicesLimit}} services", map[string]interface{}{"ServicesLimit": quota.ServicesLimit})) 167 spaceQuotaFields = append(spaceQuotaFields, T("paid services {{.NonBasicServicesAllowed}}", map[string]interface{}{"NonBasicServicesAllowed": formatters.Allowed(quota.NonBasicServicesAllowed)})) 168 spaceQuotaFields = append(spaceQuotaFields, T("{{.AppInstanceLimit}} app instance limit", map[string]interface{}{"AppInstanceLimit": quota.FormattedAppInstanceLimit()})) 169 170 routePorts := quota.FormattedRoutePortsLimit() 171 if routePorts != "" { 172 spaceQuotaFields = append(spaceQuotaFields, T("{{.ReservedRoutePorts}} route ports", map[string]interface{}{"ReservedRoutePorts": routePorts})) 173 } 174 175 spaceQuota := fmt.Sprintf("%s (%s)", quota.Name, strings.Join(spaceQuotaFields, ", ")) 176 177 return spaceQuota, nil 178 } 179 180 func (cmd *ShowSpace) populatePluginModel(space models.Space) { 181 cmd.pluginModel.Name = space.Name 182 cmd.pluginModel.Guid = space.GUID 183 184 cmd.pluginModel.Organization.Name = space.Organization.Name 185 cmd.pluginModel.Organization.Guid = space.Organization.GUID 186 187 for _, app := range space.Applications { 188 a := plugin_models.GetSpace_Apps{ 189 Name: app.Name, 190 Guid: app.GUID, 191 } 192 cmd.pluginModel.Applications = append(cmd.pluginModel.Applications, a) 193 } 194 195 for _, domain := range space.Domains { 196 d := plugin_models.GetSpace_Domains{ 197 Name: domain.Name, 198 Guid: domain.GUID, 199 OwningOrganizationGuid: domain.OwningOrganizationGUID, 200 Shared: domain.Shared, 201 } 202 cmd.pluginModel.Domains = append(cmd.pluginModel.Domains, d) 203 } 204 205 for _, service := range space.ServiceInstances { 206 si := plugin_models.GetSpace_ServiceInstance{ 207 Name: service.Name, 208 Guid: service.GUID, 209 } 210 cmd.pluginModel.ServiceInstances = append(cmd.pluginModel.ServiceInstances, si) 211 } 212 for _, group := range space.SecurityGroups { 213 sg := plugin_models.GetSpace_SecurityGroup{ 214 Name: group.Name, 215 Guid: group.GUID, 216 Rules: group.Rules, 217 } 218 cmd.pluginModel.SecurityGroups = append(cmd.pluginModel.SecurityGroups, sg) 219 } 220 221 quota, err := cmd.quotaRepo.FindByGUID(space.SpaceQuotaGUID) 222 if err == nil { 223 cmd.pluginModel.SpaceQuota.Name = quota.Name 224 cmd.pluginModel.SpaceQuota.Guid = quota.GUID 225 cmd.pluginModel.SpaceQuota.MemoryLimit = quota.MemoryLimit 226 cmd.pluginModel.SpaceQuota.InstanceMemoryLimit = quota.InstanceMemoryLimit 227 cmd.pluginModel.SpaceQuota.RoutesLimit = quota.RoutesLimit 228 cmd.pluginModel.SpaceQuota.ServicesLimit = quota.ServicesLimit 229 cmd.pluginModel.SpaceQuota.NonBasicServicesAllowed = quota.NonBasicServicesAllowed 230 } 231 }