-
Notifications
You must be signed in to change notification settings - Fork 989
Expand file tree
/
Copy pathupdate_route_command.go
More file actions
140 lines (121 loc) · 4.81 KB
/
update_route_command.go
File metadata and controls
140 lines (121 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package v7
import (
"fmt"
"code.cloudfoundry.org/cli/v8/actor/actionerror"
"code.cloudfoundry.org/cli/v8/api/cloudcontroller/ccversion"
"code.cloudfoundry.org/cli/v8/command"
"code.cloudfoundry.org/cli/v8/command/flag"
"code.cloudfoundry.org/cli/v8/resources"
)
type UpdateRouteCommand struct {
BaseCommand
RequiredArgs flag.Domain `positional-args:"yes"`
Hostname string `long:"hostname" short:"n" description:"Hostname for the HTTP route (required for shared domains)"`
Path flag.V7RoutePath `long:"path" description:"Path for the HTTP route"`
Options []string `long:"option" short:"o" description:"Set the value of a per-route option"`
RemoveOptions []string `long:"remove-option" short:"r" description:"Remove an option with the given name"`
relatedCommands interface{} `related_commands:"check-route, domains, map-route, routes, unmap-route"`
}
func (cmd UpdateRouteCommand) Usage() string {
return `
Update an existing HTTP route:
CF_NAME update-route DOMAIN [--hostname HOSTNAME] [--path PATH] [--option OPTION=VALUE] [--remove-option OPTION]`
}
func (cmd UpdateRouteCommand) Examples() string {
return `
CF_NAME update-route example.com -o loadbalancing=round-robin # use round-robin load balancing for route
CF_NAME update-route example.com -o loadbalancing=least-connection # use least-connection load balancing for route
CF_NAME update-route example.com -o loadbalancing=hash -o hash_header=My-Hash-Header # use hash-based load balancing for route
CF_NAME update-route example.com -o loadbalancing=hash -o hash_header=My-Hash-Header -o hash_balance=1.3 # use hash-based load balancing with balance factor
CF_NAME update-route example.com -r loadbalancing # remove load balancing option
CF_NAME update-route example.com --hostname myhost --path foo -o loadbalancing=round-robin # update route myhost.example.com/foo
`
}
func (cmd UpdateRouteCommand) Execute(args []string) error {
err := cmd.SharedActor.CheckTarget(true, true)
if err != nil {
return err
}
user, err := cmd.Actor.GetCurrentUser()
if err != nil {
return err
}
domain, warnings, err := cmd.Actor.GetDomainByName(cmd.RequiredArgs.Domain)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
path := cmd.Path.Path
route, warnings, err := cmd.Actor.GetRouteByAttributes(domain, cmd.Hostname, path, 0)
url := desiredURL(domain.Name, cmd.Hostname, path, 0)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
if _, ok := err.(actionerror.RouteNotFoundError); !ok {
return err
}
}
// Update route only works for per route options. The command will fail instead instead of just
// ignoring the per-route options like it is the case for create-route and map-route.
err = cmd.validateAPIVersionForPerRouteOptions()
if err != nil {
return err
}
if cmd.Options == nil && cmd.RemoveOptions == nil {
return actionerror.RouteOptionSupportError{
ErrorText: fmt.Sprintf("No options were specified for the update of the Route %s", route.URL)}
}
if len(cmd.Options) > 0 {
routeOpts, wrongOptSpec := resources.CreateRouteOptions(cmd.Options)
if wrongOptSpec != nil {
return actionerror.RouteOptionError{
Name: *wrongOptSpec,
DomainName: domain.Name,
Path: path,
Host: cmd.Hostname,
}
}
cmd.UI.DisplayTextWithFlavor("Updating route {{.URL}} for org {{.OrgName}} / space {{.SpaceName}} as {{.User}}...",
map[string]interface{}{
"URL": url,
"User": user.Name,
"SpaceName": cmd.Config.TargetedSpace().Name,
"OrgName": cmd.Config.TargetedOrganization().Name,
})
route, warnings, err = cmd.Actor.UpdateRoute(
route.GUID,
routeOpts,
)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
}
if cmd.RemoveOptions != nil {
inputRouteOptions := resources.RemoveRouteOptions(cmd.RemoveOptions)
route, warnings, err = cmd.Actor.UpdateRoute(
route.GUID,
inputRouteOptions,
)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
}
cmd.UI.DisplayText("Route {{.URL}} has been updated",
map[string]interface{}{
"URL": route.URL,
})
cmd.UI.DisplayOK()
return nil
}
func (cmd UpdateRouteCommand) validateAPIVersionForPerRouteOptions() error {
err := command.MinimumCCAPIVersionCheck(cmd.Config.APIVersion(), ccversion.MinVersionPerRouteOpts)
if err != nil {
cmd.UI.DisplayWarning("Your CC API version ({{.APIVersion}}) does not support per-route options."+
"Upgrade to a newer version of the API (minimum version {{.MinSupportedVersion}}). ", map[string]interface{}{
"APIVersion": cmd.Config.APIVersion(),
"MinSupportedVersion": ccversion.MinVersionPerRouteOpts,
})
}
return err
}