-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from tzumainn/event-history
Added event object
- Loading branch information
Showing
15 changed files
with
586 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import datetime | ||
import pecan | ||
from pecan import rest | ||
import wsme | ||
from wsme import types as wtypes | ||
import wsmeext.pecan as wsme_pecan | ||
|
||
from esi_leap.api.controllers import base | ||
from esi_leap.api.controllers import types | ||
from esi_leap.api.controllers.v1 import utils | ||
from esi_leap.common import exception | ||
from esi_leap.common import keystone | ||
import esi_leap.conf | ||
from esi_leap.objects import event as event_obj | ||
from esi_leap.resource_objects import get_resource_object | ||
|
||
CONF = esi_leap.conf.CONF | ||
|
||
|
||
class Event(base.ESILEAPBase): | ||
|
||
id = wsme.wsattr(int, readonly=True) | ||
event_type = wsme.wsattr(wtypes.text, readonly=True) | ||
event_time = wsme.wsattr(datetime.datetime, readonly=True) | ||
object_type = wsme.wsattr(wtypes.text, readonly=True) | ||
object_uuid = wsme.wsattr(wtypes.text, readonly=True) | ||
resource_type = wsme.wsattr(wtypes.text, readonly=True) | ||
resource_uuid = wsme.wsattr(wtypes.text, readonly=True) | ||
lessee_id = wsme.wsattr(wtypes.text, readonly=True) | ||
owner_id = wsme.wsattr(wtypes.text, readonly=True) | ||
|
||
def __init__(self, **kwargs): | ||
|
||
self.fields = event_obj.Event.fields | ||
for field in self.fields: | ||
setattr(self, field, kwargs.get(field, wtypes.Unset)) | ||
|
||
|
||
class EventCollection(types.Collection): | ||
events = [Event] | ||
|
||
def __init__(self, **kwargs): | ||
self._type = 'events' | ||
|
||
|
||
class EventsController(rest.RestController): | ||
|
||
@wsme_pecan.wsexpose(EventCollection, int, wtypes.text, | ||
datetime.datetime, wtypes.text, wtypes.text, | ||
wtypes.text, wtypes.text) | ||
def get_all(self, last_event_id=None, lessee_or_owner_id=None, | ||
last_event_time=None, event_type=None, | ||
resource_type=None, resource_uuid=None): | ||
request = pecan.request.context | ||
cdict = request.to_policy_values() | ||
|
||
try: | ||
utils.policy_authorize('esi_leap:offer:offer_admin', cdict, cdict) | ||
except exception.HTTPForbidden: | ||
lessee_or_owner_id = cdict['project_id'] | ||
|
||
if lessee_or_owner_id is not None: | ||
lessee_or_owner_id = keystone.get_project_uuid_from_ident( | ||
lessee_or_owner_id) | ||
|
||
if resource_uuid is not None: | ||
if resource_type is None: | ||
resource_type = CONF.api.default_resource_type | ||
resource = get_resource_object(resource_type, resource_uuid) | ||
resource_uuid = resource.get_uuid() | ||
|
||
filters = { | ||
'last_event_id': last_event_id, | ||
'last_event_time': last_event_time, | ||
'lessee_or_owner_id': lessee_or_owner_id, | ||
'event_type': event_type, | ||
'resource_type': resource_type, | ||
'resource_uuid': resource_uuid, | ||
} | ||
|
||
# unpack iterator to tuple so we can use 'del' | ||
for k, v in tuple(filters.items()): | ||
if v is None: | ||
del filters[k] | ||
|
||
events = event_obj.Event.get_all(filters, request) | ||
event_collection = EventCollection() | ||
event_collection.events = [] | ||
for event in events: | ||
e = Event(id=event.id, | ||
event_type=event.event_type, | ||
event_time=event.event_time, | ||
object_type=event.object_type, | ||
object_uuid=event.object_uuid, | ||
resource_type=event.resource_type, | ||
resource_uuid=event.resource_uuid, | ||
lessee_id=event.lessee_id, | ||
owner_id=event.owner_id) | ||
event_collection.events.append(e) | ||
|
||
return event_collection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
esi_leap/db/sqlalchemy/alembic/versions/a1ea63fec697_create_events_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
"""create events table | ||
Revision ID: a1ea63fec697 | ||
Revises: | ||
Create Date: 2023-06-26 14:22:34.822066 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'a1ea63fec697' | ||
down_revision = None | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.create_table( | ||
'events', | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('event_type', sa.String(length=255), nullable=False), | ||
sa.Column('event_time', sa.DateTime(), nullable=False), | ||
sa.Column('object_type', sa.String(length=255), nullable=True), | ||
sa.Column('object_uuid', sa.String(length=36), nullable=True), | ||
sa.Column('resource_type', sa.String(length=255), nullable=True), | ||
sa.Column('resource_uuid', sa.String(length=36), nullable=True), | ||
sa.Column('lessee_id', sa.String(length=36), nullable=True), | ||
sa.Column('owner_id', sa.String(length=36), nullable=True), | ||
sa.Column('created_at', sa.DateTime(), nullable=True), | ||
sa.Column('updated_at', sa.DateTime(), nullable=True), | ||
sa.PrimaryKeyConstraint('id'), | ||
) | ||
|
||
op.create_index('event_type_idx', 'events', ['event_type'], | ||
unique=False) | ||
op.create_index('event_lessee_id_idx', 'events', ['lessee_id'], | ||
unique=False) | ||
op.create_index('event_owner_id_idx', 'events', ['owner_id'], | ||
unique=False) | ||
op.create_index('event_resource_idx', 'events', | ||
['resource_type', 'resource_uuid'], | ||
unique=False) | ||
|
||
|
||
def downgrade(): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
def register_all(): | ||
__import__('esi_leap.objects.event') | ||
__import__('esi_leap.objects.lease') | ||
__import__('esi_leap.objects.offer') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from esi_leap.db import api as dbapi | ||
from esi_leap.objects import base | ||
from esi_leap.objects import fields | ||
|
||
from oslo_config import cfg | ||
from oslo_log import log as logging | ||
from oslo_versionedobjects import base as versioned_objects_base | ||
|
||
CONF = cfg.CONF | ||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
@versioned_objects_base.VersionedObjectRegistry.register | ||
class Event(base.ESILEAPObject): | ||
dbapi = dbapi.get_instance() | ||
|
||
fields = { | ||
'id': fields.IntegerField(), | ||
'event_type': fields.StringField(), | ||
'event_time': fields.DateTimeField(), | ||
'object_type': fields.StringField(nullable=True), | ||
'object_uuid': fields.StringField(nullable=True), | ||
'resource_type': fields.StringField(nullable=True), | ||
'resource_uuid': fields.StringField(nullable=True), | ||
'lessee_id': fields.StringField(nullable=True), | ||
'owner_id': fields.StringField(nullable=True), | ||
} | ||
|
||
@classmethod | ||
def get_all(cls, filters, context=None): | ||
db_events = cls.dbapi.event_get_all(filters) | ||
return cls._from_db_object_list(context, db_events) | ||
|
||
def create(self, context=None): | ||
updates = self.obj_get_changes() | ||
|
||
LOG.info('Creating event') | ||
db_event = self.dbapi.event_create(updates) | ||
self._from_db_object(context, self, db_event) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.