Skip to content

Commit 8216d70

Browse files
authored
Merge pull request #26 from platformsh/routes-utils
Add useful route filtering utilities
2 parents 24f41c6 + c37813b commit 8216d70

5 files changed

Lines changed: 289 additions & 94 deletions

File tree

Pipfile.lock

Lines changed: 45 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

platformshconfig/config.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,54 @@ def routes(self):
284284
)
285285
return self._routesDef
286286

287+
def get_primary_route(self):
288+
"""Returns the primary route.
289+
290+
The primary route is the one marked primary in `routes.yaml`, or else
291+
the first non-direct route in that file if none are marked.
292+
293+
Returns:
294+
The route definition. The generated URL of the route is added as a "url" key.
295+
296+
"""
297+
for (url, route) in self.routes().items():
298+
if route["primary"]:
299+
return route
300+
raise KeyError("No primary route found. This isn't supposed to happen.")
301+
302+
def get_upstream_routes(self, app_name=None):
303+
"""Returns just those routes that point to a valid upstream.
304+
305+
The method is similar to routes(), but filters out redirect routes that are rarely
306+
useful for app configuration. If desired it can also filter to just those routes
307+
whose upstream is a given application name. To retrieve routes that point to the
308+
current application where the code is being run, use:
309+
310+
routes = config.get_upstream_routes(config._applicationName)
311+
312+
Args:
313+
app_name (string|None):
314+
The name of the upstream app on which to filter, if any.
315+
316+
Returns:
317+
A dictionary of route definitions.
318+
319+
"""
320+
if app_name:
321+
# On Dedicated, the upstream name sometimes is `app:http` instead of just `app`.
322+
# If no name is specified then don't bother checking.
323+
return {
324+
url: route
325+
for url, route in self.routes().items()
326+
if route["type"] == "upstream" and app_name == route["upstream"].split(":")[0]
327+
}
328+
else:
329+
return {
330+
url: route
331+
for url, route in self.routes().items()
332+
if route["type"] == "upstream"
333+
}
334+
287335
def get_route(self, route_id):
288336
"""Get route definition by route ID.
289337

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
cwd = os.path.abspath(os.path.dirname(__file__))
1414

15-
VERSION = "2.2.3"
15+
VERSION = "2.2.4"
1616

1717
with open('README.md', 'r', encoding='utf-8') as f:
1818
__readme__ = f.read()

tests/test_config.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,44 @@ def test_get_non_existent_route_throws_exception(self):
117117
with self.assertRaises(KeyError):
118118
config.get_route('missing')
119119

120+
def test_primary_route_returns_correct_route(self):
121+
122+
config = Config(self.mockEnvironmentDeploy)
123+
route = config.get_primary_route()
124+
125+
self.assertEqual("https://www.{default}/", route["original_url"])
126+
127+
def test_upstream_routes(self):
128+
129+
config = Config(self.mockEnvironmentDeploy)
130+
routes = config.get_upstream_routes()
131+
132+
self.assertEqual(len(routes), 3)
133+
self.assertTrue("https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/" in routes)
134+
self.assertEqual("https://www.{default}/", routes["https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/"]["original_url"])
135+
136+
def test_upstream_routes_for_app(self):
137+
138+
config = Config(self.mockEnvironmentDeploy)
139+
routes = config.get_upstream_routes("app")
140+
141+
self.assertEqual(len(routes), 2)
142+
self.assertTrue("https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/" in routes)
143+
self.assertEqual("https://www.{default}/", routes["https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/"]["original_url"])
144+
145+
def test_upstream_routes_for_app_on_dedicated(self):
146+
env = self.mockEnvironmentDeploy
147+
routeData = self.loadJsonFile('PLATFORM_ROUTES')
148+
routeData['https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/']['upstream'] = 'app:http'
149+
env['PLATFORM_ROUTES'] = self.encode(routeData)
150+
151+
config = Config(env)
152+
routes = config.get_upstream_routes("app")
153+
154+
self.assertEqual(len(routes), 2)
155+
self.assertTrue("https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/" in routes)
156+
self.assertEqual("https://www.{default}/", routes["https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/"]["original_url"])
157+
120158
def test_onenterprise_returns_true_on_enterprise(self):
121159

122160
env = self.mockEnvironmentDeploy

0 commit comments

Comments
 (0)