Skip to content

Commit e130e3a

Browse files
Initial changes to satisfy lint requirements
1 parent 67b8cb1 commit e130e3a

32 files changed

+496
-546
lines changed

plugins/action/tests/integration/ndfc_interface_validate.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,17 @@
77
import json
88
from ansible.utils.display import Display
99
from ansible.plugins.action import ActionBase
10-
from ansible.module_utils.six import raise_from
11-
from ansible.errors import AnsibleError
1210
from ansible.module_utils.common.text.converters import to_native
1311
from ..plugin_utils.tools import load_yaml_file, process_deepdiff
1412
from ..plugin_utils.pydantic_schemas.dcnm_interface.schemas import DcnmInterfaceQuerySchema
1513

1614
try:
1715
from deepdiff import DeepDiff
18-
except ImportError as imp_exc:
19-
DEEPDIFF_IMPORT_ERROR = imp_exc
20-
else:
16+
HAS_DEEPDIFF = True
2117
DEEPDIFF_IMPORT_ERROR = None
22-
23-
if DEEPDIFF_IMPORT_ERROR:
24-
raise_from(
25-
AnsibleError('DeepDiff must be installed to use this plugin. Use pip or install test-requirements.'),
26-
DEEPDIFF_IMPORT_ERROR)
18+
except ImportError as imp_exc:
19+
HAS_DEEPDIFF = False
20+
DEEPDIFF_IMPORT_ERROR = str(imp_exc)
2721

2822
display = Display()
2923

@@ -34,6 +28,12 @@ def run(self, tmp=None, task_vars=None):
3428
results = super(ActionModule, self).run(tmp, task_vars)
3529
results['failed'] = False
3630

31+
# Check if deepdiff is available at runtime
32+
if not HAS_DEEPDIFF:
33+
results['failed'] = True
34+
results['msg'] = f"DeepDiff must be installed to use this plugin. Use pip or install test-requirements. Import error: {DEEPDIFF_IMPORT_ERROR}"
35+
return results
36+
3737
ndfc_data = self._task.args.get('ndfc_data', None)
3838
test_data = self._task.args.get('test_data', None)
3939
config_path = self._task.args.get('config_path', None)

plugins/action/tests/integration/ndfc_inventory_validate.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,31 @@
22
from ansible.utils.display import Display
33
from ansible.plugins.action import ActionBase
44
from typing import List, Dict, Optional, Union
5-
from pydantic import BaseModel, model_validator, validator, ValidationError
65
import re
76
import json
7+
8+
try:
9+
from pydantic import BaseModel, model_validator, validator, ValidationError
10+
HAS_PYDANTIC = True
11+
except ImportError:
12+
HAS_PYDANTIC = False
13+
# Create dummy classes to allow import without pydantic
14+
BaseModel = object
15+
16+
def model_validator(**kwargs):
17+
"""Dummy model_validator decorator that accepts any arguments"""
18+
def decorator(func):
19+
return func
20+
return decorator
21+
22+
def validator(*args, **kwargs):
23+
"""Dummy validator decorator that accepts any arguments"""
24+
def decorator(func):
25+
return func
26+
return decorator
27+
28+
ValidationError = Exception
29+
830
__metaclass__ = type
931

1032
display = Display()
@@ -128,7 +150,7 @@ def validate_lists_equality(cls, values):
128150
if ((seed_ip_one is not None and ip_address_two is not None and ip_address_two == seed_ip_one) or (ignore_fields['seed_ip'])):
129151
seed_ip_match = True
130152

131-
if ((role_one is not None and switch_role_two is not None and switch_role_two == role_one) or (ignore_fields['role'])) :
153+
if ((role_one is not None and switch_role_two is not None and switch_role_two == role_one) or (ignore_fields['role'])):
132154
role_match = True
133155

134156
if seed_ip_match and role_match:

plugins/action/tests/integration/ndfc_network_validate.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,17 @@
77
import json
88
from ansible.utils.display import Display
99
from ansible.plugins.action import ActionBase
10-
from ansible.module_utils.six import raise_from
11-
from ansible.errors import AnsibleError
1210
from ansible.module_utils.common.text.converters import to_native
1311
from ..plugin_utils.tools import load_yaml_file, process_deepdiff
1412
from ..plugin_utils.pydantic_schemas.dcnm_network.schemas import DcnmNetworkQuerySchema
1513

1614
try:
1715
from deepdiff import DeepDiff
18-
except ImportError as imp_exc:
19-
DEEPDIFF_IMPORT_ERROR = imp_exc
20-
else:
16+
HAS_DEEPDIFF = True
2117
DEEPDIFF_IMPORT_ERROR = None
22-
23-
if DEEPDIFF_IMPORT_ERROR:
24-
raise_from(
25-
AnsibleError('DeepDiff must be installed to use this plugin. Use pip or install test-requirements.'),
26-
DEEPDIFF_IMPORT_ERROR)
18+
except ImportError as imp_exc:
19+
HAS_DEEPDIFF = False
20+
DEEPDIFF_IMPORT_ERROR = str(imp_exc)
2721

2822
display = Display()
2923

@@ -81,6 +75,12 @@ def run(self, tmp=None, task_vars=None):
8175
results = super(ActionModule, self).run(tmp, task_vars)
8276
results['failed'] = False
8377

78+
# Check if deepdiff is available at runtime
79+
if not HAS_DEEPDIFF:
80+
results['failed'] = True
81+
results['msg'] = f"DeepDiff must be installed to use this plugin. Use pip or install test-requirements. Import error: {DEEPDIFF_IMPORT_ERROR}"
82+
return results
83+
8484
ndfc_data = self._task.args.get('ndfc_data', None)
8585
test_data = self._task.args.get('test_data', None)
8686
config_path = self._task.args.get('config_path', None)

plugins/action/tests/integration/ndfc_vpc_pair_validate.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,17 @@
77
import json
88
from ansible.utils.display import Display
99
from ansible.plugins.action import ActionBase
10-
from ansible.module_utils.six import raise_from
11-
from ansible.errors import AnsibleError
1210
from ansible.module_utils.common.text.converters import to_native
1311
from ..plugin_utils.tools import load_yaml_file, process_deepdiff
1412
from ..plugin_utils.pydantic_schemas.dcnm_vpc_pair.schemas import DcnmVpcPairQuerySchema
1513

1614
try:
1715
from deepdiff import DeepDiff
18-
except ImportError as imp_exc:
19-
DEEPDIFF_IMPORT_ERROR = imp_exc
20-
else:
16+
HAS_DEEPDIFF = True
2117
DEEPDIFF_IMPORT_ERROR = None
22-
23-
if DEEPDIFF_IMPORT_ERROR:
24-
raise_from(
25-
AnsibleError('DeepDiff must be installed to use this plugin. Use pip or install test-requirements.'),
26-
DEEPDIFF_IMPORT_ERROR)
18+
except ImportError as imp_exc:
19+
HAS_DEEPDIFF = False
20+
DEEPDIFF_IMPORT_ERROR = str(imp_exc)
2721

2822
display = Display()
2923

@@ -202,6 +196,12 @@ def run(self, tmp=None, task_vars=None):
202196
results = super(ActionModule, self).run(tmp, task_vars)
203197
results['failed'] = False
204198

199+
# Check if deepdiff is available at runtime
200+
if not HAS_DEEPDIFF:
201+
results['failed'] = True
202+
results['msg'] = f"DeepDiff must be installed to use this plugin. Use pip or install test-requirements. Import error: {DEEPDIFF_IMPORT_ERROR}"
203+
return results
204+
205205
ndfc_data = self._task.args.get('ndfc_data', None)
206206
test_data = self._task.args.get('test_data', None)
207207
config_path = self._task.args.get('config_path', None)

plugins/action/tests/plugin_utils/pydantic_schemas/dcnm_interface/schemas.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
from __future__ import absolute_import, division, print_function
22
from typing import List, Optional
33
import re
4-
from ansible.module_utils.six import raise_from
5-
from ansible.errors import AnsibleError
64

75
try:
86
from pydantic import BaseModel, field_validator
9-
except ImportError as imp_exc:
10-
PYDANTIC_IMPORT_ERROR = imp_exc
11-
else:
12-
PYDANTIC_IMPORT_ERROR = None
13-
14-
if PYDANTIC_IMPORT_ERROR:
15-
raise_from(
16-
AnsibleError('Pydantic must be installed to use this plugin. Use pip or install test-requirements.'),
17-
PYDANTIC_IMPORT_ERROR)
7+
HAS_PYDANTIC = True
8+
except ImportError:
9+
HAS_PYDANTIC = False
10+
# Create dummy classes to allow import without pydantic
11+
BaseModel = object
12+
13+
def field_validator(*args, **kwargs):
14+
"""Dummy field_validator decorator that accepts any arguments"""
15+
def decorator(func):
16+
return func
17+
return decorator
18+
19+
# Note: If pydantic is not available, schemas will be imported but non-functional
1820

1921

2022
def expand_interface_name(name):

plugins/action/tests/plugin_utils/pydantic_schemas/dcnm_network/schemas.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
from __future__ import absolute_import, division, print_function
22
from typing import List, Optional, Annotated
3-
from ansible.module_utils.six import raise_from
4-
from ansible.errors import AnsibleError
53

64
try:
75
from pydantic import BaseModel, model_validator, BeforeValidator
8-
except ImportError as imp_exc:
9-
PYDANTIC_IMPORT_ERROR = imp_exc
10-
else:
11-
PYDANTIC_IMPORT_ERROR = None
12-
13-
if PYDANTIC_IMPORT_ERROR:
14-
raise_from(
15-
AnsibleError('Pydantic must be installed to use this plugin. Use pip or install test-requirements.'),
16-
PYDANTIC_IMPORT_ERROR)
6+
HAS_PYDANTIC = True
7+
except ImportError:
8+
HAS_PYDANTIC = False
9+
# Create dummy classes to allow import without pydantic
10+
BaseModel = object
11+
12+
def model_validator(*args, **kwargs):
13+
"""Dummy model_validator decorator that accepts any arguments"""
14+
def decorator(func):
15+
return func
16+
return decorator
17+
18+
def BeforeValidator(func):
19+
"""Dummy BeforeValidator wrapper"""
20+
return func
21+
22+
# Note: If pydantic is not available, schemas will be imported but non-functional
1723

1824
# Top level schema
1925
# Format: ModuleMethodSchema

plugins/action/tests/plugin_utils/pydantic_schemas/dcnm_vpc_pair/schemas.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
from __future__ import absolute_import, division, print_function
22
from typing import List, Optional, Annotated
3-
from ansible.module_utils.six import raise_from
4-
from ansible.errors import AnsibleError
53

64
try:
75
from pydantic import BaseModel, model_validator, BeforeValidator
8-
except ImportError as imp_exc:
9-
PYDANTIC_IMPORT_ERROR = imp_exc
10-
else:
11-
PYDANTIC_IMPORT_ERROR = None
12-
13-
if PYDANTIC_IMPORT_ERROR:
14-
raise_from(
15-
AnsibleError('Pydantic must be installed to use this plugin. Use pip or install test-requirements.'),
16-
PYDANTIC_IMPORT_ERROR)
6+
HAS_PYDANTIC = True
7+
except ImportError:
8+
HAS_PYDANTIC = False
9+
# Create dummy classes to allow import without pydantic
10+
BaseModel = object
11+
12+
def model_validator(*args, **kwargs):
13+
"""Dummy model_validator decorator that accepts any arguments"""
14+
def decorator(func):
15+
return func
16+
return decorator
17+
18+
def BeforeValidator(func):
19+
"""Dummy BeforeValidator wrapper"""
20+
return func
21+
22+
# Note: If pydantic is not available, schemas will be imported but non-functional
1723

1824
# Top level schema
1925
# Format: ModuleMethodSchema

plugins/module_utils/common/sender_requests.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,17 @@
3131
HAS_REQUESTS = True
3232
except ImportError:
3333
HAS_REQUESTS = False
34+
requests = None
3435

3536
try:
3637
import urllib3
3738

3839
HAS_URLLIB3 = True
3940
except ImportError:
4041
HAS_URLLIB3 = False
42+
urllib3 = None
4143

42-
if HAS_REQUESTS is False:
43-
msg = "requests is not installed. "
44-
msg += "install with e.g. pip install requests"
45-
raise ImportError(msg)
46-
47-
if HAS_URLLIB3 is False:
48-
msg = "urllib3 is not installed. "
49-
msg += "install with e.g. pip install urllib3"
50-
raise ImportError(msg)
44+
# These will be checked at runtime rather than import time
5145

5246

5347
class Sender:
@@ -118,7 +112,8 @@ def __init__(self):
118112
self.class_name = self.__class__.__name__
119113
self._implements = "sender_v1"
120114

121-
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
115+
if HAS_URLLIB3:
116+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
122117
self.log = logging.getLogger(f"dcnm.{self.class_name}")
123118

124119
self._domain = environ.get("ND_DOMAIN", "local")
@@ -183,6 +178,13 @@ def commit(self):
183178
method_name = inspect.stack()[0][3]
184179
caller = inspect.stack()[1][3]
185180
method_name = inspect.stack()[0][3]
181+
182+
# Check for required dependencies at runtime
183+
if not HAS_REQUESTS:
184+
msg = "requests is not installed. "
185+
msg += "install with e.g. pip install requests"
186+
raise ImportError(msg)
187+
186188
msg = f"{self.class_name}.{method_name}: "
187189
msg += f"Caller: {caller}, ENTERED"
188190
self.log.debug(msg)

plugins/module_utils/network/dcnm/dcnm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,14 +613,14 @@ def dcnm_get_url(module, fabric, path, items, module_name):
613613
elif iter != (send_count - 1):
614614
itemstr = ",".join(
615615
itemlist[
616-
(iter * (len(itemlist) // send_count)) : (
616+
(iter * (len(itemlist) // send_count)): (
617617
(iter + 1) * (len(itemlist) // send_count)
618618
)
619619
]
620620
)
621621
url = path.format(fabric, itemstr)
622622
else:
623-
itemstr = ",".join(itemlist[iter * (len(itemlist) // send_count) :])
623+
itemstr = ",".join(itemlist[iter * (len(itemlist) // send_count):])
624624
url = path.format(fabric, itemstr)
625625

626626
att_objects = dcnm_send(module, method, url)

0 commit comments

Comments
 (0)