github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/service/marketplace.go (about) 1 package service 2 3 import ( 4 "errors" 5 "fmt" 6 "sort" 7 "strings" 8 9 "code.cloudfoundry.org/cli/cf/flags" 10 . "code.cloudfoundry.org/cli/cf/i18n" 11 12 "code.cloudfoundry.org/cli/cf/actors/servicebuilder" 13 "code.cloudfoundry.org/cli/cf/commandregistry" 14 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 15 "code.cloudfoundry.org/cli/cf/models" 16 "code.cloudfoundry.org/cli/cf/requirements" 17 "code.cloudfoundry.org/cli/cf/terminal" 18 ) 19 20 type MarketplaceServices struct { 21 ui terminal.UI 22 config coreconfig.Reader 23 serviceBuilder servicebuilder.ServiceBuilder 24 } 25 26 func init() { 27 commandregistry.Register(&MarketplaceServices{}) 28 } 29 30 func (cmd *MarketplaceServices) MetaData() commandregistry.CommandMetadata { 31 fs := make(map[string]flags.FlagSet) 32 fs["s"] = &flags.StringFlag{ShortName: "s", Usage: T("Show plan details for a particular service offering")} 33 34 return commandregistry.CommandMetadata{ 35 Name: "marketplace", 36 ShortName: "m", 37 Description: T("List available offerings in the marketplace"), 38 Usage: []string{ 39 "CF_NAME marketplace ", 40 fmt.Sprintf("[-s %s] ", T("SERVICE")), 41 }, 42 Flags: fs, 43 } 44 } 45 46 func (cmd *MarketplaceServices) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 47 usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd), 48 T("No argument required"), 49 func() bool { 50 return len(fc.Args()) != 0 51 }, 52 ) 53 54 reqs := []requirements.Requirement{ 55 usageReq, 56 requirementsFactory.NewAPIEndpointRequirement(), 57 } 58 59 return reqs, nil 60 } 61 62 func (cmd *MarketplaceServices) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 63 cmd.ui = deps.UI 64 cmd.config = deps.Config 65 cmd.serviceBuilder = deps.ServiceBuilder 66 return cmd 67 } 68 69 func (cmd *MarketplaceServices) Execute(c flags.FlagContext) error { 70 serviceName := c.String("s") 71 72 var err error 73 if serviceName != "" { 74 err = cmd.marketplaceByService(serviceName) 75 } else { 76 err = cmd.marketplace() 77 } 78 if err != nil { 79 return err 80 } 81 82 return nil 83 } 84 85 func (cmd MarketplaceServices) marketplaceByService(serviceName string) error { 86 var serviceOffering models.ServiceOffering 87 var err error 88 89 if cmd.config.HasSpace() { 90 cmd.ui.Say(T("Getting service plan information for service {{.ServiceName}} as {{.CurrentUser}}...", 91 map[string]interface{}{ 92 "ServiceName": terminal.EntityNameColor(serviceName), 93 "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), 94 })) 95 serviceOffering, err = cmd.serviceBuilder.GetServiceByNameForSpaceWithPlans(serviceName, cmd.config.SpaceFields().GUID) 96 } else if !cmd.config.IsLoggedIn() { 97 cmd.ui.Say(T("Getting service plan information for service {{.ServiceName}}...", map[string]interface{}{"ServiceName": terminal.EntityNameColor(serviceName)})) 98 serviceOffering, err = cmd.serviceBuilder.GetServiceByNameWithPlans(serviceName) 99 } else { 100 err = errors.New(T("Cannot list plan information for {{.ServiceName}} without a targeted space", 101 map[string]interface{}{"ServiceName": terminal.EntityNameColor(serviceName)})) 102 } 103 if err != nil { 104 return err 105 } 106 107 cmd.ui.Ok() 108 cmd.ui.Say("") 109 110 if serviceOffering.GUID == "" { 111 cmd.ui.Say(T("Service offering not found")) 112 return nil 113 } 114 115 table := cmd.ui.Table([]string{T("service plan"), T("description"), T("free or paid")}) 116 for _, plan := range serviceOffering.Plans { 117 var freeOrPaid string 118 if plan.Free { 119 freeOrPaid = "free" 120 } else { 121 freeOrPaid = "paid" 122 } 123 table.Add(plan.Name, plan.Description, freeOrPaid) 124 } 125 126 err = table.Print() 127 if err != nil { 128 return err 129 } 130 return nil 131 } 132 133 func (cmd MarketplaceServices) marketplace() error { 134 var serviceOfferings models.ServiceOfferings 135 var err error 136 137 if cmd.config.HasSpace() { 138 cmd.ui.Say(T("Getting services from marketplace in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", 139 map[string]interface{}{ 140 "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), 141 "SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name), 142 "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), 143 })) 144 serviceOfferings, err = cmd.serviceBuilder.GetServicesForSpaceWithPlans(cmd.config.SpaceFields().GUID) 145 } else if !cmd.config.IsLoggedIn() { 146 cmd.ui.Say(T("Getting all services from marketplace...")) 147 serviceOfferings, err = cmd.serviceBuilder.GetAllServicesWithPlans() 148 } else { 149 err = errors.New(T("Cannot list marketplace services without a targeted space")) 150 } 151 if err != nil { 152 return err 153 } 154 155 cmd.ui.Ok() 156 cmd.ui.Say("") 157 158 if len(serviceOfferings) == 0 { 159 cmd.ui.Say(T("No service offerings found")) 160 return nil 161 } 162 163 table := cmd.ui.Table([]string{T("service"), T("plans"), T("description")}) 164 165 sort.Sort(serviceOfferings) 166 var paidPlanExists bool 167 for _, offering := range serviceOfferings { 168 planNames := "" 169 170 for _, plan := range offering.Plans { 171 if plan.Name == "" { 172 continue 173 } 174 if plan.Free { 175 planNames += ", " + plan.Name 176 } else { 177 paidPlanExists = true 178 planNames += ", " + plan.Name + "*" 179 } 180 } 181 182 planNames = strings.TrimPrefix(planNames, ", ") 183 184 table.Add(offering.Label, planNames, offering.Description) 185 } 186 187 err = table.Print() 188 if err != nil { 189 return err 190 } 191 if paidPlanExists { 192 cmd.ui.Say(T("\n* These service plans have an associated cost. Creating a service instance will incur this cost.")) 193 } 194 cmd.ui.Say(T("\nTIP: Use 'cf marketplace -s SERVICE' to view descriptions of individual plans of a given service.")) 195 return nil 196 }