184
184
"""
185
185
186
186
187
- import yaml
188
187
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 = ''
189
198
190
199
from ansible .module_utils .basic import AnsibleModule
200
+ from ansible .module_utils .basic import missing_required_lib
191
201
192
202
from ..module_utils .arguments import AUTH_ARGSPEC
193
203
from ..module_utils .client import Client
196
206
from ..module_utils .errors import EDAError
197
207
198
208
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 ]:
200
212
"""
201
213
Finds a matching source based on the source_name in the event.
202
214
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
210
222
return source # Return the matching source if found
211
223
212
224
# 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." )
216
226
217
227
return {} # Explicit return to satisfy mypy
218
228
@@ -261,8 +271,12 @@ def process_event_streams(
261
271
# Handle source_index
262
272
if event .get ("source_index" ) is not None :
263
273
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
+ )
266
280
except IndexError as e :
267
281
module .fail_json (
268
282
msg = f"The specified source_index { event ['source_index' ]} is out of range: { e } "
@@ -275,9 +289,7 @@ def process_event_streams(
275
289
source_mapping ["rulebook_hash" ] = matching_source .get ("rulebook_hash" )
276
290
277
291
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." )
281
293
282
294
# Lookup event_stream_id
283
295
event_stream_id = lookup_resource_id (
@@ -389,11 +401,13 @@ def create_params(
389
401
390
402
if not is_aap_24 and module .params .get ("event_streams" ):
391
403
# 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
+ )
397
411
398
412
if not is_aap_24 and module .params .get ("log_level" ):
399
413
activation_params ["log_level" ] = module .params ["log_level" ]
@@ -454,6 +468,12 @@ def main() -> None:
454
468
argument_spec = argument_spec , required_if = required_if , supports_check_mode = True
455
469
)
456
470
471
+ if not HAS_YAML :
472
+ module .fail_json (
473
+ msg = missing_required_lib ('pyyaml' ),
474
+ exception = YAML_IMPORT_ERROR )
475
+
476
+
457
477
client = Client (
458
478
host = module .params .get ("controller_host" ),
459
479
username = module .params .get ("controller_username" ),
0 commit comments