github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/integration/helpers/route.go (about) 1 package helpers 2 3 import ( 4 "fmt" 5 "path" 6 "regexp" 7 "strings" 8 9 "github.com/LukasHeimann/cloudfoundrycli/v8/resources" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/gbytes" 12 . "github.com/onsi/gomega/gexec" 13 ) 14 15 // MinTestPort should be defined by the CF router group for integration tests. 16 const MinTestPort = 1024 17 18 // MaxTestPort should be defined by the CF router group for integration tests. 19 const MaxTestPort = 1034 20 21 // FindOrCreateTCPRouterGroup uses the routing API to find a router group with name 22 // INTEGRATION-TCP-NODE-<node>, or create one if it does not exist. Returns the name of 23 // the router group. 24 func FindOrCreateTCPRouterGroup(node int) string { 25 routerGroupName := fmt.Sprintf("INTEGRATION-TCP-NODE-%d", node) 26 27 session := CF("curl", fmt.Sprintf("/routing/v1/router_groups?name=%s", routerGroupName)) 28 Eventually(session).Should(Exit(0)) 29 doesNotExist := regexp.MustCompile("ResourceNotFoundError") 30 if doesNotExist.Match(session.Out.Contents()) { 31 jsonBody := fmt.Sprintf(`{"name": "%s", "type": "tcp", "reservable_ports": "%d-%d"}`, routerGroupName, MinTestPort, MaxTestPort) 32 session := CF("curl", "-d", jsonBody, "-X", "POST", "/routing/v1/router_groups") 33 Eventually(session).Should(Say(`"name":\s*"%s"`, routerGroupName)) 34 Eventually(session).Should(Say(`"type":\s*"tcp"`)) 35 Eventually(session).Should(Exit(0)) 36 } 37 38 return routerGroupName 39 } 40 41 // Route represents a route. 42 type Route struct { 43 Domain string 44 Host string 45 Path string 46 Port int 47 Space string 48 } 49 50 // NewRoute constructs a route with given space, domain, hostname, and path. 51 func NewRoute(space string, domain string, hostname string, path string) Route { 52 return Route{ 53 Space: space, 54 Domain: domain, 55 Host: hostname, 56 Path: path, 57 } 58 } 59 60 // NewTCPRoute constructs a TCP route with given space, domain, and port. 61 func NewTCPRoute(space string, domain string, port int) Route { 62 return Route{ 63 Space: space, 64 Domain: domain, 65 Port: port, 66 } 67 } 68 69 // Create creates a route using the 'cf create-route' command. 70 func (r Route) Create() { 71 if r.Port != 0 { 72 Eventually(CF("create-route", r.Space, r.Domain, "--port", fmt.Sprint(r.Port))).Should(Exit(0)) 73 } else { 74 Eventually(CF("create-route", r.Space, r.Domain, "--hostname", r.Host, "--path", r.Path)).Should(Exit(0)) 75 } 76 } 77 78 // Create creates a route using the 'cf create-route' command. 79 func (r Route) V7Create() { 80 if r.Port != 0 { 81 Eventually(CF("create-route", r.Domain, "--port", fmt.Sprint(r.Port))).Should(Exit(0)) 82 } else { 83 Eventually(CF("create-route", r.Domain, "--hostname", r.Host, "--path", r.Path)).Should(Exit(0)) 84 } 85 } 86 87 // Delete deletes a route using the 'cf delete-route' command. 88 func (r Route) Delete() { 89 if r.Port != 0 { 90 Eventually(CF("delete-route", r.Domain, "--port", fmt.Sprint(r.Port))).Should(Exit(0)) 91 } else { 92 Eventually(CF("delete-route", r.Domain, "--hostname", r.Host, "--path", r.Path, "-f")).Should(Exit(0)) 93 } 94 } 95 96 // String stringifies a route (e.g. "host.domain.com:port/path") 97 func (r Route) String() string { 98 routeString := r.Domain 99 100 if r.Port != 0 { 101 routeString = fmt.Sprintf("%s:%d", routeString, r.Port) 102 } 103 104 if r.Host != "" { 105 routeString = fmt.Sprintf("%s.%s", r.Host, routeString) 106 } 107 108 if r.Path != "" { 109 routeString = path.Join(routeString, r.Path) 110 } 111 112 return routeString 113 } 114 115 func (r Route) GUID() string { 116 var domainReceiver struct { 117 Domains []resources.Domain `json:"resources"` 118 } 119 Curl(&domainReceiver, "/v3/domains?names=%s", r.Domain) 120 Expect(domainReceiver.Domains).To(HaveLen(1)) 121 122 query := []string{fmt.Sprintf("domain_guids=%s", domainReceiver.Domains[0].GUID)} 123 if r.Host != "" { 124 query = append(query, fmt.Sprintf("hosts=%s", r.Host)) 125 } 126 if r.Path != "" { 127 path := r.Path 128 if !strings.HasPrefix(path, "/") { 129 path = "/" + path 130 } 131 query = append(query, fmt.Sprintf("paths=%s", path)) 132 } 133 if r.Port != 0 { 134 query = append(query, fmt.Sprintf("ports=%d", r.Port)) 135 } 136 137 var routeReceiver struct { 138 Routes []resources.Route `json:"resources"` 139 } 140 Curl(&routeReceiver, "/v3/routes?%s", strings.Join(query, "&")) 141 Expect(routeReceiver.Routes).To(HaveLen(1)) 142 143 return routeReceiver.Routes[0].GUID 144 }