github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/actor/v2action/route.go (about) 1 package v2action 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 7 ) 8 9 // Route represents a CLI Route. 10 type Route struct { 11 GUID string 12 Host string 13 Domain string 14 Path string 15 Port int 16 } 17 18 // String formats the route in a human readable format. 19 func (r Route) String() string { 20 routeString := r.Domain 21 22 if r.Port != 0 { 23 routeString = fmt.Sprintf("%s:%d", routeString, r.Port) 24 return routeString 25 } 26 27 if r.Host != "" { 28 routeString = fmt.Sprintf("%s.%s", r.Host, routeString) 29 } 30 31 if r.Path != "" { 32 routeString = fmt.Sprintf("%s%s", routeString, r.Path) 33 } 34 35 return routeString 36 } 37 38 // OrphanedRoutesNotFoundError is an error wrapper that represents the case 39 // when no orphaned routes are found. 40 type OrphanedRoutesNotFoundError struct{} 41 42 // Error method to display the error message. 43 func (e OrphanedRoutesNotFoundError) Error() string { 44 return fmt.Sprintf("No orphaned routes were found.") 45 } 46 47 // GetOrphanedRoutesBySpace returns a list of orphaned routes associated with 48 // the provided Space GUID. 49 func (actor Actor) GetOrphanedRoutesBySpace(spaceGUID string) ([]Route, Warnings, error) { 50 var ( 51 orphanedRoutes []Route 52 allWarnings Warnings 53 ) 54 55 routes, warnings, err := actor.GetSpaceRoutes(spaceGUID, nil) 56 allWarnings = append(allWarnings, warnings...) 57 if err != nil { 58 return nil, allWarnings, err 59 } 60 61 for _, route := range routes { 62 apps, warnings, err := actor.GetRouteApplications(route.GUID, nil) 63 allWarnings = append(allWarnings, warnings...) 64 if err != nil { 65 return nil, allWarnings, err 66 } 67 68 if len(apps) == 0 { 69 orphanedRoutes = append(orphanedRoutes, route) 70 } 71 } 72 73 if len(orphanedRoutes) == 0 { 74 return nil, allWarnings, OrphanedRoutesNotFoundError{} 75 } 76 77 return orphanedRoutes, allWarnings, nil 78 } 79 80 // GetApplicationRoutes returns a list of routes associated with the provided Application GUID 81 func (actor Actor) GetApplicationRoutes(applicationGUID string, query []ccv2.Query) ([]Route, Warnings, error) { 82 var allWarnings Warnings 83 ccv2Routes, warnings, err := actor.CloudControllerClient.GetApplicationRoutes(applicationGUID, query) 84 allWarnings = append(allWarnings, warnings...) 85 if err != nil { 86 return nil, allWarnings, err 87 } 88 89 routes, domainWarnings, err := actor.applyDomain(ccv2Routes) 90 91 return routes, append(allWarnings, domainWarnings...), err 92 } 93 94 // GetSpaceRoutes returns a list of routes associated with the provided Space GUID 95 func (actor Actor) GetSpaceRoutes(spaceGUID string, query []ccv2.Query) ([]Route, Warnings, error) { 96 var allWarnings Warnings 97 ccv2Routes, warnings, err := actor.CloudControllerClient.GetSpaceRoutes(spaceGUID, query) 98 allWarnings = append(allWarnings, warnings...) 99 if err != nil { 100 return nil, allWarnings, err 101 } 102 103 routes, domainWarnings, err := actor.applyDomain(ccv2Routes) 104 105 return routes, append(allWarnings, domainWarnings...), err 106 } 107 108 // DeleteRoute deletes the Route associated with the provided Route GUID. 109 func (actor Actor) DeleteRoute(routeGUID string) (Warnings, error) { 110 warnings, err := actor.CloudControllerClient.DeleteRoute(routeGUID) 111 return Warnings(warnings), err 112 } 113 114 func (actor Actor) applyDomain(ccv2Routes []ccv2.Route) ([]Route, Warnings, error) { 115 var routes []Route 116 var allWarnings Warnings 117 118 for _, ccv2Route := range ccv2Routes { 119 domain, warnings, err := actor.GetDomain(ccv2Route.DomainGUID) 120 allWarnings = append(allWarnings, warnings...) 121 if err != nil { 122 return nil, allWarnings, err 123 } 124 routes = append(routes, Route{ 125 GUID: ccv2Route.GUID, 126 Host: ccv2Route.Host, 127 Domain: domain.Name, 128 Path: ccv2Route.Path, 129 Port: ccv2Route.Port, 130 }) 131 } 132 133 return routes, allWarnings, nil 134 }