forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_service_key.go
112 lines (94 loc) · 4.14 KB
/
create_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
package servicekey
import (
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/commandregistry"
"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/flags"
"github.com/cloudfoundry/cli/json"
. "github.com/cloudfoundry/cli/cf/i18n"
)
type CreateServiceKey struct {
ui terminal.UI
config coreconfig.Reader
serviceRepo api.ServiceRepository
serviceKeyRepo api.ServiceKeyRepository
serviceInstanceRequirement requirements.ServiceInstanceRequirement
}
func init() {
commandregistry.Register(&CreateServiceKey{})
}
func (cmd *CreateServiceKey) MetaData() commandregistry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["c"] = &flags.StringFlag{ShortName: "c", Usage: T("Valid JSON object containing service-specific configuration parameters, provided either in-line or in a file. For a list of supported configuration parameters, see documentation for the particular service offering.")}
return commandregistry.CommandMetadata{
Name: "create-service-key",
ShortName: "csk",
Description: T("Create key for a service instance"),
Usage: []string{
T(`CF_NAME create-service-key SERVICE_INSTANCE SERVICE_KEY [-c PARAMETERS_AS_JSON]
Optionally provide service-specific configuration parameters in a valid JSON object in-line.
CF_NAME create-service-key SERVICE_INSTANCE SERVICE_KEY -c '{"name":"value","name":"value"}'
Optionally provide a file containing service-specific configuration parameters in a valid JSON object. The path to the parameters file can be an absolute or relative path to a file.
CF_NAME create-service-key SERVICE_INSTANCE SERVICE_KEY -c PATH_TO_FILE
Example of valid JSON object:
{
"permissions": "read-only"
}`),
},
Examples: []string{
`CF_NAME create-service-key mydb mykey -c '{"permissions":"read-only"}'`,
`CF_NAME create-service-key mydb mykey -c ~/workspace/tmp/instance_config.json`,
},
Flags: fs,
}
}
func (cmd *CreateServiceKey) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) []requirements.Requirement {
if len(fc.Args()) != 2 {
cmd.ui.Failed(T("Incorrect Usage. Requires SERVICE_INSTANCE and SERVICE_KEY as arguments\n\n") + commandregistry.Commands.CommandUsage("create-service-key"))
}
loginRequirement := requirementsFactory.NewLoginRequirement()
cmd.serviceInstanceRequirement = requirementsFactory.NewServiceInstanceRequirement(fc.Args()[0])
targetSpaceRequirement := requirementsFactory.NewTargetedSpaceRequirement()
reqs := []requirements.Requirement{
loginRequirement,
cmd.serviceInstanceRequirement,
targetSpaceRequirement,
}
return reqs
}
func (cmd *CreateServiceKey) 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 *CreateServiceKey) Execute(c flags.FlagContext) error {
serviceInstance := cmd.serviceInstanceRequirement.GetServiceInstance()
serviceKeyName := c.Args()[1]
params := c.String("c")
paramsMap, err := json.ParseJSONFromFileOrString(params)
if err != nil {
return errors.New(T("Invalid configuration provided for -c flag. Please provide a valid JSON object or path to a file containing a valid JSON object."))
}
cmd.ui.Say(T("Creating service key {{.ServiceKeyName}} for service instance {{.ServiceInstanceName}} as {{.CurrentUser}}...",
map[string]interface{}{
"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name),
"ServiceKeyName": terminal.EntityNameColor(serviceKeyName),
"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
}))
err = cmd.serviceKeyRepo.CreateServiceKey(serviceInstance.GUID, serviceKeyName, paramsMap)
switch err.(type) {
case nil:
cmd.ui.Ok()
case *errors.ModelAlreadyExistsError:
cmd.ui.Ok()
cmd.ui.Warn(err.Error())
default:
return err
}
return nil
}