github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/service/create_user_provided_service.go (about) 1 package service 2 3 import ( 4 "encoding/json" 5 "strings" 6 7 "code.cloudfoundry.org/cli/cf" 8 "code.cloudfoundry.org/cli/cf/flagcontext" 9 "code.cloudfoundry.org/cli/cf/flags" 10 . "code.cloudfoundry.org/cli/cf/i18n" 11 12 "fmt" 13 14 "code.cloudfoundry.org/cli/cf/api" 15 "code.cloudfoundry.org/cli/cf/commandregistry" 16 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 17 "code.cloudfoundry.org/cli/cf/requirements" 18 "code.cloudfoundry.org/cli/cf/terminal" 19 ) 20 21 type CreateUserProvidedService struct { 22 ui terminal.UI 23 config coreconfig.Reader 24 userProvidedServiceInstanceRepo api.UserProvidedServiceInstanceRepository 25 } 26 27 func init() { 28 commandregistry.Register(&CreateUserProvidedService{}) 29 } 30 31 func (cmd *CreateUserProvidedService) MetaData() commandregistry.CommandMetadata { 32 fs := make(map[string]flags.FlagSet) 33 fs["p"] = &flags.StringFlag{ShortName: "p", Usage: T("Credentials, provided inline or in a file, to be exposed in the VCAP_SERVICES environment variable for bound applications")} 34 fs["l"] = &flags.StringFlag{ShortName: "l", Usage: T("URL to which logs for bound applications will be streamed")} 35 fs["r"] = &flags.StringFlag{ShortName: "r", Usage: T("URL to which requests for bound routes will be forwarded. Scheme for this URL must be https")} 36 37 return commandregistry.CommandMetadata{ 38 Name: "create-user-provided-service", 39 ShortName: "cups", 40 Description: T("Make a user-provided service instance available to CF apps"), 41 Usage: []string{ 42 T(`CF_NAME create-user-provided-service SERVICE_INSTANCE [-p CREDENTIALS] [-l SYSLOG_DRAIN_URL] [-r ROUTE_SERVICE_URL] 43 44 Pass comma separated credential parameter names to enable interactive mode: 45 CF_NAME create-user-provided-service SERVICE_INSTANCE -p "comma, separated, parameter, names" 46 47 Pass credential parameters as JSON to create a service non-interactively: 48 CF_NAME create-user-provided-service SERVICE_INSTANCE -p '{"key1":"value1","key2":"value2"}' 49 50 Specify a path to a file containing JSON: 51 CF_NAME create-user-provided-service SERVICE_INSTANCE -p PATH_TO_FILE`), 52 }, 53 Examples: []string{ 54 `CF_NAME create-user-provided-service my-db-mine -p "username, password"`, 55 `CF_NAME create-user-provided-service my-db-mine -p /path/to/credentials.json`, 56 `CF_NAME create-user-provided-service my-drain-service -l syslog://example.com`, 57 `CF_NAME create-user-provided-service my-route-service -r https://example.com`, 58 ``, 59 fmt.Sprintf("%s:", T(`Linux/Mac`)), 60 ` CF_NAME create-user-provided-service my-db-mine -p '{"username":"admin","password":"pa55woRD"}'`, 61 ``, 62 fmt.Sprintf("%s:", T(`Windows Command Line`)), 63 ` CF_NAME create-user-provided-service my-db-mine -p "{\"username\":\"admin\",\"password\":\"pa55woRD\"}"`, 64 ``, 65 fmt.Sprintf("%s:", T(`Windows PowerShell`)), 66 ` CF_NAME create-user-provided-service my-db-mine -p '{\"username\":\"admin\",\"password\":\"pa55woRD\"}'`, 67 }, 68 Flags: fs, 69 } 70 } 71 72 func (cmd *CreateUserProvidedService) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 73 if len(fc.Args()) != 1 { 74 cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("create-user-provided-service")) 75 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1) 76 } 77 78 reqs := []requirements.Requirement{ 79 requirementsFactory.NewLoginRequirement(), 80 requirementsFactory.NewTargetedSpaceRequirement(), 81 } 82 83 if fc.IsSet("r") { 84 reqs = append(reqs, requirementsFactory.NewMinAPIVersionRequirement("Option '-r'", cf.MultipleAppPortsMinimumAPIVersion)) 85 } 86 87 return reqs, nil 88 } 89 90 func (cmd *CreateUserProvidedService) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 91 cmd.ui = deps.UI 92 cmd.config = deps.Config 93 cmd.userProvidedServiceInstanceRepo = deps.RepoLocator.GetUserProvidedServiceInstanceRepository() 94 return cmd 95 } 96 97 func (cmd *CreateUserProvidedService) Execute(c flags.FlagContext) error { 98 name := c.Args()[0] 99 drainURL := c.String("l") 100 routeServiceURL := c.String("r") 101 credentials := strings.Trim(c.String("p"), `"'`) 102 credentialsMap := make(map[string]interface{}) 103 104 if c.IsSet("p") { 105 jsonBytes, err := flagcontext.GetContentsFromFlagValue(credentials) 106 if err != nil { 107 return err 108 } 109 110 err = json.Unmarshal(jsonBytes, &credentialsMap) 111 if err != nil { 112 for _, param := range strings.Split(credentials, ",") { 113 param = strings.Trim(param, " ") 114 credentialsMap[param] = cmd.ui.Ask(param) 115 } 116 } 117 } 118 119 cmd.ui.Say(T("Creating user provided service {{.ServiceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", 120 map[string]interface{}{ 121 "ServiceName": terminal.EntityNameColor(name), 122 "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), 123 "SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name), 124 "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), 125 })) 126 127 err := cmd.userProvidedServiceInstanceRepo.Create(name, drainURL, routeServiceURL, credentialsMap) 128 if err != nil { 129 return err 130 } 131 132 cmd.ui.Ok() 133 return nil 134 }