Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit 8a62830

Browse files
authored
reduce how often list_containers is called (#196)
1 parent b675ee7 commit 8a62830

File tree

5 files changed

+46
-30
lines changed

5 files changed

+46
-30
lines changed

src/api-service/__app__/containers/__init__.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ..onefuzzlib.azure.containers import (
1616
create_container,
1717
delete_container,
18+
get_container_metadata,
1819
get_container_sas_url,
1920
get_containers,
2021
)
@@ -28,28 +29,29 @@ def get(req: func.HttpRequest) -> func.HttpResponse:
2829

2930
if isinstance(request, Error):
3031
return not_ok(request, context="container get")
31-
32-
containers = get_containers()
33-
3432
if request is not None:
35-
if request.name in containers:
36-
info = ContainerInfo(
37-
name=request.name,
38-
sas_url=get_container_sas_url(
39-
request.name,
40-
read=True,
41-
write=True,
42-
create=True,
43-
delete=True,
44-
list=True,
45-
),
46-
metadata=containers[request.name],
33+
metadata = get_container_metadata(request.name)
34+
if metadata is None:
35+
return not_ok(
36+
Error(code=ErrorCode.INVALID_REQUEST, errors=["invalid container"]),
37+
context=request.name,
4738
)
48-
return ok(info)
49-
return not_ok(
50-
Error(code=ErrorCode.INVALID_REQUEST, errors=["invalid container"]),
51-
context=request.name,
39+
40+
info = ContainerInfo(
41+
name=request.name,
42+
sas_url=get_container_sas_url(
43+
request.name,
44+
read=True,
45+
write=True,
46+
create=True,
47+
delete=True,
48+
list=True,
49+
),
50+
metadata=metadata,
5251
)
52+
return ok(info)
53+
54+
containers = get_containers()
5355

5456
container_info = []
5557
for name in containers:

src/api-service/__app__/download/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
from onefuzztypes.enums import ErrorCode
88
from onefuzztypes.models import Error, FileEntry
99

10-
from ..onefuzzlib.azure.containers import blob_exists, get_containers, get_file_sas_url
10+
from ..onefuzzlib.azure.containers import (
11+
blob_exists,
12+
container_exists,
13+
get_file_sas_url,
14+
)
1115
from ..onefuzzlib.request import not_ok, parse_uri, redirect
1216

1317

@@ -16,7 +20,7 @@ def get(req: func.HttpRequest) -> func.HttpResponse:
1620
if isinstance(request, Error):
1721
return not_ok(request, context="download")
1822

19-
if request.container not in get_containers():
23+
if not container_exists(request.container):
2024
return not_ok(
2125
Error(code=ErrorCode.INVALID_REQUEST, errors=["invalid container"]),
2226
context=request.container,

src/api-service/__app__/notifications/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from onefuzztypes.models import Error
1111
from onefuzztypes.requests import NotificationCreate, NotificationGet
1212

13-
from ..onefuzzlib.azure.containers import get_containers
13+
from ..onefuzzlib.azure.containers import container_exists
1414
from ..onefuzzlib.notifications.main import Notification
1515
from ..onefuzzlib.request import not_ok, ok, parse_request
1616

@@ -29,8 +29,7 @@ def post(req: func.HttpRequest) -> func.HttpResponse:
2929
if isinstance(request, Error):
3030
return not_ok(request, context="notification create")
3131

32-
containers = get_containers()
33-
if request.container not in containers:
32+
if not container_exists(request.container):
3433
return not_ok(
3534
Error(code=ErrorCode.INVALID_REQUEST, errors=["invalid container"]),
3635
context=request.container,

src/api-service/__app__/onefuzzlib/azure/containers.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,20 @@
1010

1111
from azure.common import AzureHttpError, AzureMissingResourceHttpError
1212
from azure.storage.blob import BlobPermissions, ContainerPermissions
13+
from memoization import cached
1314

1415
from .creds import get_blob_service
1516

1617

18+
@cached(ttl=5)
19+
def container_exists(name: str, account_id: Optional[str] = None) -> bool:
20+
try:
21+
get_blob_service(account_id).get_container_properties(name)
22+
return True
23+
except AzureHttpError:
24+
return False
25+
26+
1727
def get_containers(account_id: Optional[str] = None) -> Dict[str, Dict[str, str]]:
1828
return {
1929
x.name: x.metadata
@@ -27,8 +37,7 @@ def get_container_metadata(
2737
) -> Optional[Dict[str, str]]:
2838
try:
2939
result = get_blob_service(account_id).get_container_metadata(name)
30-
if result:
31-
return cast(Dict[str, str], result)
40+
return cast(Dict[str, str], result)
3241
except AzureHttpError:
3342
pass
3443
return None

src/api-service/__app__/onefuzzlib/tasks/config.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from onefuzztypes.enums import Compare, ContainerPermission, ContainerType, TaskFeature
1212
from onefuzztypes.models import TaskConfig, TaskDefinition, TaskUnitConfig
1313

14-
from ..azure.containers import blob_exists, get_container_sas_url, get_containers
14+
from ..azure.containers import blob_exists, container_exists, get_container_sas_url
1515
from ..azure.creds import get_fuzz_storage, get_instance_url
1616
from ..azure.queue import get_queue_sas
1717
from .defs import TASK_DEFINITIONS
@@ -58,12 +58,14 @@ def check_container(
5858

5959

6060
def check_containers(definition: TaskDefinition, config: TaskConfig) -> None:
61-
all_containers = set(get_containers().keys())
61+
checked = set()
6262

6363
containers: Dict[ContainerType, List[str]] = {}
6464
for container in config.containers:
65-
if container.name not in all_containers:
66-
raise TaskConfigError("missing container: %s" % container.name)
65+
if container.name not in checked:
66+
if not container_exists(container.name):
67+
raise TaskConfigError("missing container: %s" % container.name)
68+
checked.add(container.name)
6769

6870
if container.type not in containers:
6971
containers[container.type] = []

0 commit comments

Comments
 (0)