github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/servicekey/create_service_key.go (about) 1 package servicekey 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/cf/api" 7 "code.cloudfoundry.org/cli/cf/commandregistry" 8 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 9 "code.cloudfoundry.org/cli/cf/errors" 10 "code.cloudfoundry.org/cli/cf/flags" 11 "code.cloudfoundry.org/cli/cf/requirements" 12 "code.cloudfoundry.org/cli/cf/terminal" 13 "code.cloudfoundry.org/cli/cf/util/json" 14 15 . "code.cloudfoundry.org/cli/cf/i18n" 16 ) 17 18 type CreateServiceKey struct { 19 ui terminal.UI 20 config coreconfig.Reader 21 serviceRepo api.ServiceRepository 22 serviceKeyRepo api.ServiceKeyRepository 23 serviceInstanceRequirement requirements.ServiceInstanceRequirement 24 } 25 26 func init() { 27 commandregistry.Register(&CreateServiceKey{}) 28 } 29 30 func (cmd *CreateServiceKey) MetaData() commandregistry.CommandMetadata { 31 fs := make(map[string]flags.FlagSet) 32 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.")} 33 34 return commandregistry.CommandMetadata{ 35 Name: "create-service-key", 36 ShortName: "csk", 37 Description: T("Create key for a service instance"), 38 Usage: []string{ 39 T(`CF_NAME create-service-key SERVICE_INSTANCE SERVICE_KEY [-c PARAMETERS_AS_JSON] 40 41 Optionally provide service-specific configuration parameters in a valid JSON object in-line. 42 CF_NAME create-service-key SERVICE_INSTANCE SERVICE_KEY -c '{"name":"value","name":"value"}' 43 44 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. 45 CF_NAME create-service-key SERVICE_INSTANCE SERVICE_KEY -c PATH_TO_FILE 46 47 Example of valid JSON object: 48 { 49 "permissions": "read-only" 50 }`), 51 }, 52 Examples: []string{ 53 `CF_NAME create-service-key mydb mykey -c '{"permissions":"read-only"}'`, 54 `CF_NAME create-service-key mydb mykey -c ~/workspace/tmp/instance_config.json`, 55 }, 56 Flags: fs, 57 } 58 } 59 60 func (cmd *CreateServiceKey) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 61 if len(fc.Args()) != 2 { 62 cmd.ui.Failed(T("Incorrect Usage. Requires SERVICE_INSTANCE and SERVICE_KEY as arguments\n\n") + commandregistry.Commands.CommandUsage("create-service-key")) 63 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2) 64 } 65 66 loginRequirement := requirementsFactory.NewLoginRequirement() 67 cmd.serviceInstanceRequirement = requirementsFactory.NewServiceInstanceRequirement(fc.Args()[0]) 68 targetSpaceRequirement := requirementsFactory.NewTargetedSpaceRequirement() 69 70 reqs := []requirements.Requirement{ 71 loginRequirement, 72 cmd.serviceInstanceRequirement, 73 targetSpaceRequirement, 74 } 75 76 return reqs, nil 77 } 78 79 func (cmd *CreateServiceKey) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 80 cmd.ui = deps.UI 81 cmd.config = deps.Config 82 cmd.serviceRepo = deps.RepoLocator.GetServiceRepository() 83 cmd.serviceKeyRepo = deps.RepoLocator.GetServiceKeyRepository() 84 return cmd 85 } 86 87 func (cmd *CreateServiceKey) Execute(c flags.FlagContext) error { 88 serviceInstance := cmd.serviceInstanceRequirement.GetServiceInstance() 89 serviceKeyName := c.Args()[1] 90 params := c.String("c") 91 92 paramsMap, err := json.ParseJSONFromFileOrString(params) 93 if err != nil { 94 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.")) 95 } 96 97 cmd.ui.Say(T("Creating service key {{.ServiceKeyName}} for service instance {{.ServiceInstanceName}} as {{.CurrentUser}}...", 98 map[string]interface{}{ 99 "ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name), 100 "ServiceKeyName": terminal.EntityNameColor(serviceKeyName), 101 "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), 102 })) 103 104 err = cmd.serviceKeyRepo.CreateServiceKey(serviceInstance.GUID, serviceKeyName, paramsMap) 105 switch err.(type) { 106 case nil: 107 cmd.ui.Ok() 108 case *errors.ModelAlreadyExistsError: 109 cmd.ui.Ok() 110 cmd.ui.Warn(err.Error()) 111 default: 112 return err 113 } 114 return nil 115 }