Пример #1
0
    def handle(self, env, values):
        router = values['payload']['router']
        host_id = values["publisher_id"].replace("network.", "", 1)
        project_id = values['_context_project_id']
        router_id = encode_router_id(router['id'])
        host = self.inv.get_by_id(env, host_id)

        fetcher = CliFetchHostVservice()
        fetcher.setup(env=env, origin=self.origin)
        router_doc = fetcher.get_vservice(host_id, router_id)
        gateway_info = router['external_gateway_info']

        if gateway_info:
            network_id = gateway_info['network_id']
            self.add_router_document(env, network_id, router_doc, host)
            self.add_children_documents(env, project_id, network_id, host,
                                        router_doc)
        else:
            self.add_router_document(env, None, router_doc, host)

        # scan links and cliques
        FindLinksForVserviceVnics().add_links(search={"parent_id": router_id})
        scanner = Scanner()
        scanner.setup(env=env, origin=self.origin)
        scanner.scan_cliques()
        self.log.info("Finished router added.")

        return EventResult(result=True,
                           related_object=router_id,
                           display_context=router_id)
Пример #2
0
    def get_by_id(self, env, object_id):
        interface = self.values["payload"]["router_interface"]
        host_id = self.values["publisher_id"].replace("network.", "", 1)
        router_id = encode_router_id(interface['id'])

        if object_id == host_id:
            return HOST
        elif object_id == router_id:
            return ROUTER_DOCUMENT
        elif object_id == ROUTER_DOCUMENT["gw_port_id"]:
            return PORT_DOC
        else:
            return None
Пример #3
0
    def _do_test(self, values, scanner_class_mock):
        self.values = values
        self.payload = self.values['payload']
        self.router = self.payload['router']
        self.host_id = self.values['publisher_id'].replace("network.", "", 1)
        self.router_id = encode_router_id(self.router['id'])
        self.gw_port_id = ROUTER_DOCUMENT['gw_port_id']

        scanner_mock = scanner_class_mock.return_value

        self.inv.get_by_id.side_effect = self.get_by_id

        res = EventRouterUpdate().handle(self.env, self.values)

        self.assertTrue(res.result)
        self.assertTrue(scanner_mock.scan_cliques.called)
Пример #4
0
    def test_handle_interface_delete(self, port_delete_class_mock):
        self.values = EVENT_PAYLOAD_INTERFACE_DELETE
        self.payload = self.values['payload']
        self.interface = self.payload['router_interface']
        self.port_id = self.interface['port_id']
        self.router_id = encode_router_id(self.interface['id'])

        port_delete_mock = port_delete_class_mock.return_value
        port_delete_mock.delete_port.return_value = EventResult(result=True)

        self.inv.get_by_id.side_effect = self.get_by_id

        res = EventInterfaceDelete().handle(self.env, self.values)

        self.assertTrue(res.result)
        self.assertTrue(port_delete_mock.delete_port.called)
        self.inv.set.assert_called_with(ROUTER_DOCUMENT)
Пример #5
0
    def test_handle_router_add(self, cli_fetch_vservice_class_mock,
                               find_links_class_mock, scanner_class_mock,
                               subnet_add_class_mock, port_add_class_mock):
        self.values = EVENT_PAYLOAD_ROUTER_ADD
        self.payload = self.values['payload']
        self.router = self.payload['router']
        self.network_id = self.router['external_gateway_info']['network_id']
        self.host_id = self.values["publisher_id"].replace("network.", "", 1)
        self.router_id = encode_router_id(self.router['id'])

        self.inv.get_by_id.side_effect = self.get_by_id

        cli_fetch_vservice_mock = cli_fetch_vservice_class_mock.return_value
        cli_fetch_vservice_mock.get_vservice.return_value = ROUTER_DOCUMENT

        find_links_mock = find_links_class_mock.return_value
        scanner_mock = scanner_class_mock.return_value
        subnet_add_mock = subnet_add_class_mock.return_value
        subnet_add_mock.add_port_document.return_value = True
        port_add_mock = port_add_class_mock.return_value
        port_add_mock.add_vnic_document.return_value = True
        port_add_mock.add_vnic_folder.return_value = True

        res = EventRouterAdd().handle(self.env, self.values)

        self.assertTrue(res.result)

        # Assert that router has been added to db
        router_insertions = [
            call_args for call_args in self.inv.set.call_args_list
            if call_args[0][0]['type'] == 'vservice'
        ]
        self.assertTrue(router_insertions)
        self.assertTrue(subnet_add_mock.add_ports_folder.called)
        self.assertTrue(subnet_add_mock.add_port_document.called)
        self.assertTrue(port_add_mock.add_vnics_folder.called)
        self.assertTrue(port_add_mock.add_vnic_document.called)
        find_links_mock.add_links\
            .assert_called_with(search={"parent_id": self.router_id})
        self.assertTrue(scanner_mock.scan_cliques.called)
Пример #6
0
    def handle(self, env, values):
        payload = values['payload']

        if 'publisher_id' not in values:
            self.log.error(
                "Publisher_id is not in event values. Aborting router delete")
            return EventResult(result=False, retry=False)

        if 'router_id' in payload:
            router_id = payload['router_id']
        elif 'id' in payload:
            router_id = payload['id']
        else:
            router_id = payload.get('router', {}).get('id')

        if not router_id:
            self.log.error(
                "Router id is not in payload. Aborting router delete")
            return EventResult(result=False, retry=False)

        router_full_id = encode_router_id(router_id)
        return self.delete_handler(env, router_full_id, "vservice")
Пример #7
0
    def handle(self, env, values):
        interface = values['payload']['router_interface']
        port_id = interface['port_id']
        router_id = encode_router_id(interface['id'])

        # update router document
        port_doc = self.inv.get_by_id(env, port_id)
        if not port_doc:
            self.log.info("Interface deleting handler: port document not found.")
            return EventResult(result=False, retry=False)
        network_id = port_doc['network_id']

        router_doc = self.inv.get_by_id(env, router_id)
        if router_doc and network_id in router_doc.get('network', []):
            router_doc['network'].remove(network_id)
            self.inv.set(router_doc)

        # delete port document
        result = EventPortDelete().delete_port(env, port_id)
        result.related_object = interface['id']
        result.display_context = network_id
        return result
Пример #8
0
    def handle(self, env, values):
        interface = values['payload']['router_interface']
        project_id = values['_context_project_id']
        project = values['_context_project_name']
        host_id = values["publisher_id"].replace("network.", "", 1)
        port_id = interface['port_id']
        subnet_id = interface['subnet_id']
        router_id = encode_router_id(interface['id'])

        network_document = self.inv.get_by_field(env,
                                                 "network",
                                                 "subnet_ids",
                                                 subnet_id,
                                                 get_single=True)
        if not network_document:
            self.log.info(
                "network document not found, aborting interface adding")
            return EventResult(result=False, retry=True)
        network_name = network_document['name']
        network_id = network_document['id']

        # add router-interface port document.
        if not ApiAccess.regions:
            fetcher = ApiFetchRegions()
            fetcher.setup(env=env, origin=self.origin)
            fetcher.get(project_id)
        port_doc = EventSubnetAdd().add_port_document(
            env, port_id, network_name=network_name)

        mac_address = port_doc['mac_address'] if port_doc else None

        # add vnic document
        host = self.inv.get_by_id(env, host_id)
        router_doc = self.inv.get_by_id(env, router_id)

        add_vnic_document = partial(EventPortAdd().add_vnic_document,
                                    env=env,
                                    host=host,
                                    object_id=interface['id'],
                                    object_type='router',
                                    network_name=network_name,
                                    router_name=router_doc['name'],
                                    mac_address=mac_address)

        ret = add_vnic_document()
        if ret is False:
            # try it again to fetch vnic document, vnic will be created a little bit late before CLI fetch.
            time.sleep(self.delay)
            self.log.info(
                "Wait {} seconds, and then fetch vnic document again.".format(
                    self.delay))
            add_vnic_document()

        # update the router document: gw_port_id, network.
        self.update_router(env, project, network_id, network_name, router_doc,
                           host_id)

        # update vservice-vnic, vnic-network,
        FindLinksForVserviceVnics().add_links(search={"parent_id": router_id})
        scanner = Scanner()
        scanner.setup(env=env, origin=self.origin)

        scanner.scan_cliques()
        self.log.info("Finished router-interface added.")

        return EventResult(result=True,
                           related_object=interface['id'],
                           display_context=network_id)
Пример #9
0
    def handle(self, env, values):
        payload = values['payload']
        router = payload['router']

        project_id = values['_context_project_id']
        host_id = values["publisher_id"].replace("network.", "", 1)
        router_id = payload['id'] if 'id' in payload else router['id']

        router_full_id = encode_router_id(router_id)
        router_doc = self.inv.get_by_id(env, router_full_id)
        if not router_doc:
            self.log.info(
                "Router document not found, aborting router updating")
            return EventResult(result=False, retry=True)

        router_doc['admin_state_up'] = router['admin_state_up']
        router_doc['name'] = router['name']
        gateway_info = router.get('external_gateway_info')
        if gateway_info is None:
            # when delete gateway, need to delete the port relate document.
            port_doc = {}
            if router_doc.get('gw_port_id'):
                port_doc = self.inv.get_by_id(env, router_doc['gw_port_id'])
                EventPortDelete().delete_port(env, router_doc['gw_port_id'])

            if router_doc.get('network'):
                if port_doc:
                    router_doc['network'].remove(port_doc['network_id'])
                router_doc['gw_port_id'] = None

                # remove related links
                self.inv.delete('links', {'source_id': router_full_id})
        else:
            if 'network' in router_doc:
                if gateway_info['network_id'] not in router_doc['network']:
                    router_doc['network'].append(gateway_info['network_id'])
            else:
                router_doc['network'] = [gateway_info['network_id']]
            # update static route
            router_doc['routes'] = router['routes']

            # add gw_port_id info and port document.
            fetcher = CliFetchHostVservice()
            fetcher.setup(env=env, origin=self.origin)
            router_vservice = fetcher.get_vservice(host_id, router_full_id)
            if router_vservice.get('gw_port_id'):
                router_doc['gw_port_id'] = router_vservice['gw_port_id']

            host = self.inv.get_by_id(env, host_id)
            EventRouterAdd().add_children_documents(env, project_id,
                                                    gateway_info['network_id'],
                                                    host, router_doc)

            # rescan the vnic links.
            FindLinksForVserviceVnics().add_links(
                search={'parent_id': router_full_id + '-vnics'})
        self.inv.set(router_doc)

        # update the cliques.
        scanner = Scanner()
        scanner.setup(env=env, origin=self.origin)
        scanner.scan_cliques()
        self.log.info("Finished router update.")
        return EventResult(result=True,
                           related_object=router_full_id,
                           display_context=router_full_id)