From 470fea025e74aa2061559b02569c12235f252fdc Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Mon, 24 Dec 2018 15:23:28 +0200 Subject: [PATCH] Add manager status validation to validate connection When validating if a connection with the NSX manager is up, also check the manager status directly Change-Id: I5de7054a058a74e8237a3344e0951ce37976c135 --- vmware_nsxlib/v3/__init__.py | 29 +++++++++++++++++++++++++++-- vmware_nsxlib/v3/cluster.py | 4 ++++ vmware_nsxlib/v3/config.py | 4 +++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/vmware_nsxlib/v3/__init__.py b/vmware_nsxlib/v3/__init__.py index c39a99a1..92414e93 100644 --- a/vmware_nsxlib/v3/__init__.py +++ b/vmware_nsxlib/v3/__init__.py @@ -69,8 +69,10 @@ class NsxLibBase(object): def set_config(self, nsxlib_config): """Set config user provided and extend it according to application""" self.nsxlib_config = nsxlib_config - self.nsxlib_config.extend(keepalive_section=self.keepalive_section, - url_base=self.client_url_prefix) + self.nsxlib_config.extend( + keepalive_section=self.keepalive_section, + validate_connection_method=self.validate_connection_method, + url_base=self.client_url_prefix) @abc.abstractproperty def client_url_prefix(self): @@ -80,6 +82,10 @@ class NsxLibBase(object): def keepalive_section(self): pass + @abc.abstractproperty + def validate_connection_method(self): + pass + @abc.abstractmethod def init_api(self): pass @@ -312,6 +318,20 @@ class NsxLib(NsxLibBase): def keepalive_section(self): return 'transport-zones' + @property + def validate_connection_method(self): + """Return a method that will validate the NSX manager status""" + def check_manager_status(client, manager_url): + status = client.get('node/services/manager/status', silent=True) + if (not status or 'runtime_state' not in status or + status['runtime_state'] != 'running'): + msg = _("Manager is not in running state: %s") % status + LOG.warning(msg) + raise exceptions.ResourceNotFound( + manager=manager_url, operation=msg) + + return check_manager_status + def get_version(self): if self.nsx_version: return self.nsx_version @@ -451,6 +471,11 @@ class NsxPolicyLib(NsxLibBase): def keepalive_section(self): return 'infra' + @property + def validate_connection_method(self): + # TODO(asarfaty): Find an equivalent api to check policy status + pass + def get_version(self): """Get the NSX Policy manager version diff --git a/vmware_nsxlib/v3/cluster.py b/vmware_nsxlib/v3/cluster.py index 08380bf9..b11a51e7 100644 --- a/vmware_nsxlib/v3/cluster.py +++ b/vmware_nsxlib/v3/cluster.py @@ -189,6 +189,10 @@ class NSXRequestsHTTPProvider(AbstractHTTPProvider): LOG.warning(msg) raise exceptions.ResourceNotFound( manager=endpoint.provider.url, operation=msg) + # Also check the manager state directly + if cluster_api.nsxlib_config.validate_connection_method: + cluster_api.nsxlib_config.validate_connection_method( + client, endpoint.provider.url) def new_connection(self, cluster_api, provider): config = cluster_api.nsxlib_config diff --git a/vmware_nsxlib/v3/config.py b/vmware_nsxlib/v3/config.py index 71333dbc..7d462fa1 100644 --- a/vmware_nsxlib/v3/config.py +++ b/vmware_nsxlib/v3/config.py @@ -145,9 +145,11 @@ class NsxLibConfig(object): 'dhcp_profile_uuid is not used by the nsxlib, and will ' 'be removed from its configuration in the future.') - def extend(self, keepalive_section, url_base=None): + def extend(self, keepalive_section, validate_connection_method=None, + url_base=None): """Called by library code to initialize application-specific data""" self.keepalive_section = keepalive_section + self.validate_connection_method = validate_connection_method self.url_base = url_base def _attribute_by_index(self, scalar_or_list, index):