-
Notifications
You must be signed in to change notification settings - Fork 927
/
create_service_command.go
109 lines (90 loc) · 4.89 KB
/
create_service_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
package v6
import (
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v2action"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/command/v6/shared"
)
//go:generate counterfeiter . CreateServiceActor
type CreateServiceActor interface {
CreateServiceInstance(spaceGUID, serviceName, servicePlanName, serviceInstanceName, brokerName string, params map[string]interface{}, tags []string) (v2action.ServiceInstance, v2action.Warnings, error)
}
type CreateServiceCommand struct {
RequiredArgs flag.CreateServiceArgs `positional-args:"yes"`
ServiceBroker string `short:"b" description:"Create a service instance from a particular broker. Required when service name is ambiguous"`
ParametersAsJSON flag.JSONOrFileWithValidation `short:"c" description:"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."`
Tags flag.Tags `short:"t" description:"User provided tags"`
usage interface{} `usage:"CF_NAME create-service SERVICE PLAN SERVICE_INSTANCE [-b BROKER] [-c PARAMETERS_AS_JSON] [-t TAGS]\n\n Optionally provide service-specific configuration parameters in a valid JSON object in-line:\n\n CF_NAME create-service SERVICE PLAN SERVICE_INSTANCE -c '{\"name\":\"value\",\"name\":\"value\"}'\n\n Optionally provide a file containing service-specific configuration parameters in a valid JSON object.\n The path to the parameters file can be an absolute or relative path to a file:\n\n CF_NAME create-service SERVICE PLAN SERVICE_INSTANCE -c PATH_TO_FILE\n\n Example of valid JSON object:\n {\n \"cluster_nodes\": {\n \"count\": 5,\n \"memory_mb\": 1024\n }\n }\n\nTIP:\n Use 'CF_NAME create-user-provided-service' to make user-provided services available to CF apps\n\nEXAMPLES:\n Linux/Mac:\n CF_NAME create-service db-service silver mydb -c '{\"ram_gb\":4}'\n\n Windows Command Line:\n CF_NAME create-service db-service silver mydb -c \"{\\\"ram_gb\\\":4}\"\n\n Windows PowerShell:\n CF_NAME create-service db-service silver mydb -c '{\\\"ram_gb\\\":4}'\n\n CF_NAME create-service db-service silver mydb -c ~/workspace/tmp/instance_config.json\n\n CF_NAME create-service db-service silver mydb -t \"list, of, tags\""`
relatedCommands interface{} `related_commands:"bind-service, create-user-provided-service, marketplace, services"`
UI command.UI
Config command.Config
SharedActor command.SharedActor
Actor CreateServiceActor
}
func (cmd *CreateServiceCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
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 CreateServiceCommand) Execute(args []string) error {
if len(args) > 0 {
return translatableerror.TooManyArgumentsError{
ExtraArgument: args[0],
}
}
if err := cmd.SharedActor.CheckTarget(true, true); err != nil {
return err
}
user, err := cmd.Config.CurrentUser()
if err != nil {
return err
}
cmd.UI.DisplayTextWithFlavor("Creating service instance {{.ServiceInstance}} in org {{.Org}} / space {{.Space}} as {{.User}}...",
map[string]interface{}{
"ServiceInstance": cmd.RequiredArgs.ServiceInstance,
"Org": cmd.Config.TargetedOrganization().Name,
"Space": cmd.Config.TargetedSpace().Name,
"User": user.Name,
})
instance, warnings, err := cmd.Actor.CreateServiceInstance(
cmd.Config.TargetedSpace().GUID,
cmd.RequiredArgs.Service,
cmd.RequiredArgs.ServicePlan,
cmd.RequiredArgs.ServiceInstance,
cmd.ServiceBroker,
cmd.ParametersAsJSON,
cmd.Tags.Value,
)
cmd.UI.DisplayWarnings(warnings)
if _, nameTakenError := err.(ccerror.ServiceInstanceNameTakenError); nameTakenError {
cmd.UI.DisplayOK()
cmd.UI.DisplayTextWithFlavor("Service {{.ServiceInstance}} already exists",
map[string]interface{}{
"ServiceInstance": cmd.RequiredArgs.ServiceInstance,
})
return nil
}
if err != nil {
return err
}
if instance.LastOperation.State == constant.LastOperationInProgress {
cmd.UI.DisplayOK()
cmd.UI.DisplayTextWithFlavor("Create in progress. Use 'cf services' or 'cf service {{.ServiceInstance}}' to check operation status.",
map[string]interface{}{
"ServiceInstance": cmd.RequiredArgs.ServiceInstance,
})
return nil
}
cmd.UI.DisplayOK()
return nil
}