Merge "Select only necessary columns in allocation API"
This commit is contained in:
commit
8663d58d1f
@ -100,8 +100,14 @@ def get_reservation_allocations_by_host_ids(host_ids, start_date, end_date,
|
||||
session = get_session()
|
||||
border0 = start_date <= models.Lease.end_date
|
||||
border1 = models.Lease.start_date <= end_date
|
||||
query = (session.query(models.Reservation, models.ComputeHostAllocation)
|
||||
.join(models.Lease).join(models.ComputeHostAllocation)
|
||||
query = (session.query(models.Reservation.id,
|
||||
models.Reservation.lease_id,
|
||||
models.ComputeHostAllocation.compute_host_id)
|
||||
.join(models.Lease,
|
||||
models.Lease.id == models.Reservation.lease_id)
|
||||
.join(models.ComputeHostAllocation,
|
||||
models.ComputeHostAllocation.reservation_id ==
|
||||
models.Reservation.id)
|
||||
.filter(models.ComputeHostAllocation.compute_host_id
|
||||
.in_(host_ids))
|
||||
.filter(sa.and_(border0, border1)))
|
||||
|
@ -14,6 +14,7 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import collections
|
||||
import datetime
|
||||
|
||||
from novaclient import exceptions as nova_exceptions
|
||||
@ -541,21 +542,12 @@ class PhysicalHostPlugin(base.BasePlugin, nova.NovaClientWrapper):
|
||||
|
||||
# To reduce overhead, this method only executes one query
|
||||
# to get the allocation information
|
||||
allocs = db_utils.get_reservation_allocations_by_host_ids(
|
||||
rsv_lease_host = db_utils.get_reservation_allocations_by_host_ids(
|
||||
hosts, start, end, lease_id, reservation_id)
|
||||
|
||||
hosts_allocs = {}
|
||||
for reservation, alloc in allocs:
|
||||
if alloc['compute_host_id'] in hosts_allocs:
|
||||
hosts_allocs[alloc['compute_host_id']].append({
|
||||
'lease_id': reservation['lease_id'],
|
||||
'id': reservation['id']
|
||||
})
|
||||
else:
|
||||
hosts_allocs[alloc['compute_host_id']] = [{
|
||||
'lease_id': reservation['lease_id'],
|
||||
'id': reservation['id']
|
||||
}]
|
||||
hosts_allocs = collections.defaultdict(list)
|
||||
for rsv, lease, host in rsv_lease_host:
|
||||
hosts_allocs[host].append({'lease_id': lease, 'id': rsv})
|
||||
return hosts_allocs
|
||||
|
||||
def _matching_hosts(self, hypervisor_properties, resource_properties,
|
||||
|
@ -407,73 +407,63 @@ class SQLAlchemyDBUtilsTestCase(tests.DBTestCase):
|
||||
self.check_reservation([], ['r4'],
|
||||
'2030-01-01 07:00', '2030-01-01 15:00')
|
||||
|
||||
def test_get_reservation_allocations_by_host_ids(self):
|
||||
def create_allocation_tuple(lease_id):
|
||||
def _create_allocation_tuple(self, lease_id):
|
||||
reservation = db_api.reservation_get_all_by_lease_id(lease_id)[0]
|
||||
allocation = db_api.host_allocation_get_all_by_values(
|
||||
reservation_id=reservation['id'])[0]
|
||||
return (reservation['id'], allocation['id'])
|
||||
return (reservation['id'],
|
||||
reservation['lease_id'],
|
||||
allocation['compute_host_id'])
|
||||
|
||||
def test_get_reservation_allocations_by_host_ids(self):
|
||||
self._setup_leases()
|
||||
|
||||
# query all allocations of lease1, lease2 and lease3
|
||||
expected = [
|
||||
create_allocation_tuple('lease1'),
|
||||
create_allocation_tuple('lease2'),
|
||||
create_allocation_tuple('lease3'),
|
||||
self._create_allocation_tuple('lease1'),
|
||||
self._create_allocation_tuple('lease2'),
|
||||
self._create_allocation_tuple('lease3'),
|
||||
]
|
||||
ret = db_utils.get_reservation_allocations_by_host_ids(
|
||||
['r1', 'r2'], '2030-01-01 08:00', '2030-01-01 15:00')
|
||||
|
||||
self.assertListEqual(expected, [(r['id'], a['id']) for r, a in ret])
|
||||
self.assertListEqual(expected, ret)
|
||||
|
||||
# query allocations of lease2 and lease3
|
||||
expected = [
|
||||
create_allocation_tuple('lease2'),
|
||||
create_allocation_tuple('lease3'),
|
||||
self._create_allocation_tuple('lease2'),
|
||||
self._create_allocation_tuple('lease3'),
|
||||
]
|
||||
ret = db_utils.get_reservation_allocations_by_host_ids(
|
||||
['r1', 'r2'], '2030-01-01 11:30', '2030-01-01 15:00')
|
||||
|
||||
self.assertListEqual(expected, [(r['id'], a['id']) for r, a in ret])
|
||||
self.assertListEqual(expected, ret)
|
||||
|
||||
def test_get_reservation_allocations_by_host_ids_with_lease_id(self):
|
||||
def create_allocation_tuple(lease_id):
|
||||
reservation = db_api.reservation_get_all_by_lease_id(lease_id)[0]
|
||||
allocation = db_api.host_allocation_get_all_by_values(
|
||||
reservation_id=reservation['id'])[0]
|
||||
return (reservation['id'], allocation['id'])
|
||||
|
||||
self._setup_leases()
|
||||
|
||||
# query all allocations of lease1, lease2 and lease3
|
||||
expected = [
|
||||
create_allocation_tuple('lease1'),
|
||||
self._create_allocation_tuple('lease1'),
|
||||
]
|
||||
ret = db_utils.get_reservation_allocations_by_host_ids(
|
||||
['r1', 'r2'], '2030-01-01 08:00', '2030-01-01 15:00', 'lease1')
|
||||
|
||||
self.assertListEqual(expected, [(r['id'], a['id']) for r, a in ret])
|
||||
self.assertListEqual(expected, ret)
|
||||
|
||||
def test_get_reservation_allocations_by_host_ids_with_reservation_id(self):
|
||||
def create_allocation_tuple(lease_id):
|
||||
reservation = db_api.reservation_get_all_by_lease_id(lease_id)[0]
|
||||
allocation = db_api.host_allocation_get_all_by_values(
|
||||
reservation_id=reservation['id'])[0]
|
||||
return (reservation['id'], allocation['id'])
|
||||
|
||||
self._setup_leases()
|
||||
reservation1 = db_api.reservation_get_all_by_lease_id('lease1')[0]
|
||||
|
||||
# query allocations of lease1
|
||||
expected = [
|
||||
create_allocation_tuple('lease1'),
|
||||
self._create_allocation_tuple('lease1'),
|
||||
]
|
||||
ret = db_utils.get_reservation_allocations_by_host_ids(
|
||||
['r1', 'r2'], '2030-01-01 08:00', '2030-01-01 15:00',
|
||||
reservation_id=reservation1['id'])
|
||||
|
||||
self.assertListEqual(expected, [(r['id'], a['id']) for r, a in ret])
|
||||
self.assertListEqual(expected, ret)
|
||||
|
||||
def test_get_plugin_reservation_with_host(self):
|
||||
patch_host_reservation_get = self.patch(db_api, 'host_reservation_get')
|
||||
|
@ -430,19 +430,16 @@ class PhysicalHostPluginTestCase(tests.TestCase):
|
||||
self.fake_host_id)
|
||||
|
||||
def test_list_allocations(self):
|
||||
def reservation_allocation_tuple(r_id, l_id, h_id):
|
||||
return ({'id': r_id, 'lease_id': l_id}, {'compute_host_id': h_id})
|
||||
|
||||
self.db_get_reserv_allocs = self.patch(
|
||||
self.db_utils, 'get_reservation_allocations_by_host_ids')
|
||||
|
||||
# Expecting a list of (Reservation, Allocation)
|
||||
self.db_get_reserv_allocs.return_value = [
|
||||
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-1'),
|
||||
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-2'),
|
||||
reservation_allocation_tuple('reservation-2', 'lease-1', 'host-2'),
|
||||
reservation_allocation_tuple('reservation-2', 'lease-1', 'host-3'),
|
||||
reservation_allocation_tuple('reservation-3', 'lease-2', 'host-1'),
|
||||
('reservation-1', 'lease-1', 'host-1'),
|
||||
('reservation-1', 'lease-1', 'host-2'),
|
||||
('reservation-2', 'lease-1', 'host-2'),
|
||||
('reservation-2', 'lease-1', 'host-3'),
|
||||
('reservation-3', 'lease-2', 'host-1'),
|
||||
]
|
||||
|
||||
expected = [
|
||||
@ -477,18 +474,15 @@ class PhysicalHostPluginTestCase(tests.TestCase):
|
||||
self.assertListEqual(expected, ret)
|
||||
|
||||
def test_list_allocations_with_lease_id(self):
|
||||
def reservation_allocation_tuple(r_id, l_id, h_id):
|
||||
return ({'id': r_id, 'lease_id': l_id}, {'compute_host_id': h_id})
|
||||
|
||||
self.db_get_reserv_allocs = self.patch(
|
||||
self.db_utils, 'get_reservation_allocations_by_host_ids')
|
||||
|
||||
# Expecting a list of (Reservation, Allocation)
|
||||
self.db_get_reserv_allocs.return_value = [
|
||||
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-1'),
|
||||
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-2'),
|
||||
reservation_allocation_tuple('reservation-2', 'lease-1', 'host-2'),
|
||||
reservation_allocation_tuple('reservation-2', 'lease-1', 'host-3'),
|
||||
('reservation-1', 'lease-1', 'host-1'),
|
||||
('reservation-1', 'lease-1', 'host-2'),
|
||||
('reservation-2', 'lease-1', 'host-2'),
|
||||
('reservation-2', 'lease-1', 'host-3'),
|
||||
]
|
||||
|
||||
expected = [
|
||||
@ -522,16 +516,13 @@ class PhysicalHostPluginTestCase(tests.TestCase):
|
||||
self.assertListEqual(expected, ret)
|
||||
|
||||
def test_list_allocations_with_reservation_id(self):
|
||||
def reservation_allocation_tuple(r_id, l_id, h_id):
|
||||
return ({'id': r_id, 'lease_id': l_id}, {'compute_host_id': h_id})
|
||||
|
||||
self.db_get_reserv_allocs = self.patch(
|
||||
self.db_utils, 'get_reservation_allocations_by_host_ids')
|
||||
|
||||
# Expecting a list of (Reservation, Allocation)
|
||||
self.db_get_reserv_allocs.return_value = [
|
||||
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-1'),
|
||||
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-2'),
|
||||
('reservation-1', 'lease-1', 'host-1'),
|
||||
('reservation-1', 'lease-1', 'host-2'),
|
||||
]
|
||||
|
||||
expected = [
|
||||
@ -559,19 +550,16 @@ class PhysicalHostPluginTestCase(tests.TestCase):
|
||||
self.assertListEqual(expected, ret)
|
||||
|
||||
def test_get_allocations(self):
|
||||
def reservation_allocation_tuple(r_id, l_id, h_id):
|
||||
return ({'id': r_id, 'lease_id': l_id}, {'compute_host_id': h_id})
|
||||
|
||||
self.db_get_reserv_allocs = self.patch(
|
||||
self.db_utils, 'get_reservation_allocations_by_host_ids')
|
||||
|
||||
# Expecting a list of (Reservation, Allocation)
|
||||
self.db_get_reserv_allocs.return_value = [
|
||||
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-1'),
|
||||
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-2'),
|
||||
reservation_allocation_tuple('reservation-2', 'lease-1', 'host-2'),
|
||||
reservation_allocation_tuple('reservation-2', 'lease-1', 'host-3'),
|
||||
reservation_allocation_tuple('reservation-3', 'lease-2', 'host-1'),
|
||||
('reservation-1', 'lease-1', 'host-1'),
|
||||
('reservation-1', 'lease-1', 'host-2'),
|
||||
('reservation-2', 'lease-1', 'host-2'),
|
||||
('reservation-2', 'lease-1', 'host-3'),
|
||||
('reservation-3', 'lease-2', 'host-1'),
|
||||
]
|
||||
|
||||
expected = {
|
||||
@ -589,15 +577,12 @@ class PhysicalHostPluginTestCase(tests.TestCase):
|
||||
self.assertDictEqual(expected, ret)
|
||||
|
||||
def test_get_allocations_with_lease_id(self):
|
||||
def reservation_allocation_tuple(r_id, l_id, h_id):
|
||||
return ({'id': r_id, 'lease_id': l_id}, {'compute_host_id': h_id})
|
||||
|
||||
self.db_get_reserv_allocs = self.patch(
|
||||
self.db_utils, 'get_reservation_allocations_by_host_ids')
|
||||
|
||||
# Expecting a list of (Reservation, Allocation)
|
||||
self.db_get_reserv_allocs.return_value = [
|
||||
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-1'),
|
||||
('reservation-1', 'lease-1', 'host-1'),
|
||||
]
|
||||
|
||||
expected = {
|
||||
@ -615,15 +600,12 @@ class PhysicalHostPluginTestCase(tests.TestCase):
|
||||
self.assertDictEqual(expected, ret)
|
||||
|
||||
def test_get_allocations_with_reservation_id(self):
|
||||
def reservation_allocation_tuple(r_id, l_id, h_id):
|
||||
return ({'id': r_id, 'lease_id': l_id}, {'compute_host_id': h_id})
|
||||
|
||||
self.db_get_reserv_allocs = self.patch(
|
||||
self.db_utils, 'get_reservation_allocations_by_host_ids')
|
||||
|
||||
# Expecting a list of (Reservation, Allocation)
|
||||
self.db_get_reserv_allocs.return_value = [
|
||||
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-1'),
|
||||
('reservation-1', 'lease-1', 'host-1'),
|
||||
]
|
||||
|
||||
expected = {
|
||||
@ -641,19 +623,16 @@ class PhysicalHostPluginTestCase(tests.TestCase):
|
||||
self.assertDictEqual(expected, ret)
|
||||
|
||||
def test_get_allocations_with_invalid_host(self):
|
||||
def reservation_allocation_tuple(r_id, l_id, h_id):
|
||||
return ({'id': r_id, 'lease_id': l_id}, {'compute_host_id': h_id})
|
||||
|
||||
self.db_get_reserv_allocs = self.patch(
|
||||
self.db_utils, 'get_reservation_allocations_by_host_ids')
|
||||
|
||||
# Expecting a list of (Reservation, Allocation)
|
||||
self.db_get_reserv_allocs.return_value = [
|
||||
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-1'),
|
||||
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-2'),
|
||||
reservation_allocation_tuple('reservation-2', 'lease-1', 'host-2'),
|
||||
reservation_allocation_tuple('reservation-2', 'lease-1', 'host-3'),
|
||||
reservation_allocation_tuple('reservation-3', 'lease-2', 'host-1'),
|
||||
('reservation-1', 'lease-1', 'host-1'),
|
||||
('reservation-1', 'lease-1', 'host-2'),
|
||||
('reservation-2', 'lease-1', 'host-2'),
|
||||
('reservation-2', 'lease-1', 'host-3'),
|
||||
('reservation-3', 'lease-2', 'host-1'),
|
||||
]
|
||||
expected = {'resource_id': 'no-reserved-host', 'reservations': []}
|
||||
ret = self.fake_phys_plugin.get_allocations('no-reserved-host', {})
|
||||
|
Loading…
Reference in New Issue
Block a user