mirror of https://git.48k.eu/ogcli/
				
				
				
			
		
			
				
	
	
		
			141 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			Python
		
	
	
			
		
		
	
	
			141 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			Python
		
	
	
# Copyright (C) 2020-2024 Soleta Networks <opengnsys@soleta.eu>
 | 
						|
#
 | 
						|
# This program is free software: you can redistribute it and/or modify it under
 | 
						|
# the terms of the GNU Affero General Public License as published by the
 | 
						|
# Free Software Foundation; either version 3 of the License, or
 | 
						|
# (at your option) any later version.
 | 
						|
 | 
						|
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('--desc',
 | 
						|
                    nargs='?',
 | 
						|
                    required=False,
 | 
						|
                    help='room description')
 | 
						|
        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('--folder',
 | 
						|
                    nargs='?',
 | 
						|
                    type=int,
 | 
						|
                    required=False,
 | 
						|
                    help='id of the folder 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.folder:
 | 
						|
            payload['folder_id'] = parsed_args.folder
 | 
						|
        if parsed_args.desc:
 | 
						|
            payload['location'] = parsed_args.desc
 | 
						|
        rest.post('/room/add', payload=payload)
 | 
						|
 | 
						|
    @staticmethod
 | 
						|
    def update_room(rest, args):
 | 
						|
        parser = argparse.ArgumentParser(prog='ogcli update room')
 | 
						|
        parser.add_argument('--id',
 | 
						|
                    type=int,
 | 
						|
                    nargs='?',
 | 
						|
                    required=True,
 | 
						|
                    help='room id in database')
 | 
						|
        parser.add_argument('--name',
 | 
						|
                    nargs='?',
 | 
						|
                    required=True,
 | 
						|
                    help='the updated name for the room')
 | 
						|
        parser.add_argument('--netmask',
 | 
						|
                    nargs='?',
 | 
						|
                    required=True,
 | 
						|
                    help='the updated netmask for the room')
 | 
						|
        parser.add_argument('--gateway',
 | 
						|
                    nargs='?',
 | 
						|
                    required=True,
 | 
						|
                    help='updated address of the main gateway in 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 err:
 | 
						|
            parser.print_help(file=sys.stderr)
 | 
						|
            sys.exit(1)
 | 
						|
 | 
						|
        payload = {
 | 
						|
            'id': parsed_args.id,
 | 
						|
            'name': parsed_args.name,
 | 
						|
            'netmask': parsed_args.netmask,
 | 
						|
            'gateway': parsed_args.gateway,
 | 
						|
        }
 | 
						|
        
 | 
						|
        rest.post('/room/update', payload=payload)
 | 
						|
 | 
						|
    @staticmethod
 | 
						|
    def delete_room(rest, args):
 | 
						|
        parser = argparse.ArgumentParser(prog='ogcli delete room')
 | 
						|
        parser.add_argument('--id',
 | 
						|
                            type=int,
 | 
						|
                            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)
 | 
						|
 |