Multi action helper for start, stop, get, ...

def _action(action='get', search=None, one=True, force=False):
    '''
    Multi action helper for start, stop, get, ...
    '''
    vms = {}
    matched_vms = []
    client = salt.client.get_local_client(__opts__['conf_file'])

    ## lookup vms
    try:
        vmadm_args = {}
        vmadm_args['order'] = 'uuid,alias,hostname,state'
        if '=' in search:
            vmadm_args['search'] = search
        for cn in client.cmd_iter('G@virtual:physical and G@os:smartos',
                                  'vmadm.list', kwarg=vmadm_args,
                                  tgt_type='compound'):
            if not cn:
                continue
            node = next(six.iterkeys(cn))
            if not isinstance(cn[node], dict) or \
                    'ret' not in cn[node] or \
                    not isinstance(cn[node]['ret'], dict):
                continue
            for vm in cn[node]['ret']:
                vmcfg = cn[node]['ret'][vm]
                vmcfg['node'] = node
                vms[vm] = vmcfg
    except SaltClientError as client_error:
        pass

    ## check if we have vms
    if not vms:
        return {'Error': 'No vms found.'}

    ## simple search
    if '=' not in search:
        loop_pass = 0
        while loop_pass < 3:
            ## each pass will try a different field
            if loop_pass == 0:
                field = 'uuid'
            elif loop_pass == 1:
                field = 'hostname'
            else:
                field = 'alias'

            ## loop vms and try to match
            for vm in vms:
                if field == 'uuid' and vm == search:
                    matched_vms.append(vm)
                    break  # exit for on uuid match (max = 1)
                elif field in vms[vm] and vms[vm][field] == search:
                    matched_vms.append(vm)

            ## exit on match(es) or try again
            if matched_vms:
                break
            else:
                loop_pass += 1
    else:
        for vm in vms:
            matched_vms.append(vm)

    ## check if we have vms
    if not matched_vms:
        return {'Error': 'No vms matched.'}

    ## multiple allowed?
    if one and len(matched_vms) > 1:
        return {
            'Error': 'Matched {0} vms, only one allowed!'.format(len(matched_vms)),
            'Matches': matched_vms
        }

    ## perform action
    ret = {}
    if action in ['start', 'stop', 'reboot', 'get']:
        for vm in matched_vms:
            vmadm_args = {
                'key': 'uuid',
                'vm': vm
            }
            try:
                for vmadm_res in client.cmd_iter(vms[vm]['node'], 'vmadm.{0}'.format(action), kwarg=vmadm_args):
                    if not vmadm_res:
                        continue
                    if vms[vm]['node'] in vmadm_res:
                        ret[vm] = vmadm_res[vms[vm]['node']]['ret']
            except SaltClientError as client_error:
                ret[vm] = False
    elif action in ['is_running']:
        ret = True
        for vm in matched_vms:
            if vms[vm]['state'] != 'running':
                ret = False
                break
    return ret