diff options
author | Javier Hernandez <jhernandez@soleta.eu> | 2023-10-19 10:21:44 +0200 |
---|---|---|
committer | Jose M. Guisado <jguisado@soleta.eu> | 2023-10-19 12:26:31 +0200 |
commit | 1c2e5c4c96f3c8b114508de7f713dda8ebfcb877 (patch) | |
tree | c0171176de8d971dcd734fb573bdd341bf4f0479 /cli | |
parent | 7c30d569100a351f2a0a6430093abb8791f44fcf (diff) |
cli: add room
add functionality to add and remove a room
to add a new room use the command: ogcli add room --name <name>
--netmask <netmask> --center <center> --gateway <gateway> [--location
<location>] [--ntp <ntp>] [--dns <dns>] [--group <group>]
for instance, to create a room with name 'dummyroom', netmask
'255.255.255.0' and gateway 10.141.10.1 that is inside the center with
id 1, use:
$ ogcli add room --name dummyroom --netmask 255.255.255.0 --gateway
10.141.10.1 --center 1
Optionally, it is possible to provide additional information such as
location (--location <location>), ntp server (--ntp <ntp>), dns server
(--dns <dns>), and a group to contain the room (--group <group>).
to delete a room use: ogcli delete room --id <id>
for instance, to delete room with id 4 use:
$ ogcli delete room --id 4
Diffstat (limited to 'cli')
-rw-r--r-- | cli/cli.py | 9 | ||||
-rw-r--r-- | cli/objects/room.py | 88 | ||||
-rw-r--r-- | cli/utils.py | 9 |
3 files changed, 103 insertions, 3 deletions
@@ -16,6 +16,7 @@ from cli.objects.reboot import OgReboot from cli.objects.repo import OgRepo from cli.objects.server import OgServer from cli.objects.center import OgCenter +from cli.objects.room import OgRoom import argparse import requests import sys @@ -196,7 +197,7 @@ class OgCLI(): OgImage.update_image(self.rest, args[1:]) def delete(self, args): - choices = ['server', 'center'] + choices = ['server', 'center', 'room'] parser = argparse.ArgumentParser(prog='ogcli delete') parser.add_argument('delete_obj', choices=choices) @@ -210,9 +211,11 @@ class OgCLI(): OgServer.delete_server(self.rest, args[1:]) elif parsed_args.delete_obj == 'center': OgCenter.delete_center(self.rest, args[1:]) + elif parsed_args.delete_obj == 'room': + OgRoom.delete_room(self.rest, args[1:]) def add(self, args): - choices = ['server', 'repo', 'center'] + choices = ['server', 'repo', 'center', 'room'] parser = argparse.ArgumentParser(prog='ogcli add') parser.add_argument('add_obj', choices=choices) @@ -228,3 +231,5 @@ class OgCLI(): OgRepo.add_repo(self.rest, args[1:]) elif parsed_args.add_obj == 'center': OgCenter.add_center(self.rest, args[1:]) + elif parsed_args.add_obj == 'room': + OgRoom.add_room(self.rest, args[1:]) diff --git a/cli/objects/room.py b/cli/objects/room.py new file mode 100644 index 0000000..66d0dba --- /dev/null +++ b/cli/objects/room.py @@ -0,0 +1,88 @@ +import sys +import argparse +from cli.utils import check_address + +class OgRoom(): + + @staticmethod + def add_room(rest, args): + parser = argparse.ArgumentParser(prog='ogcli add room') + parser.add_argument('--name', + nargs='?', + required=True, + help='give a name to the room') + parser.add_argument('--netmask', + nargs='?', + required=True, + help='provide the netmask for the room') + parser.add_argument('--center', + nargs='?', + type=int, + required=True, + help='provide the id of the center that will contain the room') + parser.add_argument('--location', + nargs='?', + required=False, + help='specify the location of the room') + parser.add_argument('--gateway', + nargs='?', + required=True, + help='address of the main gateway in the room') + parser.add_argument('--ntp', + nargs='?', + required=False, + help='address of the ntp server') + parser.add_argument('--dns', + nargs='?', + required=False, + help='address of the dns server') + parser.add_argument('--group', + nargs='?', + type=int, + required=False, + help='id of the group that will contain the room') + parsed_args = parser.parse_args(args) + + err = False + if parsed_args.netmask and not check_address(parsed_args.netmask): + print('invalid netmask address', file=sys.stderr) + err = True + if parsed_args.gateway and not check_address(parsed_args.gateway): + print('invalid gateway address', file=sys.stderr) + err = True + if parsed_args.ntp and not check_address(parsed_args.ntp): + print('invalid ntp address', file=sys.stderr) + err = True + if parsed_args.dns and not check_address(parsed_args.dns): + print('invalid dns address', file=sys.stderr) + err = True + if err: + parser.print_help(file=sys.stderr) + sys.exit(1) + + payload = { + 'name': parsed_args.name, + 'netmask': parsed_args.netmask, + 'center': parsed_args.center + } + if parsed_args.gateway: + payload['gateway'] = parsed_args.gateway + if parsed_args.ntp: + payload['ntp'] = parsed_args.ntp + if parsed_args.dns: + payload['dns'] = parsed_args.dns + if parsed_args.group: + payload['group'] = parsed_args.group + rest.post('/room/add', payload=payload) + + @staticmethod + def delete_room(rest, args): + parser = argparse.ArgumentParser(prog='ogcli delete room') + parser.add_argument('--id', + nargs='?', + required=True, + help='room id in database') + parsed_args = parser.parse_args(args) + payload = {'id': parsed_args.id} + rest.post('/room/delete', payload=payload) + diff --git a/cli/utils.py b/cli/utils.py index b267207..88c15a4 100644 --- a/cli/utils.py +++ b/cli/utils.py @@ -6,7 +6,7 @@ # (at your option) any later version. import json - +import ipaddress def scope_lookup(scope_id, scope_type, d): if scope_id == d.get('id') and scope_type == d.get('type'): @@ -32,3 +32,10 @@ def ips_in_scope(scope): def print_json(text): payload = json.loads(text) print(json.dumps(payload, sort_keys=True, indent=2)) + +def check_address(addr): + try: + ip = ipaddress.ip_address(addr) + return True + except: + return False |