if filters:
query = query.filter_by(**filters)
- return query.join(models.Snapshot.volume).filter(
- models.Volume.host == host).options(
- joinedload('snapshot_metadata')).all()
+ # As a side effect of the introduction of pool-aware scheduler,
+ # newly created volumes will have pool information appended to
+ # 'host' field of a volume record. So a volume record in DB can
+ # now be either form below:
+ # Host
+ # Host#Pool
+ if host and isinstance(host, six.string_types):
+ session = get_session()
+ with session.begin():
+ host_attr = getattr(models.Volume, 'host')
+ conditions = [host_attr == host,
+ host_attr.op('LIKE')(host + '#%')]
+ query = query.join(models.Snapshot.volume).filter(
+ or_(*conditions)).options(joinedload('snapshot_metadata'))
+ return query.all()
+ elif not host:
+ return []
@require_context
'host2', {'fake_key': 'fake'}),
ignored_keys='volume')
+ def test_snapshot_get_by_host_with_pools(self):
+ db.volume_create(self.ctxt, {'id': 1, 'host': 'host1#pool1'})
+ db.volume_create(self.ctxt, {'id': 2, 'host': 'host1#pool2'})
+
+ snapshot1 = db.snapshot_create(self.ctxt, {'id': 1, 'volume_id': 1})
+ snapshot2 = db.snapshot_create(self.ctxt, {'id': 2, 'volume_id': 2})
+
+ self._assertEqualListsOfObjects([snapshot1, snapshot2],
+ db.snapshot_get_by_host(
+ self.ctxt,
+ 'host1'),
+ ignored_keys='volume')
+ self._assertEqualListsOfObjects([snapshot1],
+ db.snapshot_get_by_host(
+ self.ctxt,
+ 'host1#pool1'),
+ ignored_keys='volume')
+
+ self._assertEqualListsOfObjects([],
+ db.snapshot_get_by_host(
+ self.ctxt,
+ 'host1#pool0'),
+ ignored_keys='volume')
+
def test_snapshot_get_all_by_project(self):
db.volume_create(self.ctxt, {'id': 1})
db.volume_create(self.ctxt, {'id': 2})