github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/bind_service_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 BindServiceCommand struct {
    12  	BaseCommand
    13  
    14  	RequiredArgs     flag.BindServiceArgs          `positional-args:"yes"`
    15  	BindingName      flag.BindingName              `long:"binding-name" description:"Name to expose service instance to app process with (Default: service instance name)"`
    16  	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."`
    17  	Wait             bool                          `short:"w" long:"wait" description:"Wait for the operation to complete"`
    18  	relatedCommands  interface{}                   `related_commands:"services"`
    19  }
    20  
    21  func (cmd BindServiceCommand) Execute(args []string) error {
    22  	if err := cmd.SharedActor.CheckTarget(true, true); err != nil {
    23  		return err
    24  	}
    25  
    26  	if err := cmd.displayIntro(); err != nil {
    27  		return err
    28  	}
    29  
    30  	stream, warnings, err := cmd.Actor.CreateServiceAppBinding(v7action.CreateServiceAppBindingParams{
    31  		SpaceGUID:           cmd.Config.TargetedSpace().GUID,
    32  		ServiceInstanceName: cmd.RequiredArgs.ServiceInstanceName,
    33  		AppName:             cmd.RequiredArgs.AppName,
    34  		BindingName:         cmd.BindingName.Value,
    35  		Parameters:          types.OptionalObject(cmd.ParametersAsJSON),
    36  	})
    37  	cmd.UI.DisplayWarnings(warnings)
    38  
    39  	switch err.(type) {
    40  	case nil:
    41  	case actionerror.ResourceAlreadyExistsError:
    42  		cmd.UI.DisplayText("App {{.AppName}} is already bound to service instance {{.ServiceInstanceName}}.", cmd.names())
    43  		cmd.UI.DisplayOK()
    44  		return nil
    45  	default:
    46  		return err
    47  	}
    48  
    49  	completed, err := shared.WaitForResult(stream, cmd.UI, cmd.Wait)
    50  	switch {
    51  	case err != nil:
    52  		return err
    53  	case completed:
    54  		cmd.UI.DisplayOK()
    55  		cmd.UI.DisplayText("TIP: Use 'cf restage {{.AppName}}' to ensure your env variable changes take effect", cmd.names())
    56  		return nil
    57  	default:
    58  		cmd.UI.DisplayOK()
    59  		cmd.UI.DisplayText("Binding in progress. Use 'cf service {{.ServiceInstanceName}}' to check operation status.", cmd.names())
    60  		cmd.UI.DisplayNewline()
    61  		cmd.UI.DisplayText("TIP: Once this operation succeeds, use 'cf restage {{.AppName}}' to ensure your env variable changes take effect", cmd.names())
    62  		return nil
    63  	}
    64  }
    65  
    66  func (cmd BindServiceCommand) Usage() string {
    67  	return `CF_NAME bind-service APP_NAME SERVICE_INSTANCE [-c PARAMETERS_AS_JSON] [--binding-name BINDING_NAME]
    68  
    69  Optionally provide service-specific configuration parameters in a valid JSON object in-line:
    70  
    71  CF_NAME bind-service APP_NAME SERVICE_INSTANCE -c '{"name":"value","name":"value"}'
    72  
    73  Optionally provide a file containing service-specific configuration parameters in a valid JSON object.
    74  The path to the parameters file can be an absolute or relative path to a file.
    75  
    76  CF_NAME bind-service APP_NAME SERVICE_INSTANCE -c PATH_TO_FILE
    77  
    78  Example of valid JSON object:
    79  {
    80     "permissions": "read-only"
    81  }
    82  
    83  Optionally provide a binding name for the association between an app and a service instance:
    84  
    85  CF_NAME bind-service APP_NAME SERVICE_INSTANCE --binding-name BINDING_NAME`
    86  }
    87  
    88  func (cmd BindServiceCommand) Examples() string {
    89  	return `
    90  Linux/Mac:
    91     CF_NAME bind-service myapp mydb -c '{"permissions":"read-only"}'
    92  
    93  Windows Command Line:
    94     CF_NAME bind-service myapp mydb -c "{\"permissions\":\"read-only\"}"
    95  
    96  Windows PowerShell:
    97     CF_NAME bind-service myapp mydb -c '{\"permissions\":\"read-only\"}'
    98  
    99  CF_NAME bind-service myapp mydb -c ~/workspace/tmp/instance_config.json --binding-name BINDING_NAME
   100  `
   101  }
   102  
   103  func (cmd BindServiceCommand) displayIntro() error {
   104  	user, err := cmd.Actor.GetCurrentUser()
   105  	if err != nil {
   106  		return err
   107  	}
   108  
   109  	cmd.UI.DisplayTextWithFlavor(
   110  		"Binding service instance {{.ServiceInstance}} to app {{.AppName}} in org {{.Org}} / space {{.Space}} as {{.User}}...",
   111  		map[string]interface{}{
   112  			"ServiceInstance": cmd.RequiredArgs.ServiceInstanceName,
   113  			"AppName":         cmd.RequiredArgs.AppName,
   114  			"User":            user.Name,
   115  			"Space":           cmd.Config.TargetedSpace().Name,
   116  			"Org":             cmd.Config.TargetedOrganization().Name,
   117  		},
   118  	)
   119  
   120  	return nil
   121  }
   122  
   123  func (cmd BindServiceCommand) names() map[string]interface{} {
   124  	return map[string]interface{}{
   125  		"ServiceInstanceName": cmd.RequiredArgs.ServiceInstanceName,
   126  		"AppName":             cmd.RequiredArgs.AppName,
   127  	}
   128  }