From 8e3bcb0861eaba8d3daf8e2b4f38e34c6fa05fc6 Mon Sep 17 00:00:00 2001 From: Salvatore Orlando Date: Thu, 20 May 2021 09:58:52 -0700 Subject: [PATCH] Admin util for validating VC clusters Add operation check-compute-clusters to validate whether required clusters are present on VC Change-Id: Ib1a00014bc0fcbda34e97bdd73422ea8cb2197e1 --- .../admin/plugins/nsxv/resources/config.py | 35 +++++++++++++++++++ vmware_nsx/shell/resources.py | 4 ++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/vmware_nsx/shell/admin/plugins/nsxv/resources/config.py b/vmware_nsx/shell/admin/plugins/nsxv/resources/config.py index 59e80d8c2a..22cfea9ab5 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv/resources/config.py +++ b/vmware_nsx/shell/admin/plugins/nsxv/resources/config.py @@ -12,10 +12,14 @@ # License for the specific language governing permissions and limitations # under the License. +import sys + from neutron_lib.callbacks import registry from oslo_config import cfg from oslo_log import log as logging +from oslo_vmware import vim_util +from vmware_nsx.dvs import dvs from vmware_nsx.plugins.nsx_v.vshield.common import exceptions from vmware_nsx.shell.admin.plugins.common import constants from vmware_nsx.shell.admin.plugins.common import utils as admin_utils @@ -39,6 +43,37 @@ def validate_configuration(resource, event, trigger, **kwargs): LOG.info("Configuration validation succeeded") +def check_clusters(resource, event, trigger, **kwargs): + clusters_str = "" + if kwargs.get('property'): + properties = admin_utils.parse_multi_keyval_opt(kwargs['property']) + clusters_str = properties.get('clusters', None) + + if not clusters_str: + LOG.error("No cluster to look for was specified") + return + + clusters = clusters_str.split(",") + mgr = dvs.VCManagerBase() + session = mgr.get_vc_session() + data = session.invoke_api(vim_util, 'get_objects', session.vim, + 'ClusterComputeResource', 100) + while data: + for item in data.objects: + if item.obj.value in clusters[:]: + clusters.remove(item.obj.value) + data = vim_util.continue_retrieval(session.vim, data) + if not clusters: + LOG.info("Clusters %s found on VC backend", clusters_str) + else: + LOG.error("Clusters %s not found on VC backend", ",".join(clusters)) + sys.exit(1) + + registry.subscribe(validate_configuration, constants.CONFIG, shell.Operations.VALIDATE.value) + +registry.subscribe(check_clusters, + constants.CONFIG, + shell.Operations.CHECK_COMPUTE_CLUSTERS.value) diff --git a/vmware_nsx/shell/resources.py b/vmware_nsx/shell/resources.py index 33161bbe46..5104af22ae 100644 --- a/vmware_nsx/shell/resources.py +++ b/vmware_nsx/shell/resources.py @@ -78,6 +78,7 @@ class Operations(enum.Enum): UPDATE_TIER0 = 'update-tier0' UPDATE_FIREWALL_MATCH = 'update-nat-firewall-match' SET_STATUS_ERROR = 'set-status-error' + CHECK_COMPUTE_CLUSTERS = 'check-compute-clusters' ops = [op.value for op in Operations] @@ -246,7 +247,8 @@ nsxv_resources = { [Operations.NSX_LIST.value, Operations.NSX_CLEAN.value]), constants.CONFIG: Resource(constants.CONFIG, - [Operations.VALIDATE.value]), + [Operations.VALIDATE.value, + Operations.CHECK_COMPUTE_CLUSTERS.value]), constants.BGP_GW_EDGE: Resource(constants.BGP_GW_EDGE, [Operations.CREATE.value, Operations.DELETE.value,