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