github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/create_service_key_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror"
     5  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action"
     6  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/flag"
     7  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/shared"
     8  	"github.com/LukasHeimann/cloudfoundrycli/v8/types"
     9  )
    10  
    11  type CreateServiceKeyCommand struct {
    12  	BaseCommand
    13  
    14  	RequiredArgs     flag.ServiceInstanceKey       `positional-args:"yes"`
    15  	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."`
    16  	Wait             bool                          `short:"w" long:"wait" description:"Wait for the operation to complete"`
    17  	relatedCommands  interface{}                   `related_commands:"service-key"`
    18  }
    19  
    20  func (cmd CreateServiceKeyCommand) Execute(args []string) error {
    21  	if err := cmd.SharedActor.CheckTarget(true, true); err != nil {
    22  		return err
    23  	}
    24  
    25  	if err := cmd.displayIntro(); err != nil {
    26  		return err
    27  	}
    28  
    29  	stream, warnings, err := cmd.Actor.CreateServiceKey(v7action.CreateServiceKeyParams{
    30  		SpaceGUID:           cmd.Config.TargetedSpace().GUID,
    31  		ServiceInstanceName: cmd.RequiredArgs.ServiceInstance,
    32  		ServiceKeyName:      cmd.RequiredArgs.ServiceKey,
    33  		Parameters:          types.OptionalObject(cmd.ParametersAsJSON),
    34  	})
    35  	cmd.UI.DisplayWarnings(warnings)
    36  	switch err.(type) {
    37  	case nil:
    38  	case actionerror.ResourceAlreadyExistsError:
    39  		cmd.displayAlreadyExists()
    40  		return nil
    41  	default:
    42  		return err
    43  	}
    44  
    45  	completed, err := shared.WaitForResult(stream, cmd.UI, cmd.Wait)
    46  	switch {
    47  	case err != nil:
    48  		return err
    49  	case completed:
    50  		cmd.UI.DisplayOK()
    51  		return nil
    52  	default:
    53  		cmd.UI.DisplayOK()
    54  		cmd.UI.DisplayText("Create in progress.")
    55  		return nil
    56  	}
    57  }
    58  
    59  func (cmd CreateServiceKeyCommand) Usage() string {
    60  	return `
    61  CF_NAME create-service-key SERVICE_INSTANCE SERVICE_KEY [-c PARAMETERS_AS_JSON] [--wait]
    62  
    63  Optionally provide service-specific configuration parameters in a valid JSON object in-line.
    64  CF_NAME create-service-key SERVICE_INSTANCE SERVICE_KEY -c '{"name":"value","name":"value"}'
    65  
    66  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.
    67  CF_NAME create-service-key SERVICE_INSTANCE SERVICE_KEY -c PATH_TO_FILE
    68  
    69  Example of valid JSON object:
    70  {
    71    "permissions": "read-only"
    72  }
    73  `
    74  }
    75  
    76  func (cmd CreateServiceKeyCommand) Examples() string {
    77  	return `
    78  CF_NAME create-service-key mydb mykey -c '{"permissions":"read-only"}'
    79  CF_NAME create-service-key mydb mykey -c ~/workspace/tmp/instance_config.json
    80  `
    81  }
    82  
    83  func (cmd CreateServiceKeyCommand) displayIntro() error {
    84  	user, err := cmd.Actor.GetCurrentUser()
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	cmd.UI.DisplayTextWithFlavor(
    90  		"Creating service key {{.ServiceKey}} for service instance {{.ServiceInstance}} as {{.User}}...",
    91  		map[string]interface{}{
    92  			"ServiceInstance": cmd.RequiredArgs.ServiceInstance,
    93  			"ServiceKey":      cmd.RequiredArgs.ServiceKey,
    94  			"User":            user.Name,
    95  		},
    96  	)
    97  
    98  	return nil
    99  }
   100  
   101  func (cmd CreateServiceKeyCommand) displayAlreadyExists() {
   102  	cmd.UI.DisplayNewline()
   103  	cmd.UI.DisplayText(
   104  		"Service key {{.ServiceKey}} already exists",
   105  		map[string]interface{}{"ServiceKey": cmd.RequiredArgs.ServiceKey},
   106  	)
   107  	cmd.UI.DisplayOK()
   108  }