Skip to content

Commit

Permalink
Merge pull request #20 from jsickcodes/support-dashboards
Browse files Browse the repository at this point in the history
Support bucket mappings for dashboards
  • Loading branch information
jonathansick authored Jun 25, 2022
2 parents 24c3fb3 + af05e1c commit f9ef208
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 22 deletions.
2 changes: 1 addition & 1 deletion manifests/base/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: Kustomization

images:
- name: "ghcr.io/jsickcodes/ltd-proxy"
newTag: 0.3.0
newTag: 0.4.0

resources:
- configmap.yaml
Expand Down
96 changes: 76 additions & 20 deletions src/ltdproxy/urlmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,95 @@

from __future__ import annotations

from typing import List

__all__ = ["map_s3_path"]


def map_s3_path(bucket_prefix: str, request_path: str) -> str:
"""Map a request URL to an S3 bucket key."""
# decompose the path into the project and whether it is a /v/ edition or
# not
path_parts = create_bucket_path(request_path)
if bucket_prefix:
print("inserting bucket prefix")
path_parts.insert(0, bucket_prefix)
else:
print(f"No bucket prefix: {bucket_prefix}.")

bucket_path = "/".join(path_parts)
bucket_path = bucket_path.rstrip("/") # happens if edition_path is ""

return bucket_path


def create_bucket_path(request_path: str) -> List[str]:
parts = request_path.split("/")
parts_count = len(parts)
project_name = parts[0].lower()

if (len(parts) >= 3) and parts[1].lower() == "v":
edition_name = parts[2]
edition_path = "/".join(parts[3:])
if parts_count == 1:
return [project_name, "v", "__main"]
elif parts[1].lower() == "v":
if parts_count == 2:
return [project_name, "v"]
elif parts_count == 3:
if parts[2] == "":
return [project_name, "v", "index.html"]
else:
return [project_name, "v", parts[2]]
else:
return create_edition_path(
project=project_name, edition=parts[2], path=parts[3:]
)
elif parts[1].lower() == "builds":
if parts_count == 2:
return [project_name, "builds"]
elif parts_count == 3:
if parts[2] == "":
return [project_name, "builds", "index.html"]
else:
return [project_name, "builds", parts[2]]
else:
return create_build_path(
project=project_name, build=parts[2], path=parts[3:]
)
elif parts[1] == "_dashboard-assets":
return parts
else:
edition_name = "__main" # default edition
edition_path = "/".join(parts[1:])
edition_name = "__main"
return create_edition_path(
project=project_name, edition=edition_name, path=parts[1:]
)

# if edition_path == "" or edition_path.endswith("/"):
if request_path.endswith("/"):
edition_path = f"{edition_path}index.html"

if bucket_prefix == "":
path_parts = [project_name, "v", edition_name, edition_path]
def create_edition_path(
*, project: str, edition: str, path: List[str]
) -> List[str]:
if path[-1] == "":
path_str = f'{"/".join(path)}index.html'
else:
path_parts = [
bucket_prefix,
project_name,
"v",
edition_name,
edition_path,
]
path_str = "/".join(path)

bucket_path = "/".join(path_parts)
bucket_path = bucket_path.rstrip("/") # happens if edition_path is ""
return [
project,
"v",
edition,
path_str,
]

return bucket_path

def create_build_path(
*, project: str, build: str, path: List[str]
) -> List[str]:
if path[-1] == "":
path_str = f'{"/".join(path)}index.html'
else:
path_str = "/".join(path)

return [
project,
"builds",
build,
path_str,
]
40 changes: 40 additions & 0 deletions tests/urlmap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,36 @@
"myproject/v/dev/a/b/",
"myproject/v/dev/a/b/index.html",
),
(
"",
"myproject/v/",
"myproject/v/index.html",
),
(
"",
"myproject/v",
"myproject/v",
),
(
"",
"myproject/v/index.html",
"myproject/v/index.html",
),
(
"",
"myproject/builds/",
"myproject/builds/index.html",
),
(
"",
"myproject/builds",
"myproject/builds",
),
(
"",
"myproject/builds/index.html",
"myproject/builds/index.html",
),
(
"prefix",
"myproject/",
Expand All @@ -70,6 +100,16 @@
"myproject/v/dev/index.html",
"prefix/myproject/v/dev/index.html",
),
(
"",
"myproject/_dashboard-assets/app.css",
"myproject/_dashboard-assets/app.css",
),
(
"prefix",
"myproject/_dashboard-assets/app.css",
"prefix/myproject/_dashboard-assets/app.css",
),
],
)
def test_map_s3_path(
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ setenv =
LTDPROXY_AUTH_CONFIG = tests/githubauth.example.yaml
LTDPROXY_REWRITES_CONFIG = tests/rewrites.example.yaml
commands =
pytest --cov=ltdproxy --cov-branch --cov-report= {posargs}
pytest -vv --cov=ltdproxy --cov-branch --cov-report= {posargs}

[testenv:coverage-report]
description = Compile coverage from each test run.
Expand Down

0 comments on commit f9ef208

Please sign in to comment.