From 59cf5e601d459bf8d3f69c0e9e2e42f470ecaaa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Tr=C3=A1vn=C3=ADk?= Date: Tue, 10 Dec 2024 12:45:48 +0100 Subject: [PATCH 1/2] deduplication --- plugins/modules/cml_lab.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/plugins/modules/cml_lab.py b/plugins/modules/cml_lab.py index edf8d95..aa9e209 100644 --- a/plugins/modules/cml_lab.py +++ b/plugins/modules/cml_lab.py @@ -135,8 +135,9 @@ def run_module(): else: lab = None - if cml.params['state'] == 'present': + if cml.params['state'] in ['present', 'started']: if lab is None: + # create the lab if cml.params['topology']: lab = cml.client.import_lab(cml.params['topology'], title=cml.params['lab']) elif cml.params['file']: @@ -147,26 +148,18 @@ def run_module(): lab = cml.client.import_lab_from_path(topology_file, title=cml.params['lab']) else: lab = cml.client.create_lab(title=cml.params['lab']) - lab.title = cml.params['lab'] - cml.result['changed'] = True - elif cml.params['state'] == 'started': - if lab is None: - if cml.params['topology']: - lab = cml.client.import_lab(cml.params['topology'], title=cml.params['lab']) - lab.start(wait=cml.params['wait']) - elif cml.params['file']: - lab = cml.client.import_lab_from_path(cml.params['file'], title=cml.params['lab']) - lab.start(wait=cml.params['wait']) - else: - lab = cml.client.create_lab(title=cml.params['lab']) + # start the new lab + if cml.params['state'] == 'started': lab.start(wait=cml.params['wait']) lab.title = cml.params['lab'] cml.result['changed'] = True - elif lab.state() == "STOPPED": + elif lab.state() == "STOPPED" and cml.params['state'] == 'started': + # start existing stopped lab lab.start(wait=cml.params['wait']) cml.result['changed'] = True elif cml.params['state'] == 'absent': if lab: + # remove existing lab cml.result['changed'] = True if lab.state() == "STARTED": lab.stop(wait=True) @@ -177,11 +170,13 @@ def run_module(): elif cml.params['state'] == 'stopped': if lab: if lab.state() == "STARTED": + # stop existing running lab cml.result['changed'] = True lab.stop(wait=True) elif cml.params['state'] == 'wiped': if lab: if lab.state() == "STOPPED": + # wipe existing stopped lab cml.result['changed'] = True lab.wipe(wait=True) From 9a83f68e717b5af44497d066528f6511a39a7cc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Tr=C3=A1vn=C3=ADk?= Date: Tue, 10 Dec 2024 12:46:03 +0100 Subject: [PATCH 2/2] check_mode support --- plugins/modules/cml_lab.py | 10 ++++++++++ plugins/modules/cml_node.py | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/plugins/modules/cml_lab.py b/plugins/modules/cml_lab.py index aa9e209..58acaae 100644 --- a/plugins/modules/cml_lab.py +++ b/plugins/modules/cml_lab.py @@ -137,6 +137,8 @@ def run_module(): if cml.params['state'] in ['present', 'started']: if lab is None: + if module.check_mode: + module.exit_json(changed=True) # create the lab if cml.params['topology']: lab = cml.client.import_lab(cml.params['topology'], title=cml.params['lab']) @@ -154,11 +156,15 @@ def run_module(): lab.title = cml.params['lab'] cml.result['changed'] = True elif lab.state() == "STOPPED" and cml.params['state'] == 'started': + if module.check_mode: + module.exit_json(changed=True) # start existing stopped lab lab.start(wait=cml.params['wait']) cml.result['changed'] = True elif cml.params['state'] == 'absent': if lab: + if module.check_mode: + module.exit_json(changed=True) # remove existing lab cml.result['changed'] = True if lab.state() == "STARTED": @@ -170,12 +176,16 @@ def run_module(): elif cml.params['state'] == 'stopped': if lab: if lab.state() == "STARTED": + if module.check_mode: + module.exit_json(changed=True) # stop existing running lab cml.result['changed'] = True lab.stop(wait=True) elif cml.params['state'] == 'wiped': if lab: if lab.state() == "STOPPED": + if module.check_mode: + module.exit_json(changed=True) # wipe existing stopped lab cml.result['changed'] = True lab.wipe(wait=True) diff --git a/plugins/modules/cml_node.py b/plugins/modules/cml_node.py index da86e04..d7c1081 100644 --- a/plugins/modules/cml_node.py +++ b/plugins/modules/cml_node.py @@ -153,12 +153,16 @@ def run_module(): node = cml.get_node_by_name(lab, cml.params['name']) if cml.params['state'] == 'present': if node is None: + if module.check_mode: + module.exit_json(changed=True) node = lab.create_node(label=cml.params['name'], node_definition=cml.params['node_definition']) cml.result['changed'] = True elif cml.params['state'] == 'started': if node is None: cml.fail_json("Node must be created before it is started") if node.state not in ['STARTED', 'BOOTED']: + if module.check_mode: + module.exit_json(changed=True) if node.state == 'DEFINED_ON_CORE' and cml.params['config']: node.config = cml.params['config'] if cml.params['image_definition']: @@ -171,6 +175,8 @@ def run_module(): if node is None: cml.fail_json("Node must be created before it is stopped") if node.state not in ['STOPPED', 'DEFINED_ON_CORE']: + if module.check_mode: + module.exit_json(changed=True) if cml.params['wait'] is False: lab.wait_for_covergence = False node.stop() @@ -179,6 +185,8 @@ def run_module(): if node is None: cml.fail_json("Node must be created before it is wiped") if node.state not in ['DEFINED_ON_CORE']: + if module.check_mode: + module.exit_json(changed=True) node.wipe(wait=cml.params['wait']) cml.result['changed'] = True cml.exit_json(**cml.result)