Skip to content

Commit 70e1eec

Browse files
authored
[core][feat] Add node command (#2212)
1 parent 4695370 commit 70e1eec

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

fixcore/fixcore/cli/command.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
from fixcore.db.model import QueryModel
111111
from fixcore.db.runningtaskdb import RunningTaskData
112112
from fixcore.error import CLIParseError, ClientError, CLIExecutionError, NotEnoughPermissions
113-
from fixcore.ids import ConfigId, TaskId, InfraAppName, TaskDescriptorId, GraphName, Email, Password
113+
from fixcore.ids import ConfigId, TaskId, InfraAppName, TaskDescriptorId, GraphName, Email, Password, NodeId
114114
from fixcore.infra_apps.manifest import AppManifest
115115
from fixcore.infra_apps.package_manager import Failure
116116
from fixcore.model.graph_access import Section, EdgeTypes
@@ -6553,6 +6553,46 @@ async def process_element(el: JsonElement) -> JsonElement:
65536553
return CLIFlow(setup_stream, required_permissions={Permission.read})
65546554

65556555

6556+
class NodeCommand(CLICommand):
6557+
"""
6558+
node delete <node_id>
6559+
"""
6560+
6561+
@property
6562+
def name(self) -> str:
6563+
return "node"
6564+
6565+
def args_info(self) -> ArgsInfo:
6566+
return {
6567+
"delete": [
6568+
ArgInfo("--keep-history", False, help_text=""),
6569+
ArgInfo(None, True, help_text="<node_id>"),
6570+
]
6571+
}
6572+
6573+
def info(self) -> str:
6574+
return "Operations on nodes"
6575+
6576+
def parse(self, arg: Optional[str] = None, ctx: CLIContext = EmptyContext, **kwargs: Any) -> CLIAction:
6577+
async def delete_node(node_id: NodeId, keep_history: bool) -> AsyncIterator[str]:
6578+
model = await self.dependencies.model_handler.load_model(ctx.graph_name)
6579+
await self.dependencies.db_access.get_graph_db(ctx.graph_name).delete_node(node_id, model, keep_history)
6580+
yield f"Node {node_id} deleted."
6581+
6582+
if arg:
6583+
args = arg.split(maxsplit=1)
6584+
if len(args) == 2 and args[0] == "delete":
6585+
parser = NoExitArgumentParser()
6586+
parser.add_argument("node_id", type=str)
6587+
parser.add_argument("--keep-history", action="store_true")
6588+
parsed = parser.parse_args(args_parts_unquoted_parser.parse(args[1]))
6589+
return CLISource.single(
6590+
fn=partial(delete_node, node_id=parsed.node_id, keep_history=parsed.keep_history),
6591+
required_permissions={Permission.write},
6592+
)
6593+
return CLISource.single(lambda: stream.just(self.rendered_help(ctx)), required_permissions={Permission.read})
6594+
6595+
65566596
def all_commands(d: TenantDependencies) -> List[CLICommand]:
65576597
commands = [
65586598
AggregateCommand(d, "search"),
@@ -6582,6 +6622,7 @@ def all_commands(d: TenantDependencies) -> List[CLICommand]:
65826622
KindsCommand(d, "search", allowed_in_source_position=True),
65836623
LimitPart(d, "search"),
65846624
ListCommand(d, "format"),
6625+
NodeCommand(d, "action", allowed_in_source_position=True),
65856626
TemplatesCommand(d, "search", allowed_in_source_position=True),
65866627
PredecessorsPart(d, "search"),
65876628
ProtectCommand(d, "action"),

fixcore/tests/fixcore/cli/command_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,3 +1530,17 @@ async def detect(to_check: JsonElement) -> List[JsonElement]:
15301530
}
15311531
]
15321532
assert await detect({"foo": "innocent string"}) == []
1533+
1534+
1535+
@pytest.mark.asyncio
1536+
async def test_node(cli: CLI) -> None:
1537+
async def exec(cmd: str) -> List[JsonElement]:
1538+
res = await cli.execute_cli_command(cmd, list_sink)
1539+
return cast(List[JsonElement], res[0])
1540+
1541+
# Assert full graph
1542+
assert await exec("search all | count") == ["total matched: 113", "total unmatched: 0"]
1543+
# Delete account node
1544+
await exec("node delete sub_root")
1545+
# Graph_root and cloud node still exist
1546+
assert await exec("search all | count") == ["total matched: 2", "total unmatched: 0"]

fixcore/tests/fixcore/web/api_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ async def test_cli(core_client: FixInventoryClient) -> None:
398398

399399
# list all cli commands
400400
info = AccessJson(await core_client.cli_info())
401-
assert len(info.commands) == 46
401+
assert len(info.commands) == 47
402402

403403

404404
@pytest.mark.asyncio

0 commit comments

Comments
 (0)