From 8604be624b02aa675fba9c6ca240afc933766127 Mon Sep 17 00:00:00 2001 From: LoveIsGrief Date: Sat, 9 Jan 2016 16:32:21 +0100 Subject: [PATCH] Green-test: VirtualboxProviderHeavyTests::test_stop_action We can now tell instances to stop Related to saltstack/salt#27089 Saltcloud virtualbox provider --- salt/cloud/clouds/virtualbox.py | 18 +++++++++++++++++- salt/utils/virtualbox.py | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/salt/cloud/clouds/virtualbox.py b/salt/cloud/clouds/virtualbox.py index abb62a1fd6..dc053ebaca 100644 --- a/salt/cloud/clouds/virtualbox.py +++ b/salt/cloud/clouds/virtualbox.py @@ -22,7 +22,7 @@ from salt.exceptions import SaltCloudSystemExit import salt.config as config import salt.utils.cloud as cloud from utils.virtualbox import vb_list_machines, vb_clone_vm, HAS_LIBS, vb_machine_exists, vb_destroy_machine, \ - vb_machinestate_to_str, vb_get_machine + vb_get_machine, vb_stop_vm, treat_machine_dict log = logging.getLogger(__name__) @@ -313,6 +313,22 @@ def destroy(name, call=None): # TODO implement actions e.g start, stop, restart, etc. +def stop(name, call=None): + """ + Stop a running machine. + """ + if call != 'action': + raise SaltCloudSystemExit( + 'The instance action must be called with -a or --action.' + ) + + log.info("Stopping machine: %s" % name) + vb_stop_vm(name) + machine = vb_get_machine(name) + del machine["name"] + return treat_machine_dict(machine) + + # TODO implement functions def show_image(kwargs, call=None): diff --git a/salt/utils/virtualbox.py b/salt/utils/virtualbox.py index edc1e6c006..ef8d196e3c 100644 --- a/salt/utils/virtualbox.py +++ b/salt/utils/virtualbox.py @@ -382,6 +382,31 @@ def vb_xpcom_to_attribute_dict(xpcom return dict(attribute_tuples) +def treat_machine_dict(machine): + """ + Make machine presentable for outside world. + + !!!Modifies the input machine!!! + + @param machine: + @type machine: dict + @return: the modified input machine + @rtype: dict + """ + machine.update({ + "id": machine.get("id", ""), + "image": machine.get("image", ""), + "size": "%s MB" % machine.get("memorySize", 0), + "state": vb_machinestate_to_str(machine.get("state", -1))[0], + "private_ips": [], + "public_ips": [], + }) + + # Replaced keys + if "memorySize" in machine: + del machine["memorySize"] + return machine + def vb_machinestate_to_str(machinestate): """