def _find_volume(self, volume_id=None, name=None, device=None):
        vol = None
        if volume_id:
            vol = self._volume_by_id(volume_id)
        else:
            all_volumes = [v for v in self.ec2.get_all_volumes() if v.status in ('in-use', 'available')]
            if device:
                for v in all_volumes:
                    if v.attach_data and v.attach_data.device == device and v.attach_data.instance_id == self.resource.env.config.aws.instance_id:
                        vol = v
                        break
            if not vol and name:
                if '{index}' in name:
                    allnames = dict((v.tags.get('Name'), v) for v in all_volumes)
                    for i in itertools.count(1):
                        vname = name.format(index=i)
                        try:
                            v = allnames[vname]
                            if v.status == "available":
                                vol = v
                                break
                        except KeyError:
                            self.resource.name = vname
                            break
                else:
                    for v in all_volumes:
                        if v.tags.get('Name') == name:
                            vol = v
                            break
        if vol and '{index}' in name:
            self.resource.name = vol.tags.get('Name')
        return vol
dataset/ETHPy150Open samuel/kokki/kokki/cookbooks/aws/libraries/providers.py/EBSVolumeProvider._find_volume/original