Added create container grpc method
Signed-off-by: Ilyas Niyazov <i.niyazov@yadro.com>
This commit is contained in:
parent
9a1b5d778b
commit
f8465e5b99
34 changed files with 532 additions and 53 deletions
30
frostfs_sdk/client/frostfs_client.py
Normal file
30
frostfs_sdk/client/frostfs_client.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
# Create channel and Stubs
|
||||
import grpc
|
||||
from frostfs_sdk.client.models.client_environment import ClientEnvironment
|
||||
from frostfs_sdk.client.models.client_settings import ClientSettings
|
||||
from frostfs_sdk.client.models.ecdsa import ECDSA
|
||||
from frostfs_sdk.client.services.container import ContainerClient
|
||||
|
||||
|
||||
class FrostfsClient:
|
||||
def __init__(self, client_settings: ClientSettings):
|
||||
self.channel = grpc.insecure_channel(f"{client_settings.host}:{client_settings.port}")
|
||||
self.ecdsa: ECDSA = ECDSA(wif=client_settings.wif)
|
||||
|
||||
client_environment = ClientEnvironment(self.ecdsa, self.channel)
|
||||
self.container = ContainerClient(client_environment)
|
||||
|
||||
def close(self):
|
||||
self.channel.close()
|
||||
|
||||
|
||||
|
||||
"""
|
||||
import frostfs_sdk
|
||||
|
||||
WIF = "L5XNVUzPnma6m4mPrWEN6CcTscJERcfX3yvb1cdffdxe1iriAshU"
|
||||
address = "10.78.128.25:8080"
|
||||
client = frostfs_sdk.FrostfsClient(ClientSettings(WIF, address))
|
||||
params = frostfs_sdk.models.PrmsCreateContainer(name="1234")
|
||||
client.container.create(params)
|
||||
"""
|
8
frostfs_sdk/client/models/client_environment.py
Normal file
8
frostfs_sdk/client/models/client_environment.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
import grpc
|
||||
from frostfs_sdk.client.models.ecdsa import ECDSA
|
||||
|
||||
|
||||
class ClientEnvironment:
|
||||
def __init__(self, ecdsa: ECDSA, channel: grpc.Channel):
|
||||
self.ecdsa = ecdsa
|
||||
self.channel = channel
|
19
frostfs_sdk/client/models/client_settings.py
Normal file
19
frostfs_sdk/client/models/client_settings.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
class ClientSettings:
|
||||
def __init__(self, wif: str = None, address: str = None):
|
||||
"""
|
||||
Initializes client settings with validation.
|
||||
|
||||
Args:
|
||||
wif: Wallet import format string
|
||||
address: FrostFS node host address
|
||||
"""
|
||||
self.wif = wif
|
||||
self.address = address
|
||||
|
||||
# Perform validation after initialization
|
||||
self.validate()
|
||||
|
||||
def validate(self):
|
||||
"""Performs runtime validation of the settings"""
|
||||
if not (self.address and self.wif):
|
||||
raise ValueError("The value must be specified ADDRESS and WIF")
|
8
frostfs_sdk/client/models/ecdsa.py
Normal file
8
frostfs_sdk/client/models/ecdsa.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
from frostfs_sdk.cryptography.key_extension import KeyExtension
|
||||
|
||||
|
||||
class ECDSA:
|
||||
def __init__(self, wif: str):
|
||||
self.wif = wif
|
||||
self.private_key: bytes = KeyExtension().get_private_key_from_wif(wif)
|
||||
self.public_key: bytes = KeyExtension().get_public_key(self.private_key)
|
18
frostfs_sdk/client/parameters/container_create.py
Normal file
18
frostfs_sdk/client/parameters/container_create.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
from dataclasses import dataclass, field
|
||||
from typing import Optional, Dict
|
||||
|
||||
from frostfs_sdk.models.dto.container import Container
|
||||
from frostfs_sdk.models.dto.session_token import SessionToken
|
||||
from frostfs_sdk.client.parameters.wait import PrmWait
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PrmContainerCreate:
|
||||
container: Container
|
||||
wait_params: Optional[PrmWait] = None
|
||||
session_token: Optional[SessionToken] = None
|
||||
x_headers: Dict[str, str] = field(default_factory=dict)
|
||||
|
||||
def __post_init__(self):
|
||||
if self.wait_params is None:
|
||||
object.__setattr__(self, 'wait_params', PrmWait())
|
21
frostfs_sdk/client/parameters/wait.py
Normal file
21
frostfs_sdk/client/parameters/wait.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
from dataclasses import dataclass, field
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PrmWait:
|
||||
DEFAULT_TIMEOUT: timedelta = field(default=timedelta(seconds=120), init=False)
|
||||
DEFAULT_POLL_INTERVAL: timedelta = field(default=timedelta(seconds=5), init=False)
|
||||
|
||||
timeout: timedelta = DEFAULT_TIMEOUT
|
||||
poll_interval: timedelta = DEFAULT_POLL_INTERVAL
|
||||
|
||||
def __post_init__(self):
|
||||
if self.timeout is None:
|
||||
object.__setattr__(self, 'timeout', self.DEFAULT_TIMEOUT)
|
||||
if self.poll_interval is None:
|
||||
object.__setattr__(self, 'poll_interval', self.DEFAULT_POLL_INTERVAL)
|
||||
|
||||
def get_deadline(self) -> datetime:
|
||||
return datetime.now() + self.timeout
|
32
frostfs_sdk/client/services/container.py
Normal file
32
frostfs_sdk/client/services/container.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
# implementation Conainer methods
|
||||
from frostfs_sdk.client.models.client_environment import ClientEnvironment
|
||||
from frostfs_sdk.cryptography.signer import Signer
|
||||
from frostfs_sdk.models.dto.container import ContainerId
|
||||
import protos.models.container.service_pb2_grpc as service_pb2_grpc_container
|
||||
import protos.models.container.service_pb2 as service_pb2_container
|
||||
|
||||
from frostfs_sdk.client.parameters.container_create import PrmContainerCreate
|
||||
from frostfs_sdk.models.mappers.container_mapper import ContainerMapper
|
||||
|
||||
|
||||
|
||||
class ContainerClient:
|
||||
def __init__(self, client_environment: ClientEnvironment):
|
||||
self.container_stub = service_pb2_grpc_container.ContainerServiceStub(client_environment.channel)
|
||||
self.ecdsa = client_environment.ecdsa
|
||||
|
||||
def create(self, prm_container_create: PrmContainerCreate) -> ContainerId:
|
||||
request = self.create_put_request(prm_container_create)
|
||||
response: service_pb2_container.PutResponse = self.container_stub.Put(request)
|
||||
return ContainerId(value=response.body.container_id)
|
||||
|
||||
def create_put_request(self, prm: PrmContainerCreate):
|
||||
grpc_container=ContainerMapper().to_grpc_message(prm.container)
|
||||
body = service_pb2_container.PutRequest.Body(
|
||||
container=grpc_container,
|
||||
signature=Signer.sign_rfc6979(self.ecdsa.private_key, grpc_container)
|
||||
)
|
||||
|
||||
request = service_pb2_container.PutRequest(body=body)
|
||||
signed_request = Signer.sign(self.ecdsa.private_key, request)
|
||||
return signed_request
|
Loading…
Add table
Add a link
Reference in a new issue