Skip to content

Commit df68ced

Browse files
committed
Fix yaml library import
Signed-off-by: Alina Buzachis <[email protected]>
1 parent 6d9bfa5 commit df68ced

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

.config/requirements-docs.in

-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ myst-parser
77
setuptools # https://issues.redhat.com/browse/AAH-3375
88
sphinx
99
sphinx-ansible-theme>=0.10.3
10-
pyyaml

.config/requirements-test.in

-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ requests>=2.31.0
1010
ansible-rulebook>=1.0.0
1111
tox>=4.15.1
1212
podman-compose
13-
pyyaml

plugins/modules/activation.py

+35-15
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,20 @@
184184
"""
185185

186186

187-
import yaml
188187
from typing import Any, Dict, List
188+
import traceback
189+
190+
try:
191+
import yaml
192+
except ImportError:
193+
HAS_YAML = False
194+
YAML_IMPORT_ERROR = traceback.format_exc()
195+
else:
196+
HAS_YAML = True
197+
YAML_IMPORT_ERROR = ''
189198

190199
from ansible.module_utils.basic import AnsibleModule
200+
from ansible.module_utils.basic import missing_required_lib
191201

192202
from ..module_utils.arguments import AUTH_ARGSPEC
193203
from ..module_utils.client import Client
@@ -196,7 +206,9 @@
196206
from ..module_utils.errors import EDAError
197207

198208

199-
def find_matching_source(event: Dict[str, Any], sources: List[Dict[str, Any]], module: AnsibleModule) -> Dict[str, Any]:
209+
def find_matching_source(
210+
event: Dict[str, Any], sources: List[Dict[str, Any]], module: AnsibleModule
211+
) -> Dict[str, Any]:
200212
"""
201213
Finds a matching source based on the source_name in the event.
202214
Raises an error if no match is found.
@@ -210,9 +222,7 @@ def find_matching_source(event: Dict[str, Any], sources: List[Dict[str, Any]], m
210222
return source # Return the matching source if found
211223

212224
# If no match is found, raise an error
213-
module.fail_json(
214-
msg=f"The specified source_name {source_name} does not exist."
215-
)
225+
module.fail_json(msg=f"The specified source_name {source_name} does not exist.")
216226

217227
return {} # Explicit return to satisfy mypy
218228

@@ -261,8 +271,12 @@ def process_event_streams(
261271
# Handle source_index
262272
if event.get("source_index") is not None:
263273
try:
264-
source_mapping["source_name"] = sources[event["source_index"]].get("name")
265-
source_mapping["rulebook_hash"] = sources[event["source_index"]].get("rulebook_hash")
274+
source_mapping["source_name"] = sources[event["source_index"]].get(
275+
"name"
276+
)
277+
source_mapping["rulebook_hash"] = sources[event["source_index"]].get(
278+
"rulebook_hash"
279+
)
266280
except IndexError as e:
267281
module.fail_json(
268282
msg=f"The specified source_index {event['source_index']} is out of range: {e}"
@@ -275,9 +289,7 @@ def process_event_streams(
275289
source_mapping["rulebook_hash"] = matching_source.get("rulebook_hash")
276290

277291
if event.get("event_stream") is None:
278-
module.fail_json(
279-
msg="You must specify an event stream name."
280-
)
292+
module.fail_json(msg="You must specify an event stream name.")
281293

282294
# Lookup event_stream_id
283295
event_stream_id = lookup_resource_id(
@@ -389,11 +401,13 @@ def create_params(
389401

390402
if not is_aap_24 and module.params.get("event_streams"):
391403
# Process event streams and source mappings
392-
activation_params["source_mappings"] = yaml.dump(process_event_streams(
393-
rulebook_id=rulebook_id,
394-
controller=controller,
395-
module=module,
396-
))
404+
activation_params["source_mappings"] = yaml.dump(
405+
process_event_streams(
406+
rulebook_id=rulebook_id,
407+
controller=controller,
408+
module=module,
409+
)
410+
)
397411

398412
if not is_aap_24 and module.params.get("log_level"):
399413
activation_params["log_level"] = module.params["log_level"]
@@ -454,6 +468,12 @@ def main() -> None:
454468
argument_spec=argument_spec, required_if=required_if, supports_check_mode=True
455469
)
456470

471+
if not HAS_YAML:
472+
module.fail_json(
473+
msg=missing_required_lib('pyyaml'),
474+
exception=YAML_IMPORT_ERROR)
475+
476+
457477
client = Client(
458478
host=module.params.get("controller_host"),
459479
username=module.params.get("controller_username"),

0 commit comments

Comments
 (0)