Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add move-route command to cf cli #2302

Merged
merged 20 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions actor/v7action/cloud_controller_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ type CloudControllerClient interface {
ShareServiceInstanceToSpaces(serviceInstanceGUID string, spaceGUIDs []string) (resources.RelationshipList, ccv3.Warnings, error)
ShareRoute(routeGUID string, spaceGUID string) (ccv3.Warnings, error)
TargetCF(settings ccv3.TargetSettings)
TransferRouteOwner(routeGUID string, spaceGUID string) (ccv3.Warnings, error)
UnbindSecurityGroupRunningSpace(securityGroupGUID string, spaceGUID string) (ccv3.Warnings, error)
UnbindSecurityGroupStagingSpace(securityGroupGUID string, spaceGUID string) (ccv3.Warnings, error)
UnmapRoute(routeGUID string, destinationGUID string) (ccv3.Warnings, error)
Expand Down
5 changes: 5 additions & 0 deletions actor/v7action/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ func (actor Actor) GetApplicationRoutes(appGUID string) ([]resources.Route, Warn
return routes, allWarnings, nil
}

func (actor Actor) TransferRouteOwner(routeGUID string, spaceGUID string) (Warnings, error) {
warnings, err := actor.CloudControllerClient.TransferRouteOwner(routeGUID, spaceGUID)
return Warnings(warnings), err
}

func getDomainName(fullURL, host, path string, port int) string {
domainWithoutHost := strings.TrimPrefix(fullURL, host+".")
domainWithoutPath := strings.TrimSuffix(domainWithoutHost, path)
Expand Down
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
Expand Up @@ -130,6 +130,7 @@ const (
PatchSpaceFeaturesRequest = "PatchSpaceFeatures"
PatchSpaceQuotaRequest = "PatchSpaceQuota"
PatchStackRequest = "PatchStack"
PatchTransferRouteOwnerRequest = "PatchTransferRouteOwnerRequest"
PostApplicationActionApplyManifest = "PostApplicationActionApplyM"
PostApplicationActionRestartRequest = "PostApplicationActionRestart"
PostApplicationActionStartRequest = "PostApplicationActionStart"
Expand Down Expand Up @@ -279,6 +280,7 @@ var APIRoutes = map[string]Route{
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},
PatchTransferRouteOwnerRequest: {Path: "/v3/routes/:route-guid/transfer_owner", Method: http.MethodPatch},
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},
Expand Down
25 changes: 25 additions & 0 deletions api/cloudcontroller/ccv3/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,28 @@ func (client Client) ShareRoute(routeGUID string, spaceGUID string) (Warnings, e
})
return warnings, err
}

func (client Client) TransferRouteOwner(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.PatchTransferRouteOwnerRequest,
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
Expand Up @@ -173,6 +173,7 @@ type commandList struct {
Target v7.TargetCommand `command:"target" alias:"t" description:"Set or view the targeted org or space"`
Tasks v7.TasksCommand `command:"tasks" description:"List tasks of an app"`
TerminateTask v7.TerminateTaskCommand `command:"terminate-task" description:"Terminate a running task of an app"`
TransferRouteOwner v7.TransferRouteOwnerCommand `command:"transfer-route-owner" description:"Assign a route to a different space"`
UnbindRouteService v7.UnbindRouteServiceCommand `command:"unbind-route-service" alias:"urs" description:"Unbind a service instance from an HTTP route"`
UnbindRunningSecurityGroup v7.UnbindRunningSecurityGroupCommand `command:"unbind-running-security-group" description:"Unbind a security group from the set of security groups for running applications globally"`
UnbindSecurityGroup v7.UnbindSecurityGroupCommand `command:"unbind-security-group" description:"Unbind a security group from a space"`
Expand Down
1 change: 1 addition & 0 deletions command/common/internal/help_all_display.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ var HelpCategoryList = []HelpCategory{
{"delete-orphaned-routes"},
{"update-destination"},
{"share-route"},
{"transfer-route-owner"},
},
},
{
Expand Down
1 change: 1 addition & 0 deletions command/v7/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ type Actor interface {
StartApplication(appGUID string) (v7action.Warnings, error)
StopApplication(appGUID string) (v7action.Warnings, error)
TerminateTask(taskGUID string) (resources.Task, v7action.Warnings, error)
TransferRouteOwner(routeGUID string, spaceGUID string) (v7action.Warnings, error)
UnbindSecurityGroup(securityGroupName string, orgGUID string, spaceGUID string, lifecycle constant.SecurityGroupLifecycle) (v7action.Warnings, error)
UnmapRoute(routeGUID string, destinationGUID string) (v7action.Warnings, error)
UnsetEnvironmentVariableByApplicationNameAndSpace(appName string, spaceGUID string, EnvironmentVariableName string) (v7action.Warnings, error)
Expand Down
101 changes: 101 additions & 0 deletions command/v7/transfer_route_owner_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 TransferRouteOwnerCommand 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"`
DestinationSpace string `short:"s" description:"The space of the destination app (Default: targeted space)"`
DestinationOrg string `short:"o" description:"The org of the destination app (Default: targeted org)"`

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

func (cmd TransferRouteOwnerCommand) Usage() string {
return `
Transfers the ownership of a route to a another space:
CF_NAME transfer-route-owner DOMAIN [--hostname HOSTNAME] [--path PATH] -s OTHER_SPACE [-o OTHER_ORG]`
}

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

func (cmd TransferRouteOwnerCommand) 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 transfer ownership of 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 transfer ownership of 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 transfer ownership of route:")
return err
}
}

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

return nil
}
Loading