Merge in RR/pyinfra from prefetch_adjustment to master
Squashed commit of the following:
commit 6f9d75bf49ad196bf5728386527499025ac27b3a
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Apr 21 13:47:40 2022 +0200
removed tests without much value that caused teardown problems with docker containers
commit b7ccbe20e3babbf1127ea5738a1d710d8029c90b
Merge: 51a459c 5925737
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Apr 21 13:04:34 2022 +0200
Merge branch 'sonarscan_refac' into prefetch_adjustment
commit 51a459cbf04e9884cf6b7c2c3145206ecf1a0ffb
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Apr 21 13:03:48 2022 +0200
set prefetch count to 1; removed obsolete imports
commit 592573793cdfd098012a98cfc7ab0cc1fbfd0e44
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Tue Apr 19 18:01:46 2022 +0200
refactoring; cleanup
66 lines
2.7 KiB
Python
66 lines
2.7 KiB
Python
import logging
|
|
from itertools import repeat
|
|
from operator import attrgetter
|
|
|
|
from azure.storage.blob import ContainerClient, BlobServiceClient
|
|
|
|
from pyinfra.storage.adapters.adapter import StorageAdapter
|
|
|
|
logger = logging.getLogger(__name__)
|
|
logging.getLogger("azure").setLevel(logging.WARNING)
|
|
logging.getLogger("urllib3").setLevel(logging.WARNING)
|
|
|
|
|
|
class AzureStorageAdapter(StorageAdapter):
|
|
def __init__(self, client):
|
|
super().__init__(client=client)
|
|
self.__client: BlobServiceClient = self._StorageAdapter__client
|
|
|
|
def has_bucket(self, bucket_name):
|
|
container_client = self.__client.get_container_client(bucket_name)
|
|
return container_client.exists()
|
|
|
|
def make_bucket(self, bucket_name):
|
|
container_client = self.__client.get_container_client(bucket_name)
|
|
container_client if container_client.exists() else self.__client.create_container(bucket_name)
|
|
|
|
def __provide_container_client(self, bucket_name) -> ContainerClient:
|
|
self.make_bucket(bucket_name)
|
|
container_client = self.__client.get_container_client(bucket_name)
|
|
return container_client
|
|
|
|
def put_object(self, bucket_name, object_name, data):
|
|
logger.debug(f"Uploading '{object_name}'...")
|
|
container_client = self.__provide_container_client(bucket_name)
|
|
blob_client = container_client.get_blob_client(object_name)
|
|
blob_client.upload_blob(data, overwrite=True)
|
|
|
|
def get_object(self, bucket_name, object_name):
|
|
logger.debug(f"Downloading '{object_name}'...")
|
|
container_client = self.__provide_container_client(bucket_name)
|
|
blob_client = container_client.get_blob_client(object_name)
|
|
blob_data = blob_client.download_blob()
|
|
return blob_data.readall()
|
|
|
|
def get_all_objects(self, bucket_name):
|
|
|
|
container_client = self.__provide_container_client(bucket_name)
|
|
blobs = container_client.list_blobs()
|
|
for blob in blobs:
|
|
logger.debug(f"Downloading '{blob.name}'...")
|
|
blob_client = container_client.get_blob_client(blob)
|
|
blob_data = blob_client.download_blob()
|
|
data = blob_data.readall()
|
|
yield data
|
|
|
|
def clear_bucket(self, bucket_name):
|
|
logger.debug(f"Clearing Azure container '{bucket_name}'...")
|
|
container_client = self.__client.get_container_client(bucket_name)
|
|
blobs = container_client.list_blobs()
|
|
container_client.delete_blobs(*blobs)
|
|
|
|
def get_all_object_names(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))
|