Skip to content

Commit

Permalink
fixup! move cloud endpoint validation to class
Browse files Browse the repository at this point in the history
  • Loading branch information
jeqo committed Jan 2, 2024
1 parent 9ff51d4 commit 9a3dd93
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
15 changes: 2 additions & 13 deletions rohmu/object_storage/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
SourceStorageModelT,
)
from rohmu.object_storage.config import ( # pylint: disable=unused-import
AZURE_ENDPOINT_SUFFIXES,
AZURE_MAX_BLOCK_SIZE as MAX_BLOCK_SIZE,
AzureObjectStorageConfig as Config,
calculate_azure_max_block_size as calculate_max_block_size,
Expand All @@ -42,14 +43,6 @@
from azure.storage.blob._models import BlobPrefix, BlobType # type: ignore


ENDPOINT_SUFFIXES = {
None: "core.windows.net",
"germany": "core.cloudapi.de", # Azure Germany is a completely separate cloud from the regular Azure Public cloud
"china": "core.chinacloudapi.cn",
"public": "core.windows.net",
}


# Reduce Azure logging verbocity of http requests and responses
logging.getLogger("azure.core.pipeline.policies.http_logging_policy").setLevel(logging.WARNING)

Expand Down Expand Up @@ -129,11 +122,7 @@ def conn_string(
f"AccountKey={account_key}",
]
if not host and not port:
try:
endpoint_suffix = ENDPOINT_SUFFIXES[azure_cloud]
except KeyError:
raise InvalidConfigurationError(f"Unknown azure cloud {repr(azure_cloud)}")

endpoint_suffix = AZURE_ENDPOINT_SUFFIXES[azure_cloud]
conn.append(f"EndpointSuffix={endpoint_suffix}")
else:
conn.append(f"BlobEndpoint={protocol}://{host}:{port}/{account_name}")
Expand Down
15 changes: 14 additions & 1 deletion rohmu/object_storage/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from enum import Enum, unique
from pathlib import Path
from pydantic import Field, root_validator
from pydantic import Field, root_validator, validator
from rohmu.common.models import ProxyInfo, StorageDriver, StorageModel
from typing import Any, Dict, Final, Literal, Optional, TypeVar

Expand Down Expand Up @@ -42,6 +42,12 @@ def calculate_azure_max_block_size() -> int:
return max(min(int(total_mem_mib / 1000), 100), 4) * 1024 * 1024


AZURE_ENDPOINT_SUFFIXES = {
None: "core.windows.net",
"germany": "core.cloudapi.de", # Azure Germany is a completely separate cloud from the regular Azure Public cloud
"china": "core.chinacloudapi.cn",
"public": "core.windows.net",
}
# Increase block size based on host memory. Azure supports up to 50k blocks and up to 5 TiB individual
# files. Default block size is set to 4 MiB so only ~200 GB files can be uploaded. In order to get close
# to that 5 TiB increase the block size based on host memory; we don't want to use the max 100 for all
Expand Down Expand Up @@ -98,6 +104,13 @@ def host_and_port_must_be_set_together(cls, values: Dict[str, Any]) -> Dict[str,
raise ValueError("host and port must be set together")
return values

@validator("azure_cloud")
@classmethod
def valid_azure_cloud_endpoint(cls, v: str) -> str:
if v not in AZURE_ENDPOINT_SUFFIXES:
raise ValueError(f"azure_cloud must be one of {AZURE_ENDPOINT_SUFFIXES.keys()}")
return v


class GoogleObjectStorageConfig(StorageModel):
project_id: str
Expand Down
5 changes: 5 additions & 0 deletions test/object_storage/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ def test_azure_config_host_port_set_together() -> None:
AzureObjectStorageConfig(account_name="test", port=10000)


def test_valid_azure_cloud_endpoint() -> None:
with pytest.raises(ValueError):
AzureObjectStorageConfig(account_name="test", azure_cloud="invalid")


@pytest.mark.parametrize(
"host,port,is_secured,expected",
[
Expand Down

0 comments on commit 9a3dd93

Please sign in to comment.