storage: factorize not implemented methods

There's a lot of driver that are not implementing everything, and as
time goes by, this is getting and going to be more and more common. Not
all drivers are suited to do everything, so implementing every methods
as abstract makes a lot of NotImplementedError raising redundant.

This patch makes stop using that and remove a lot of useless copy pasted
code.

Change-Id: I9e48c4f0484033061a3c3461d761e4cea4b8e75e
This commit is contained in:
Julien Danjou 2013-12-15 14:50:23 +01:00 committed by Gerrit Code Review
parent 7d1164d28d
commit 8a589eb927
5 changed files with 68 additions and 363 deletions

View File

@ -112,20 +112,19 @@ class StorageEngine(object):
"""Return a Connection instance based on the configuration settings.""" """Return a Connection instance based on the configuration settings."""
@six.add_metaclass(abc.ABCMeta)
class Connection(object): class Connection(object):
"""Base class for storage system connections.""" """Base class for storage system connections."""
@abc.abstractmethod @staticmethod
def __init__(self, conf): def __init__(conf):
"""Constructor.""" """Constructor."""
@abc.abstractmethod @staticmethod
def upgrade(self): def upgrade():
"""Migrate the database to `version` or the most recent version.""" """Migrate the database to `version` or the most recent version."""
@abc.abstractmethod @staticmethod
def record_metering_data(self, data): def record_metering_data(data):
"""Write the data to the backend storage system. """Write the data to the backend storage system.
:param data: a dictionary such as returned by :param data: a dictionary such as returned by
@ -133,32 +132,36 @@ class Connection(object):
All timestamps must be naive utc datetime object. All timestamps must be naive utc datetime object.
""" """
raise NotImplementedError(_('Projects not implemented'))
@abc.abstractmethod @staticmethod
def clear_expired_metering_data(self, ttl): def clear_expired_metering_data(ttl):
"""Clear expired data from the backend storage system according to the """Clear expired data from the backend storage system according to the
time-to-live. time-to-live.
:param ttl: Number of seconds to keep records for. :param ttl: Number of seconds to keep records for.
""" """
raise NotImplementedError(_('Clearing samples not implemented'))
@abc.abstractmethod @staticmethod
def get_users(self, source=None): def get_users(source=None):
"""Return an iterable of user id strings. """Return an iterable of user id strings.
:param source: Optional source filter. :param source: Optional source filter.
""" """
raise NotImplementedError(_('Users not implemented'))
@abc.abstractmethod @staticmethod
def get_projects(self, source=None): def get_projects(source=None):
"""Return an iterable of project id strings. """Return an iterable of project id strings.
:param source: Optional source filter. :param source: Optional source filter.
""" """
raise NotImplementedError(_('Projects not implemented'))
@abc.abstractmethod @staticmethod
def get_resources(self, user=None, project=None, source=None, def get_resources(user=None, project=None, source=None,
start_timestamp=None, start_timestamp_op=None, start_timestamp=None, start_timestamp_op=None,
end_timestamp=None, end_timestamp_op=None, end_timestamp=None, end_timestamp_op=None,
metaquery={}, resource=None, pagination=None): metaquery={}, resource=None, pagination=None):
@ -176,9 +179,10 @@ class Connection(object):
:param resource: Optional resource filter. :param resource: Optional resource filter.
:param pagination: Optional pagination query. :param pagination: Optional pagination query.
""" """
raise NotImplementedError(_('Resources not implemented'))
@abc.abstractmethod @staticmethod
def get_meters(self, user=None, project=None, resource=None, source=None, def get_meters(user=None, project=None, resource=None, source=None,
metaquery={}, pagination=None): metaquery={}, pagination=None):
"""Return an iterable of model.Meter instances containing meter """Return an iterable of model.Meter instances containing meter
information. information.
@ -190,47 +194,51 @@ class Connection(object):
:param metaquery: Optional dict with metadata to match on. :param metaquery: Optional dict with metadata to match on.
:param pagination: Optional pagination query. :param pagination: Optional pagination query.
""" """
raise NotImplementedError(_('Meters not implemented'))
@abc.abstractmethod @staticmethod
def get_samples(self, sample_filter, limit=None): def get_samples(sample_filter, limit=None):
"""Return an iterable of model.Sample instances. """Return an iterable of model.Sample instances.
:param sample_filter: Filter. :param sample_filter: Filter.
:param limit: Maximum number of results to return. :param limit: Maximum number of results to return.
""" """
raise NotImplementedError(_('Samples not implemented'))
@abc.abstractmethod @staticmethod
def get_meter_statistics(self, sample_filter, period=None, groupby=None): def get_meter_statistics(sample_filter, period=None, groupby=None):
"""Return an iterable of model.Statistics instances. """Return an iterable of model.Statistics instances.
The filter must have a meter value set. The filter must have a meter value set.
""" """
raise NotImplementedError(_('Statistics not implemented'))
@abc.abstractmethod @staticmethod
def get_alarms(self, name=None, user=None, def get_alarms(name=None, user=None,
project=None, enabled=None, alarm_id=None, pagination=None): project=None, enabled=None, alarm_id=None, pagination=None):
"""Yields a lists of alarms that match filters """Yields a lists of alarms that match filters."""
""" raise NotImplementedError(_('Alarms not implemented'))
@abc.abstractmethod @staticmethod
def create_alarm(self, alarm): def create_alarm(alarm):
"""Create an alarm. Returns the alarm as created. """Create an alarm. Returns the alarm as created.
:param alarm: The alarm to create. :param alarm: The alarm to create.
""" """
raise NotImplementedError(_('Alarms not implemented'))
@abc.abstractmethod @staticmethod
def update_alarm(self, alarm): def update_alarm(alarm):
"""update alarm """Update alarm."""
""" raise NotImplementedError(_('Alarms not implemented'))
@abc.abstractmethod @staticmethod
def delete_alarm(self, alarm_id): def delete_alarm(alarm_id):
"""Delete a alarm """Delete an alarm."""
""" raise NotImplementedError(_('Alarms not implemented'))
@abc.abstractmethod @staticmethod
def get_alarm_changes(self, alarm_id, on_behalf_of, def get_alarm_changes(alarm_id, on_behalf_of,
user=None, project=None, type=None, user=None, project=None, type=None,
start_timestamp=None, start_timestamp_op=None, start_timestamp=None, start_timestamp_op=None,
end_timestamp=None, end_timestamp_op=None): end_timestamp=None, end_timestamp_op=None):
@ -257,47 +265,53 @@ class Connection(object):
:param end_timestamp: Optional modified timestamp end range :param end_timestamp: Optional modified timestamp end range
:param end_timestamp_op: Optional timestamp end range operation :param end_timestamp_op: Optional timestamp end range operation
""" """
raise NotImplementedError(_('Alarm history not implemented'))
@abc.abstractmethod @staticmethod
def record_alarm_change(self, alarm_change): def record_alarm_change(alarm_change):
"""Record alarm change event. """Record alarm change event."""
""" raise NotImplementedError(_('Alarm history not implemented'))
@abc.abstractmethod @staticmethod
def clear(self): def clear():
"""Clear database.""" """Clear database."""
@abc.abstractmethod @staticmethod
def record_events(self, events): def record_events(events):
"""Write the events to the backend storage system. """Write the events to the backend storage system.
:param events: a list of model.Event objects. :param events: a list of model.Event objects.
""" """
raise NotImplementedError(_('Events not implemented.'))
@abc.abstractmethod @staticmethod
def get_events(self, event_filter): def get_events(event_filter):
"""Return an iterable of model.Event objects. """Return an iterable of model.Event objects.
""" """
raise NotImplementedError(_('Events not implemented.'))
@abc.abstractmethod @staticmethod
def get_event_types(self): def get_event_types():
"""Return all event types as an iterable of strings. """Return all event types as an iterable of strings.
""" """
raise NotImplementedError(_('Events not implemented.'))
@abc.abstractmethod @staticmethod
def get_trait_types(self, event_type): def get_trait_types(event_type):
"""Return a dictionary containing the name and data type of """Return a dictionary containing the name and data type of
the trait type. Only trait types for the provided event_type are the trait type. Only trait types for the provided event_type are
returned. returned.
:param event_type: the type of the Event :param event_type: the type of the Event
""" """
raise NotImplementedError(_('Events not implemented.'))
@abc.abstractmethod @staticmethod
def get_traits(self, event_type, trait_type=None): def get_traits(event_type, trait_type=None):
"""Return all trait instances associated with an event_type. If """Return all trait instances associated with an event_type. If
trait_type is specified, only return instances of that trait type. trait_type is specified, only return instances of that trait type.
:param event_type: the type of the Event to filter by :param event_type: the type of the Event to filter by
:param trait_type: the name of the Trait to filter by :param trait_type: the name of the Trait to filter by
""" """
raise NotImplementedError(_('Events not implemented.'))

View File

@ -349,15 +349,6 @@ class Connection(base.Connection):
record['_id'] = str(bson.objectid.ObjectId()) record['_id'] = str(bson.objectid.ObjectId())
self.db.meter.insert(record) self.db.meter.insert(record)
def clear_expired_metering_data(self, ttl):
"""Clear expired data from the backend storage system according to the
time-to-live.
:param ttl: Number of seconds to keep records for.
"""
raise NotImplementedError('TTL not implemented.')
def get_users(self, source=None): def get_users(self, source=None):
"""Return an iterable of user id strings. """Return an iterable of user id strings.
@ -720,81 +711,3 @@ class Connection(base.Connection):
"""Delete an alarm """Delete an alarm
""" """
self.db.alarm.remove({'alarm_id': alarm_id}) self.db.alarm.remove({'alarm_id': alarm_id})
def get_alarm_changes(self, alarm_id, on_behalf_of,
user=None, project=None, type=None,
start_timestamp=None, start_timestamp_op=None,
end_timestamp=None, end_timestamp_op=None):
"""Yields list of AlarmChanges describing alarm history
Changes are always sorted in reverse order of occurence, given
the importance of currency.
Segregation for non-administrative users is done on the basis
of the on_behalf_of parameter. This allows such users to have
visibility on both the changes initiated by themselves directly
(generally creation, rule changes, or deletion) and also on those
changes initiated on their behalf by the alarming service (state
transitions after alarm thresholds are crossed).
:param alarm_id: ID of alarm to return changes for
:param on_behalf_of: ID of tenant to scope changes query (None for
administrative user, indicating all projects)
:param user: Optional ID of user to return changes for
:param project: Optional ID of project to return changes for
:project type: Optional change type
:param start_timestamp: Optional modified timestamp start range
:param start_timestamp_op: Optional timestamp start range operation
:param end_timestamp: Optional modified timestamp end range
:param end_timestamp_op: Optional timestamp end range operation
"""
raise NotImplementedError(_('Alarm history not implemented'))
def record_alarm_change(self, alarm_change):
"""Record alarm change event.
"""
raise NotImplementedError(_('Alarm history not implemented'))
@staticmethod
def record_events(events):
"""Write the events.
:param events: a list of model.Event objects.
"""
raise NotImplementedError(_('Events not implemented.'))
@staticmethod
def get_events(event_filter):
"""Return an iterable of model.Event objects.
:param event_filter: EventFilter instance
"""
raise NotImplementedError(_('Events not implemented.'))
@staticmethod
def get_event_types():
"""Return all event types as an iterable of strings.
"""
raise NotImplementedError(_('Events not implemented.'))
@staticmethod
def get_trait_types(event_type):
"""Return a dictionary containing the name and data type of
the trait type. Only trait types for the provided event_type are
returned.
:param event_type: the type of the Event
"""
raise NotImplementedError(_('Events not implemented.'))
@staticmethod
def get_traits(event_type, trait_type=None):
"""Return all trait instances associated with an event_type. If
trait_type is specified, only return instances of that trait type.
:param event_type: the type of the Event to filter by
:param trait_type: the name of the Trait to filter by
"""
raise NotImplementedError(_('Events not implemented.'))

View File

@ -255,15 +255,6 @@ class Connection(base.Connection):
record['f:message'] = json.dumps(data) record['f:message'] = json.dumps(data)
meter_table.put(row, record) meter_table.put(row, record)
def clear_expired_metering_data(self, ttl):
"""Clear expired data from the backend storage system according to the
time-to-live.
:param ttl: Number of seconds to keep records for.
"""
raise NotImplementedError
def get_users(self, source=None): def get_users(self, source=None):
"""Return an iterable of user id strings. """Return an iterable of user id strings.
@ -592,101 +583,6 @@ class Connection(base.Connection):
self._update_meter_stats(results[-1], meter) self._update_meter_stats(results[-1], meter)
return results return results
def get_alarms(self, name=None, user=None,
project=None, enabled=None, alarm_id=None, pagination=None):
"""Yields a lists of alarms that match filters
raise NotImplementedError('metaquery not implemented')
"""
raise NotImplementedError(_('Alarms not implemented'))
def create_alarm(self, alarm):
"""update alarm
"""
raise NotImplementedError(_('Alarms not implemented'))
def update_alarm(self, alarm):
"""update alarm
"""
raise NotImplementedError(_('Alarms not implemented'))
def get_alarm_changes(self, alarm_id, on_behalf_of,
user=None, project=None, type=None,
start_timestamp=None, start_timestamp_op=None,
end_timestamp=None, end_timestamp_op=None):
"""Yields list of AlarmChanges describing alarm history
Changes are always sorted in reverse order of occurence, given
the importance of currency.
Segregation for non-administrative users is done on the basis
of the on_behalf_of parameter. This allows such users to have
visibility on both the changes initiated by themselves directly
(generally creation, rule changes, or deletion) and also on those
changes initiated on their behalf by the alarming service (state
transitions after alarm thresholds are crossed).
:param alarm_id: ID of alarm to return changes for
:param on_behalf_of: ID of tenant to scope changes query (None for
administrative user, indicating all projects)
:param user: Optional ID of user to return changes for
:param project: Optional ID of project to return changes for
:project type: Optional change type
:param start_timestamp: Optional modified timestamp start range
:param start_timestamp_op: Optional timestamp start range operation
:param end_timestamp: Optional modified timestamp end range
:param end_timestamp_op: Optional timestamp end range operation
"""
raise NotImplementedError(_('Alarm history not implemented'))
def record_alarm_change(self, alarm_change):
"""Record alarm change event.
"""
raise NotImplementedError(_('Alarm history not implemented'))
def delete_alarm(self, alarm_id):
"""Delete a alarm
"""
raise NotImplementedError(_('Alarms not implemented'))
def record_events(self, events):
"""Write the events.
:param events: a list of model.Event objects.
"""
raise NotImplementedError(_('Events not implemented.'))
def get_events(self, event_filter):
"""Return an iterable of model.Event objects.
:param event_filter: EventFilter instance
"""
raise NotImplementedError(_('Events not implemented.'))
def get_event_types(self):
"""Return all event types as an iterable of strings.
"""
raise NotImplementedError(_('Events not implemented.'))
def get_trait_types(self, event_type):
"""Return a dictionary containing the name and data type of
the trait type. Only trait types for the provided event_type are
returned.
:param event_type: the type of the Event
"""
raise NotImplementedError(_('Events not implemented.'))
def get_traits(self, event_type, trait_type=None):
"""Return all trait instances associated with an event_type. If
trait_type is specified, only return instances of that trait type.
:param event_type: the type of the Event to filter by
:param trait_type: the name of the Trait to filter by
"""
raise NotImplementedError(_('Events not implemented.'))
############### ###############
# This is a very crude version of "in-memory HBase", which implements just # This is a very crude version of "in-memory HBase", which implements just

View File

@ -176,78 +176,4 @@ class Connection(base.Connection):
return alarm return alarm
def delete_alarm(self, alarm_id): def delete_alarm(self, alarm_id):
"""Delete a alarm """Delete a alarm."""
"""
def get_alarm_changes(self, alarm_id, on_behalf_of,
user=None, project=None, type=None,
start_timestamp=None, start_timestamp_op=None,
end_timestamp=None, end_timestamp_op=None):
"""Yields list of AlarmChanges describing alarm history
Changes are always sorted in reverse order of occurence, given
the importance of currency.
Segregation for non-administrative users is done on the basis
of the on_behalf_of parameter. This allows such users to have
visibility on both the changes initiated by themselves directly
(generally creation, rule changes, or deletion) and also on those
changes initiated on their behalf by the alarming service (state
transitions after alarm thresholds are crossed).
:param alarm_id: ID of alarm to return changes for
:param on_behalf_of: ID of tenant to scope changes query (None for
administrative user, indicating all projects)
:param user: Optional ID of user to return changes for
:param project: Optional ID of project to return changes for
:project type: Optional change type
:param start_timestamp: Optional modified timestamp start range
:param start_timestamp_op: Optional timestamp start range operation
:param end_timestamp: Optional modified timestamp end range
:param end_timestamp_op: Optional timestamp end range operation
"""
raise NotImplementedError(_('Alarm history not implemented'))
def record_alarm_change(self, alarm_change):
"""Record alarm change event.
"""
raise NotImplementedError(_('Alarm history not implemented'))
def record_events(self, events):
"""Write the events.
:param events: a list of model.Event objects.
"""
raise NotImplementedError(_('Events not implemented.'))
def get_events(self, event_filter):
"""Return an iterable of model.Event objects.
:param event_filter: EventFilter instance
"""
raise NotImplementedError(_('Events not implemented.'))
def get_event_types(self):
"""Return all event types as an iterable of strings.
"""
raise NotImplementedError(_('Events not implemented.'))
def get_trait_types(self, event_type):
"""Return a dictionary containing the name and data type of
the trait type. Only trait types for the provided event_type are
returned.
:param event_type: the type of the Event
"""
raise NotImplementedError(_('Events not implemented.'))
def get_traits(self, event_type, trait_type=None):
"""Return all trait instances associated with an event_type. If
trait_type is specified, only return instances of that trait type.
:param event_type: the type of the Event to filter by
:param trait_type: the name of the Trait to filter by
"""
raise NotImplementedError(_('Events not implemented.'))

View File

@ -958,47 +958,3 @@ class Connection(base.Connection):
"""Record alarm change event. """Record alarm change event.
""" """
self.db.alarm_history.insert(alarm_change) self.db.alarm_history.insert(alarm_change)
@staticmethod
def record_events(events):
"""Write the events.
:param events: a list of model.Event objects.
"""
raise NotImplementedError(_('Events not implemented.'))
@staticmethod
def get_events(event_filter):
"""Return an iterable of model.Event objects.
:param event_filter: EventFilter instance
"""
raise NotImplementedError(_('Events not implemented.'))
@staticmethod
def get_event_types():
"""Return all event types as an iterable of strings.
"""
raise NotImplementedError(_('EventTypes not implemented.'))
@staticmethod
def get_trait_types(event_type):
"""Return a dictionary containing the name and data type of
the trait type. Only trait types for the provided event_type are
returned.
:param event_type: the type of the Event
"""
raise NotImplementedError(_('Events not implemented.'))
@staticmethod
def get_traits(event_type, trait_type=None):
"""Return all trait instances associated with an event_type. If
trait_type is specified, only return instances of that trait type.
:param event_type: the type of the Event to filter by
:param trait_type: the name of the Trait to filter by
"""
raise NotImplementedError(_('Events not implemented.'))