Skip to content

Commit

Permalink
Ensure that packaging is installed (netbox-community#900)
Browse files Browse the repository at this point in the history
When the ImportError happens, fail and raise the error that we were
not able to import Version, instead of dying later on with a
'Version' is not defined error

This is related to netbox-community#899
  • Loading branch information
sc68cal authored and rodvand committed Dec 14, 2022
1 parent 278dc02 commit 195a7a8
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions plugins/module_utils/netbox_dcim.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

__metaclass__ = type

from ansible.module_utils.basic import missing_required_lib
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
NetboxModule,
ENDPOINT_NAME_MAPPING,
Expand Down Expand Up @@ -51,14 +52,19 @@

try:
from packaging.version import Version

HAS_PACKAGING = True
except ImportError as imp_exc:
PACKAGING_IMPORT_ERROR = imp_exc
else:
PACKAGING_IMPORT_ERROR = None
HAS_PACKAGING = False


class NetboxDcimModule(NetboxModule):
def __init__(self, module, endpoint):
if not HAS_PACKAGING:
self.module.fail_json(
msg=missing_required_lib("packaging"), exception=PACKAGING_IMPORT_ERROR
)
super().__init__(module, endpoint)

def run(self):
Expand Down

0 comments on commit 195a7a8

Please sign in to comment.