added list bucket files & refactor serve.py to take new storage logic

This commit is contained in:
Julius Unverfehrt 2022-02-24 16:26:10 +01:00
parent b20d6fd26b
commit 7d6dbabf30
11 changed files with 43 additions and 9 deletions

View File

@ -37,7 +37,6 @@ probing_webserver:
port: $PROBING_WEBSERVER_PORT|8080 # Probe webserver port port: $PROBING_WEBSERVER_PORT|8080 # Probe webserver port
mode: $PROBING_WEBSERVER_MODE|production # webserver mode: {development, production} mode: $PROBING_WEBSERVER_MODE|production # webserver mode: {development, production}
test: test:
minio: minio:
s3: s3:
@ -54,3 +53,5 @@ test:
azure: azure:
azure_blob_storage: azure_blob_storage:
connection_string: "DefaultEndpointsProtocol=https;AccountName=iqserdevelopment;AccountKey=4imAbV9PYXaztSOMpIyAClg88bAZCXuXMGJG0GA1eIBpdh2PlnFGoRBnKqLy2YZUSTmZ3wJfC7tzfHtuC6FEhQ==;EndpointSuffix=core.windows.net" connection_string: "DefaultEndpointsProtocol=https;AccountName=iqserdevelopment;AccountKey=4imAbV9PYXaztSOMpIyAClg88bAZCXuXMGJG0GA1eIBpdh2PlnFGoRBnKqLy2YZUSTmZ3wJfC7tzfHtuC6FEhQ==;EndpointSuffix=core.windows.net"
bucket: "pyinfra-test-bucket"

View File

@ -28,3 +28,7 @@ class StorageAdapter(ABC):
@abstractmethod @abstractmethod
def clear_bucket(self, bucket_name): def clear_bucket(self, bucket_name):
pass pass
@abstractmethod
def list_bucket_files(self, bucket_name):
pass

View File

@ -1,5 +1,7 @@
from itertools import repeat
import logging import logging
from functools import wraps from functools import wraps
from operator import attrgetter
from azure.storage.blob import ContainerClient, BlobServiceClient from azure.storage.blob import ContainerClient, BlobServiceClient
from retry import retry from retry import retry
@ -70,3 +72,8 @@ class AzureStorageAdapter(StorageAdapter):
container_client = self.__client.get_container_client(bucket_name) container_client = self.__client.get_container_client(bucket_name)
blobs = container_client.list_blobs() blobs = container_client.list_blobs()
container_client.delete_blobs(*blobs) container_client.delete_blobs(*blobs)
def list_bucket_files(self, bucket_name):
container_client = self.__provide_container_client(bucket_name)
blobs = container_client.list_blobs()
return zip(repeat(bucket_name), map(attrgetter("name"), blobs))

View File

@ -1,5 +1,7 @@
import io import io
from itertools import repeat
import logging import logging
from operator import attrgetter
from minio import Minio from minio import Minio
@ -47,3 +49,7 @@ class S3StorageAdapter(StorageAdapter):
objects = self.__client.list_objects(bucket_name) objects = self.__client.list_objects(bucket_name)
for obj in objects: for obj in objects:
self.__client.remove_object(bucket_name, obj.object_name) self.__client.remove_object(bucket_name, obj.object_name)
def list_bucket_files(self, bucket_name):
objs = self.__client.list_objects(bucket_name)
return zip(repeat(bucket_name), map(attrgetter("object_name"), objs))

View File

@ -20,6 +20,9 @@ class Storage:
def clear_bucket(self, bucket_name): def clear_bucket(self, bucket_name):
return self.__adapter.clear_bucket(bucket_name) return self.__adapter.clear_bucket(bucket_name)
def list_bucket_files(self, bucket_name):
return self.__adapter.list_bucket_files(bucket_name)
# import abc # import abc
# import gzip # import gzip

View File

@ -25,3 +25,6 @@ class StorageAdapterMock(StorageAdapter):
def clear_bucket(self, bucket_name): def clear_bucket(self, bucket_name):
return self.__client.clear_bucket(bucket_name) return self.__client.clear_bucket(bucket_name)
def list_bucket_files(self, bucket_name):
return self.__client.list_bucket_files(bucket_name)

View File

@ -1,3 +1,6 @@
from itertools import repeat
class StorageClientMock: class StorageClientMock:
def __init__(self): def __init__(self):
self.__data = {} self.__data = {}
@ -19,3 +22,6 @@ class StorageClientMock:
def clear_bucket(self, bucket_name): def clear_bucket(self, bucket_name):
self.__data[bucket_name] = {} self.__data[bucket_name] = {}
def list_bucket_files(self, bucket_name):
return zip(repeat(bucket_name), self.__data[bucket_name])

View File

@ -36,6 +36,12 @@ class TestStorage:
storage.make_bucket(bucket_name) storage.make_bucket(bucket_name)
assert storage.bucket_exists(bucket_name) assert storage.bucket_exists(bucket_name)
def test_listing_bucket_files_yields_all_files_in_bucket(self, storage, bucket_name):
storage.put(bucket_name, "file1", b"content 1")
storage.put(bucket_name, "file2", b"content 2")
full_names_recived = storage.list_bucket_files(bucket_name)
assert {(bucket_name, "file1"), (bucket_name, "file2")} == {*full_names_recived}
def get_adapter(client_name, s3_backend): def get_adapter(client_name, s3_backend):
if client_name == "mock": if client_name == "mock":

View File

@ -1,5 +1,4 @@
[pytest] [pytest]
log_cli = 1 log_cli = 1
log_cli_level = DEBUG log_cli_level = DEBUG
addopts = --docker-compose=docker-compose.yaml

View File

@ -1,13 +1,13 @@
import json import json
from pyinfra.config import CONFIG from pyinfra.config import CONFIG
from pyinfra.storage.minio import MinioHandle
from pyinfra.rabbitmq import make_channel, declare_queue, make_connection from pyinfra.rabbitmq import make_channel, declare_queue, make_connection
from pyinfra.storage.storages import get_s3_storage
def build_message_bodies(): def build_message_bodies():
minio_client = MinioHandle() minio_client = get_s3_storage()
for dossier_id, pdf_name in minio_client.list_folders_and_files(): for dossier_id, pdf_name in minio_client.list_bucket_files(CONFIG.test.bucket):
file_id = pdf_name.split(".")[0] file_id = pdf_name.split(".")[0]
yield json.dumps({"dossierId": dossier_id, "fileId": file_id}) yield json.dumps({"dossierId": dossier_id, "fileId": file_id})

View File

@ -13,17 +13,16 @@ from pyinfra.consume import consume, ConsumerError
from pyinfra.core import make_payload_processor, make_storage_data_loader, make_analyzer from pyinfra.core import make_payload_processor, make_storage_data_loader, make_analyzer
from pyinfra.exceptions import UnknownStorageBackend from pyinfra.exceptions import UnknownStorageBackend
from pyinfra.flask import run_probing_webserver, set_up_probing_webserver from pyinfra.flask import run_probing_webserver, set_up_probing_webserver
from pyinfra.storage.azure_blob_storage import AzureBlobStorageHandle from pyinfra.storage.storages import get_azure_storage, get_s3_storage
from pyinfra.storage.minio import MinioHandle
def get_storage(): def get_storage():
storage_backend = CONFIG.service.storage_backend storage_backend = CONFIG.service.storage_backend
if storage_backend == "s3": if storage_backend == "s3":
storage = MinioHandle() storage = get_s3_storage()
elif storage_backend == "azure": elif storage_backend == "azure":
storage = AzureBlobStorageHandle() storage = get_azure_storage()
else: else:
raise UnknownStorageBackend(f"Unknown storage backend '{storage_backend}'.") raise UnknownStorageBackend(f"Unknown storage backend '{storage_backend}'.")