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