Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt napalm modules to the new library structure #44439

Merged
merged 3 commits into from Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 17 additions & 15 deletions salt/proxy/napalm.py
Expand Up @@ -16,9 +16,21 @@
The ``napalm`` proxy module requires NAPALM_ library to be installed: ``pip install napalm``
Please check Installation_ for complete details.

.. _NAPALM: https://napalm.readthedocs.io
.. _Installation: https://napalm.readthedocs.io/en/latest/installation.html
.. _NAPALM: https://napalm-automation.net/
.. _Installation: http://napalm.readthedocs.io/en/latest/installation/index.html

.. note::

Beginning with Salt release 2017.7.3, it is recommended to use
``napalm`` >= ``2.0.0``. The library has been unified into a monolithic
package, as in opposite to separate packages per driver. For more details
you can check `this document <https://napalm-automation.net/reunification/>`_.
While it will still work with the old packages, bear in mind that the NAPALM
core team will maintain only the main ``napalm`` package.

Moreover, for additional capabilities, the users can always define a
library that extends NAPALM's base capabilities and configure the
``provider`` option (see below).

Pillar
------
Expand Down Expand Up @@ -59,7 +71,7 @@
.. versionadded:: 2017.7.0

provider: ``napalm_base``
The module that provides the ``get_network_device`` function.
The library that provides the ``get_network_device`` function.
This option is useful when the user has more specific needs and requires
to extend the NAPALM capabilities using a private library implementation.
The only constraint is that the alternative library needs to have the
Expand Down Expand Up @@ -129,17 +141,7 @@
import logging
log = logging.getLogger(__file__)

# Import third party lib
try:
# will try to import NAPALM
# https://github.com/napalm-automation/napalm
# pylint: disable=W0611
import napalm_base
# pylint: enable=W0611
HAS_NAPALM = True
except ImportError:
HAS_NAPALM = False

# Import Salt modules
from salt.ext import six
import salt.utils.napalm

Expand All @@ -163,7 +165,7 @@


def __virtual__():
return HAS_NAPALM or (False, 'Please install the NAPALM library: `pip install napalm`!')
return salt.utils.napalm.virtual(__opts__, 'napalm', __file__)

# ----------------------------------------------------------------------------------------------------------------------
# helper functions -- will not be exported
Expand Down
22 changes: 20 additions & 2 deletions salt/utils/napalm.py
Expand Up @@ -31,11 +31,27 @@
# will try to import NAPALM
# https://github.com/napalm-automation/napalm
# pylint: disable=W0611
import napalm_base
import napalm
import napalm.base as napalm_base
# pylint: enable=W0611
HAS_NAPALM = True
HAS_NAPALM_BASE = False # doesn't matter anymore, but needed for the logic below
log.debug('napalm seems to be installed')
try:
NAPALM_MAJOR = int(napalm.__version__.split('.')[0])
log.debug('napalm version: %s', napalm.__version__)
except AttributeError:
NAPALM_MAJOR = 0
except ImportError:
log.info('napalm doesnt seem to be installed, trying to import napalm_base')
HAS_NAPALM = False
try:
import napalm_base
log.debug('napalm_base seems to be installed')
HAS_NAPALM_BASE = True
except ImportError:
log.info('napalm_base doesnt seem to be installed either')
HAS_NAPALM_BASE = False

try:
# try importing ConnectionClosedException
Expand Down Expand Up @@ -81,7 +97,9 @@ def virtual(opts, virtualname, filename):
'''
Returns the __virtual__.
'''
if HAS_NAPALM and (is_proxy(opts) or is_minion(opts)):
if ((HAS_NAPALM and NAPALM_MAJOR >= 2) or HAS_NAPALM_BASE) and (is_proxy(opts) or is_minion(opts)):
if HAS_NAPALM_BASE:
log.info('You still seem to use napalm_base. Please consider upgrading to napalm >= 2.0.0')
return virtualname
else:
return (
Expand Down