Skip to content

Commit 5fb42cd

Browse files
authored
Merge pull request #13 from MarkGodwin/feature/raw_data
Feature/raw data
2 parents 990db8e + 7291064 commit 5fb42cd

File tree

13 files changed

+170
-68
lines changed

13 files changed

+170
-68
lines changed

.vscode/launch.json

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,36 @@
55
"version": "0.2.0",
66
"configurations": [
77
{
8-
"name": "Python: Test Client",
8+
"name": "CLI: List Devices",
99
"type": "python",
1010
"request": "launch",
11-
"program": "sample_client.py",
12-
"console": "integratedTerminal",
11+
"module": "tplink_omada_client.cli",
1312
"justMyCode": true,
14-
"args": ["https://192.168.1.50", "admin", "admin"]
13+
"args": ["devices"]
14+
},
15+
{
16+
"name": "CLI: List Targets",
17+
"type": "python",
18+
"request": "launch",
19+
"module": "tplink_omada_client.cli",
20+
"justMyCode": true,
21+
"args": ["targets"]
22+
},
23+
{
24+
"name": "CLI: List Switches",
25+
"type": "python",
26+
"request": "launch",
27+
"module": "tplink_omada_client.cli",
28+
"justMyCode": true,
29+
"args": ["switches"]
30+
},
31+
{
32+
"name": "CLI: Get Switch",
33+
"type": "python",
34+
"request": "launch",
35+
"module": "tplink_omada_client.cli",
36+
"justMyCode": true,
37+
"args": ["switch", "Main Switch", "--dump"]
1538
}
1639
]
1740
}

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ This package provides a simple CLI for interacting with one or more Omada Contro
2929
CLI, you must first target a Controller.
3030

3131
```sh
32-
$ omada -t NAME target --url https://your.omada.controller.here --user admin --password password --site MySite
32+
$ omada -t NAME target --url https://your.omada.controller.here --user admin --password password --site MySite --set-default
3333
```
3434

3535
Where `NAME` is a name of your choosing to identify the targeted controller. `--site` defaults to the Omada
@@ -39,7 +39,7 @@ password.
3939
Once you have successfully targeted a controller you can test that things are working by running:
4040

4141
```sh
42-
$ omada -t NAME devices
42+
$ omada devices
4343
```
4444

4545
This will list all the devices being managed by your controller.
@@ -50,6 +50,9 @@ To see a list of all the available commands, run:
5050
$ omada -h
5151
```
5252

53+
You can set up multiple targets (controllers and sites), and specify the target with the `-t <NAME>` parameter.
54+
If you don't specify a target, the default will be used, if that has been set.
55+
5356
The CLI is still young so if there is any functionality you need, please create an issue and let us know.
5457

5558
## Future

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "tplink_omada_client"
7-
version = "1.2.1"
7+
version = "1.2.2"
88
authors = [
99
{ name="Mark Godwin", email="author@example.com" },
1010
]

src/tplink_omada_client/cli/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
command_switches,
1313
command_target,
1414
command_targets,
15+
command_default
1516
)
1617

1718
def main(argv: Union[Sequence[str], None] = None) -> int:
@@ -22,6 +23,7 @@ def main(argv: Union[Sequence[str], None] = None) -> int:
2223
parser.add_argument(
2324
'-t', '--target',
2425
help="The target Omada controller",
26+
default=""
2527
)
2628

2729
subparsers = parser.add_subparsers(
@@ -34,6 +36,7 @@ def main(argv: Union[Sequence[str], None] = None) -> int:
3436
command_switches.arg_parser(subparsers)
3537
command_target.arg_parser(subparsers)
3638
command_targets.arg_parser(subparsers)
39+
command_default.arg_parser(subparsers)
3740

3841
try:
3942
args = parser.parse_args(args=argv)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Implementation of 'default' command"""
2+
from argparse import _SubParsersAction, ArgumentError
3+
import getpass
4+
5+
from .util import assert_target_argument
6+
from .config import ControllerConfig, set_default_target, to_omada_connection, get_target_config
7+
8+
async def command_target(args) -> int:
9+
"""Executes 'default' command"""
10+
target = assert_target_argument(args)
11+
config = get_target_config(target)
12+
13+
set_default_target(target)
14+
return 0
15+
16+
def arg_parser(subparsers: _SubParsersAction) -> None:
17+
"""Configures argument parser for 'default' command"""
18+
set_parser = subparsers.add_parser(
19+
"default",
20+
help="Sets the default target",
21+
)
22+
set_parser.set_defaults(func=command_target)
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
"""Implementation for 'devices' command"""
22

33
from argparse import _SubParsersAction
4-
4+
import json
55
from .config import get_target_config, to_omada_connection
6-
from .util import assert_target_argument
6+
from .util import dump_raw_data, get_target_argument
77

88
async def command_devices(args) -> int:
99
"""Executes 'devices' command"""
10-
controller = assert_target_argument(args)
10+
controller = get_target_argument(args)
1111
config = get_target_config(controller)
1212

1313
async with to_omada_connection(config) as client:
1414
site_client = await client.get_site_client(config.site)
1515
for device in await site_client.get_devices():
16-
print(f"{device.mac} {device.ip_address:>15} {device.type:>6} {device.name:20} {device.model_display_name}")
16+
print(f"{device.mac} {device.ip_address:>15} {device.type:>7} {device.name:20} {device.model_display_name}")
17+
dump_raw_data(args, device)
1718
return 0
1819

1920
def arg_parser(subparsers: _SubParsersAction) -> None:
@@ -22,4 +23,6 @@ def arg_parser(subparsers: _SubParsersAction) -> None:
2223
"devices",
2324
aliases=['d'],
2425
help="Lists devices managed by Omada Controller")
26+
devices_parser.add_argument('-d', '--dump', help="Output raw device information", action='store_true')
27+
2528
devices_parser.set_defaults(func=command_devices)

src/tplink_omada_client/cli/command_switch.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
from argparse import ArgumentParser
44

55
from .config import get_target_config, to_omada_connection
6-
from .util import assert_target_argument
6+
from .util import dump_raw_data, get_mac, get_target_argument
77

88
async def command_switch(args) -> int:
99
"""Executes 'switch' command"""
10-
controller = assert_target_argument(args)
10+
controller = get_target_argument(args)
1111
config = get_target_config(controller)
1212

1313
async with to_omada_connection(config) as client:
1414
site_client = await client.get_site_client(config.site)
15-
switch = await site_client.get_switch(args['mac'])
15+
mac = await get_mac(site_client, args['mac'])
16+
switch = await site_client.get_switch(mac)
1617
print(f"Name: {switch.name}")
1718
print(f"Address: {switch.mac} ({switch.ip_address})")
1819
print(f"Ports: {switch.number_of_ports}")
@@ -29,6 +30,8 @@ async def command_switch(args) -> int:
2930
for downlink in switch.downlink:
3031
print(f"- {downlink.mac} {downlink.name}")
3132

33+
dump_raw_data(args, switch)
34+
3235
return 0
3336

3437
def arg_parser(subparsers) -> None:
@@ -41,5 +44,7 @@ def arg_parser(subparsers) -> None:
4144

4245
switch_parser.add_argument(
4346
"mac",
44-
help="The MAC address of the switch",
47+
help="The MAC address or name of the switch",
4548
)
49+
switch_parser.add_argument('-d', '--dump', help="Output raw device information", action='store_true')
50+

src/tplink_omada_client/cli/command_switches.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from argparse import _SubParsersAction
44

55
from .config import get_target_config, to_omada_connection
6-
from .util import assert_target_argument
6+
from .util import dump_raw_data, get_target_argument
77

88
async def command_switches(args) -> int:
99
"""Executes 'switches' command"""
10-
controller = assert_target_argument(args)
10+
controller = get_target_argument(args)
1111
config = get_target_config(controller)
1212

1313
async with to_omada_connection(config) as client:
@@ -25,6 +25,8 @@ async def command_switches(args) -> int:
2525
print("\u26a1", end="")
2626
else:
2727
print(" ", end="")
28+
29+
dump_raw_data(args, switch)
2830
print()
2931
return 0
3032

@@ -35,3 +37,4 @@ def arg_parser(subparsers: _SubParsersAction) -> None:
3537
aliases=['s'],
3638
help="Lists switches managed by Omada Controller")
3739
switches_parser.set_defaults(func=command_switches)
40+
switches_parser.add_argument('-d', '--dump', help="Output raw device information", action='store_true')

src/tplink_omada_client/cli/command_target.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
"""Implementation of 'target' command"""
2-
from argparse import _SubParsersAction
2+
from argparse import _SubParsersAction, ArgumentError
33
import getpass
4+
import json
45

5-
from .util import assert_target_argument
6+
from .util import get_target_argument
67
from .config import ControllerConfig, set_target_config, to_omada_connection
78

89
async def command_target(args) -> int:
910
"""Executes 'target' command"""
10-
target = assert_target_argument(args)
11+
target = get_target_argument(args)
12+
1113
if args['password']:
1214
password = args['password']
1315
else:
@@ -18,15 +20,16 @@ async def command_target(args) -> int:
1820
password=password,
1921
site=args['site'],
2022
)
23+
2124
# Connect to controller to validate config
2225
async with to_omada_connection(config) as client:
2326
name = await client.get_controller_name()
2427
for site in await client.get_sites():
2528
if args['site'] == site.name:
2629
print(f"Set target {target} to controller {name} and site {site.name}")
27-
set_target_config(target, config)
30+
set_target_config(target, config, args['set_default'])
2831

29-
return 1
32+
return 0
3033

3134
def arg_parser(subparsers: _SubParsersAction) -> None:
3235
"""Configures argument parser for 'target' command"""
@@ -54,4 +57,9 @@ def arg_parser(subparsers: _SubParsersAction) -> None:
5457
help="The Omada site to user",
5558
default = "Default",
5659
)
60+
set_parser.add_argument(
61+
'-sd',
62+
'--set-default',
63+
help="Set this target as the default",
64+
action='store_true')
5765

src/tplink_omada_client/cli/command_targets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
async def command_targets(args) -> int: # pylint: disable=unused-argument
99
"""Executes 'targets' command"""
1010
controllers = get_targets()
11-
for (controller, url) in controllers:
12-
print(f"{controller:15} {url}")
11+
for (controller, url, is_default) in controllers:
12+
print(f"{('*' if is_default else' ')} {controller:15} {url}")
1313
return 0
1414

1515
def arg_parser(subparsers: _SubParsersAction) -> None:

0 commit comments

Comments
 (0)