def _existing_allocations(self, reservations): allocations = {} for reservation in reservations: resource_type = reservation['resource_type'] if resource_type not in self.plugins: raise exceptions.UnsupportedResourceType( resource_type=resource_type) plugin = self.plugins.get(resource_type) if not plugin: raise common_ex.BlazarException( 'Invalid plugin names are specified: %s' % resource_type) resource_ids = [ x['resource_id'] for x in plugin.list_allocations( dict(reservation_id=reservation['id'])) if x['reservations']] allocations[resource_type] = [ plugin.get(rid) for rid in resource_ids] return allocations
def _allocation_candidates(self, lease, reservations): """Returns dict by resource type of reservation candidates.""" allocations = {} for reservation in reservations: res = reservation.copy() resource_type = reservation['resource_type'] res['start_date'] = lease['start_date'] res['end_date'] = lease['end_date'] if resource_type not in self.plugins: raise exceptions.UnsupportedResourceType( resource_type=resource_type) plugin = self.plugins.get(resource_type) if not plugin: raise common_ex.BlazarException( 'Invalid plugin names are specified: %s' % resource_type) candidate_ids = plugin.allocation_candidates(res) allocations[resource_type] = [ plugin.get(cid) for cid in candidate_ids] return allocations
def _get_events(resource_id, start_date, end_date, resource_type): """Create a list of events.""" events = {} if resource_type == 'host': leases = _get_leases_from_host_id(resource_id, start_date, end_date) elif resource_type == 'floatingip': leases = _get_leases_from_fip_id(resource_id, start_date, end_date) else: mgr_exceptions.UnsupportedResourceType(resource_type) for lease in leases: if lease.start_date < start_date: min_date = start_date else: min_date = lease.start_date if lease.end_date > end_date: max_date = end_date else: max_date = lease.end_date if min_date in events.keys(): events[min_date]['quantity'] += 1 else: events[min_date] = {'quantity': 1} if max_date in events.keys(): events[max_date]['quantity'] -= 1 else: events[max_date] = {'quantity': -1} return events
def get_plugin_reservation(resource_type, resource_id): if resource_type == host_plugin.RESOURCE_TYPE: return api.host_reservation_get(resource_id) elif resource_type == instance_plugin.RESOURCE_TYPE: return api.instance_reservation_get(resource_id) else: raise mgr_exceptions.UnsupportedResourceType(resource_type)
def _create_reservation(self, values): resource_type = values['resource_type'] if resource_type not in self.plugins: raise exceptions.UnsupportedResourceType(resource_type) reservation_values = { 'lease_id': values['lease_id'], 'resource_type': resource_type, 'status': status.reservation.PENDING } reservation = db_api.reservation_create(reservation_values) resource_id = self.plugins[resource_type].reserve_resource( reservation['id'], values) db_api.reservation_update(reservation['id'], {'resource_id': resource_id})
def __getattr__(self, name): """RPC Dispatcher for plugins methods.""" fn = None try: resource_type, method = name.rsplit(':', 1) except ValueError: # NOTE(sbauza) : the dispatcher needs to know which plugin to use, # raising error if consequently not raise AttributeError(name) try: try: fn = getattr(self.plugins[resource_type], method) except KeyError: LOG.error("Plugin with resource type %s not found", resource_type) raise exceptions.UnsupportedResourceType(resource_type) except AttributeError: LOG.error("Plugin %s doesn't include method %s", self.plugins[resource_type], method) if fn is not None: return fn raise AttributeError(name)