Skip to content

Commit

Permalink
Merge pull request #49 from tzumainn/event-command
Browse files Browse the repository at this point in the history
Add event list command
  • Loading branch information
tzumainn authored Jun 29, 2023
2 parents 6ca2d8c + 0cef009 commit 6bab2e6
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 1 deletion.
81 changes: 81 additions & 0 deletions esileapclient/osc/v1/event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# 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 logging

from osc_lib.command import command
from osc_lib import utils as oscutils

from esileapclient.v1.event import Event as EVENT_RESOURCE

LOG = logging.getLogger(__name__)


class ListEvent(command.Lister):
"""List events."""

log = logging.getLogger(__name__ + ".ListEvent")

def get_parser(self, prog_name):
parser = super(ListEvent, self).get_parser(prog_name)

parser.add_argument(
'--project',
dest='project_id',
required=False,
help="Show all events associated with given project ID or name.")
parser.add_argument(
'--last-event-id',
dest='last_event_id',
required=False,
help="Show events after this event ID.")
parser.add_argument(
'--last-notification-time',
dest='last_event_time',
required=False,
help="Show events after this notification time.")
parser.add_argument(
'--event-type',
dest='event_type',
required=False,
help="Show events matching this event type.")
parser.add_argument(
'--resource-type',
dest='resource_type',
required=False,
help="Show events matching this resource type.")
parser.add_argument(
'--resource-uuid',
dest='resource_uuid',
required=False,
help="Show events matching this resource ID or name.")

return parser

def take_action(self, parsed_args):
client = self.app.client_manager.lease

filters = {
'lessee_or_owner_id': parsed_args.project_id,
'last_event_id': parsed_args.last_event_id,
'last_event_time': parsed_args.last_event_time,
'event_type': parsed_args.event_type,
'resource_type': parsed_args.resource_type,
'resource_uuid': parsed_args.resource_uuid,
}

data = client.event.list(filters)
columns = EVENT_RESOURCE.fields.keys()
labels = EVENT_RESOURCE.fields.values()

return (labels,
(oscutils.get_item_properties(s, columns) for s in data))
16 changes: 16 additions & 0 deletions esileapclient/tests/unit/osc/v1/fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
node_name = "fake-node"
node_uuid = "fake-uuid"
node_owner = "fake-owner"
event_id = 7
event_type = 'fake.event'
event_time = "3000-07-01T12"
object_type = 'lease'

OFFER = {
'availabilities': json.loads(lease_availabilities),
Expand Down Expand Up @@ -86,3 +90,15 @@
'uuid': node_uuid,
'owner': node_owner
}

EVENT = {
'id': event_id,
'event_type': event_type,
'event_time': event_time,
'object_type': object_type,
'object_uuid': lease_uuid,
'resource_type': lease_resource_type,
'resource_uuid': lease_resource_uuid,
'lessee_id': lease_project_id,
'owner_id': lease_owner_id,
}
69 changes: 69 additions & 0 deletions esileapclient/tests/unit/osc/v1/test_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import copy

from esileapclient.osc.v1 import event
from esileapclient.tests.unit.osc.v1 import base
from esileapclient.tests.unit.osc.v1 import fakes


class TestEvent(base.TestESILeapCommand):

def setUp(self):
super(TestEvent, self).setUp()

self.client_mock = self.app.client_manager.lease
self.client_mock.reset_mock()


class TestEventList(TestEvent):

def setUp(self):
super(TestEventList, self).setUp()

self.client_mock.event.list.return_value = [
base.FakeResource(copy.deepcopy(fakes.EVENT))
]
self.cmd = event.ListEvent(self.app, None)

def test_event_list(self):
arglist = []
verifylist = []

parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)

filters = {
'lessee_or_owner_id': parsed_args.project_id,
'last_event_id': parsed_args.last_event_id,
'last_event_time': parsed_args.last_event_time,
'event_type': parsed_args.event_type,
'resource_type': parsed_args.resource_type,
'resource_uuid': parsed_args.resource_uuid,
}

self.client_mock.event.list.assert_called_with(filters)

collist = [
"ID",
"Event Type",
"Event Time",
"Object Type",
"Object UUID",
"Resource Type",
"Resource UUID",
"Lessee ID",
"Owner ID",
]

self.assertEqual(collist, list(columns))

datalist = ((fakes.event_id,
fakes.event_type,
fakes.event_time,
fakes.object_type,
fakes.lease_uuid,
fakes.lease_resource_type,
fakes.lease_resource_uuid,
fakes.lease_project_id,
fakes.lease_owner_id,
),)
self.assertEqual(datalist, tuple(data))
2 changes: 2 additions & 0 deletions esileapclient/v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from esileapclient.common import http
from esileapclient.common.http import DEFAULT_VER
from esileapclient.v1 import event
from esileapclient.v1 import lease
from esileapclient.v1 import node
from esileapclient.v1 import offer
Expand All @@ -41,6 +42,7 @@ def __init__(self, *args, **kwargs):
"it automatically")

self.http_client = http._construct_http_client(*args, **kwargs)
self.event = event.EventManager(self.http_client)
self.lease = lease.LeaseManager(self.http_client)
self.node = node.NodeManager(self.http_client)
self.offer = offer.OfferManager(self.http_client)
73 changes: 73 additions & 0 deletions esileapclient/v1/event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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 logging

from esileapclient.common import base

LOG = logging.getLogger(__name__)


class Event(base.Resource):

detailed_fields = {
'id': "ID",
'event_type': "Event Type",
'event_time': "Event Time",
'object_type': "Object Type",
'object_uuid': "Object UUID",
'resource_type': "Resource Type",
'resource_uuid': "Resource UUID",
'lessee_id': "Lessee ID",
'owner_id': "Owner ID",
}

fields = {
'id': "ID",
'event_type': "Event Type",
'event_time': "Event Time",
'object_type': "Object Type",
'object_uuid': "Object UUID",
'resource_type': "Resource Type",
'resource_uuid': "Resource UUID",
'lessee_id': "Lessee ID",
'owner_id': "Owner ID",
}

_creation_attributes = ['id', 'event_type', 'event_time',
'object_type', 'object_uuid',
'resource_type', 'resource_uuid',
'lessee_id', 'owner_id']

def __repr__(self):
return "<Event %s>" % self._info


class EventManager(base.Manager):
resource_class = Event
_resource_name = 'events'

def list(self, filters, os_esileap_api_version=None):
"""Retrieve a list of events.
:returns: A list of events.
"""

resource_id = ''

url_variables = EventManager._url_variables(filters)
url = self._path(resource_id) + url_variables

events = self._list(url,
os_esileap_api_version=os_esileap_api_version)

if type(events) is list:
return events
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
openstacksdk<1.3.0
pbr!=2.1.0,>=2.0.0 # Apache-2.0
python-openstackclient>=3.18.0
six>=1.12.0
six>=1.12.0
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ openstack.cli.extension =
lease = esileapclient.osc.plugin

openstack.lease.v1 =
esi_event_list = esileapclient.osc.v1.event:ListEvent
esi_lease_list = esileapclient.osc.v1.lease:ListLease
esi_lease_create = esileapclient.osc.v1.lease:CreateLease
esi_lease_show = esileapclient.osc.v1.lease:ShowLease
Expand Down

0 comments on commit 6bab2e6

Please sign in to comment.