github.com/loafoe/cli@v7.1.0+incompatible/command/v6/create_service_command.go (about) 1 package v6 2 3 import ( 4 "code.cloudfoundry.org/cli/actor/sharedaction" 5 "code.cloudfoundry.org/cli/actor/v2action" 6 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 8 "code.cloudfoundry.org/cli/command" 9 "code.cloudfoundry.org/cli/command/flag" 10 "code.cloudfoundry.org/cli/command/translatableerror" 11 "code.cloudfoundry.org/cli/command/v6/shared" 12 ) 13 14 //go:generate counterfeiter . CreateServiceActor 15 16 type CreateServiceActor interface { 17 CreateServiceInstance(spaceGUID, serviceName, servicePlanName, serviceInstanceName, brokerName string, params map[string]interface{}, tags []string) (v2action.ServiceInstance, v2action.Warnings, error) 18 } 19 20 type CreateServiceCommand struct { 21 RequiredArgs flag.CreateServiceArgs `positional-args:"yes"` 22 ServiceBroker string `short:"b" description:"Create a service instance from a particular broker. Required when service name is ambiguous"` 23 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."` 24 Tags flag.Tags `short:"t" description:"User provided tags"` 25 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\""` 26 relatedCommands interface{} `related_commands:"bind-service, create-user-provided-service, marketplace, services"` 27 28 UI command.UI 29 Config command.Config 30 SharedActor command.SharedActor 31 Actor CreateServiceActor 32 } 33 34 func (cmd *CreateServiceCommand) Setup(config command.Config, ui command.UI) error { 35 cmd.UI = ui 36 cmd.Config = config 37 38 cmd.SharedActor = sharedaction.NewActor(config) 39 40 ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui) 41 if err != nil { 42 return err 43 } 44 45 cmd.Actor = v2action.NewActor(ccClient, uaaClient, config) 46 47 return nil 48 } 49 50 func (cmd CreateServiceCommand) Execute(args []string) error { 51 if len(args) > 0 { 52 return translatableerror.TooManyArgumentsError{ 53 ExtraArgument: args[0], 54 } 55 } 56 57 if err := cmd.SharedActor.CheckTarget(true, true); err != nil { 58 return err 59 } 60 61 user, err := cmd.Config.CurrentUser() 62 if err != nil { 63 return err 64 } 65 66 cmd.UI.DisplayTextWithFlavor("Creating service instance {{.ServiceInstance}} in org {{.Org}} / space {{.Space}} as {{.User}}...", 67 map[string]interface{}{ 68 "ServiceInstance": cmd.RequiredArgs.ServiceInstance, 69 "Org": cmd.Config.TargetedOrganization().Name, 70 "Space": cmd.Config.TargetedSpace().Name, 71 "User": user.Name, 72 }) 73 74 instance, warnings, err := cmd.Actor.CreateServiceInstance( 75 cmd.Config.TargetedSpace().GUID, 76 cmd.RequiredArgs.Service, 77 cmd.RequiredArgs.ServicePlan, 78 cmd.RequiredArgs.ServiceInstance, 79 cmd.ServiceBroker, 80 cmd.ParametersAsJSON, 81 cmd.Tags.Value, 82 ) 83 84 cmd.UI.DisplayWarnings(warnings) 85 86 if _, nameTakenError := err.(ccerror.ServiceInstanceNameTakenError); nameTakenError { 87 cmd.UI.DisplayOK() 88 cmd.UI.DisplayTextWithFlavor("Service {{.ServiceInstance}} already exists", 89 map[string]interface{}{ 90 "ServiceInstance": cmd.RequiredArgs.ServiceInstance, 91 }) 92 return nil 93 } 94 if err != nil { 95 return err 96 } 97 98 if instance.LastOperation.State == constant.LastOperationInProgress { 99 cmd.UI.DisplayOK() 100 cmd.UI.DisplayTextWithFlavor("Create in progress. Use 'cf services' or 'cf service {{.ServiceInstance}}' to check operation status.", 101 map[string]interface{}{ 102 "ServiceInstance": cmd.RequiredArgs.ServiceInstance, 103 }) 104 return nil 105 } 106 107 cmd.UI.DisplayOK() 108 return nil 109 }