-
Notifications
You must be signed in to change notification settings - Fork 929
/
bind_route_service.go
147 lines (127 loc) · 5.1 KB
/
bind_route_service.go
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
141
142
143
144
145
146
147
package service
import (
"fmt"
"strings"
"code.cloudfoundry.org/cli/cf/api"
"code.cloudfoundry.org/cli/cf/commandregistry"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/errors"
"code.cloudfoundry.org/cli/cf/flagcontext"
"code.cloudfoundry.org/cli/cf/flags"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/requirements"
"code.cloudfoundry.org/cli/cf/terminal"
)
type BindRouteService struct {
ui terminal.UI
config coreconfig.Reader
routeRepo api.RouteRepository
routeServiceBindingRepo api.RouteServiceBindingRepository
domainReq requirements.DomainRequirement
serviceInstanceReq requirements.ServiceInstanceRequirement
}
func init() {
commandregistry.Register(&BindRouteService{})
}
func (cmd *BindRouteService) MetaData() commandregistry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["hostname"] = &flags.StringFlag{
Name: "hostname",
ShortName: "n",
Usage: T("Hostname used in combination with DOMAIN to specify the route to bind"),
}
fs["path"] = &flags.StringFlag{
Name: "path",
Usage: T("Path for the HTTP route"),
}
fs["parameters"] = &flags.StringFlag{
ShortName: "c",
Usage: T("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."),
}
fs["f"] = &flags.BackwardsCompatibilityFlag{}
return commandregistry.CommandMetadata{
Name: "bind-route-service",
ShortName: "brs",
Description: T("Bind a service instance to an HTTP route"),
Usage: []string{
T(`CF_NAME bind-route-service DOMAIN SERVICE_INSTANCE [--hostname HOSTNAME] [--path PATH] [-c PARAMETERS_AS_JSON]`),
},
Examples: []string{
`CF_NAME bind-route-service example.com myratelimiter --hostname myapp --path foo`,
`CF_NAME bind-route-service example.com myratelimiter -c file.json`,
`CF_NAME bind-route-service example.com myratelimiter -c '{"valid":"json"}'`,
``,
T(`In Windows PowerShell use double-quoted, escaped JSON: "{\"valid\":\"json\"}"`),
T(`In Windows Command Line use single-quoted, escaped JSON: '{\"valid\":\"json\"}'`),
},
Flags: fs,
}
}
func (cmd *BindRouteService) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
if len(fc.Args()) != 2 {
cmd.ui.Failed(T("Incorrect Usage. Requires DOMAIN and SERVICE_INSTANCE as arguments\n\n") + commandregistry.Commands.CommandUsage("bind-route-service"))
return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2)
}
domainName := fc.Args()[0]
cmd.domainReq = requirementsFactory.NewDomainRequirement(domainName)
serviceName := fc.Args()[1]
cmd.serviceInstanceReq = requirementsFactory.NewServiceInstanceRequirement(serviceName)
reqs := []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
requirementsFactory.NewTargetedSpaceRequirement(),
cmd.domainReq,
cmd.serviceInstanceReq,
}
return reqs, nil
}
func (cmd *BindRouteService) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
cmd.ui = deps.UI
cmd.config = deps.Config
cmd.routeRepo = deps.RepoLocator.GetRouteRepository()
cmd.routeServiceBindingRepo = deps.RepoLocator.GetRouteServiceBindingRepository()
return cmd
}
func (cmd *BindRouteService) Execute(c flags.FlagContext) error {
var port int
host := c.String("hostname")
domain := cmd.domainReq.GetDomain()
path := c.String("path")
if !strings.HasPrefix(path, "/") && len(path) > 0 {
path = fmt.Sprintf("/%s", path)
}
var parameters string
if c.IsSet("parameters") {
jsonBytes, err := flagcontext.GetContentsFromFlagValue(c.String("parameters"))
if err != nil {
return err
}
parameters = string(jsonBytes)
}
route, err := cmd.routeRepo.Find(host, domain, path, port)
if err != nil {
return err
}
serviceInstance := cmd.serviceInstanceReq.GetServiceInstance()
cmd.ui.Say(T("Binding route {{.URL}} to service instance {{.ServiceInstanceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
map[string]interface{}{
"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name),
"URL": terminal.EntityNameColor(route.URL()),
"OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
}))
err = cmd.routeServiceBindingRepo.Bind(serviceInstance.GUID, route.GUID, serviceInstance.IsUserProvided(), parameters)
if err != nil {
if httpErr, ok := err.(errors.HTTPError); ok && httpErr.ErrorCode() == errors.ServiceInstanceAlreadyBoundToSameRoute {
cmd.ui.Warn(T("Route {{.URL}} is already bound to service instance {{.ServiceInstanceName}}.",
map[string]interface{}{
"URL": route.URL(),
"ServiceInstanceName": serviceInstance.Name,
}))
} else {
return err
}
}
cmd.ui.Ok()
return nil
}