Skip to content

Commit 0e454bd

Browse files
committed
feat(cli): Implement Rule Engine management commands
1 parent efc09c3 commit 0e454bd

5 files changed

Lines changed: 120 additions & 1 deletion

File tree

src/enapter/cli/http/api/command.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .blueprint_command import BlueprintCommand
66
from .command_command import CommandCommand
77
from .device_command import DeviceCommand
8+
from .rule_engine_command import RuleEngineCommand
89
from .site_command import SiteCommand
910
from .telemetry_command import TelemetryCommand
1011

@@ -21,6 +22,7 @@ def register(parent: cli.Subparsers) -> None:
2122
BlueprintCommand,
2223
CommandCommand,
2324
DeviceCommand,
25+
RuleEngineCommand,
2426
SiteCommand,
2527
TelemetryCommand,
2628
]:
@@ -35,9 +37,11 @@ async def run(args: argparse.Namespace) -> None:
3537
await CommandCommand.run(args)
3638
case "device":
3739
await DeviceCommand.run(args)
40+
case "rule-engine":
41+
await RuleEngineCommand.run(args)
3842
case "site":
3943
await SiteCommand.run(args)
4044
case "telemetry":
4145
await TelemetryCommand.run(args)
4246
case _:
43-
raise NotImplementedError(args.device_command)
47+
raise NotImplementedError(args.api_command)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Rule Engine command group."""
2+
3+
import argparse
4+
5+
from enapter import cli
6+
7+
from .rule_engine_get_command import RuleEngineGetCommand
8+
from .rule_engine_resume_command import RuleEngineResumeCommand
9+
from .rule_engine_suspend_command import RuleEngineSuspendCommand
10+
11+
12+
class RuleEngineCommand(cli.Command):
13+
"""Command group for Rule Engine management."""
14+
15+
@staticmethod
16+
def register(parent: cli.Subparsers) -> None:
17+
"""Register the command group in the subparsers."""
18+
parser = parent.add_parser(
19+
"rule-engine", formatter_class=argparse.ArgumentDefaultsHelpFormatter
20+
)
21+
subparsers = parser.add_subparsers(dest="rule_engine_command", required=True)
22+
for command in [
23+
RuleEngineGetCommand,
24+
RuleEngineSuspendCommand,
25+
RuleEngineResumeCommand,
26+
]:
27+
command.register(subparsers)
28+
29+
@staticmethod
30+
async def run(args: argparse.Namespace) -> None:
31+
"""Run the sub-command."""
32+
match args.rule_engine_command:
33+
case "get":
34+
await RuleEngineGetCommand.run(args)
35+
case "suspend":
36+
await RuleEngineSuspendCommand.run(args)
37+
case "resume":
38+
await RuleEngineResumeCommand.run(args)
39+
case _:
40+
raise NotImplementedError(args.rule_engine_command)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Rule Engine get command."""
2+
3+
import argparse
4+
import json
5+
6+
from enapter import cli, http
7+
8+
9+
class RuleEngineGetCommand(cli.Command):
10+
"""Command to get the rule engine state."""
11+
12+
@staticmethod
13+
def register(parent: cli.Subparsers) -> None:
14+
"""Register the command in the subparsers."""
15+
parser = parent.add_parser(
16+
"get", formatter_class=argparse.ArgumentDefaultsHelpFormatter
17+
)
18+
parser.add_argument("-s", "--site-id", help="ID of the site to retrieve")
19+
20+
@staticmethod
21+
async def run(args: argparse.Namespace) -> None:
22+
"""Run the command."""
23+
async with http.api.Client(http.api.Config.from_env()) as client:
24+
engine = await client.rule_engine.get(site_id=args.site_id)
25+
print(json.dumps(engine.to_dto()))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Rule Engine resume command."""
2+
3+
import argparse
4+
import json
5+
6+
from enapter import cli, http
7+
8+
9+
class RuleEngineResumeCommand(cli.Command):
10+
"""Command to resume the rule engine."""
11+
12+
@staticmethod
13+
def register(parent: cli.Subparsers) -> None:
14+
"""Register the command in the subparsers."""
15+
parser = parent.add_parser(
16+
"resume", formatter_class=argparse.ArgumentDefaultsHelpFormatter
17+
)
18+
parser.add_argument("-s", "--site-id", help="ID of the site to resume")
19+
20+
@staticmethod
21+
async def run(args: argparse.Namespace) -> None:
22+
"""Run the command."""
23+
async with http.api.Client(http.api.Config.from_env()) as client:
24+
engine = await client.rule_engine.resume(site_id=args.site_id)
25+
print(json.dumps(engine.to_dto()))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Rule Engine suspend command."""
2+
3+
import argparse
4+
import json
5+
6+
from enapter import cli, http
7+
8+
9+
class RuleEngineSuspendCommand(cli.Command):
10+
"""Command to suspend the rule engine."""
11+
12+
@staticmethod
13+
def register(parent: cli.Subparsers) -> None:
14+
"""Register the command in the subparsers."""
15+
parser = parent.add_parser(
16+
"suspend", formatter_class=argparse.ArgumentDefaultsHelpFormatter
17+
)
18+
parser.add_argument("-s", "--site-id", help="ID of the site to suspend")
19+
20+
@staticmethod
21+
async def run(args: argparse.Namespace) -> None:
22+
"""Run the command."""
23+
async with http.api.Client(http.api.Config.from_env()) as client:
24+
engine = await client.rule_engine.suspend(site_id=args.site_id)
25+
print(json.dumps(engine.to_dto()))

0 commit comments

Comments
 (0)