From 9af8f89305fb014fe7bf40c7d65d9d49f5aa26c8 Mon Sep 17 00:00:00 2001 From: Vladimir Avdeev Date: Mon, 31 Oct 2022 12:31:28 +0300 Subject: [PATCH] Add storagegroup, session and sign in neofs_cli lib Signed-off-by: Vladimir Avdeev --- src/neofs_testlib/blockchain/multisig.py | 6 +- .../blockchain/role_designation.py | 22 +-- src/neofs_testlib/blockchain/rpc_client.py | 8 +- src/neofs_testlib/cli/cli_command.py | 2 + src/neofs_testlib/cli/neofs_cli/cli.py | 9 ++ src/neofs_testlib/cli/neofs_cli/container.py | 14 +- src/neofs_testlib/cli/neofs_cli/netmap.py | 6 +- src/neofs_testlib/cli/neofs_cli/object.py | 16 +- src/neofs_testlib/cli/neofs_cli/session.py | 41 +++++ .../cli/neofs_cli/storagegroup.py | 147 ++++++++++++++++++ src/neofs_testlib/cli/neofs_cli/util.py | 56 +++++++ 11 files changed, 290 insertions(+), 37 deletions(-) create mode 100644 src/neofs_testlib/cli/neofs_cli/session.py create mode 100644 src/neofs_testlib/cli/neofs_cli/storagegroup.py create mode 100644 src/neofs_testlib/cli/neofs_cli/util.py diff --git a/src/neofs_testlib/blockchain/multisig.py b/src/neofs_testlib/blockchain/multisig.py index 9dafd72..229f2a0 100644 --- a/src/neofs_testlib/blockchain/multisig.py +++ b/src/neofs_testlib/blockchain/multisig.py @@ -1,5 +1,3 @@ -from typing import List - from neofs_testlib.cli import NeoGo @@ -14,8 +12,8 @@ class Multisig: contract_hash: str, contract_args: str, multisig_hash: str, - wallets: List[str], - passwords: List[str], + wallets: list[str], + passwords: list[str], address: str, endpoint: str, ) -> None: diff --git a/src/neofs_testlib/blockchain/role_designation.py b/src/neofs_testlib/blockchain/role_designation.py index a97438e..cfbce29 100644 --- a/src/neofs_testlib/blockchain/role_designation.py +++ b/src/neofs_testlib/blockchain/role_designation.py @@ -1,6 +1,6 @@ import json from time import sleep -from typing import List, Optional +from typing import Optional from cli import NeoGo from shell import Shell @@ -24,7 +24,7 @@ class RoleDesignation: def set_notary_nodes( self, addr: str, - pubkeys: List[str], + pubkeys: list[str], script_hash: str, wallet: str, passwd: str, @@ -47,7 +47,7 @@ class RoleDesignation: def set_inner_ring( self, addr: str, - pubkeys: List[str], + pubkeys: list[str], script_hash: str, wallet: str, passwd: str, @@ -70,7 +70,7 @@ class RoleDesignation: def set_oracles( self, addr: str, - pubkeys: List[str], + pubkeys: list[str], script_hash: str, wallet: str, passwd: str, @@ -92,10 +92,10 @@ class RoleDesignation: def set_notary_nodes_multisig_tx( self, - pubkeys: List[str], + pubkeys: list[str], script_hash: str, - wallets: List[str], - passwords: List[str], + wallets: list[str], + passwords: list[str], address: str, endpoint: str, invoke_tx_file: str, @@ -118,10 +118,10 @@ class RoleDesignation: def set_inner_ring_multisig_tx( self, - pubkeys: List[str], + pubkeys: list[str], script_hash: str, - wallets: List[str], - passwords: List[str], + wallets: list[str], + passwords: list[str], address: str, endpoint: str, invoke_tx_file: str, @@ -142,7 +142,7 @@ class RoleDesignation: ) sleep(self.block_period) - def check_candidates(self, contract_hash: str, endpoint: str) -> Optional[List[str]]: + def check_candidates(self, contract_hash: str, endpoint: str) -> Optional[list[str]]: out = self.neogo.contract.testinvokefunction( scripthash=contract_hash, method="innerRingCandidates", diff --git a/src/neofs_testlib/blockchain/rpc_client.py b/src/neofs_testlib/blockchain/rpc_client.py index b4e85c1..0ca0212 100644 --- a/src/neofs_testlib/blockchain/rpc_client.py +++ b/src/neofs_testlib/blockchain/rpc_client.py @@ -1,6 +1,6 @@ import json import logging -from typing import Any, Dict, List, Optional +from typing import Any, Dict, Optional import requests @@ -29,8 +29,8 @@ class RPCClient: self, sc_hash: str, function: str, - params: Optional[List] = None, - signers: Optional[List] = None, + params: Optional[list] = None, + signers: Optional[list] = None, ) -> Dict[str, Any]: return self._call_endpoint( "invokefunction", params=[sc_hash, function, params or [], signers or []] @@ -75,6 +75,6 @@ class RPCClient: ) from exc -def _build_payload(method, params: Optional[List] = None): +def _build_payload(method, params: Optional[list] = None): payload = json.dumps({"jsonrpc": "2.0", "method": method, "params": params or [], "id": 1}) return payload.replace("'", '"') diff --git a/src/neofs_testlib/cli/cli_command.py b/src/neofs_testlib/cli/cli_command.py index 772b9da..d3ce86d 100644 --- a/src/neofs_testlib/cli/cli_command.py +++ b/src/neofs_testlib/cli/cli_command.py @@ -17,6 +17,8 @@ class CliCommand: "doc_type": "type", "to_address": "to", "from_address": "from", + "to_file": "to", + "from_file": "from", } def __init__(self, shell: Shell, cli_exec_path: str, **base_params): diff --git a/src/neofs_testlib/cli/neofs_cli/cli.py b/src/neofs_testlib/cli/neofs_cli/cli.py index c65d86f..d80c3e8 100644 --- a/src/neofs_testlib/cli/neofs_cli/cli.py +++ b/src/neofs_testlib/cli/neofs_cli/cli.py @@ -5,6 +5,9 @@ from neofs_testlib.cli.neofs_cli.acl import NeofsCliACL from neofs_testlib.cli.neofs_cli.container import NeofsCliContainer from neofs_testlib.cli.neofs_cli.netmap import NeofsCliNetmap from neofs_testlib.cli.neofs_cli.object import NeofsCliObject +from neofs_testlib.cli.neofs_cli.session import NeofsCliSession +from neofs_testlib.cli.neofs_cli.storagegroup import NeofsCliStorageGroup +from neofs_testlib.cli.neofs_cli.util import NeofsCliUtil from neofs_testlib.cli.neofs_cli.version import NeofsCliVersion from neofs_testlib.shell import Shell @@ -15,6 +18,9 @@ class NeofsCli: container: Optional[NeofsCliContainer] = None netmap: Optional[NeofsCliNetmap] = None object: Optional[NeofsCliObject] = None + session: Optional[NeofsCliSession] = None + storagegroup: Optional[NeofsCliStorageGroup] = None + util: Optional[NeofsCliUtil] = None version: Optional[NeofsCliVersion] = None def __init__(self, shell: Shell, neofs_cli_exec_path: str, config_file: Optional[str] = None): @@ -23,4 +29,7 @@ class NeofsCli: self.container = NeofsCliContainer(shell, neofs_cli_exec_path, config=config_file) self.netmap = NeofsCliNetmap(shell, neofs_cli_exec_path, config=config_file) self.object = NeofsCliObject(shell, neofs_cli_exec_path, config=config_file) + self.session = NeofsCliSession(shell, neofs_cli_exec_path, config=config_file) + self.storagegroup = NeofsCliStorageGroup(shell, neofs_cli_exec_path, config=config_file) + self.util = NeofsCliUtil(shell, neofs_cli_exec_path, config=config_file) self.version = NeofsCliVersion(shell, neofs_cli_exec_path, config=config_file) diff --git a/src/neofs_testlib/cli/neofs_cli/container.py b/src/neofs_testlib/cli/neofs_cli/container.py index 270d820..ca0b081 100644 --- a/src/neofs_testlib/cli/neofs_cli/container.py +++ b/src/neofs_testlib/cli/neofs_cli/container.py @@ -42,7 +42,7 @@ class NeofsCliContainer(CliCommand): subnet: String representation of container subnetwork. ttl: TTL value in request meta header (default 2). wallet: WIF (NEP-2) string or path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. @@ -77,7 +77,7 @@ class NeofsCliContainer(CliCommand): session: Path to a JSON-encoded container session token. ttl: TTL value in request meta header (default 2). wallet: WIF (NEP-2) string or path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. @@ -112,7 +112,7 @@ class NeofsCliContainer(CliCommand): to: Path to dump encoded container. ttl: TTL value in request meta header (default 2). wallet: WIF (NEP-2) string or path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. @@ -146,7 +146,7 @@ class NeofsCliContainer(CliCommand): session: Path to a JSON-encoded container session token. ttl: TTL value in request meta header (default 2). wallet: WIF (NEP-2) string or path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. @@ -176,7 +176,7 @@ class NeofsCliContainer(CliCommand): rpc_endpoint: Remote node address (as 'multiaddr' or ':'). ttl: TTL value in request meta header (default 2). wallet: WIF (NEP-2) string or path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. @@ -204,7 +204,7 @@ class NeofsCliContainer(CliCommand): rpc_endpoint: Remote node address (as 'multiaddr' or ':'). ttl: TTL value in request meta header (default 2). wallet: WIF (NEP-2) string or path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. @@ -239,7 +239,7 @@ class NeofsCliContainer(CliCommand): table: Path to file with JSON or binary encoded EACL table. ttl: TTL value in request meta header (default 2). wallet: WIF (NEP-2) string or path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. diff --git a/src/neofs_testlib/cli/neofs_cli/netmap.py b/src/neofs_testlib/cli/neofs_cli/netmap.py index 7144b8f..0100fc4 100644 --- a/src/neofs_testlib/cli/neofs_cli/netmap.py +++ b/src/neofs_testlib/cli/neofs_cli/netmap.py @@ -23,7 +23,7 @@ class NeofsCliNetmap(CliCommand): rpc_endpoint: Remote node address (as 'multiaddr' or ':'). ttl: TTL value in request meta header (default 2). wallet: Path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. @@ -81,7 +81,7 @@ class NeofsCliNetmap(CliCommand): rpc_endpoint: Remote node address (as 'multiaddr' or ':'). ttl: TTL value in request meta header (default 2). wallet: Path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. @@ -109,7 +109,7 @@ class NeofsCliNetmap(CliCommand): rpc_endpoint: Remote node address (as 'multiaddr' or ':'). ttl: TTL value in request meta header (default 2). wallet: Path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. diff --git a/src/neofs_testlib/cli/neofs_cli/object.py b/src/neofs_testlib/cli/neofs_cli/object.py index ba2cbce..0e54595 100644 --- a/src/neofs_testlib/cli/neofs_cli/object.py +++ b/src/neofs_testlib/cli/neofs_cli/object.py @@ -29,7 +29,7 @@ class NeofsCliObject(CliCommand): session: Path to a JSON-encoded container session token. ttl: TTL value in request meta header (default 2). wallet: WIF (NEP-2) string or path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. @@ -71,7 +71,7 @@ class NeofsCliObject(CliCommand): session: Path to a JSON-encoded container session token. ttl: TTL value in request meta header (default 2). wallet: WIF (NEP-2) string or path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. @@ -109,7 +109,7 @@ class NeofsCliObject(CliCommand): ttl: TTL value in request meta header (default 2). hash_type: Hash type. Either 'sha256' or 'tz' (default "sha256"). wallet: WIF (NEP-2) string or path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. @@ -155,7 +155,7 @@ class NeofsCliObject(CliCommand): session: Path to a JSON-encoded container session token. ttl: TTL value in request meta header (default 2). wallet: WIF (NEP-2) string or path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. @@ -191,7 +191,7 @@ class NeofsCliObject(CliCommand): session: Path to a JSON-encoded container session token. ttl: TTL value in request meta header (default 2). wallet: WIF (NEP-2) string or path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. @@ -238,7 +238,7 @@ class NeofsCliObject(CliCommand): session: Path to a JSON-encoded container session token. ttl: TTL value in request meta header (default 2). wallet: WIF (NEP-2) string or path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. @@ -280,7 +280,7 @@ class NeofsCliObject(CliCommand): session: Path to a JSON-encoded container session token. ttl: TTL value in request meta header (default 2). wallet: WIF (NEP-2) string or path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. @@ -320,7 +320,7 @@ class NeofsCliObject(CliCommand): session: Path to a JSON-encoded container session token. ttl: TTL value in request meta header (default 2). wallet: WIF (NEP-2) string or path to the wallet or binary key. - xhdr: Request X-Headers in form of Key=Value. + xhdr: Dict with request X-Headers. Returns: Command's result. diff --git a/src/neofs_testlib/cli/neofs_cli/session.py b/src/neofs_testlib/cli/neofs_cli/session.py new file mode 100644 index 0000000..4e33a7a --- /dev/null +++ b/src/neofs_testlib/cli/neofs_cli/session.py @@ -0,0 +1,41 @@ +from typing import Optional + +from neofs_testlib.cli.cli_command import CliCommand +from neofs_testlib.shell import CommandResult + + +class NeofsCliSession(CliCommand): + def create( + self, + rpc_endpoint: str, + wallet: str, + wallet_password: str, + out: str, + lifetime: Optional[int] = None, + address: Optional[str] = None, + json: Optional[bool] = False, + ) -> CommandResult: + """ + Create session token. + + Args: + address: Address of wallet account. + out: File to write session token to. + lifetime: Number of epochs for token to stay valid. + json: Output token in JSON. + wallet: WIF (NEP-2) string or path to the wallet or binary key. + wallet_password: Wallet password. + rpc_endpoint: Remote node address (as 'multiaddr' or ':'). + + Returns: + Command's result. + """ + return self._execute_with_password( + "session create", + wallet_password, + **{ + param: value + for param, value in locals().items() + if param not in ["self", "wallet_password"] + }, + ) diff --git a/src/neofs_testlib/cli/neofs_cli/storagegroup.py b/src/neofs_testlib/cli/neofs_cli/storagegroup.py new file mode 100644 index 0000000..514abf5 --- /dev/null +++ b/src/neofs_testlib/cli/neofs_cli/storagegroup.py @@ -0,0 +1,147 @@ +from typing import Optional + +from neofs_testlib.cli.cli_command import CliCommand +from neofs_testlib.shell import CommandResult + + +class NeofsCliStorageGroup(CliCommand): + def put( + self, + rpc_endpoint: str, + wallet: str, + cid: str, + members: list[str], + ttl: Optional[int] = None, + bearer: Optional[str] = None, + lifetime: Optional[int] = None, + address: Optional[str] = None, + xhdr: Optional[dict] = None, + ) -> CommandResult: + """ + Put storage group to NeoFS. + + Args: + address: Address of wallet account. + bearer: File with signed JSON or binary encoded bearer token. + cid: Container ID. + members: ID list of storage group members. + lifetime: Storage group lifetime in epochs. + rpc_endpoint: Remote node address (as 'multiaddr' or ':'). + ttl: TTL value in request meta header. + wallet: WIF (NEP-2) string or path to the wallet or binary key. + xhdr: Dict with request X-Headers. + + Returns: + Command's result. + """ + members = ",".join(members) + return self._execute( + "storagegroup put", + **{param: value for param, value in locals().items() if param not in ["self"]}, + ) + + def get( + self, + rpc_endpoint: str, + wallet: str, + cid: str, + id: str, + raw: Optional[bool] = False, + ttl: Optional[int] = None, + bearer: Optional[str] = None, + lifetime: Optional[int] = None, + address: Optional[str] = None, + xhdr: Optional[dict] = None, + ) -> CommandResult: + """ + Get storage group from NeoFS. + + Args: + address: Address of wallet account. + bearer: File with signed JSON or binary encoded bearer token. + cid: Container ID. + id: Storage group identifier. + raw: Set raw request option. + lifetime: Storage group lifetime in epochs. + rpc_endpoint: Remote node address (as 'multiaddr' or ':'). + ttl: TTL value in request meta header. + wallet: WIF (NEP-2) string or path to the wallet or binary key. + xhdr: Dict with request X-Headers. + + Returns: + Command's result. + """ + return self._execute( + "storagegroup get", + **{param: value for param, value in locals().items() if param not in ["self"]}, + ) + + def list( + self, + rpc_endpoint: str, + wallet: str, + cid: str, + raw: Optional[bool] = False, + ttl: Optional[int] = None, + bearer: Optional[str] = None, + lifetime: Optional[int] = None, + address: Optional[str] = None, + xhdr: Optional[dict] = None, + ) -> CommandResult: + """ + List storage groups in NeoFS container. + + Args: + address: Address of wallet account. + bearer: File with signed JSON or binary encoded bearer token. + cid: Container ID. + raw: Set raw request option. + lifetime: Storage group lifetime in epochs. + rpc_endpoint: Remote node address (as 'multiaddr' or ':'). + ttl: TTL value in request meta header. + wallet: WIF (NEP-2) string or path to the wallet or binary key. + xhdr: Dict with request X-Headers. + + Returns: + Command's result. + """ + return self._execute( + "storagegroup list", + **{param: value for param, value in locals().items() if param not in ["self"]}, + ) + + def delete( + self, + rpc_endpoint: str, + wallet: str, + cid: str, + id: str, + raw: Optional[bool] = False, + ttl: Optional[int] = None, + bearer: Optional[str] = None, + lifetime: Optional[int] = None, + address: Optional[str] = None, + xhdr: Optional[dict] = None, + ) -> CommandResult: + """ + Delete storage group from NeoFS. + + Args: + address: Address of wallet account. + bearer: File with signed JSON or binary encoded bearer token. + cid: Container ID. + id: Storage group identifier. + raw: Set raw request option. + lifetime: Storage group lifetime in epochs. + rpc_endpoint: Remote node address (as 'multiaddr' or ':'). + ttl: TTL value in request meta header. + wallet: WIF (NEP-2) string or path to the wallet or binary key. + xhdr: Dict with request X-Headers. + + Returns: + Command's result. + """ + return self._execute( + "storagegroup delete", + **{param: value for param, value in locals().items() if param not in ["self"]}, + ) diff --git a/src/neofs_testlib/cli/neofs_cli/util.py b/src/neofs_testlib/cli/neofs_cli/util.py new file mode 100644 index 0000000..786c156 --- /dev/null +++ b/src/neofs_testlib/cli/neofs_cli/util.py @@ -0,0 +1,56 @@ +from typing import Optional + +from neofs_testlib.cli.cli_command import CliCommand +from neofs_testlib.shell import CommandResult + + +class NeofsCliUtil(CliCommand): + def sign_bearer_token( + self, + wallet: str, + from_file: str, + to_file: str, + address: Optional[str] = None, + json: Optional[bool] = False, + ) -> CommandResult: + """ + Sign bearer token to use it in requests. + + Args: + address: Address of wallet account. + from_file: File with JSON or binary encoded bearer token to sign. + to_file: File to dump signed bearer token (default: binary encoded). + json: Dump bearer token in JSON encoding. + wallet: WIF (NEP-2) string or path to the wallet or binary key. + + Returns: + Command's result. + """ + return self._execute( + "util sign bearer-token", + **{param: value for param, value in locals().items() if param not in ["self"]}, + ) + + def sign_session_token( + self, + wallet: str, + from_file: str, + to_file: str, + address: Optional[str] = None, + ) -> CommandResult: + """ + Sign session token to use it in requests. + + Args: + address: Address of wallet account. + from_file: File with JSON encoded session token to sign. + to_file: File to dump signed bearer token (default: binary encoded). + wallet: WIF (NEP-2) string or path to the wallet or binary key. + + Returns: + Command's result. + """ + return self._execute( + "util sign session-token", + **{param: value for param, value in locals().items() if param not in ["self"]}, + )