Skip to content

Commit

Permalink
Merge pull request #5 from tvarohohlavy/check_mode_support
Browse files Browse the repository at this point in the history
check_mode support for cml_lab and cml_node modules
  • Loading branch information
tvarohohlavy authored Dec 11, 2024
2 parents 083743d + 9a83f68 commit b0216ee
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
33 changes: 19 additions & 14 deletions plugins/modules/cml_lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,11 @@ def run_module():
else:
lab = None

if cml.params['state'] == 'present':
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'])
elif cml.params['file']:
Expand All @@ -147,26 +150,22 @@ 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':
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":
lab.stop(wait=True)
Expand All @@ -177,11 +176,17 @@ 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)

Expand Down
8 changes: 8 additions & 0 deletions plugins/modules/cml_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']:
Expand All @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit b0216ee

Please sign in to comment.