Skip to content

Commit af6e4b1

Browse files
authored
Ensure that packaging is installed (#900)
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 #899
1 parent dcd8672 commit af6e4b1

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

plugins/module_utils/netbox_dcim.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
__metaclass__ = type
88

9+
from ansible.module_utils.basic import missing_required_lib
910
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
1011
NetboxModule,
1112
ENDPOINT_NAME_MAPPING,
@@ -51,14 +52,19 @@
5152

5253
try:
5354
from packaging.version import Version
55+
56+
HAS_PACKAGING = True
5457
except ImportError as imp_exc:
5558
PACKAGING_IMPORT_ERROR = imp_exc
56-
else:
57-
PACKAGING_IMPORT_ERROR = None
59+
HAS_PACKAGING = False
5860

5961

6062
class NetboxDcimModule(NetboxModule):
6163
def __init__(self, module, endpoint):
64+
if not HAS_PACKAGING:
65+
self.module.fail_json(
66+
msg=missing_required_lib("packaging"), exception=PACKAGING_IMPORT_ERROR
67+
)
6268
super().__init__(module, endpoint)
6369

6470
def run(self):

0 commit comments

Comments
 (0)