From 04b707569873d9a0bb35255fa2703821f2e96fc8 Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Mon, 30 May 2016 09:55:41 +0300 Subject: [PATCH] [Admin-Util][NSX-v3]: list routers which are missing from backend To run this: nsxadmin -r routers -o list-mismatches Positive output example: All routers exist on the NSX manager Negative output example: Found 1 routers missing from the NSX manager +------+--------------------------------------+--------------------------------------+ | name | neutron_id | nsx_id | +------+--------------------------------------+--------------------------------------+ | rtr2 | faf48970-444b-4d9e-9e88-952246c635c7 | bf2a1965-299f-4714-8925-9b9c82e2c9e8 | +------+--------------------------------------+--------------------------------------+ Change-Id: I163dae3d1eecf6afc075cbc0e7020cd1cfb87af2 --- .../shell/admin/plugins/common/constants.py | 1 + .../admin/plugins/nsxv3/resources/routers.py | 85 +++++++++++++++++++ vmware_nsx/shell/nsxadmin.py | 2 + 3 files changed, 88 insertions(+) create mode 100644 vmware_nsx/shell/admin/plugins/nsxv3/resources/routers.py diff --git a/vmware_nsx/shell/admin/plugins/common/constants.py b/vmware_nsx/shell/admin/plugins/common/constants.py index 56613faa05..533758a0d2 100644 --- a/vmware_nsx/shell/admin/plugins/common/constants.py +++ b/vmware_nsx/shell/admin/plugins/common/constants.py @@ -22,6 +22,7 @@ NSXV_PLUGIN = 'vmware_nsx.plugin.NsxVPlugin' # Common Resource Constants NETWORKS = 'networks' +ROUTERS = 'routers' # NSXV3 Resource Constants FIREWALL_SECTIONS = 'firewall-sections' diff --git a/vmware_nsx/shell/admin/plugins/nsxv3/resources/routers.py b/vmware_nsx/shell/admin/plugins/nsxv3/resources/routers.py new file mode 100644 index 0000000000..6d421901b6 --- /dev/null +++ b/vmware_nsx/shell/admin/plugins/nsxv3/resources/routers.py @@ -0,0 +1,85 @@ +# Copyright 2016 VMware, Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +import logging + +from vmware_nsx._i18n import _LI +from vmware_nsx.common import exceptions as nsx_exc +from vmware_nsx.db import db as nsx_db +from vmware_nsx.nsxlib.v3 import client as nsx_client +from vmware_nsx.nsxlib.v3 import cluster as nsx_cluster +from vmware_nsx.nsxlib.v3 import resources as nsx_resources +from vmware_nsx.shell.admin.plugins.common import constants +from vmware_nsx.shell.admin.plugins.common import formatters +from vmware_nsx.shell.admin.plugins.common import utils as admin_utils +from vmware_nsx.shell import nsxadmin as shell + +from neutron.callbacks import registry +from neutron import context as neutron_context +from neutron.db import db_base_plugin_v2 +from neutron.db import l3_db + +LOG = logging.getLogger(__name__) + + +class RoutersPlugin(db_base_plugin_v2.NeutronDbPluginV2, + l3_db.L3_NAT_db_mixin): + pass + + +def get_router_client(): + _api_cluster = nsx_cluster.NSXClusteredAPI() + _nsx_client = nsx_client.NSX3Client(_api_cluster) + return nsx_resources.LogicalRouter(_nsx_client) + + +@admin_utils.output_header +def list_missing_routers(resource, event, trigger, **kwargs): + """List neutron routers that are missing the NSX backend router + """ + plugin = RoutersPlugin() + admin_cxt = neutron_context.get_admin_context() + neutron_routers = plugin.get_routers(admin_cxt) + router_client = get_router_client() + routers = [] + for router in neutron_routers: + neutron_id = router['id'] + # get the network nsx id from the mapping table + nsx_id = nsx_db.get_nsx_router_id(admin_cxt.session, + neutron_id) + if not nsx_id: + routers.append({'name': router['name'], + 'neutron_id': neutron_id, + 'nsx_id': None}) + else: + try: + router_client.get(nsx_id) + except nsx_exc.ResourceNotFound: + routers.append({'name': router['name'], + 'neutron_id': neutron_id, + 'nsx_id': nsx_id}) + if len(routers) > 0: + title = _LI("Found %d routers missing from the NSX " + "manager:") % len(routers) + LOG.info(formatters.output_formatter( + title, routers, + ['name', 'neutron_id', 'nsx_id'])) + else: + LOG.info(_LI("All routers exist on the NSX manager")) + + +registry.subscribe(list_missing_routers, + constants.ROUTERS, + shell.Operations.LIST_MISMATCHES.value) diff --git a/vmware_nsx/shell/nsxadmin.py b/vmware_nsx/shell/nsxadmin.py index 184bc6245e..ded5cdac3e 100644 --- a/vmware_nsx/shell/nsxadmin.py +++ b/vmware_nsx/shell/nsxadmin.py @@ -87,6 +87,8 @@ nsxv3_resources = { [Operations.LIST_MISMATCHES.value]), constants.PORTS: Resource(constants.PORTS, [Operations.LIST_MISMATCHES.value]), + constants.ROUTERS: Resource(constants.ROUTERS, + [Operations.LIST_MISMATCHES.value]), } # Add supported NSX-V resources in this dictionary