github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/organization/org.go (about) 1 package organization 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 8 "code.cloudfoundry.org/cli/cf/api/resources" 9 "code.cloudfoundry.org/cli/cf/commandregistry" 10 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 11 "code.cloudfoundry.org/cli/cf/flags" 12 "code.cloudfoundry.org/cli/cf/formatters" 13 . "code.cloudfoundry.org/cli/cf/i18n" 14 "code.cloudfoundry.org/cli/cf/models" 15 "code.cloudfoundry.org/cli/cf/requirements" 16 "code.cloudfoundry.org/cli/cf/terminal" 17 "code.cloudfoundry.org/cli/plugin/models" 18 ) 19 20 type ShowOrg struct { 21 ui terminal.UI 22 config coreconfig.Reader 23 orgReq requirements.OrganizationRequirement 24 pluginModel *plugin_models.GetOrg_Model 25 pluginCall bool 26 } 27 28 func init() { 29 commandregistry.Register(&ShowOrg{}) 30 } 31 32 func (cmd *ShowOrg) MetaData() commandregistry.CommandMetadata { 33 fs := make(map[string]flags.FlagSet) 34 fs["guid"] = &flags.BoolFlag{Name: "guid", Usage: T("Retrieve and display the given org's guid. All other output for the org is suppressed.")} 35 return commandregistry.CommandMetadata{ 36 Name: "org", 37 Description: T("Show org info"), 38 Usage: []string{ 39 T("CF_NAME org ORG"), 40 }, 41 Flags: fs, 42 } 43 } 44 45 func (cmd *ShowOrg) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 46 if len(fc.Args()) != 1 { 47 cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("org")) 48 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1) 49 } 50 51 cmd.orgReq = requirementsFactory.NewOrganizationRequirement(fc.Args()[0]) 52 53 reqs := []requirements.Requirement{ 54 requirementsFactory.NewLoginRequirement(), 55 cmd.orgReq, 56 } 57 58 return reqs, nil 59 } 60 61 func (cmd *ShowOrg) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 62 cmd.ui = deps.UI 63 cmd.config = deps.Config 64 cmd.pluginCall = pluginCall 65 cmd.pluginModel = deps.PluginModels.Organization 66 return cmd 67 } 68 69 func (cmd *ShowOrg) Execute(c flags.FlagContext) error { 70 org := cmd.orgReq.GetOrganization() 71 72 if c.Bool("guid") { 73 cmd.ui.Say(org.GUID) 74 } else { 75 cmd.ui.Say(T("Getting info for org {{.OrgName}} as {{.Username}}...", 76 map[string]interface{}{ 77 "OrgName": terminal.EntityNameColor(org.Name), 78 "Username": terminal.EntityNameColor(cmd.config.Username())})) 79 cmd.ui.Ok() 80 cmd.ui.Say("") 81 82 table := cmd.ui.Table([]string{terminal.EntityNameColor(org.Name) + ":", "", ""}) 83 84 domains := []string{} 85 for _, domain := range org.Domains { 86 domains = append(domains, domain.Name) 87 } 88 89 spaces := []string{} 90 for _, space := range org.Spaces { 91 spaces = append(spaces, space.Name) 92 } 93 94 spaceQuotas := []string{} 95 for _, spaceQuota := range org.SpaceQuotas { 96 spaceQuotas = append(spaceQuotas, spaceQuota.Name) 97 } 98 99 quota := org.QuotaDefinition 100 101 var reservedPortLimit string 102 switch string(quota.ReservedRoutePorts) { 103 case "": 104 break 105 case resources.UnlimitedReservedRoutePorts: 106 reservedPortLimit = T("unlimited") 107 default: 108 if _, err := quota.ReservedRoutePorts.Int64(); err != nil { 109 return err 110 } 111 reservedPortLimit = string(quota.ReservedRoutePorts) 112 } 113 114 appInstanceLimit := strconv.Itoa(quota.AppInstanceLimit) 115 if quota.AppInstanceLimit == resources.UnlimitedAppInstances { 116 appInstanceLimit = T("unlimited") 117 } 118 119 orgQuotaFields := []string{} 120 orgQuotaFields = append(orgQuotaFields, T("{{.MemoryLimit}}M memory limit", map[string]interface{}{"MemoryLimit": quota.MemoryLimit})) 121 orgQuotaFields = append(orgQuotaFields, T("{{.InstanceMemoryLimit}} instance memory limit", map[string]interface{}{"InstanceMemoryLimit": formatters.InstanceMemoryLimit(quota.InstanceMemoryLimit)})) 122 orgQuotaFields = append(orgQuotaFields, T("{{.RoutesLimit}} routes", map[string]interface{}{"RoutesLimit": quota.RoutesLimit})) 123 orgQuotaFields = append(orgQuotaFields, T("{{.ServicesLimit}} services", map[string]interface{}{"ServicesLimit": quota.ServicesLimit})) 124 orgQuotaFields = append(orgQuotaFields, T("paid services {{.NonBasicServicesAllowed}}", map[string]interface{}{"NonBasicServicesAllowed": formatters.Allowed(quota.NonBasicServicesAllowed)})) 125 orgQuotaFields = append(orgQuotaFields, T("{{.AppInstanceLimit}} app instance limit", map[string]interface{}{"AppInstanceLimit": appInstanceLimit})) 126 127 if reservedPortLimit != "" { 128 orgQuotaFields = append(orgQuotaFields, T("{{.ReservedRoutePorts}} route ports", map[string]interface{}{"ReservedRoutePorts": reservedPortLimit})) 129 } 130 131 orgQuota := fmt.Sprintf("%s (%s)", quota.Name, strings.Join(orgQuotaFields, ", ")) 132 133 if cmd.pluginCall { 134 cmd.populatePluginModel(org, quota) 135 } else { 136 table.Add("", T("domains:"), terminal.EntityNameColor(strings.Join(domains, ", "))) 137 table.Add("", T("quota:"), terminal.EntityNameColor(orgQuota)) 138 table.Add("", T("spaces:"), terminal.EntityNameColor(strings.Join(spaces, ", "))) 139 table.Add("", T("space quotas:"), terminal.EntityNameColor(strings.Join(spaceQuotas, ", "))) 140 141 err := table.Print() 142 if err != nil { 143 return err 144 } 145 } 146 } 147 return nil 148 } 149 150 func (cmd *ShowOrg) populatePluginModel(org models.Organization, quota models.QuotaFields) { 151 cmd.pluginModel.Name = org.Name 152 cmd.pluginModel.Guid = org.GUID 153 cmd.pluginModel.QuotaDefinition.Name = quota.Name 154 cmd.pluginModel.QuotaDefinition.MemoryLimit = quota.MemoryLimit 155 cmd.pluginModel.QuotaDefinition.InstanceMemoryLimit = quota.InstanceMemoryLimit 156 cmd.pluginModel.QuotaDefinition.RoutesLimit = quota.RoutesLimit 157 cmd.pluginModel.QuotaDefinition.ServicesLimit = quota.ServicesLimit 158 cmd.pluginModel.QuotaDefinition.NonBasicServicesAllowed = quota.NonBasicServicesAllowed 159 160 for _, domain := range org.Domains { 161 d := plugin_models.GetOrg_Domains{ 162 Name: domain.Name, 163 Guid: domain.GUID, 164 OwningOrganizationGuid: domain.OwningOrganizationGUID, 165 Shared: domain.Shared, 166 } 167 cmd.pluginModel.Domains = append(cmd.pluginModel.Domains, d) 168 } 169 170 for _, space := range org.Spaces { 171 s := plugin_models.GetOrg_Space{ 172 Name: space.Name, 173 Guid: space.GUID, 174 } 175 cmd.pluginModel.Spaces = append(cmd.pluginModel.Spaces, s) 176 } 177 178 for _, spaceQuota := range org.SpaceQuotas { 179 sq := plugin_models.GetOrg_SpaceQuota{ 180 Name: spaceQuota.Name, 181 Guid: spaceQuota.GUID, 182 MemoryLimit: spaceQuota.MemoryLimit, 183 InstanceMemoryLimit: spaceQuota.InstanceMemoryLimit, 184 } 185 cmd.pluginModel.SpaceQuotas = append(cmd.pluginModel.SpaceQuotas, sq) 186 } 187 }