'Returns a dict(instance_id, [security_groups]) to allow obtaining
all of the instances and their security groups in one shot.'

def get_instances_security_groups_bindings(self, context, servers, detailed=False):

    neutron = neutronapi.get_client(context)
    ports = self._get_ports_from_server_list(servers, neutron)
    security_groups = self._get_secgroups_from_port_list(ports, neutron)
    instances_security_group_bindings = {}
    for port in ports:
        for port_sg_id in port.get('security_groups', []):
            port_sg = security_groups.get(port_sg_id)
            if port_sg:
                if detailed:
                    sg_entry = self._convert_to_nova_security_group_format(port_sg)
                    instances_security_group_bindings.setdefault(port['device_id'], []).append(sg_entry)
                else:
                    name = port_sg.get('name')
                    if (not name):
                        name = port_sg.get('id')
                    sg_entry = {'name': name}
                    instances_security_group_bindings.setdefault(port['device_id'], []).append(sg_entry)
    return instances_security_group_bindings
