# Buggy code
def get_vmnames_by_action(self, action):
    query_map = self.interpolated_map("list_nodes")
    matching_states = {
        "start": ["stopped"],
        "stop": ["running", "active"],
        "reboot": ["running", "active"],
    }
    vm_names = []
    for alias, drivers in query_map.items():
        for driver, vms in drivers.items():
            for vm_name, vm_details in vms.items():
                if (vm_details != "Absent") and (
                    vm_details["state"].lower() in matching_states[action]
                ):
                    vm_names.append(vm_name)
    return vm_names


# Traceback (KeyError)
# salt-cloud -m cloud-california.map -a ssh_interface
[INFO    ] salt-cloud starting
[INFO    ] Applying map from 'cloud-california.map'.
[INFO    ] Starting new HTTPS connection (1): ec2.us-west-1.amazonaws.com
[INFO    ] Starting new HTTPS connection (1): ec2.eu-west-1.amazonaws.com
[INFO    ] Starting new HTTPS connection (1): ec2.us-west-1.amazonaws.com
[INFO    ] Starting new HTTPS connection (1): ec2.eu-west-1.amazonaws.com
[INFO    ] Starting new HTTPS connection (1): ec2.eu-west-1.amazonaws.com
[INFO    ] Starting new HTTPS connection (1): ec2.us-west-1.amazonaws.com
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
KeyError: 'ssh_interface'
Traceback (most recent call last):
File "/usr/bin/salt-cloud", line 10, in <module>
salt_cloud()
File "/usr/lib/python2.6/site-packages/salt/scripts.py", line 210, in salt_cloud
client.run()
File "/usr/lib/python2.6/site-packages/salt/cloud/cli.py", line 175, in run
names = mapper.get_vmnames_by_action(self.options.action)
File "/usr/lib/python2.6/site-packages/salt/cloud/__init__.py", line 1569, in get_vmnames_by_action
matching_states[action]
KeyError: 'ssh_interface'
Traceback (most recent call last):
File "/usr/bin/salt-cloud", line 10, in <module>
salt_cloud()
File "/usr/lib/python2.6/site-packages/salt/scripts.py", line 210, in salt_cloud
client.run()
File "/usr/lib/python2.6/site-packages/salt/cloud/cli.py", line 175, in run
names = mapper.get_vmnames_by_action(self.options.action)
File "/usr/lib/python2.6/site-packages/salt/cloud/__init__.py", line 1569, in get_vmnames_by_action
matching_states[action]
KeyError: 'ssh_interface'

# Fixed code
def get_vmnames_by_action(self, action):
    query_map = self.interpolated_map("list_nodes")
    matching_states = {
        "start": ["stopped"],
        "stop": ["running", "active"],
        "reboot": ["running", "active"],
    }
    vm_names = []
    for alias, drivers in query_map.items():
        for driver, vms in drivers.items():
            for vm_name, vm_details in vms.items():
                # Only certain actions are support in to use in this case. Those actions are the
                # "Global" salt-cloud actions defined in the "matching_states" dictionary above.
                # If a more specific action is passed in, we shouldn't stack-trace - exit gracefully.
                try:
                    state_action = matching_states[action]
                except KeyError:
                    log.error(
                        "The use of '{0}' as an action is not supported in this context. "
                        "Only 'start', 'stop', and 'reboot' are supported options.".format(
                            action
                        )
                    )
                    raise SaltCloudException()
                if (vm_details != "Absent") and (
                    vm_details["state"].lower() in state_action
                ):
                    vm_names.append(vm_name)
    return vm_names