Skip to content

Commit 2063c48

Browse files
committed
http: api: devices: generate device name if not provided
1 parent 4204bf3 commit 2063c48

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@ def register(parent: cli.Subparsers) -> None:
1111
parser = parent.add_parser(
1212
"create-lua", formatter_class=argparse.ArgumentDefaultsHelpFormatter
1313
)
14-
parser.add_argument("-r", "--runtime-id", help="Runtime ID of the Lua device")
1514
parser.add_argument(
16-
"-b", "--blueprint-id", help="Blueprint ID of the Lua device"
15+
"-r", "--runtime-id", required=True, help="Runtime ID of the Lua device"
16+
)
17+
parser.add_argument(
18+
"-b", "--blueprint-id", required=True, help="Blueprint ID of the Lua device"
1719
)
1820
parser.add_argument("-s", "--slug", help="Slug of the Lua device")
19-
parser.add_argument("name", help="Name of the Lua device to create")
21+
parser.add_argument("-n", "--name", help="Name of the Lua device to create")
2022

2123
@staticmethod
2224
async def run(args: argparse.Namespace) -> None:
2325
async with http.api.Client(http.api.Config.from_env()) as client:
2426
device = await client.devices.create_lua(
25-
name=args.name,
2627
runtime_id=args.runtime_id,
2728
blueprint_id=args.blueprint_id,
29+
name=args.name,
2830
slug=args.slug,
2931
)
3032
print(json.dumps(device.to_dto()))

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ def register(parent: cli.Subparsers) -> None:
1111
parser = parent.add_parser(
1212
"create-standalone", formatter_class=argparse.ArgumentDefaultsHelpFormatter
1313
)
14-
parser.add_argument("name", help="Name of the standalone device to create")
1514
parser.add_argument(
16-
"-s", "--site-id", help="Site ID to create device in", default=None
15+
"-n", "--name", help="Name of the standalone device to create"
1716
)
17+
parser.add_argument("--slug", help="Slug of the standalone device to create")
18+
parser.add_argument("-s", "--site-id", help="Site ID to create device in")
1819

1920
@staticmethod
2021
async def run(args: argparse.Namespace) -> None:
2122
async with http.api.Client(http.api.Config.from_env()) as client:
2223
device = await client.devices.create_standalone(
23-
args.name, site_id=args.site_id
24+
name=args.name, site_id=args.site_id, slug=args.slug
2425
)
2526
print(json.dumps(device.to_dto()))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def register(parent: cli.Subparsers) -> None:
1313
)
1414
parser.add_argument("-s", "--site-id", help="Site ID to create device in")
1515
parser.add_argument("--hardware-id", help="Hardware ID of the VUCM device")
16-
parser.add_argument("name", help="Name of the VUCM device to create")
16+
parser.add_argument("-n", "--name", help="Name of the VUCM device to create")
1717

1818
@staticmethod
1919
async def run(args: argparse.Namespace) -> None:

src/enapter/http/api/devices/client.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import random
2+
import time
23
from typing import AsyncGenerator
34

45
import httpx
@@ -8,6 +9,7 @@
89

910
from .communication_config import CommunicationConfig
1011
from .device import Device
12+
from .device_type import DeviceType
1113
from .mqtt_protocol import MQTTProtocol
1214

1315

@@ -17,8 +19,13 @@ def __init__(self, client: httpx.AsyncClient) -> None:
1719
self._client = client
1820

1921
async def create_standalone(
20-
self, name: str, site_id: str | None = None, slug: str | None = None
22+
self,
23+
name: str | None = None,
24+
site_id: str | None = None,
25+
slug: str | None = None,
2126
) -> Device:
27+
if name is None:
28+
name = random_device_name(DeviceType.STANDALONE)
2229
url = "v3/provisioning/standalone"
2330
response = await self._client.post(
2431
url, json={"name": name, "site_id": site_id, "slug": slug}
@@ -27,8 +34,13 @@ async def create_standalone(
2734
return await self.get(device_id=response.json()["device_id"])
2835

2936
async def create_vucm(
30-
self, name: str, hardware_id: str | None = None, site_id: str | None = None
37+
self,
38+
name: str | None = None,
39+
hardware_id: str | None = None,
40+
site_id: str | None = None,
3141
) -> Device:
42+
if name is None:
43+
name = random_device_name(DeviceType.VIRTUAL_UCM)
3244
if hardware_id is None:
3345
hardware_id = random_hardware_id()
3446
url = "v3/provisioning/vucm"
@@ -41,8 +53,14 @@ async def create_vucm(
4153
)
4254

4355
async def create_lua(
44-
self, name: str, runtime_id: str, blueprint_id: str, slug: str | None = None
56+
self,
57+
runtime_id: str,
58+
blueprint_id: str,
59+
name: str | None = None,
60+
slug: str | None = None,
4561
) -> Device:
62+
if name is None:
63+
name = random_device_name(DeviceType.LUA)
4664
url = "v3/provisioning/lua_device"
4765
response = await self._client.post(
4866
url,
@@ -136,5 +154,9 @@ async def generate_communication_config(
136154
return CommunicationConfig.from_dto(response.json()["config"])
137155

138156

157+
def random_device_name(device_type: DeviceType) -> str:
158+
return f"{device_type.value} ({time.time()})"
159+
160+
139161
def random_hardware_id() -> str:
140162
return "V" + "".join(f"{b:02X}" for b in random.randbytes(16))

0 commit comments

Comments
 (0)