diff options
author | Gordon Chung <chungg@ca.ibm.com> | 2013-10-24 19:04:13 -0400 |
---|---|---|
committer | Gordon Chung <chungg@ca.ibm.com> | 2013-10-25 11:39:22 -0400 |
commit | 2e436ada18e5b6d37342ff7969b189d4827df45d (patch) | |
tree | ec89a381ab2884c5dcb5b63182cfc770f939fe3b /pycadf/event.py | |
parent | 52aa7818bba4c16229d9d2b402009a36d781ea5f (diff) | |
download | pycadf-2e436ada18e5b6d37342ff7969b189d4827df45d.tar.gz |
improve model validation
- add isset to check "real" attribute and not descriptor
- verify only id is set in shortform
- verify either resource or resourceId value is set, not both.
blueprint improve-validation
related-bug: #1242830
Change-Id: Ie9e3f26c5d30cd36e6013a1f0b77c8fe466cb3f7
Diffstat (limited to 'pycadf/event.py')
-rw-r--r-- | pycadf/event.py | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/pycadf/event.py b/pycadf/event.py index e9a91e4..434bf3f 100644 --- a/pycadf/event.py +++ b/pycadf/event.py @@ -46,6 +46,7 @@ EVENT_KEYNAME_MEASUREMENTS = "measurements" EVENT_KEYNAME_TAGS = "tags" EVENT_KEYNAME_ATTACHMENTS = "attachments" EVENT_KEYNAME_OBSERVER = "observer" +EVENT_KEYNAME_OBSERVERID = "observerId" EVENT_KEYNAME_REPORTERCHAIN = "reporterchain" EVENT_KEYNAMES = [EVENT_KEYNAME_TYPEURI, @@ -64,6 +65,7 @@ EVENT_KEYNAMES = [EVENT_KEYNAME_TYPEURI, EVENT_KEYNAME_TAGS, EVENT_KEYNAME_ATTACHMENTS, EVENT_KEYNAME_OBSERVER, + EVENT_KEYNAME_OBSERVERID, EVENT_KEYNAME_REPORTERCHAIN] @@ -100,12 +102,14 @@ class Event(cadftype.CADFAbstractType): observer = cadftype.ValidatorDescriptor( EVENT_KEYNAME_OBSERVER, (lambda x: isinstance(x, resource.Resource) and x.is_valid())) + observerId = cadftype.ValidatorDescriptor( + EVENT_KEYNAME_OBSERVERID, lambda x: identifier.is_valid(x)) def __init__(self, eventType=cadftype.EVENTTYPE_ACTIVITY, id=None, eventTime=None, action=cadftaxonomy.UNKNOWN, outcome=cadftaxonomy.UNKNOWN, initiator=None, initiatorId=None, target=None, targetId=None, - severity=None, reason=None, observer=None): + severity=None, reason=None, observer=None, observerId=None): # Establish typeURI for the CADF Event data type # TODO(mrutkows): support extended typeURIs for Event subtypes @@ -127,13 +131,16 @@ class Event(cadftype.CADFAbstractType): # Event.outcome (Mandatory) setattr(self, EVENT_KEYNAME_OUTCOME, outcome) - # Event.observer (Mandatory) - setattr(self, EVENT_KEYNAME_OBSERVER, observer) + # Event.observer (Mandatory if no observerId) + if observer is not None: + setattr(self, EVENT_KEYNAME_OBSERVER, observer) + # Event.observerId (Dependent) + if observerId is not None: + setattr(self, EVENT_KEYNAME_OBSERVERID, observerId) # Event.initiator (Mandatory if no initiatorId) if initiator is not None: setattr(self, EVENT_KEYNAME_INITIATOR, initiator) - # Event.initiatorId (Dependent) if initiatorId is not None: setattr(self, EVENT_KEYNAME_INITIATORID, initiatorId) @@ -141,7 +148,6 @@ class Event(cadftype.CADFAbstractType): # Event.target (Mandatory if no targetId) if target is not None: setattr(self, EVENT_KEYNAME_TARGET, target) - # Event.targetId (Dependent) if targetId is not None: setattr(self, EVENT_KEYNAME_TARGETID, targetId) @@ -222,15 +228,17 @@ class Event(cadftype.CADFAbstractType): # TODO(mrutkows): Eventually, make sure all attributes are # from either the CADF spec. (or profiles thereof) # TODO(mrutkows): validate all child attributes that are CADF types - # TODO(mrutkows): Cannot have both an initiator and initiatorId - # TODO(mrutkows): Cannot have both an target and targetId return ( - hasattr(self, EVENT_KEYNAME_TYPEURI) and - hasattr(self, EVENT_KEYNAME_EVENTTYPE) and - hasattr(self, EVENT_KEYNAME_ID) and - hasattr(self, EVENT_KEYNAME_EVENTTIME) and - hasattr(self, EVENT_KEYNAME_ACTION) and - hasattr(self, EVENT_KEYNAME_OUTCOME) and - hasattr(self, EVENT_KEYNAME_INITIATOR) and - hasattr(self, EVENT_KEYNAME_TARGET) + self._isset(EVENT_KEYNAME_TYPEURI) and + self._isset(EVENT_KEYNAME_EVENTTYPE) and + self._isset(EVENT_KEYNAME_ID) and + self._isset(EVENT_KEYNAME_EVENTTIME) and + self._isset(EVENT_KEYNAME_ACTION) and + self._isset(EVENT_KEYNAME_OUTCOME) and + (self._isset(EVENT_KEYNAME_INITIATOR) ^ + self._isset(EVENT_KEYNAME_INITIATORID)) and + (self._isset(EVENT_KEYNAME_TARGET) ^ + self._isset(EVENT_KEYNAME_TARGETID)) and + (self._isset(EVENT_KEYNAME_OBSERVER) ^ + self._isset(EVENT_KEYNAME_OBSERVERID)) ) |