github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/bind_route_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 BindRouteServiceCommand struct {
    12  	BaseCommand
    13  
    14  	RequiredArgs    flag.RouteServiceArgs         `positional-args:"yes"`
    15  	Parameters      flag.JSONOrFileWithValidation `short:"c" description:"Valid JSON object containing service-specific configuration parameters, provided inline or in a file. For a list of supported configuration parameters, see documentation for the particular service offering."`
    16  	Hostname        string                        `long:"hostname" short:"n" description:"Hostname used in combination with DOMAIN to specify the route to bind"`
    17  	Path            flag.V7RoutePath              `long:"path" description:"Path used in combination with HOSTNAME and DOMAIN to specify the route to bind"`
    18  	Wait            bool                          `short:"w" long:"wait" description:"Wait for the operation to complete"`
    19  	relatedCommands interface{}                   `related_commands:"routes, services"`
    20  }
    21  
    22  func (cmd BindRouteServiceCommand) Execute(args []string) error {
    23  	if err := cmd.SharedActor.CheckTarget(true, true); err != nil {
    24  		return err
    25  	}
    26  
    27  	if err := cmd.displayIntro(); err != nil {
    28  		return err
    29  	}
    30  
    31  	stream, warnings, err := cmd.Actor.CreateRouteBinding(v7action.CreateRouteBindingParams{
    32  		SpaceGUID:           cmd.Config.TargetedSpace().GUID,
    33  		ServiceInstanceName: cmd.RequiredArgs.ServiceInstance,
    34  		DomainName:          cmd.RequiredArgs.Domain,
    35  		Hostname:            cmd.Hostname,
    36  		Path:                cmd.Path.Path,
    37  		Parameters:          types.OptionalObject(cmd.Parameters),
    38  	})
    39  	cmd.UI.DisplayWarnings(warnings)
    40  	switch err.(type) {
    41  	case nil:
    42  	case actionerror.ResourceAlreadyExistsError:
    43  		cmd.displayAlreadyExists()
    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  		return nil
    56  	default:
    57  		cmd.UI.DisplayOK()
    58  		cmd.UI.DisplayText("Binding in progress.")
    59  		return nil
    60  	}
    61  }
    62  
    63  func (cmd BindRouteServiceCommand) Usage() string {
    64  	return `CF_NAME bind-route-service DOMAIN [--hostname HOSTNAME] [--path PATH] SERVICE_INSTANCE [-c PARAMETERS_AS_JSON]`
    65  }
    66  
    67  func (cmd BindRouteServiceCommand) Examples() string {
    68  	return `
    69  CF_NAME bind-route-service example.com --hostname myapp --path foo myratelimiter
    70  CF_NAME bind-route-service example.com myratelimiter -c file.json
    71  CF_NAME bind-route-service example.com myratelimiter -c '{"valid":"json"}'
    72  
    73  In Windows PowerShell use double-quoted, escaped JSON: "{\"valid\":\"json\"}"
    74  In Windows Command Line use single-quoted, escaped JSON: '{\"valid\":\"json\"}'
    75  `
    76  }
    77  
    78  func (cmd BindRouteServiceCommand) displayIntro() error {
    79  	user, err := cmd.Actor.GetCurrentUser()
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	cmd.UI.DisplayTextWithFlavor(
    85  		"Binding route {{.URL}} to service instance {{.ServiceInstance}} in org {{.Org}} / space {{.Space}} as {{.User}}...",
    86  		map[string]interface{}{
    87  			"URL":             desiredURL(cmd.RequiredArgs.Domain, cmd.Hostname, cmd.Path.Path, 0),
    88  			"ServiceInstance": cmd.RequiredArgs.ServiceInstance,
    89  			"User":            user.Name,
    90  			"Space":           cmd.Config.TargetedSpace().Name,
    91  			"Org":             cmd.Config.TargetedOrganization().Name,
    92  		},
    93  	)
    94  
    95  	return nil
    96  }
    97  
    98  func (cmd BindRouteServiceCommand) displayAlreadyExists() {
    99  	cmd.UI.DisplayText(
   100  		"Route {{.URL}} is already bound to service instance {{.ServiceInstance}}.",
   101  		map[string]interface{}{
   102  			"URL":             desiredURL(cmd.RequiredArgs.Domain, cmd.Hostname, cmd.Path.Path, 0),
   103  			"ServiceInstance": cmd.RequiredArgs.ServiceInstance,
   104  		},
   105  	)
   106  	cmd.UI.DisplayOK()
   107  }