github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/command/v6/bind_service_command.go (about)

     1  package v6
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/actor/sharedaction"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/command"
    10  	"code.cloudfoundry.org/cli/command/flag"
    11  	"code.cloudfoundry.org/cli/command/v6/shared"
    12  )
    13  
    14  //go:generate counterfeiter . BindServiceActor
    15  
    16  type BindServiceActor interface {
    17  	BindServiceBySpace(appName string, ServiceInstanceName string, spaceGUID string, bindingName string, parameters map[string]interface{}) (v2action.ServiceBinding, v2action.Warnings, error)
    18  	CloudControllerAPIVersion() string
    19  }
    20  
    21  type BindServiceCommand struct {
    22  	RequiredArgs     flag.BindServiceArgs          `positional-args:"yes"`
    23  	BindingName      flag.BindingName              `long:"binding-name" description:"Name to expose service instance to app process with (Default: service instance name)"`
    24  	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."`
    25  	usage            interface{}                   `usage:"CF_NAME bind-service APP_NAME SERVICE_INSTANCE [-c PARAMETERS_AS_JSON] [--binding-name BINDING_NAME]\n\n   Optionally provide service-specific configuration parameters in a valid JSON object in-line:\n\n   CF_NAME bind-service APP_NAME SERVICE_INSTANCE -c '{\"name\":\"value\",\"name\":\"value\"}'\n\n   Optionally provide a file containing service-specific configuration parameters in a valid JSON object. \n   The path to the parameters file can be an absolute or relative path to a file.\n   CF_NAME bind-service APP_NAME SERVICE_INSTANCE -c PATH_TO_FILE\n\n   Example of valid JSON object:\n   {\n      \"permissions\": \"read-only\"\n   }\n\n   Optionally provide a binding name for the association between an app and a service instance:\n\n   CF_NAME bind-service APP_NAME SERVICE_INSTANCE --binding-name BINDING_NAME\n\nEXAMPLES:\n   Linux/Mac:\n      CF_NAME bind-service myapp mydb -c '{\"permissions\":\"read-only\"}'\n\n   Windows Command Line:\n      CF_NAME bind-service myapp mydb -c \"{\\\"permissions\\\":\\\"read-only\\\"}\"\n\n   Windows PowerShell:\n      CF_NAME bind-service myapp mydb -c '{\\\"permissions\\\":\\\"read-only\\\"}'\n\n   CF_NAME bind-service myapp mydb -c ~/workspace/tmp/instance_config.json --binding-name BINDING_NAME"`
    26  	relatedCommands  interface{}                   `related_commands:"services"`
    27  
    28  	UI          command.UI
    29  	Config      command.Config
    30  	SharedActor command.SharedActor
    31  	Actor       BindServiceActor
    32  }
    33  
    34  func (cmd *BindServiceCommand) Setup(config command.Config, ui command.UI) error {
    35  	cmd.UI = ui
    36  	cmd.Config = config
    37  	cmd.SharedActor = sharedaction.NewActor(config)
    38  
    39  	ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
    44  
    45  	return nil
    46  }
    47  
    48  func (cmd BindServiceCommand) Execute(args []string) error {
    49  	template := "Binding service {{.ServiceName}} to app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}..."
    50  	if cmd.BindingName.Value != "" {
    51  		template = "Binding service {{.ServiceName}} to app {{.AppName}} with binding name {{.BindingName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}..."
    52  	}
    53  
    54  	err := cmd.SharedActor.CheckTarget(true, true)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	user, err := cmd.Config.CurrentUser()
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	cmd.UI.DisplayTextWithFlavor(template, map[string]interface{}{
    65  		"ServiceName": cmd.RequiredArgs.ServiceInstanceName,
    66  		"AppName":     cmd.RequiredArgs.AppName,
    67  		"BindingName": cmd.BindingName.Value,
    68  		"OrgName":     cmd.Config.TargetedOrganization().Name,
    69  		"SpaceName":   cmd.Config.TargetedSpace().Name,
    70  		"CurrentUser": user.Name,
    71  	})
    72  
    73  	serviceBinding, warnings, err := cmd.Actor.BindServiceBySpace(cmd.RequiredArgs.AppName, cmd.RequiredArgs.ServiceInstanceName, cmd.Config.TargetedSpace().GUID, cmd.BindingName.Value, cmd.ParametersAsJSON)
    74  	cmd.UI.DisplayWarnings(warnings)
    75  	if err != nil {
    76  		if _, isTakenError := err.(ccerror.ServiceBindingTakenError); isTakenError {
    77  			cmd.UI.DisplayText("App {{.AppName}} is already bound to {{.ServiceName}}.", map[string]interface{}{
    78  				"AppName":     cmd.RequiredArgs.AppName,
    79  				"ServiceName": cmd.RequiredArgs.ServiceInstanceName,
    80  			})
    81  			cmd.UI.DisplayOK()
    82  			return nil
    83  		}
    84  		return err
    85  	}
    86  
    87  	cmd.UI.DisplayOK()
    88  
    89  	if serviceBinding.IsInProgress() {
    90  		cmd.UI.DisplayText("Binding in progress. Use '{{.CFCommand}} {{.ServiceName}}' to check operation status.", map[string]interface{}{
    91  			"CFCommand":   fmt.Sprintf("%s service", cmd.Config.BinaryName()),
    92  			"ServiceName": cmd.RequiredArgs.ServiceInstanceName,
    93  		})
    94  		cmd.UI.DisplayText("TIP: Once this operation succeeds, use '{{.CFCommand}} {{.AppName}}' to ensure your env variable changes take effect.", map[string]interface{}{
    95  			"CFCommand": fmt.Sprintf("%s restage", cmd.Config.BinaryName()),
    96  			"AppName":   cmd.RequiredArgs.AppName,
    97  		})
    98  		return nil
    99  	}
   100  
   101  	cmd.UI.DisplayText("TIP: Use '{{.CFCommand}} {{.AppName}}' to ensure your env variable changes take effect", map[string]interface{}{
   102  		"CFCommand": fmt.Sprintf("%s restage", cmd.Config.BinaryName()),
   103  		"AppName":   cmd.RequiredArgs.AppName,
   104  	})
   105  
   106  	return nil
   107  }