Skip to content

Commit

Permalink
added option to create a random port for a tcp route
Browse files Browse the repository at this point in the history
  • Loading branch information
mevansam committed Oct 17, 2017
1 parent 4427317 commit fae0e67
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
11 changes: 9 additions & 2 deletions cloudfoundry/cfapi/route_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,23 @@ func (rm *RouteManager) ReadRoute(routeID string) (route CCRoute, err error) {
}

// CreateRoute -
func (rm *RouteManager) CreateRoute(r CCRoute) (route CCRoute, err error) {
func (rm *RouteManager) CreateRoute(r CCRoute, randomPort bool) (route CCRoute, err error) {

body, err := json.Marshal(r)
if err != nil {
return
}

var path string
if randomPort {
path = "/v2/routes?generate_port=true"
} else {
path = "/v2/routes"
}

resource := CCRouteResource{}
if err = rm.ccGateway.CreateResource(rm.apiEndpoint,
"/v2/routes", bytes.NewReader(body), &resource); err != nil {
path, bytes.NewReader(body), &resource); err != nil {
return
}
route = resource.Entity
Expand Down
1 change: 0 additions & 1 deletion cloudfoundry/cfapi/service_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,6 @@ func (sm *ServiceManager) DeleteServiceKey(serviceKeyID string) (err error) {

// FindServiceKey -
func (sm *ServiceManager) FindServiceKey(name string, serviceInstanceID string) (serviceKey CCServiceKey, err error) {

path := fmt.Sprintf("/v2/service_keys?q=%s", url.QueryEscape("name:"+name))

var found bool
Expand Down
14 changes: 12 additions & 2 deletions cloudfoundry/resource_cf_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ func resourceRoute() *schema.Resource {
"port": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ConflictsWith: []string{"path", "random_port"},
},
"random_port": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"path"},
ConflictsWith: []string{"path", "port"},
},
"path": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -111,10 +116,15 @@ func resourceRouteCreate(d *schema.ResourceData, meta interface{}) (err error) {
route.Path = &vv
}

randomPort := false
if v, ok := d.GetOk("random_port"); ok {
randomPort = v.(bool)
}

rm := session.RouteManager()

// Create route
if route, err = rm.CreateRoute(route); err != nil {
if route, err = rm.CreateRoute(route, randomPort); err != nil {
return err
}
// Delete route if an error occurs
Expand Down
5 changes: 3 additions & 2 deletions website/docs/r/route.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ The following arguments are supported:

The following argument applies only to TCP routes.

- `port` - (Optional) The port to associate with the route for a TCP route.
- `port` - (Optional, Int) The port to associate with the route for a TCP route.
- `random_port` - (Optional, Bool) Set to 'true' to create a random port.

The following argument applies only to HTTP routes.

Expand All @@ -43,7 +44,7 @@ The following maps the route to an application.
- `target` - (Optional) A route mapping that will map this route to an application

- `app` - (Required, String) The ID of the [application](/docs/providers/cf/r/app.html) to map this route to.
- `port` - (Optional, int) A port that the application will be listening on. If this argument is not provided then the route will be associated with the application's default port.
- `port` - (Optional, Int) A port that the application will be listening on. If this argument is not provided then the route will be associated with the application's default port.

## Attributes Reference

Expand Down

0 comments on commit fae0e67

Please sign in to comment.