forked from cloudfoundry/cli
/
services_command.go
105 lines (86 loc) · 2.72 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
package v2
import (
"fmt"
"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/command"
"code.cloudfoundry.org/cli/command/v2/shared"
)
//go:generate counterfeiter . ServicesActor
type ServicesActor interface {
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 ServicesActor
}
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.NewClients(config, ui, true)
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
}
table := [][]string{{
cmd.UI.TranslateText("name"),
cmd.UI.TranslateText("service"),
cmd.UI.TranslateText("plan"),
cmd.UI.TranslateText("bound apps"),
cmd.UI.TranslateText("last operation"),
}}
var boundAppNames []string
for _, summary := range instanceSummaries {
serviceLabel := summary.Service.Label
if summary.ServiceInstance.Type == constant.ServiceInstanceTypeUserProvidedService {
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)},
)
}
cmd.UI.DisplayTableWithHeader("", table, 3)
return nil
}