-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_key.go
116 lines (99 loc) · 3.91 KB
/
service_key.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
package servicekey
import (
"encoding/json"
"fmt"
"code.cloudfoundry.org/cli/cf/api"
"code.cloudfoundry.org/cli/cf/commandregistry"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/errors"
"code.cloudfoundry.org/cli/cf/flags"
"code.cloudfoundry.org/cli/cf/requirements"
"code.cloudfoundry.org/cli/cf/terminal"
. "code.cloudfoundry.org/cli/cf/i18n"
)
type ServiceKey struct {
ui terminal.UI
config coreconfig.Reader
serviceRepo api.ServiceRepository
serviceKeyRepo api.ServiceKeyRepository
serviceInstanceRequirement requirements.ServiceInstanceRequirement
}
func init() {
commandregistry.Register(&ServiceKey{})
}
func (cmd *ServiceKey) MetaData() commandregistry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["guid"] = &flags.BoolFlag{Name: "guid", Usage: T("Retrieve and display the given service-key's guid. All other output for the service is suppressed.")}
return commandregistry.CommandMetadata{
Name: "service-key",
Description: T("Show service key info"),
Usage: []string{
T("CF_NAME service-key SERVICE_INSTANCE SERVICE_KEY"),
},
Examples: []string{
"CF_NAME service-key mydb mykey",
},
Flags: fs,
}
}
func (cmd *ServiceKey) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
if len(fc.Args()) != 2 {
cmd.ui.Failed(T("Incorrect Usage. Requires SERVICE_INSTANCE SERVICE_KEY as arguments\n\n") + commandregistry.Commands.CommandUsage("service-key"))
return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2)
}
loginRequirement := requirementsFactory.NewLoginRequirement()
cmd.serviceInstanceRequirement = requirementsFactory.NewServiceInstanceRequirement(fc.Args()[0])
targetSpaceRequirement := requirementsFactory.NewTargetedSpaceRequirement()
reqs := []requirements.Requirement{loginRequirement, cmd.serviceInstanceRequirement, targetSpaceRequirement}
return reqs, nil
}
func (cmd *ServiceKey) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
cmd.ui = deps.UI
cmd.config = deps.Config
cmd.serviceRepo = deps.RepoLocator.GetServiceRepository()
cmd.serviceKeyRepo = deps.RepoLocator.GetServiceKeyRepository()
return cmd
}
func (cmd *ServiceKey) Execute(c flags.FlagContext) error {
serviceInstance := cmd.serviceInstanceRequirement.GetServiceInstance()
serviceKeyName := c.Args()[1]
if !c.Bool("guid") {
cmd.ui.Say(T("Getting key {{.ServiceKeyName}} for service instance {{.ServiceInstanceName}} as {{.CurrentUser}}...",
map[string]interface{}{
"ServiceKeyName": terminal.EntityNameColor(serviceKeyName),
"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name),
"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
}))
}
serviceKey, err := cmd.serviceKeyRepo.GetServiceKey(serviceInstance.GUID, serviceKeyName)
if err != nil {
switch err.(type) {
case *errors.NotAuthorizedError:
cmd.ui.Say(T("No service key {{.ServiceKeyName}} found for service instance {{.ServiceInstanceName}}",
map[string]interface{}{
"ServiceKeyName": terminal.EntityNameColor(serviceKeyName),
"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name)}))
return nil
default:
return err
}
}
if c.Bool("guid") {
cmd.ui.Say(serviceKey.Fields.GUID)
} else {
if serviceKey.Fields.Name == "" {
return errors.New(
T("No service key {{.ServiceKeyName}} found for service instance {{.ServiceInstanceName}}",
map[string]interface{}{
"ServiceKeyName": terminal.EntityNameColor(serviceKeyName),
"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name)}))
}
jsonBytes, err := json.MarshalIndent(serviceKey.Credentials, "", " ")
if err != nil {
return err
}
cmd.ui.Say("")
cmd.ui.Say(string(jsonBytes))
}
return nil
}