Skip to content

Commit

Permalink
Implements new command share-route (#2288)
Browse files Browse the repository at this point in the history
This command is used for sharing a route in between two spaces
in order to facilitate the movement of apps in between
spaces.
jdgonzaleza authored Jun 22, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 82f39a3 commit 17feaa9
Showing 12 changed files with 779 additions and 1 deletion.
1 change: 1 addition & 0 deletions actor/v7action/cloud_controller_client.go
Original file line number Diff line number Diff line change
@@ -149,6 +149,7 @@ type CloudControllerClient interface {
SetApplicationDroplet(appGUID string, dropletGUID string) (resources.Relationship, ccv3.Warnings, error)
SharePrivateDomainToOrgs(domainGuid string, sharedOrgs ccv3.SharedOrgs) (ccv3.Warnings, error)
ShareServiceInstanceToSpaces(serviceInstanceGUID string, spaceGUIDs []string) (resources.RelationshipList, ccv3.Warnings, error)
ShareRoute(routeGUID string, spaceGUID string) (ccv3.Warnings, error)
TargetCF(settings ccv3.TargetSettings)
UnbindSecurityGroupRunningSpace(securityGroupGUID string, spaceGUID string) (ccv3.Warnings, error)
UnbindSecurityGroupStagingSpace(securityGroupGUID string, spaceGUID string) (ccv3.Warnings, error)
5 changes: 4 additions & 1 deletion actor/v7action/route.go
Original file line number Diff line number Diff line change
@@ -406,7 +406,10 @@ func (actor Actor) UnmapRoute(routeGUID string, destinationGUID string) (Warning
warnings, err := actor.CloudControllerClient.UnmapRoute(routeGUID, destinationGUID)
return Warnings(warnings), err
}

func (actor Actor) ShareRoute(routeGUID string, spaceGUID string) (Warnings, error) {
warnings, err := actor.CloudControllerClient.ShareRoute(routeGUID, spaceGUID)
return Warnings(warnings), err
}
func (actor Actor) GetApplicationRoutes(appGUID string) ([]resources.Route, Warnings, error) {
allWarnings := Warnings{}

80 changes: 80 additions & 0 deletions actor/v7action/v7actionfakes/fake_cloud_controller_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/cloudcontroller/ccv3/internal/api_routes.go
Original file line number Diff line number Diff line change
@@ -172,6 +172,7 @@ const (
PostUserRequest = "PostUser"
PutTaskCancelRequest = "PutTaskCancel"
SharePrivateDomainRequest = "SharePrivateDomainRequest"
ShareRouteRequest = "ShareRouteRequest"
UnmapRouteRequest = "UnmapRoute"
WhoAmI = "WhoAmI"
)
@@ -277,6 +278,7 @@ var APIRoutes = map[string]Route{
MapRouteRequest: {Path: "/v3/routes/:route_guid/destinations", Method: http.MethodPost},
UnmapRouteRequest: {Path: "/v3/routes/:route_guid/destinations/:destination_guid", Method: http.MethodDelete},
PatchDestinationRequest: {Path: "/v3/routes/:route_guid/destinations/:destination_guid", Method: http.MethodPatch},
ShareRouteRequest: {Path: "/v3/routes/:route_guid/relationships/shared_spaces", Method: http.MethodPost},
GetSecurityGroupsRequest: {Path: "/v3/security_groups", Method: http.MethodGet},
PostSecurityGroupRequest: {Path: "/v3/security_groups", Method: http.MethodPost},
DeleteSecurityGroupRequest: {Path: "/v3/security_groups/:security_group_guid", Method: http.MethodDelete},
25 changes: 25 additions & 0 deletions api/cloudcontroller/ccv3/route.go
Original file line number Diff line number Diff line change
@@ -148,3 +148,28 @@ func (client Client) UpdateDestination(routeGUID string, destinationGUID string,
})
return warnings, err
}

func (client Client) ShareRoute(routeGUID string, spaceGUID string) (Warnings, error) {
type space struct {
GUID string `json:"guid"`
}

type body struct {
Data []space `json:"data"`
}

requestBody := body{
Data: []space{
{GUID: spaceGUID},
},
}

var responseBody resources.Build
_, warnings, err := client.MakeRequest(RequestParams{
RequestName: internal.ShareRouteRequest,
URIParams: internal.Params{"route_guid": routeGUID},
RequestBody: &requestBody,
ResponseBody: &responseBody,
})
return warnings, err
}
1 change: 1 addition & 0 deletions command/common/command_list_v7.go
Original file line number Diff line number Diff line change
@@ -157,6 +157,7 @@ type commandList struct {
SetStagingEnvironmentVariableGroup v7.SetStagingEnvironmentVariableGroupCommand `command:"set-staging-environment-variable-group" alias:"ssevg" description:"Pass parameters as JSON to create a staging environment variable group"`
SharePrivateDomain v7.SharePrivateDomainCommand `command:"share-private-domain" description:"Share a private domain with a specific org"`
ShareService v7.ShareServiceCommand `command:"share-service" description:"Share a service instance with another space"`
ShareRoute v7.ShareRouteCommand `command:"share-route" description:"Share a route in between spaces"`
Space v7.SpaceCommand `command:"space" description:"Show space info"`
SpaceQuota v7.SpaceQuotaCommand `command:"space-quota" description:"Show space quota info"`
SpaceQuotas v7.SpaceQuotasCommand `command:"space-quotas" description:"List available space quotas"`
1 change: 1 addition & 0 deletions command/common/internal/help_all_display.go
Original file line number Diff line number Diff line change
@@ -68,6 +68,7 @@ var HelpCategoryList = []HelpCategory{
{"create-route", "check-route", "map-route", "unmap-route", "delete-route"},
{"delete-orphaned-routes"},
{"update-destination"},
{"share-route"},
},
},
{
1 change: 1 addition & 0 deletions command/v7/actor.go
Original file line number Diff line number Diff line change
@@ -216,6 +216,7 @@ type Actor interface {
SetTarget(settings v7action.TargetSettings) (v7action.Warnings, error)
SharePrivateDomain(domainName string, orgName string) (v7action.Warnings, error)
ShareServiceInstanceToSpaceAndOrg(serviceInstanceName, targetedSpaceGUID, targetedOrgGUID string, sharedToDetails v7action.ServiceInstanceSharingParams) (v7action.Warnings, error)
ShareRoute(routeGUID string, spaceGUID string) (v7action.Warnings, error)
StageApplicationPackage(pkgGUID string) (resources.Build, v7action.Warnings, error)
StagePackage(packageGUID, appName, spaceGUID string) (<-chan resources.Droplet, <-chan v7action.Warnings, <-chan error)
StartApplication(appGUID string) (v7action.Warnings, error)
101 changes: 101 additions & 0 deletions command/v7/share_route_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package v7

import (
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/command/flag"
)

type ShareRouteCommand struct {
BaseCommand

RequireArgs 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"`
DestinationOrg string `short:"o" description:"The org of the destination app (Default: targeted org)"`
DestinationSpace string `short:"s" description:"The space of the destination app (Default: targeted space)"`

relatedCommands interface{} `related_commands:"create-route, map-route, unmap-route, routes"`
}

func (cmd ShareRouteCommand) Usage() string {
return `
Share an existing route in between two spaces:
CF_NAME share-route DOMAIN [--hostname HOSTNAME] [--path PATH] -s OTHER_SPACE [-o OTHER_ORG]`
}

func (cmd ShareRouteCommand) Examples() string {
return `
CF_NAME share-route example.com --hostname myHost --path foo -s TargetSpace -o TargetOrg # myhost.example.com/foo
CF_NAME share-route example.com --hostname myHost -s TargetSpace # myhost.example.com
CF_NAME share-route example.com --hostname myHost -s TargetSpace -o TargetOrg # myhost.example.com`
}

func (cmd ShareRouteCommand) 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.RequireArgs.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)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
if _, ok := err.(actionerror.RouteNotFoundError); ok {
cmd.UI.DisplayText("Can not share route:")
return err
}
}

destinationOrgName := cmd.DestinationOrg

if destinationOrgName == "" {
destinationOrgName = cmd.Config.TargetedOrganizationName()
}

destinationOrg, warnings, err := cmd.Actor.GetOrganizationByName(destinationOrgName)

if err != nil {
if _, ok := err.(actionerror.OrganizationNotFoundError); ok {
cmd.UI.DisplayText("Can not share route:")
return err
}
}

targetedSpace, warnings, err := cmd.Actor.GetSpaceByNameAndOrganization(cmd.DestinationSpace, destinationOrg.GUID)
if err != nil {
if _, ok := err.(actionerror.SpaceNotFoundError); ok {
cmd.UI.DisplayText("Can not share route:")
return err
}
}

url := desiredURL(domain.Name, cmd.Hostname, path, 0)
cmd.UI.DisplayTextWithFlavor("Sharing route {{.URL}} to space {{.DestinationSpace}} as {{.User}}",
map[string]interface{}{
"URL": url,
"DestinationSpace": cmd.DestinationSpace,
"User": user.Name,
})
warnings, err = cmd.Actor.ShareRoute(
route.GUID,
targetedSpace.GUID,
)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayOK()

return nil
}
Loading

0 comments on commit 17feaa9

Please sign in to comment.