From: Tony Mack Date: Wed, 18 Oct 2006 19:44:12 +0000 (+0000) Subject: Initial checkin of new API implementation X-Git-Tag: pycurl-7_13_1~540 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=252dad4b8fefb93ffcd723aa95c836361aaed4ba;p=plcapi.git Initial checkin of new API implementation --- diff --git a/PLC/Events.py b/PLC/Events.py new file mode 100644 index 00000000..e72563eb --- /dev/null +++ b/PLC/Events.py @@ -0,0 +1,76 @@ +# +# Functions for interacting with the events table in the database +# +# Tony Mack +# Copyright (C) 2006 The Trustees of Princeton University +# +# +from PLC.Faults import * +from PLC.Parameter import Parameter +from PLC.Debug import profile +from PLC.Table import Row, Table + +class Event(Row): + """ + Representation of a row in the events table. + """ + + table_name = 'events' + primary_key = 'event_id' + fields = { + 'event_id': Parameter(int, "Event identifier"), + 'person_id': Parameter(int, "Identifier of person responsible for event"), + 'event_type': Parameter(str, "Type of event"), + 'object_type': Parameter(str, "Type of object affected by this event"), + 'fault_code': Parameter(int, "Event fault code"), + 'call': Parameter(str, "Call responsible for this event"), + 'time': Parameter(str, "Date/Time the event took place"), + 'object_ids': Parameter([int], "Ids of objects affected by this event") + } + + def __init__(self, api, fields = {}): + Row.__init__(self, fields) + self.api = api + + + +class Events(Table): + """ + Representation of row(s) from the events table in the database. + """ + + def __init__(self, api, event_ids = None, person_ids = None, event_types = None, \ + object_types = None, object_ids = None, fault_codes = None): + + self.api = api + + sql = "SELECT %s from view_events WHERE True" % ", ".join(Event.fields) + + if event_ids: + sql += " AND event_id IN (%s)" % ", ".join(map(str, event_ids)) + + if person_ids: + sql += " AND person_id IN (%s)" % ", ".join(map(str, person_ids)) + + if object_ids: + sql += " AND object_ids in (%s)" % ", ".join(map(str, object_ids)) + + if event_types: + sql += " AND event_type in (%s)" % ", ".join(api.db.quote(event_types)) + + if object_types: + sql += " AND object_type in (%s)" % ", ".join(api.db.quote(object_types)) + + rows = self.api.db.selectall(sql) + + for row in rows: + self[row['event_id']] = event = Event(api, row) + for aggregate in ['object_ids']: + if not event.has_key(aggregate) or event[aggregate] is None: + event[aggregate] = [] + else: + elements = event[aggregate].split(',') + try: + event[aggregate] = map(int, elements) + except ValueError: + event[aggregate] = elements diff --git a/PLC/Methods/GetEvents.py b/PLC/Methods/GetEvents.py new file mode 100644 index 00000000..05be881d --- /dev/null +++ b/PLC/Methods/GetEvents.py @@ -0,0 +1,74 @@ +import os + +from PLC.Faults import * +from PLC.Method import Method +from PLC.Parameter import Parameter, Mixed +from PLC.Events import Event, Events +from PLC.Auth import PasswordAuth + +class GetEvents(Method): + """ + Return an array of dictionaries containing details about the + specified events. + + if object_ids is specified, and object_types must be specified + with only 1 object type + """ + + roles = ['admin', 'pi', 'user', 'tech'] + + accepts = [ + PasswordAuth(), + [Event.fields['event_id']], + [Event.fields['person_id']], + Event.fields['object_ids'], + [Event.fields['event_type']], + [Event.fields['object_type']], + [Event.fields['fault_code']] + ] + + returns = [Event.fields] + + def __init__(self, *args, **kwds): + Method.__init__(self, *args, **kwds) + # Update documentation with list of default fields returned + self.__doc__ += os.linesep.join(Event.fields.keys()) + + def call(self, auth, event_id_list = None, person_id_list = None, event_type_list = None, object_type_list = None, object_id_list = None, fault_code_list = None): + + # Authenticated function + assert self.caller is not None + + # filter out invalid event types + if event_type_list: + event_type_list = [event_type.title() for event_type in event_type_list] + valid_event_types = ['Add', 'Get', 'Update', 'Delete', 'Unknown'] + if filter(lambda field: field not in valid_event_types, event_type_list): + raise PLCInvalidArgument, "Invalid event type. Must be in %s" % \ + valid_event_types + + # filter out invalid object types + if object_type_list: + object_type_list = [object_type.title() for object_type in object_type_list] + valid_object_types = ['Person', 'Site', 'Node', 'Slice', 'Address', \ + 'Attribute', 'Key', 'Nodegroup', 'Unknown'] + if filter(lambda field: field not in valid_object_types, object_type_list): + raise PLCInvalidArgument, "Invalid object type. Must be in %s" % \ + valid_object_types + + # if object ids are specified only 1 object type can be specified + if object_id_list: + if not object_type_list: + raise PLCInvalidArgument, "Object type must be specified" + elif len(object_type_list) > 1: + raise PLCInvalidArgument, "Cannot specify multiple object types when object_ids are specified" + + + # Get node information + events = Events(self.api, event_id_list, person_id_list, event_type_list, \ + object_type_list, object_id_list).values() + + # turn each node into a real dict. + events = [dict(event) for event in events] + + return events