Beispiel #1
0
 def setUp(self):
     super(TestSwtichMachineAPI, self).setUp()
     # Create one switch in database
     with database.session() as session:
         test_switch = Switch(ip=self.SWITCH_IP_ADDRESS1)
         test_switch.credential = self.SWITCH_CREDENTIAL
         session.add(test_switch)
Beispiel #2
0
 def setUp(self):
     super(TestSwtichMachineAPI, self).setUp()
     # Create one switch in database
     with database.session() as session:
         test_switch = Switch(ip=self.SWITCH_IP_ADDRESS1)
         test_switch.credential = self.SWITCH_CREDENTIAL
         session.add(test_switch)
Beispiel #3
0
    def post(self):
        """
        Insert switch IP and the credential to db. Invoke a task to poll
        switch at the same time.

        :param ip: switch IP address
        :param credential: a dict for accessing the switch
        """
        ip_addr = None
        credential = None
        logging.debug('post switch request from curl is %s', request.data)
        json_data = json.loads(request.data)
        ip_addr = json_data['switch']['ip']
        credential = json_data['switch']['credential']

        logging.info('post switch ip_addr=%s credential=%s(%s)', ip_addr,
                     credential, type(credential))

        if not util.is_valid_ip(ip_addr):
            error_msg = "Invalid IP address format!"
            return errors.handle_invalid_usage(
                errors.UserInvalidUsage(error_msg))

        new_switch = {}
        with database.session() as session:
            switch = session.query(ModelSwitch).filter_by(ip=ip_addr).first()
            logging.info('switch for ip %s: %s', ip_addr, switch)

            if switch:
                error_msg = "IP address '%s' already exists" % ip_addr
                value = {'failedSwitch': switch.id}
                return errors.handle_duplicate_object(
                    errors.ObjectDuplicateError(error_msg), value)

            switch = ModelSwitch(ip=ip_addr)
            switch.credential = credential
            session.add(switch)
            session.flush()
            new_switch['id'] = switch.id
            new_switch['ip'] = switch.ip
            new_switch['state'] = switch.state
            link = {
                'rel': 'self',
                'href': '/'.join((self.ENDPOINT, str(switch.id)))
            }
            new_switch['link'] = link

        celery.send_task("compass.tasks.pollswitch", (ip_addr, ))
        logging.info('new switch added: %s', new_switch)
        return util.make_json_response(202, {
            "status": "accepted",
            "switch": new_switch
        })
Beispiel #4
0
    def post(self):
        """
        Insert switch IP and the credential to db. Invoke a task to poll
        switch at the same time.

        :param ip: switch IP address
        :param credential: a dict for accessing the switch
        """
        ip_addr = None
        credential = None
        logging.debug('post switch request from curl is %s', request.data)
        json_data = json.loads(request.data)
        ip_addr = json_data['switch']['ip']
        credential = json_data['switch']['credential']

        logging.info('post switch ip_addr=%s credential=%s(%s)',
                     ip_addr, credential, type(credential))

        if not util.is_valid_ip(ip_addr):
            error_msg = "Invalid IP address format!"
            return errors.handle_invalid_usage(
                errors.UserInvalidUsage(error_msg)
                )

        new_switch = {}
        with database.session() as session:
            switch = session.query(ModelSwitch).filter_by(ip=ip_addr).first()
            logging.info('switch for ip %s: %s', ip_addr, switch)

            if switch:
                error_msg = "IP address '%s' already exists" % ip_addr
                value = {'failedSwitch': switch.id}
                return errors.handle_duplicate_object(
                    errors.ObjectDuplicateError(error_msg), value
                )

            switch = ModelSwitch(ip=ip_addr)
            switch.credential = credential
            session.add(switch)
            session.flush()
            new_switch['id'] = switch.id
            new_switch['ip'] = switch.ip
            new_switch['state'] = switch.state
            link = {'rel': 'self',
                    'href': '/'.join((self.ENDPOINT, str(switch.id)))}
            new_switch['link'] = link

        celery.send_task("compass.tasks.pollswitch", (ip_addr,))
        logging.info('new switch added: %s', new_switch)
        return util.make_json_response(
            202, {"status": "accepted",
                  "switch":  new_switch}
            )
Beispiel #5
0
def set_fake_switch_machine():
    with database.session() as session:
        credential = { 'version'    :  'v2c',
                       'community'  :  'public',
                     }
        switches = [ {'ip': '192.168.100.250'},
                     {'ip': '192.168.100.251'},
                     {'ip': '192.168.100.252'},
        ]
        session.query(Switch).delete()
        session.query(Machine).delete()
        ip_switch ={}
        for item in switches:
            logging.info('add switch %s', item)     
            switch = Switch(ip=item['ip'], vendor_info='huawei',
                            state='under_monitoring')
            switch.credential = credential
            session.add(switch)
            ip_switch[item['ip']] = switch
        session.flush()

        machines = [
            {'mac': '00:0c:29:32:76:85', 'port':50, 'vlan':1, 'switch_ip':'192.168.100.250'},
            {'mac': '00:0c:29:fa:cb:72', 'port':51, 'vlan':1, 'switch_ip':'192.168.100.250'},
            {'mac': '28:6e:d4:64:c7:4a', 'port':1, 'vlan':1, 'switch_ip':'192.168.100.251'},
            {'mac': '28:6e:d4:64:c7:4c', 'port':2, 'vlan':1, 'switch_ip':'192.168.100.251'},
            {'mac': '28:6e:d4:46:c4:25', 'port': 40, 'vlan': 1, 'switch_ip': '192.168.100.252'},
            {'mac': '26:6e:d4:4d:c6:be', 'port': 41, 'vlan': 1, 'switch_ip': '192.168.100.252'},
            {'mac': '28:6e:d4:62:da:38', 'port': 42, 'vlan': 1, 'switch_ip': '192.168.100.252'},
            {'mac': '28:6e:d4:62:db:76', 'port': 43, 'vlan': 1, 'switch_ip': '192.168.100.252'},
        ]
       
        for item in machines:
            logging.info('add machine %s', item)
            machine = Machine(mac=item['mac'], port=item['port'],
                              vlan=item['vlan'],
                              switch_id=ip_switch[item['switch_ip']].id)
            session.add(machine)
Beispiel #6
0
def set_fake_switch_machine():
    """Set fake switches and machines for test."""
    with database.session() as session:
        credential = {
            'version': 'v2c',
            'community': 'public',
        }
        switches = [
            {
                'ip': '192.168.100.250'
            },
            {
                'ip': '192.168.100.251'
            },
            {
                'ip': '192.168.100.252'
            },
        ]
        session.query(Switch).delete()
        session.query(Machine).delete()
        ip_switch = {}
        for item in switches:
            logging.info('add switch %s', item)
            switch = Switch(ip=item['ip'],
                            vendor_info='huawei',
                            state='under_monitoring')
            switch.credential = credential
            session.add(switch)
            ip_switch[item['ip']] = switch
        session.flush()

        machines = [
            {
                'mac': '00:0c:29:32:76:85',
                'port': 50,
                'vlan': 1,
                'switch_ip': '192.168.100.250'
            },
            {
                'mac': '00:0c:29:fa:cb:72',
                'port': 51,
                'vlan': 1,
                'switch_ip': '192.168.100.250'
            },
            {
                'mac': '28:6e:d4:64:c7:4a',
                'port': 1,
                'vlan': 1,
                'switch_ip': '192.168.100.251'
            },
            {
                'mac': '28:6e:d4:64:c7:4c',
                'port': 2,
                'vlan': 1,
                'switch_ip': '192.168.100.251'
            },
            {
                'mac': '28:6e:d4:46:c4:25',
                'port': 40,
                'vlan': 1,
                'switch_ip': '192.168.100.252'
            },
            {
                'mac': '26:6e:d4:4d:c6:be',
                'port': 41,
                'vlan': 1,
                'switch_ip': '192.168.100.252'
            },
            {
                'mac': '28:6e:d4:62:da:38',
                'port': 42,
                'vlan': 1,
                'switch_ip': '192.168.100.252'
            },
            {
                'mac': '28:6e:d4:62:db:76',
                'port': 43,
                'vlan': 1,
                'switch_ip': '192.168.100.252'
            },
        ]

        for item in machines:
            logging.info('add machine %s', item)
            machine = Machine(mac=item['mac'],
                              port=item['port'],
                              vlan=item['vlan'],
                              switch_id=ip_switch[item['switch_ip']].id)
            session.add(machine)