2022-03-15 11:58:59 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
'''
|
|
|
|
This module contains wrappers for NeoFS verbs executed via neofs-cli.
|
|
|
|
'''
|
|
|
|
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import random
|
2022-06-09 13:08:11 +00:00
|
|
|
import re
|
2022-03-15 11:58:59 +00:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
import json_transformers
|
2022-06-09 13:08:11 +00:00
|
|
|
from cli_helpers import _cmd_run
|
2022-07-07 21:31:58 +00:00
|
|
|
from common import ASSETS_DIR, NEOFS_ENDPOINT, NEOFS_NETMAP, WALLET_CONFIG
|
2022-04-25 09:53:20 +00:00
|
|
|
from data_formatters import dict_to_attrs
|
2022-07-07 21:31:58 +00:00
|
|
|
from robot.api import logger
|
|
|
|
from robot.api.deco import keyword
|
2022-03-15 11:58:59 +00:00
|
|
|
|
|
|
|
ROBOT_AUTO_KEYWORDS = False
|
|
|
|
|
|
|
|
# path to neofs-cli executable
|
|
|
|
NEOFS_CLI_EXEC = os.getenv('NEOFS_CLI_EXEC', 'neofs-cli')
|
|
|
|
|
|
|
|
|
|
|
|
@keyword('Get object')
|
2022-06-09 13:08:11 +00:00
|
|
|
def get_object(wallet: str, cid: str, oid: str, bearer_token: str = "",
|
2022-06-13 20:33:09 +00:00
|
|
|
write_object: str = "", endpoint: str = "", options: str = "",
|
|
|
|
wallet_config: str = WALLET_CONFIG):
|
2022-06-09 13:08:11 +00:00
|
|
|
"""
|
2022-03-15 11:58:59 +00:00
|
|
|
GET from NeoFS.
|
|
|
|
|
|
|
|
Args:
|
2022-06-06 13:47:13 +00:00
|
|
|
wallet (str): wallet on whose behalf GET is done
|
2022-03-15 11:58:59 +00:00
|
|
|
cid (str): ID of Container where we get the Object from
|
|
|
|
oid (str): Object ID
|
|
|
|
bearer_token (optional, str): path to Bearer Token file, appends to `--bearer` key
|
|
|
|
write_object (optional, str): path to downloaded file, appends to `--file` key
|
|
|
|
endpoint (optional, str): NeoFS endpoint to send request to, appends to `--rpc-endpoint` key
|
2022-06-13 20:33:09 +00:00
|
|
|
wallet_config(optional, str): path to the wallet config
|
2022-03-15 11:58:59 +00:00
|
|
|
options (optional, str): any options which `neofs-cli object get` accepts
|
|
|
|
Returns:
|
|
|
|
(str): path to downloaded file
|
2022-06-09 13:08:11 +00:00
|
|
|
"""
|
2022-03-15 11:58:59 +00:00
|
|
|
|
|
|
|
if not write_object:
|
|
|
|
write_object = str(uuid.uuid4())
|
|
|
|
file_path = f"{ASSETS_DIR}/{write_object}"
|
|
|
|
|
|
|
|
if not endpoint:
|
|
|
|
endpoint = random.sample(NEOFS_NETMAP, 1)[0]
|
|
|
|
|
|
|
|
cmd = (
|
2022-02-01 13:42:41 +00:00
|
|
|
f'{NEOFS_CLI_EXEC} --rpc-endpoint {endpoint} --wallet {wallet} '
|
2022-06-13 20:33:09 +00:00
|
|
|
f'object get --cid {cid} --oid {oid} --file {file_path} --config {wallet_config} '
|
2022-03-15 11:58:59 +00:00
|
|
|
f'{"--bearer " + bearer_token if bearer_token else ""} '
|
|
|
|
f'{options}'
|
|
|
|
)
|
|
|
|
_cmd_run(cmd)
|
|
|
|
return file_path
|
|
|
|
|
|
|
|
|
2022-06-06 13:47:13 +00:00
|
|
|
# TODO: make `bearer_token` optional
|
2022-03-15 11:58:59 +00:00
|
|
|
@keyword('Get Range Hash')
|
2022-06-06 13:47:13 +00:00
|
|
|
def get_range_hash(wallet: str, cid: str, oid: str, bearer_token: str, range_cut: str,
|
2022-06-13 20:33:09 +00:00
|
|
|
wallet_config: str = WALLET_CONFIG, options: str = ""):
|
2022-06-09 13:08:11 +00:00
|
|
|
"""
|
2022-03-15 11:58:59 +00:00
|
|
|
GETRANGEHASH of given Object.
|
|
|
|
|
|
|
|
Args:
|
2022-06-06 13:47:13 +00:00
|
|
|
wallet (str): wallet on whose behalf GETRANGEHASH is done
|
2022-03-15 11:58:59 +00:00
|
|
|
cid (str): ID of Container where we get the Object from
|
|
|
|
oid (str): Object ID
|
|
|
|
range_cut (str): Range to take hash from in the form offset1:length1,...,
|
|
|
|
value to pass to the `--range` parameter
|
2022-06-06 13:47:13 +00:00
|
|
|
bearer_token (optional, str): path to Bearer Token file, appends to `--bearer` key
|
2022-06-13 20:33:09 +00:00
|
|
|
wallet_config(optional, str): path to the wallet config
|
2022-03-15 11:58:59 +00:00
|
|
|
options (optional, str): any options which `neofs-cli object hash` accepts
|
|
|
|
Returns:
|
|
|
|
None
|
2022-06-09 13:08:11 +00:00
|
|
|
"""
|
2022-03-15 11:58:59 +00:00
|
|
|
cmd = (
|
2022-02-01 13:42:41 +00:00
|
|
|
f'{NEOFS_CLI_EXEC} --rpc-endpoint {NEOFS_ENDPOINT} --wallet {wallet} '
|
2022-06-13 20:33:09 +00:00
|
|
|
f'object hash --cid {cid} --oid {oid} --range {range_cut} --config {wallet_config} '
|
2022-03-15 11:58:59 +00:00
|
|
|
f'{"--bearer " + bearer_token if bearer_token else ""} '
|
|
|
|
f'{options}'
|
|
|
|
)
|
2022-06-06 13:47:13 +00:00
|
|
|
output = _cmd_run(cmd)
|
|
|
|
# cutting off output about range offset and length
|
|
|
|
return output.split(':')[1].strip()
|
2022-03-15 11:58:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
@keyword('Put object')
|
2022-06-09 13:08:11 +00:00
|
|
|
def put_object(wallet: str, path: str, cid: str, bearer: str = "", user_headers: dict = {},
|
2022-06-13 20:33:09 +00:00
|
|
|
endpoint: str = "", wallet_config: str = WALLET_CONFIG, options: str = ""):
|
2022-06-09 13:08:11 +00:00
|
|
|
"""
|
2022-03-15 11:58:59 +00:00
|
|
|
PUT of given file.
|
|
|
|
|
|
|
|
Args:
|
2022-06-06 13:47:13 +00:00
|
|
|
wallet (str): wallet on whose behalf PUT is done
|
2022-03-15 11:58:59 +00:00
|
|
|
path (str): path to file to be PUT
|
|
|
|
cid (str): ID of Container where we get the Object from
|
|
|
|
bearer (optional, str): path to Bearer Token file, appends to `--bearer` key
|
|
|
|
user_headers (optional, dict): Object attributes, append to `--attributes` key
|
|
|
|
endpoint(optional, str): NeoFS endpoint to send request to
|
2022-06-13 20:33:09 +00:00
|
|
|
wallet_config(optional, str): path to the wallet config
|
2022-03-15 11:58:59 +00:00
|
|
|
options (optional, str): any options which `neofs-cli object put` accepts
|
|
|
|
Returns:
|
|
|
|
(str): ID of uploaded Object
|
2022-06-09 13:08:11 +00:00
|
|
|
"""
|
2022-03-15 11:58:59 +00:00
|
|
|
if not endpoint:
|
|
|
|
endpoint = random.sample(NEOFS_NETMAP, 1)[0]
|
2022-07-08 17:24:55 +00:00
|
|
|
if not endpoint:
|
|
|
|
logger.info(f'---DEB:\n{NEOFS_NETMAP}')
|
2022-03-15 11:58:59 +00:00
|
|
|
cmd = (
|
2022-02-01 13:42:41 +00:00
|
|
|
f'{NEOFS_CLI_EXEC} --rpc-endpoint {endpoint} --wallet {wallet} '
|
2022-06-13 20:33:09 +00:00
|
|
|
f'object put --file {path} --cid {cid} {options} --config {wallet_config} '
|
2022-03-15 11:58:59 +00:00
|
|
|
f'{"--bearer " + bearer if bearer else ""} '
|
2022-04-25 09:53:20 +00:00
|
|
|
f'{"--attributes " + dict_to_attrs(user_headers) if user_headers else ""}'
|
2022-03-15 11:58:59 +00:00
|
|
|
)
|
|
|
|
output = _cmd_run(cmd)
|
|
|
|
# splitting CLI output to lines and taking the penultimate line
|
|
|
|
id_str = output.strip().split('\n')[-2]
|
|
|
|
oid = id_str.split(':')[1]
|
|
|
|
return oid.strip()
|
|
|
|
|
|
|
|
|
|
|
|
@keyword('Delete object')
|
2022-06-13 20:33:09 +00:00
|
|
|
def delete_object(wallet: str, cid: str, oid: str, bearer: str = "", wallet_config: str = WALLET_CONFIG,
|
2022-07-07 21:31:58 +00:00
|
|
|
options: str = ""):
|
2022-06-09 13:08:11 +00:00
|
|
|
"""
|
2022-03-15 11:58:59 +00:00
|
|
|
DELETE an Object.
|
|
|
|
|
|
|
|
Args:
|
2022-06-06 13:47:13 +00:00
|
|
|
wallet (str): wallet on whose behalf DELETE is done
|
2022-03-15 11:58:59 +00:00
|
|
|
cid (str): ID of Container where we get the Object from
|
|
|
|
oid (str): ID of Object we are going to delete
|
|
|
|
bearer (optional, str): path to Bearer Token file, appends to `--bearer` key
|
2022-06-13 20:33:09 +00:00
|
|
|
wallet_config(optional, str): path to the wallet config
|
2022-03-15 11:58:59 +00:00
|
|
|
options (optional, str): any options which `neofs-cli object delete` accepts
|
|
|
|
Returns:
|
|
|
|
(str): Tombstone ID
|
2022-06-09 13:08:11 +00:00
|
|
|
"""
|
2022-03-15 11:58:59 +00:00
|
|
|
cmd = (
|
2022-02-01 13:42:41 +00:00
|
|
|
f'{NEOFS_CLI_EXEC} --rpc-endpoint {NEOFS_ENDPOINT} --wallet {wallet} '
|
2022-06-13 20:33:09 +00:00
|
|
|
f'object delete --cid {cid} --oid {oid} {options} --config {wallet_config} '
|
2022-04-04 10:45:00 +00:00
|
|
|
f'{"--bearer " + bearer if bearer else ""}'
|
2022-03-15 11:58:59 +00:00
|
|
|
)
|
|
|
|
output = _cmd_run(cmd)
|
|
|
|
id_str = output.split('\n')[1]
|
|
|
|
tombstone = id_str.split(':')[1]
|
|
|
|
return tombstone.strip()
|
|
|
|
|
|
|
|
|
|
|
|
@keyword('Get Range')
|
2022-08-02 15:47:10 +00:00
|
|
|
def get_range(wallet: str, cid: str, oid: str, range_cut: str,
|
|
|
|
wallet_config: str = WALLET_CONFIG, bearer: str = "", options: str = ""):
|
2022-06-09 13:08:11 +00:00
|
|
|
"""
|
2022-03-15 11:58:59 +00:00
|
|
|
GETRANGE an Object.
|
|
|
|
|
|
|
|
Args:
|
2022-06-06 13:47:13 +00:00
|
|
|
wallet (str): wallet on whose behalf GETRANGE is done
|
2022-03-15 11:58:59 +00:00
|
|
|
cid (str): ID of Container where we get the Object from
|
|
|
|
oid (str): ID of Object we are going to request
|
|
|
|
range_cut (str): range to take data from in the form offset:length
|
2022-06-06 13:47:13 +00:00
|
|
|
bearer (optional, str): path to Bearer Token file, appends to `--bearer` key
|
2022-06-13 20:33:09 +00:00
|
|
|
wallet_config(optional, str): path to the wallet config
|
2022-03-15 11:58:59 +00:00
|
|
|
options (optional, str): any options which `neofs-cli object range` accepts
|
|
|
|
Returns:
|
2022-06-06 13:47:13 +00:00
|
|
|
(void)
|
2022-06-09 13:08:11 +00:00
|
|
|
"""
|
2022-06-06 13:47:13 +00:00
|
|
|
range_file = f"{ASSETS_DIR}/{uuid.uuid4()}"
|
2022-03-15 11:58:59 +00:00
|
|
|
cmd = (
|
2022-02-01 13:42:41 +00:00
|
|
|
f'{NEOFS_CLI_EXEC} --rpc-endpoint {NEOFS_ENDPOINT} --wallet {wallet} '
|
2022-06-13 20:33:09 +00:00
|
|
|
f'object range --cid {cid} --oid {oid} --range {range_cut} --config {wallet_config} '
|
2022-06-06 13:47:13 +00:00
|
|
|
f'{options} --file {range_file} '
|
2022-03-15 11:58:59 +00:00
|
|
|
f'{"--bearer " + bearer if bearer else ""} '
|
|
|
|
)
|
|
|
|
_cmd_run(cmd)
|
2022-06-06 13:47:13 +00:00
|
|
|
content = ''
|
|
|
|
with open(range_file, 'rb') as fout:
|
|
|
|
content = fout.read()
|
|
|
|
return range_file, content
|
2022-03-15 11:58:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
@keyword('Search object')
|
2022-06-09 13:08:11 +00:00
|
|
|
def search_object(wallet: str, cid: str, keys: str = "", bearer: str = "", filters: dict = {},
|
2022-07-07 21:31:58 +00:00
|
|
|
expected_objects_list=[], wallet_config: str = WALLET_CONFIG, options: str = ""):
|
2022-06-09 13:08:11 +00:00
|
|
|
"""
|
2022-06-06 13:47:13 +00:00
|
|
|
SEARCH an Object.
|
2022-03-15 11:58:59 +00:00
|
|
|
|
|
|
|
Args:
|
2022-06-06 13:47:13 +00:00
|
|
|
wallet (str): wallet on whose behalf SEARCH is done
|
2022-03-15 11:58:59 +00:00
|
|
|
cid (str): ID of Container where we get the Object from
|
|
|
|
keys(optional, str): any keys for Object SEARCH which `neofs-cli object search`
|
|
|
|
accepts, e.g. `--oid`
|
|
|
|
bearer (optional, str): path to Bearer Token file, appends to `--bearer` key
|
|
|
|
filters (optional, dict): key=value pairs to filter Objects
|
|
|
|
expected_objects_list (optional, list): a list of ObjectIDs to compare found Objects with
|
2022-06-13 20:33:09 +00:00
|
|
|
wallet_config(optional, str): path to the wallet config
|
2022-07-07 21:31:58 +00:00
|
|
|
options(optional, str): any other options which `neofs-cli object search` might accept
|
2022-03-15 11:58:59 +00:00
|
|
|
Returns:
|
|
|
|
(list): list of found ObjectIDs
|
2022-06-09 13:08:11 +00:00
|
|
|
"""
|
2022-03-15 11:58:59 +00:00
|
|
|
filters_result = ""
|
|
|
|
if filters:
|
|
|
|
filters_result += "--filters "
|
|
|
|
logger.info(filters)
|
2022-07-07 21:31:58 +00:00
|
|
|
filters_result += ','.join(
|
|
|
|
map(lambda i: f"'{i} EQ {filters[i]}'", filters))
|
2022-03-15 11:58:59 +00:00
|
|
|
|
|
|
|
cmd = (
|
2022-02-01 13:42:41 +00:00
|
|
|
f'{NEOFS_CLI_EXEC} --rpc-endpoint {NEOFS_ENDPOINT} --wallet {wallet} '
|
2022-06-13 20:33:09 +00:00
|
|
|
f'object search {keys} --cid {cid} {filters_result} --config {wallet_config} '
|
2022-07-07 21:31:58 +00:00
|
|
|
f'{"--bearer " + bearer if bearer else ""} {options}'
|
2022-03-15 11:58:59 +00:00
|
|
|
)
|
|
|
|
output = _cmd_run(cmd)
|
|
|
|
|
|
|
|
found_objects = re.findall(r'(\w{43,44})', output)
|
|
|
|
|
|
|
|
if expected_objects_list:
|
|
|
|
if sorted(found_objects) == sorted(expected_objects_list):
|
|
|
|
logger.info(f"Found objects list '{found_objects}' ",
|
|
|
|
f"is equal for expected list '{expected_objects_list}'")
|
|
|
|
else:
|
2022-06-06 13:47:13 +00:00
|
|
|
logger.warn(f"Found object list {found_objects} ",
|
2022-06-09 13:08:11 +00:00
|
|
|
f"is not equal to expected list '{expected_objects_list}'")
|
2022-03-15 11:58:59 +00:00
|
|
|
|
|
|
|
return found_objects
|
|
|
|
|
|
|
|
|
|
|
|
@keyword('Head object')
|
2022-06-09 13:08:11 +00:00
|
|
|
def head_object(wallet: str, cid: str, oid: str, bearer_token: str = "",
|
|
|
|
options: str = "", endpoint: str = "", json_output: bool = True,
|
2022-06-13 20:33:09 +00:00
|
|
|
is_raw: bool = False, is_direct: bool = False, wallet_config: str = WALLET_CONFIG):
|
2022-06-09 13:08:11 +00:00
|
|
|
"""
|
2022-03-15 11:58:59 +00:00
|
|
|
HEAD an Object.
|
|
|
|
|
|
|
|
Args:
|
2022-06-06 13:47:13 +00:00
|
|
|
wallet (str): wallet on whose behalf HEAD is done
|
2022-03-15 11:58:59 +00:00
|
|
|
cid (str): ID of Container where we get the Object from
|
|
|
|
oid (str): ObjectID to HEAD
|
|
|
|
bearer_token (optional, str): path to Bearer Token file, appends to `--bearer` key
|
|
|
|
options (optional, str): any options which `neofs-cli object head` accepts
|
|
|
|
endpoint(optional, str): NeoFS endpoint to send request to
|
|
|
|
json_output(optional, bool): return reponse in JSON format or not; this flag
|
|
|
|
turns into `--json` key
|
|
|
|
is_raw(optional, bool): send "raw" request or not; this flag
|
|
|
|
turns into `--raw` key
|
2022-04-07 18:48:03 +00:00
|
|
|
is_direct(optional, bool): send request directly to the node or not; this flag
|
|
|
|
turns into `--ttl 1` key
|
2022-06-13 20:33:09 +00:00
|
|
|
wallet_config(optional, str): path to the wallet config
|
2022-03-15 11:58:59 +00:00
|
|
|
Returns:
|
|
|
|
depending on the `json_output` parameter value, the function returns
|
|
|
|
(dict): HEAD response in JSON format
|
|
|
|
or
|
|
|
|
(str): HEAD response as a plain text
|
2022-06-09 13:08:11 +00:00
|
|
|
"""
|
2022-03-15 11:58:59 +00:00
|
|
|
cmd = (
|
|
|
|
f'{NEOFS_CLI_EXEC} --rpc-endpoint {endpoint if endpoint else NEOFS_ENDPOINT} '
|
2022-06-13 20:33:09 +00:00
|
|
|
f'--wallet {wallet} --config {wallet_config} '
|
2022-03-15 11:58:59 +00:00
|
|
|
f'object head --cid {cid} --oid {oid} {options} '
|
|
|
|
f'{"--bearer " + bearer_token if bearer_token else ""} '
|
|
|
|
f'{"--json" if json_output else ""} '
|
2022-04-07 18:48:03 +00:00
|
|
|
f'{"--raw" if is_raw else ""} '
|
|
|
|
f'{"--ttl 1" if is_direct else ""}'
|
2022-03-15 11:58:59 +00:00
|
|
|
)
|
2022-05-31 22:17:40 +00:00
|
|
|
output = _cmd_run(cmd)
|
2022-03-15 11:58:59 +00:00
|
|
|
if not json_output:
|
|
|
|
return output
|
|
|
|
|
|
|
|
decoded = ""
|
|
|
|
try:
|
|
|
|
decoded = json.loads(output)
|
|
|
|
except Exception as exc:
|
|
|
|
# If we failed to parse output as JSON, the cause might be
|
|
|
|
# the plain text string in the beginning of the output.
|
|
|
|
# Here we cut off first string and try to parse again.
|
|
|
|
logger.info(f"failed to parse output: {exc}")
|
|
|
|
logger.info("parsing output in another way")
|
|
|
|
fst_line_idx = output.find('\n')
|
|
|
|
decoded = json.loads(output[fst_line_idx:])
|
|
|
|
|
|
|
|
# If response is Complex Object header, it has `splitId` key
|
|
|
|
if 'splitId' in decoded.keys():
|
|
|
|
logger.info("decoding split header")
|
|
|
|
return json_transformers.decode_split_header(decoded)
|
|
|
|
|
|
|
|
# If response is Last or Linking Object header,
|
|
|
|
# it has `header` dictionary and non-null `split` dictionary
|
|
|
|
if 'split' in decoded['header'].keys():
|
|
|
|
if decoded['header']['split']:
|
|
|
|
logger.info("decoding linking object")
|
|
|
|
return json_transformers.decode_linking_object(decoded)
|
|
|
|
|
|
|
|
if decoded['header']['objectType'] == 'STORAGE_GROUP':
|
|
|
|
logger.info("decoding storage group")
|
|
|
|
return json_transformers.decode_storage_group(decoded)
|
|
|
|
|
2022-06-06 13:47:13 +00:00
|
|
|
if decoded['header']['objectType'] == 'TOMBSTONE':
|
|
|
|
logger.info("decoding tombstone")
|
|
|
|
return json_transformers.decode_tombstone(decoded)
|
|
|
|
|
2022-03-15 11:58:59 +00:00
|
|
|
logger.info("decoding simple header")
|
|
|
|
return json_transformers.decode_simple_header(decoded)
|