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

     1  package v7
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror"
     7  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/flag"
     8  )
     9  
    10  type UpdateDestinationCommand struct {
    11  	BaseCommand
    12  
    13  	RequiredArgs flag.AppDomain   `positional-args:"yes"`
    14  	Hostname     string           `long:"hostname" short:"n" description:"Hostname for the HTTP route (required for shared domains)"`
    15  	AppProtocol  string           `long:"app-protocol" description:"New Protocol for the route destination (http1 or http2). Only applied to HTTP routes"`
    16  	Path         flag.V7RoutePath `long:"path" description:"Path for the HTTP route"`
    17  
    18  	relatedCommands interface{} `related_commands:"routes, map-route, create-route, unmap-route"`
    19  }
    20  
    21  func (cmd UpdateDestinationCommand) Usage() string {
    22  	return `
    23  Edit an existing HTTP route:
    24     CF_NAME update-destination APP_NAME DOMAIN [--hostname HOSTNAME] [--app-protocol PROTOCOL] [--path PATH]`
    25  }
    26  
    27  func (cmd UpdateDestinationCommand) Examples() string {
    28  	return `
    29  CF_NAME update-destination my-app example.com --hostname myhost --app-protocol http2                   # myhost.example.com
    30  CF_NAME update destination my-app example.com --hostname myhost --path foo --app-protocol http2        # myhost.example.com/foo`
    31  }
    32  
    33  func (cmd UpdateDestinationCommand) Execute(args []string) error {
    34  	err := cmd.SharedActor.CheckTarget(true, true)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	user, err := cmd.Actor.GetCurrentUser()
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	domain, warnings, err := cmd.Actor.GetDomainByName(cmd.RequiredArgs.Domain)
    45  	cmd.UI.DisplayWarnings(warnings)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	spaceGUID := cmd.Config.TargetedSpace().GUID
    51  	app, warnings, err := cmd.Actor.GetApplicationByNameAndSpace(cmd.RequiredArgs.App, spaceGUID)
    52  	cmd.UI.DisplayWarnings(warnings)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	path := cmd.Path.Path
    58  	route, warnings, err := cmd.Actor.GetRouteByAttributes(domain, cmd.Hostname, path, 0)
    59  	cmd.UI.DisplayWarnings(warnings)
    60  	url := desiredURL(domain.Name, cmd.Hostname, path, 0)
    61  	if err != nil {
    62  		if _, ok := err.(actionerror.RouteNotFoundError); ok {
    63  			cmd.UI.DisplayText("Route to be updated does not exist.")
    64  			return err
    65  		}
    66  		return err
    67  	}
    68  
    69  	dest, err := cmd.Actor.GetRouteDestinationByAppGUID(route, app.GUID)
    70  	if err != nil {
    71  		if _, ok := err.(actionerror.RouteDestinationNotFoundError); !ok {
    72  			cmd.UI.DisplayText("Route's destination to be updated does not exist.")
    73  			return err
    74  		}
    75  	}
    76  
    77  	if cmd.AppProtocol == "" {
    78  		cmd.AppProtocol = "http1"
    79  	}
    80  
    81  	if cmd.AppProtocol == "tcp" {
    82  		return errors.New("Destination protocol must be 'http1' or 'http2'")
    83  	}
    84  
    85  	if dest.Protocol == cmd.AppProtocol {
    86  		cmd.UI.DisplayText(" App '{{ .AppName }}' is already using '{{ .AppProtocol }}'. Nothing has been updated", map[string]interface{}{
    87  			"AppName":     cmd.RequiredArgs.App,
    88  			"AppProtocol": cmd.AppProtocol,
    89  		})
    90  		cmd.UI.DisplayOK()
    91  		return nil
    92  	}
    93  
    94  	cmd.UI.DisplayTextWithFlavor("Updating destination protocol from {{.OldProtocol}} to {{.NewProtocol}} for route {{.URL}} in org {{.OrgName}} / space {{.SpaceName}} as {{.User}}...",
    95  		map[string]interface{}{
    96  			"OldProtocol": dest.Protocol,
    97  			"NewProtocol": cmd.AppProtocol,
    98  			"URL":         url,
    99  			"User":        user.Name,
   100  			"SpaceName":   cmd.Config.TargetedSpace().Name,
   101  			"OrgName":     cmd.Config.TargetedOrganization().Name,
   102  		})
   103  
   104  	warnings, err = cmd.Actor.UpdateDestination(
   105  		route.GUID,
   106  		dest.GUID,
   107  		cmd.AppProtocol,
   108  	)
   109  
   110  	cmd.UI.DisplayWarnings(warnings)
   111  	if err != nil {
   112  		return err
   113  	}
   114  	cmd.UI.DisplayOK()
   115  
   116  	return nil
   117  }