summaryrefslogtreecommitdiffstats
path: root/cli/objects/room.py
blob: 66d0dba68f8ebaee8e6d913b7757e91c54da1d99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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)