[#3] Added generate proto script create container method
Signed-off-by: Ilyas Niyazov <i.niyazov@yadro.com>
This commit is contained in:
parent
f8465e5b99
commit
fba6eaaa9c
34 changed files with 547 additions and 108 deletions
9
frostfs_sdk/models/__init__.py
Normal file
9
frostfs_sdk/models/__init__.py
Normal file
|
@ -0,0 +1,9 @@
|
|||
from frostfs_sdk.models.dto.container import Container, ContainerId
|
||||
from frostfs_sdk.models.dto.filter import Filter
|
||||
from frostfs_sdk.models.dto.placement_policy import PlacementPolicy
|
||||
from frostfs_sdk.models.dto.replica import Replica
|
||||
from frostfs_sdk.models.dto.selector import Selector
|
||||
|
||||
from frostfs_sdk.models.enums.basic_acl import BasicAcl
|
||||
from frostfs_sdk.models.enums.filter_operation import FilterOperation
|
||||
from frostfs_sdk.models.enums.selector_clause import SelectorClause
|
|
@ -2,6 +2,8 @@ from dataclasses import dataclass, field
|
|||
from typing import Dict, Optional
|
||||
import uuid
|
||||
|
||||
from frostfs_sdk.models.dto.owner_id import OwnerId
|
||||
from frostfs_sdk.models.dto.version import Version
|
||||
from frostfs_sdk.models.enums.basic_acl import BasicAcl
|
||||
from frostfs_sdk.models.dto.placement_policy import PlacementPolicy
|
||||
|
||||
|
@ -9,15 +11,17 @@ from frostfs_sdk.models.dto.placement_policy import PlacementPolicy
|
|||
|
||||
@dataclass
|
||||
class Container:
|
||||
basicAcl: BasicAcl
|
||||
# basicAcl: BasicAcl # TODO: will remove it?
|
||||
placementPolicy: PlacementPolicy
|
||||
nonce: uuid.UUID = field(default_factory=uuid.uuid4)
|
||||
version: Optional[str] = None
|
||||
version: Optional[Version] = None
|
||||
owner_id: Optional[OwnerId] = None
|
||||
attributes: Dict[str, str] = field(default_factory=dict)
|
||||
|
||||
def __init__(self, basicAcl: BasicAcl, placementPolicy: PlacementPolicy):
|
||||
self.basicAcl = basicAcl
|
||||
def __init__(self, placementPolicy: PlacementPolicy):
|
||||
self.nonce = uuid.uuid4()
|
||||
self.placementPolicy = placementPolicy
|
||||
self.attributes = {}
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
20
frostfs_sdk/models/dto/owner_id.py
Normal file
20
frostfs_sdk/models/dto/owner_id.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
from base58 import b58decode
|
||||
from dataclasses import dataclass
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class OwnerId:
|
||||
value: str
|
||||
|
||||
def __post_init__(self):
|
||||
if not self.value or self.value.strip() == "":
|
||||
raise ValueError(f"{self.__class__.__name__} value is not present")
|
||||
|
||||
def to_hash(self) -> bytes:
|
||||
"""Decodes the Base58-encoded value into a byte array."""
|
||||
try:
|
||||
return b58decode(self.value)
|
||||
except Exception as e:
|
||||
raise ValueError(f"Failed to decode Base58 value: {self.value}") from e
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
|
@ -2,8 +2,13 @@ from dataclasses import dataclass
|
|||
from typing import List
|
||||
|
||||
from frostfs_sdk.models.dto.replica import Replica
|
||||
from frostfs_sdk.models.dto.selector import Selector
|
||||
from frostfs_sdk.models.dto.filter import Filter
|
||||
|
||||
@dataclass
|
||||
class PlacementPolicy:
|
||||
replicas: List[Replica]
|
||||
unique: bool
|
||||
backup_factory: int
|
||||
filters: List[Filter] = None
|
||||
selectors: List[Selector] = None
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
from dataclasses import dataclass
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SessionToken:
|
||||
token: bytes
|
19
frostfs_sdk/models/dto/version.py
Normal file
19
frostfs_sdk/models/dto/version.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
from dataclasses import dataclass
|
||||
|
||||
DEFAULT_MAJOR_VERSION = 2
|
||||
DEFAULT_MINOR_VERSION = 13
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Version:
|
||||
major: int = DEFAULT_MAJOR_VERSION
|
||||
minor: int = DEFAULT_MINOR_VERSION
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"v{self.major}.{self.minor}"
|
||||
|
||||
def is_supported(self, other):
|
||||
if not isinstance(other, Version):
|
||||
return False
|
||||
return self.major == other.major
|
||||
|
|
@ -39,4 +39,4 @@ class FilterOperation:
|
|||
Returns:
|
||||
Corresponding FilterOperation instance
|
||||
"""
|
||||
return cls._value_map.get(value)
|
||||
return cls._value_map.get(value)
|
||||
|
|
|
@ -1,26 +1,14 @@
|
|||
class SelectorClause:
|
||||
"""
|
||||
Enum for selector clauses with integer value mapping
|
||||
"""
|
||||
from enum import Enum, unique
|
||||
|
||||
@unique
|
||||
class SelectorClause(Enum):
|
||||
CLAUSE_UNSPECIFIED = 0
|
||||
SAME = 1
|
||||
DISTINCT = 2
|
||||
|
||||
_value_map = {
|
||||
0: CLAUSE_UNSPECIFIED,
|
||||
1: SAME,
|
||||
2: DISTINCT
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get(cls, value: int) -> 'SelectorClause':
|
||||
"""
|
||||
Get enum instance by integer value
|
||||
|
||||
Args:
|
||||
value: Integer value of the clause
|
||||
|
||||
Returns:
|
||||
Corresponding SelectorClause instance
|
||||
"""
|
||||
return cls._value_map.get(value)
|
||||
def get(cls, value: int):
|
||||
try:
|
||||
return cls(value)
|
||||
except ValueError:
|
||||
raise KeyError(f"Unknown enum value: {value}")
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
from typing import ByteString, Optional
|
||||
from typing import Optional
|
||||
|
||||
import grpc
|
||||
|
||||
from frostfs_sdk.models.mappers.placement_policy_mapper import PlacementPolicyMapper
|
||||
import protos.models.container.types_pb2 as types_pb2_container
|
||||
from frostfs_sdk.models.mappers.owner_id_mapper import OwnerIdMapper
|
||||
from frostfs_sdk.models.mappers.version_mapper import VersionMapper
|
||||
from frostfs_sdk.models.mappers.uuid_extension import UuidExtension
|
||||
from frostfs_sdk.models.dto.container import Container
|
||||
from frostfs_sdk.protos.models.container import types_pb2 as types_pb2_container
|
||||
|
||||
|
||||
class ContainerMapper:
|
||||
|
@ -19,24 +24,23 @@ class ContainerMapper:
|
|||
"""
|
||||
if not container:
|
||||
return None
|
||||
|
||||
|
||||
attributes = [
|
||||
types_pb2_container.Container.Attribute(key=k, value=v)
|
||||
for k, v in container.attributes.items()
|
||||
]
|
||||
|
||||
|
||||
grpc_container = types_pb2_container.Container(
|
||||
# nonce=ByteString.copy(container.nonce),
|
||||
nonce=container.nonce.bytes,
|
||||
placement_policy=PlacementPolicyMapper.to_grpc_message(container.placementPolicy),
|
||||
attributes=attributes
|
||||
|
||||
)
|
||||
|
||||
# if container.owner_id:
|
||||
# grpc_container.owner_id = OwnerIdMapper.to_grpc_message(container.owner_id)
|
||||
if container.owner_id:
|
||||
grpc_container.owner_id = OwnerIdMapper.to_grpc_message(container.owner_id)
|
||||
|
||||
# if container.version:
|
||||
# grpc_container.version = VersionMapper.to_grpc_message(container.version)
|
||||
if container.version:
|
||||
grpc_container.version = VersionMapper.to_grpc_message(container.version)
|
||||
|
||||
return grpc_container
|
||||
|
||||
|
@ -57,9 +61,9 @@ class ContainerMapper:
|
|||
attributes = {attr.key: attr.value for attr in container_grpc.attributes}
|
||||
|
||||
return Container(
|
||||
# nonce=UuidUtils.as_uuid(container_grpc.nonce.to_bytes()),
|
||||
nonce=UuidExtension.to_uuid(container_grpc.nonce),
|
||||
placement_policy=PlacementPolicyMapper.to_model(container_grpc.placement_policy),
|
||||
# version=VersionMapper.to_model(container_grpc.version),
|
||||
# owner_id=OwnerIdMapper.to_model(container_grpc.owner_id),
|
||||
version=VersionMapper.to_model(container_grpc.version),
|
||||
owner_id=OwnerIdMapper.to_model(container_grpc.owner_id),
|
||||
attributes=attributes
|
||||
)
|
||||
|
|
|
@ -2,7 +2,7 @@ from typing import List, Optional
|
|||
|
||||
from frostfs_sdk.models.enums.filter_operation import FilterOperation
|
||||
from frostfs_sdk.models.dto.filter import Filter
|
||||
import protos.models.netmap.types_pb2 as types_pb2_netmap
|
||||
from frostfs_sdk.protos.models.netmap import types_pb2 as types_pb2_netmap
|
||||
|
||||
|
||||
class FilterMapper:
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
from base58 import b58encode
|
||||
|
||||
from frostfs_sdk.models.dto.owner_id import OwnerId
|
||||
from frostfs_sdk.protos.models.refs import types_pb2 as types_pb2_refs
|
||||
|
||||
class OwnerIdMapper:
|
||||
@staticmethod
|
||||
def to_grpc_message(owner_id: OwnerId) -> types_pb2_refs.OwnerID:
|
||||
"""
|
||||
Converts OwnerId DTO to gRPC message
|
||||
"""
|
||||
if not owner_id:
|
||||
return None
|
||||
|
||||
return types_pb2_refs.OwnerID(
|
||||
value=owner_id.to_hash(),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def to_model(owner_id_grpc: types_pb2_refs.OwnerID) -> OwnerId:
|
||||
"""
|
||||
Converts gRPC message to OwnerId DTO
|
||||
"""
|
||||
if not owner_id_grpc or owner_id_grpc.ByteSize() == 0:
|
||||
return None
|
||||
|
||||
try:
|
||||
return OwnerId(
|
||||
value=b58encode(owner_id_grpc.value).decode('utf-8')
|
||||
)
|
||||
except Exception as e:
|
||||
raise ValueError(f"Failed to encode Base58 value: {owner_id_grpc.value}")
|
|
@ -1,7 +1,10 @@
|
|||
from typing import Optional
|
||||
|
||||
from frostfs_sdk.models.mappers.filter_mapper import FilterMapper
|
||||
import protos.models.netmap.types_pb2 as types_pb2_netmap
|
||||
from frostfs_sdk.models.mappers.selector_mapper import SelectorMapper
|
||||
from frostfs_sdk.models.mappers.replica_mapper import ReplicaMapper
|
||||
from frostfs_sdk.models.dto.placement_policy import PlacementPolicy
|
||||
from frostfs_sdk.protos.models.netmap import types_pb2 as types_pb2_netmap
|
||||
|
||||
|
||||
class PlacementPolicyMapper:
|
||||
|
@ -21,10 +24,10 @@ class PlacementPolicyMapper:
|
|||
|
||||
return types_pb2_netmap.PlacementPolicy(
|
||||
unique=policy.unique,
|
||||
container_backup_factor=policy.backup_factor,
|
||||
container_backup_factor=policy.backup_factory,
|
||||
filters=FilterMapper.to_grpc_messages(policy.filters),
|
||||
# selectors=SelectorMapper.to_grpc_messages(policy.selectors),
|
||||
# replicas=ReplicaMapper.to_grpc_messages(policy.replicas)
|
||||
selectors=SelectorMapper.to_grpc_messages(policy.selectors),
|
||||
replicas=ReplicaMapper.to_grpc_messages(policy.replicas)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
|
@ -42,9 +45,9 @@ class PlacementPolicyMapper:
|
|||
return None
|
||||
|
||||
return PlacementPolicy(
|
||||
# replicas=ReplicaMapper.to_models(policy_grpc.replicas),
|
||||
replicas=ReplicaMapper.to_models(policy_grpc.replicas),
|
||||
unique=policy_grpc.unique,
|
||||
backup_factor=policy_grpc.container_backup_factor,
|
||||
backup_factory=policy_grpc.container_backup_factor,
|
||||
filters=FilterMapper.to_models(policy_grpc.filters),
|
||||
# selectors=SelectorMapper.to_models(policy_grpc.selectors)
|
||||
selectors=SelectorMapper.to_models(policy_grpc.selectors)
|
||||
)
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
from typing import List, Optional
|
||||
|
||||
from frostfs_sdk.models.dto.replica import Replica
|
||||
from frostfs_sdk.protos.models.netmap import types_pb2 as types_pb2_netmap
|
||||
|
||||
|
||||
class ReplicaMapper:
|
||||
@staticmethod
|
||||
def to_grpc_messages(replicas: List[Replica]) -> Optional[List[types_pb2_netmap.Replica]]:
|
||||
if not replicas:
|
||||
return None
|
||||
return [ReplicaMapper.to_grpc_message(selector) for selector in replicas]
|
||||
|
||||
@staticmethod
|
||||
def to_grpc_message(replica: Replica) -> Optional[types_pb2_netmap.Replica]:
|
||||
"""
|
||||
Converts Replica DTO to gRPC message
|
||||
|
||||
Args:
|
||||
replice: Replica DTO object
|
||||
|
||||
Returns:
|
||||
gRPC Replica message
|
||||
"""
|
||||
if not replica:
|
||||
return None
|
||||
|
||||
return types_pb2_netmap.Replica(
|
||||
count=replica.count,
|
||||
selector=replica.selector
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def to_models(grpc_replicas: List[types_pb2_netmap.Replica]) -> Optional[List[Replica]]:
|
||||
if not grpc_replicas:
|
||||
return None
|
||||
return [ReplicaMapper.to_model(grpc_replica) for grpc_replica in grpc_replicas]
|
||||
|
||||
@staticmethod
|
||||
def to_model(grpc_replica: types_pb2_netmap.Replica) -> Optional[Replica]:
|
||||
"""
|
||||
Converts gRPC message to Replica DTO
|
||||
|
||||
Args:
|
||||
grpc_replica: gRPC Replica message
|
||||
|
||||
Returns:
|
||||
Replica DTO object
|
||||
"""
|
||||
if not grpc_replica or grpc_replica.ByteSize() == 0:
|
||||
return None
|
||||
|
||||
return Replica(
|
||||
count=grpc_replica.count,
|
||||
selectors=grpc_replica.selector
|
||||
)
|
|
@ -0,0 +1,73 @@
|
|||
from typing import List, Optional
|
||||
|
||||
from frostfs_sdk.models.dto.selector import Selector
|
||||
from frostfs_sdk.models.enums.selector_clause import SelectorClause
|
||||
from frostfs_sdk.protos.models.netmap import types_pb2 as types_pb2_netmap
|
||||
|
||||
|
||||
class SelectorMapper:
|
||||
@staticmethod
|
||||
def to_grpc_messages(selectors: List[Selector]) -> Optional[List[types_pb2_netmap.Selector]]:
|
||||
if not selectors:
|
||||
return None
|
||||
return [SelectorMapper.to_grpc_message(selector) for selector in selectors]
|
||||
|
||||
@staticmethod
|
||||
def to_grpc_message(selector: Selector) -> Optional[types_pb2_netmap.Selector]:
|
||||
"""
|
||||
Converts Selector DTO to gRPC message
|
||||
|
||||
Args:
|
||||
selector: Selector DTO object
|
||||
|
||||
Returns:
|
||||
gRPC Selector message
|
||||
"""
|
||||
if not selector:
|
||||
return None
|
||||
|
||||
clause_grpc = types_pb2_netmap.Clause(selector.clause.value)
|
||||
if clause_grpc is None:
|
||||
raise ValueError(f"Unknown enum value: {selector.clause.name} for {types_pb2_netmap.Clause.__name__}")
|
||||
|
||||
|
||||
return types_pb2_netmap.Selector(
|
||||
name=selector.name,
|
||||
count=selector.count,
|
||||
clause=clause_grpc,
|
||||
attribute=selector.attribute,
|
||||
filter=selector.filter
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def to_models(grpc_selectors: List[types_pb2_netmap.Selector]) -> Optional[List[Selector]]:
|
||||
if not grpc_selectors:
|
||||
return None
|
||||
return [SelectorMapper.to_model(grpc_selector) for grpc_selector in grpc_selectors]
|
||||
|
||||
@staticmethod
|
||||
def to_model(selector_grpc: types_pb2_netmap.Selector) -> Optional[Selector]:
|
||||
"""
|
||||
Converts gRPC message to Selector DTO
|
||||
|
||||
Args:
|
||||
selector_grpc: gRPC Selector message
|
||||
|
||||
Returns:
|
||||
Selector DTO object
|
||||
"""
|
||||
if not selector_grpc or selector_grpc.ByteSize() == 0:
|
||||
return None
|
||||
|
||||
clause = SelectorClause.get(selector_grpc.clause)
|
||||
if clause is None:
|
||||
raise ValueError(f"Unknown enum value: {selector_grpc.clause} for {SelectorClause.__name__}")
|
||||
|
||||
|
||||
return Selector(
|
||||
name=selector_grpc.name,
|
||||
count=selector_grpc.count,
|
||||
clause=clause,
|
||||
attribute=selector_grpc.attribute,
|
||||
filter=selector_grpc.filter
|
||||
)
|
31
frostfs_sdk/models/mappers/uuid_extension.py
Normal file
31
frostfs_sdk/models/mappers/uuid_extension.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
import uuid
|
||||
from typing import Optional
|
||||
|
||||
UUID_BYTE_ARRAY_LENGTH = 16
|
||||
|
||||
|
||||
class UuidExtension:
|
||||
@staticmethod
|
||||
def to_uuid(bytes_: Optional[bytes]) -> uuid.UUID:
|
||||
"""
|
||||
Converts a byte array into a UUID object.
|
||||
"""
|
||||
if bytes_ is None or len(bytes_) != UUID_BYTE_ARRAY_LENGTH:
|
||||
raise ValueError(f"Wrong UUID size: expected {UUID_BYTE_ARRAY_LENGTH} bytes, got {len(bytes_)}")
|
||||
|
||||
# Unpack the byte array into two 64-bit integers
|
||||
most_sig_bits = int.from_bytes(bytes_[:8], byteorder='big', signed=False)
|
||||
least_sig_bits = int.from_bytes(bytes_[8:], byteorder='big', signed=False)
|
||||
|
||||
return uuid.UUID(int=(most_sig_bits << 64) | least_sig_bits)
|
||||
|
||||
@staticmethod
|
||||
def to_bytes(uuid_: Optional[uuid.UUID]) -> bytes:
|
||||
"""
|
||||
Converts a UUID object into a byte array.
|
||||
"""
|
||||
if uuid_ is None:
|
||||
raise ValueError(f"Input parameter is missing: {uuid.UUID.__name__}")
|
||||
|
||||
# Pack the UUID into a 16-byte array
|
||||
return (uuid_.int >> 64).to_bytes(8, byteorder='big') + (uuid_.int & 0xFFFFFFFFFFFFFFFF).to_bytes(8, byteorder='big')
|
31
frostfs_sdk/models/mappers/version_mapper.py
Normal file
31
frostfs_sdk/models/mappers/version_mapper.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
from typing import Optional
|
||||
from frostfs_sdk.models.dto.version import Version
|
||||
from frostfs_sdk.protos.models.refs import types_pb2 as types_pb2_refs
|
||||
|
||||
|
||||
class VersionMapper:
|
||||
@staticmethod
|
||||
def to_grpc_message(version: Optional[Version]) -> Optional[types_pb2_refs.Version]:
|
||||
"""
|
||||
Converts a Version object to a gRPC Version message.
|
||||
"""
|
||||
if version is None:
|
||||
return None
|
||||
|
||||
return types_pb2_refs.Version(
|
||||
major=version.major,
|
||||
minor=version.minor
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def to_model(grpc_version: Optional[types_pb2_refs.Version]) -> Optional[Version]:
|
||||
"""
|
||||
Converts a gRPC Version message to a Version object.
|
||||
"""
|
||||
if grpc_version is None or grpc_version.ByteSize() == 0:
|
||||
return None
|
||||
|
||||
return Version(
|
||||
major=grpc_version.major,
|
||||
minor=grpc_version.minor
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue