Change func parsing netmap

Signed-off-by: Dmitriy Zayakin <d.zayakin@yadro.com>
This commit is contained in:
Dmitriy Zayakin 2023-10-18 10:55:22 +03:00 committed by Dmitriy Zayakin
parent 1c3bbe26f7
commit cff5db5a67
2 changed files with 42 additions and 25 deletions

View file

@ -29,14 +29,15 @@ class StorageObjectInfo(ObjectRef):
class NodeNetmapInfo: class NodeNetmapInfo:
node_id: str = None node_id: str = None
node_status: str = None node_status: str = None
node_data_ip: str = None node_data_ips: list[str] = None
cluster_name: str = None cluster_name: str = None
continent: str = None continent: str = None
country: str = None country: str = None
country_code: str = None country_code: str = None
external_address: str = None external_address: list[str] = None
location: str = None location: str = None
node: str = None node: str = None
price: int = None
sub_div: str = None sub_div: str = None
sub_div_code: int = None sub_div_code: int = None
un_locode: str = None un_locode: str = None

View file

@ -8,6 +8,7 @@ Helper functions to use with `frostfs-cli`, `neo-go` and other CLIs.
import csv import csv
import json import json
import logging import logging
import re
import subprocess import subprocess
import sys import sys
from contextlib import suppress from contextlib import suppress
@ -138,32 +139,47 @@ def log_command_execution(cmd: str, output: Union[str, TypedDict]) -> None:
def parse_netmap_output(output: str) -> list[NodeNetmapInfo]: def parse_netmap_output(output: str) -> list[NodeNetmapInfo]:
""" """
The cli command will return something like.
Epoch: 240
Node 1: 01234 ONLINE /ip4/10.10.10.10/tcp/8080
Continent: Europe
Country: Russia
CountryCode: RU
ExternalAddr: /ip4/10.10.11.18/tcp/8080
Location: Moskva
Node: 10.10.10.12
Price: 5
SubDiv: Moskva
SubDivCode: MOW
UN-LOCODE: RU MOW
role: alphabet
The code will parse each line and return each node as dataclass. The code will parse each line and return each node as dataclass.
""" """
netmap_list = output.split("Node ")[1:] netmap_nodes = output.split("Node ")[1:]
dataclass_list = [] dataclasses_netmap = []
for node in netmap_list: result_netmap = {}
node = node.replace("\t", "").split("\n")
node = *node[0].split(" ")[1:-1], *[row.split(": ")[-1] for row in node[1:-1]]
dataclass_list.append(NodeNetmapInfo(*node))
return dataclass_list regexes = {
"node_id": r"\d+: (?P<node_id>\w+)",
"node_data_ips": r"(?P<node_data_ips>/ip4/.+?)$",
"node_status": r"(?P<node_status>ONLINE|OFFLINE)",
"cluster_name": r"ClusterName: (?P<cluster_name>\w+)",
"continent": r"Continent: (?P<continent>\w+)",
"country": r"Country: (?P<country>\w+)",
"country_code": r"CountryCode: (?P<country_code>\w+)",
"external_address": r"ExternalAddr: (?P<external_address>/ip[4].+?)$",
"location": r"Location: (?P<location>\w+.*)",
"node": r"Node: (?P<node>\d+\.\d+\.\d+\.\d+)",
"price": r"Price: (?P<price>\d+)",
"sub_div": r"SubDiv: (?P<sub_div>.*)",
"sub_div_code": r"SubDivCode: (?P<sub_div_code>\w+)",
"un_locode": r"UN-LOCODE: (?P<un_locode>\w+.*)",
"role": r"role: (?P<role>\w+)",
}
for node in netmap_nodes:
for key, regex in regexes.items():
search_result = re.search(regex, node, flags=re.MULTILINE)
if key == "node_data_ips":
result_netmap[key] = search_result[key].strip().split(" ")
continue
if key == "external_address":
result_netmap[key] = search_result[key].strip().split(",")
continue
if search_result == None:
result_netmap[key] = None
continue
result_netmap[key] = search_result[key].strip()
dataclasses_netmap.append(NodeNetmapInfo(**result_netmap))
return dataclasses_netmap
def parse_cmd_table(output: str, delimiter="|") -> list[dict[str, str]]: def parse_cmd_table(output: str, delimiter="|") -> list[dict[str, str]]: