github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/create_service_key_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/command"
     8  	"code.cloudfoundry.org/cli/command/flag"
     9  	"code.cloudfoundry.org/cli/command/v6/shared"
    10  )
    11  
    12  //go:generate counterfeiter . CreateServiceKeyActor
    13  
    14  type CreateServiceKeyActor interface {
    15  	CreateServiceKey(serviceInstanceName, keyName, spaceGUID string, parameters map[string]interface{}) (v2action.ServiceKey, v2action.Warnings, error)
    16  }
    17  
    18  type CreateServiceKeyCommand struct {
    19  	RequiredArgs     flag.ServiceInstanceKey       `positional-args:"yes"`
    20  	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."`
    21  	usage            interface{}                   `usage:"CF_NAME create-service-key SERVICE_INSTANCE SERVICE_KEY [-c PARAMETERS_AS_JSON]\n\n   Optionally provide service-specific configuration parameters in a valid JSON object in-line.\n   CF_NAME create-service-key SERVICE_INSTANCE SERVICE_KEY -c '{\"name\":\"value\",\"name\":\"value\"}'\n\n   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.\n   CF_NAME create-service-key SERVICE_INSTANCE SERVICE_KEY -c PATH_TO_FILE\n\n   Example of valid JSON object:\n   {\n      \"permissions\": \"read-only\"\n   }\n\nEXAMPLES:\n   CF_NAME create-service-key mydb mykey -c '{\"permissions\":\"read-only\"}'\n   CF_NAME create-service-key mydb mykey -c ~/workspace/tmp/instance_config.json"`
    22  	relatedCommands  interface{}                   `related_commands:"service-key"`
    23  
    24  	UI          command.UI
    25  	Config      command.Config
    26  	SharedActor command.SharedActor
    27  	Actor       CreateServiceKeyActor
    28  }
    29  
    30  func (cmd *CreateServiceKeyCommand) Setup(config command.Config, ui command.UI) error {
    31  	cmd.UI = ui
    32  	cmd.Config = config
    33  
    34  	cmd.SharedActor = sharedaction.NewActor(config)
    35  
    36  	ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
    42  
    43  	return nil
    44  }
    45  
    46  func (cmd CreateServiceKeyCommand) Execute(args []string) error {
    47  	err := cmd.SharedActor.CheckTarget(true, true)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	user, err := cmd.Config.CurrentUser()
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	serviceInstanceName := cmd.RequiredArgs.ServiceInstance
    58  	keyName := cmd.RequiredArgs.ServiceKey
    59  
    60  	cmd.UI.DisplayTextWithFlavor("Creating service key {{.KeyName}} for service instance {{.ServiceInstanceName}} as {{.User}}...",
    61  		map[string]interface{}{
    62  			"KeyName":             keyName,
    63  			"ServiceInstanceName": serviceInstanceName,
    64  			"User":                user.Name,
    65  		})
    66  
    67  	_, warnings, err := cmd.Actor.CreateServiceKey(serviceInstanceName, keyName, cmd.Config.TargetedSpace().GUID, cmd.ParametersAsJSON)
    68  	cmd.UI.DisplayWarnings(warnings)
    69  	if err != nil {
    70  		if _, isTakenError := err.(ccerror.ServiceKeyTakenError); isTakenError {
    71  			cmd.UI.DisplayOK()
    72  			cmd.UI.DisplayWarning("Service key {{.KeyName}} already exists", map[string]interface{}{
    73  				"KeyName": keyName,
    74  			})
    75  			return nil
    76  		}
    77  		return err
    78  	}
    79  
    80  	cmd.UI.DisplayOK()
    81  
    82  	return nil
    83  }