-
Notifications
You must be signed in to change notification settings - Fork 929
/
services_command.go
159 lines (133 loc) · 4.24 KB
/
services_command.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
package v6
import (
"fmt"
"sort"
"strings"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v2action"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/v6/shared"
"code.cloudfoundry.org/cli/util/sorting"
)
//go:generate counterfeiter . ServiceInstancesActor
type ServiceInstancesActor interface {
CloudControllerAPIVersion() string
GetServiceInstancesSummaryBySpace(spaceGUID string) ([]v2action.ServiceInstanceSummary, v2action.Warnings, error)
}
type ServicesCommand struct {
usage interface{} `usage:"CF_NAME services"`
relatedCommands interface{} `related_commands:"create-service, marketplace"`
UI command.UI
Config command.Config
SharedActor command.SharedActor
Actor ServiceInstancesActor
}
func (cmd *ServicesCommand) Setup(config command.Config, ui command.UI) error {
cmd.Config = config
cmd.UI = ui
cmd.SharedActor = sharedaction.NewActor(config)
ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui)
if err != nil {
return err
}
cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
return nil
}
func (cmd ServicesCommand) Execute(args []string) error {
err := cmd.SharedActor.CheckTarget(true, true)
if err != nil {
return err
}
user, err := cmd.Config.CurrentUser()
if err != nil {
return err
}
cmd.UI.DisplayTextWithFlavor("Getting services in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
map[string]interface{}{
"OrgName": cmd.Config.TargetedOrganization().Name,
"SpaceName": cmd.Config.TargetedSpace().Name,
"CurrentUser": user.Name,
})
cmd.UI.DisplayNewline()
instanceSummaries, warnings, err := cmd.Actor.GetServiceInstancesSummaryBySpace(cmd.Config.TargetedSpace().GUID)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
if len(instanceSummaries) == 0 {
cmd.UI.DisplayText("No services found")
return nil
}
sortServiceInstances(instanceSummaries)
table := [][]string{{
cmd.UI.TranslateText("name"),
cmd.UI.TranslateText("service"),
cmd.UI.TranslateText("plan"),
cmd.UI.TranslateText("bound apps"),
cmd.UI.TranslateText("last operation"),
cmd.UI.TranslateText("broker"),
cmd.UI.TranslateText("upgrade available"),
}}
var boundAppNames []string
for _, summary := range instanceSummaries {
serviceLabel := summary.Service.Label
if summary.ServiceInstance.Type == constant.UserProvidedService {
serviceLabel = "user-provided"
}
boundAppNames = []string{}
for _, boundApplication := range summary.BoundApplications {
boundAppNames = append(boundAppNames, boundApplication.AppName)
}
table = append(
table,
[]string{
summary.Name,
serviceLabel,
summary.ServicePlan.Name,
strings.Join(boundAppNames, ", "),
fmt.Sprintf("%s %s", summary.LastOperation.Type, summary.LastOperation.State),
summary.Service.ServiceBrokerName,
upgradeAvailableSummary(summary),
},
)
}
cmd.UI.DisplayTableWithHeader("", table, 3)
isOutdated, err := command.CheckVersionOutdated(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionMaintenanceInfoInSummaryV2)
if err != nil {
return err
}
if isOutdated {
cmd.UI.DisplayNewline()
cmd.UI.DisplayText("TIP: Please upgrade to CC API v{{.version}} or higher for individual service upgrades",
map[string]interface{}{
"version": ccversion.MinVersionMaintenanceInfoInSummaryV2,
})
}
return nil
}
func sortServiceInstances(instanceSummaries []v2action.ServiceInstanceSummary) {
sort.Slice(instanceSummaries, func(i, j int) bool {
return sorting.LessIgnoreCase(instanceSummaries[i].Name, instanceSummaries[j].Name)
})
for _, instance := range instanceSummaries {
sortBoundApps(instance)
}
}
func sortBoundApps(serviceInstance v2action.ServiceInstanceSummary) {
sort.Slice(
serviceInstance.BoundApplications,
func(i, j int) bool {
return sorting.LessIgnoreCase(serviceInstance.BoundApplications[i].AppName, serviceInstance.BoundApplications[j].AppName)
})
}
func upgradeAvailableSummary(s v2action.ServiceInstanceSummary) string {
if s.UpgradeSupported() {
if s.UpgradeAvailable() {
return "yes"
}
return "no"
}
return ""
}