def update(self, _id, **kwargs): """ Update pbehavior record :param str _id: pbehavior id :param dict kwargs: values pbehavior fields. If a field is None, it will **not** be updated. :raises ValueError: invalid RRULE or no pbehavior with given _id """ pb_value = self.get(_id) if pb_value is None: raise ValueError("The id does not match any pebahvior") check_valid_rrule(kwargs.get('rrule', '')) pbehavior = PBehavior(**self.get(_id)) new_data = {k: v for k, v in kwargs.items() if v is not None} pbehavior.update(**new_data) result = self.pb_storage.put_element(element=new_data, _id=_id) if (PBehaviorManager._UPDATE_FLAG in result and result[PBehaviorManager._UPDATE_FLAG]): return pbehavior.to_dict() return None
def update(self, _id, **kwargs): """ Update pbehavior record :param str _id: pbehavior id :param dict kwargs: values pbehavior fields. If a field is None, it will **not** be updated. :raises ValueError: invalid RRULE or no pbehavior with given _id """ pb_value = self.get(_id) if pb_value is None: raise ValueError("The id does not match any pebahvior") check_valid_rrule(kwargs.get('rrule', '')) pbehavior = PBehavior(**self.get(_id)) new_data = {k: v for k, v in kwargs.items() if v is not None} pbehavior.update(**new_data) result = self.pb_storage.put_element( element=new_data, _id=_id ) if (PBehaviorManager._UPDATE_FLAG in result and result[PBehaviorManager._UPDATE_FLAG]): return pbehavior.to_dict() return None
def create( self, name, filter, author, tstart, tstop, rrule='', enabled=True, comments=None, connector='canopsis', connector_name='canopsis', type_=PBehavior.DEFAULT_TYPE, reason='', timezone=None, exdate=None): """ Method creates pbehavior record :param str name: filtering options :param dict filter: a mongo filter that match entities from canopsis context :param str author: the name of the user/app that has generated the pbehavior :param timestamp tstart: timestamp that correspond to the start of the pbehavior :param timestamp tstop: timestamp that correspond to the end of the pbehavior :param str rrule: reccurent rule that is compliant with rrule spec :param bool enabled: boolean to know if pbhevior is enabled or disabled :param list of dict comments: a list of comments made by users :param str connector: a string representing the type of connector that has generated the pbehavior :param str connector_name: a string representing the name of connector that has generated the pbehavior :param str type_: associated type_ for this pbh :param str reason: associated reason for this pbh :param str timezone: the timezone of the new pbehabior. If no timezone are given, use the default one. See the pbehavior documentation for more information. :param list of str| str exdate: a list of string representation of a date following this pattern "YYYY/MM/DD HH:MM:00 TIMEZONE". The hour use the 24 hours clock system and the timezone is the name of the timezone. The month, the day of the month, the hour, the minute and second are zero-padded. :raises ValueError: invalid RRULE :raises pytz.UnknownTimeZoneError: invalid timezone :return: created element eid :rtype: str """ if timezone is None: timezone = self.default_tz if exdate is None: exdate = [] # this line allow us to raise an exception pytz.UnknownTimeZoneError, # if the timezone defined in the pbehabior configuration file is wrong pytz.timezone(timezone) if enabled in [True, "True", "true"]: enabled = True elif enabled in [False, "False", "false"]: enabled = False else: raise ValueError("The enabled value does not match a boolean") if not isinstance(exdate, list): exdate = [exdate] check_valid_rrule(rrule) if comments is not None: for comment in comments: if "author" in comment: if not isinstance(comment["author"], string_types): raise ValueError("The author field must be an string") else: raise ValueError("The author field is missing") if "message" in comment: if not isinstance(comment["message"], string_types): raise ValueError("The message field must be an string") else: raise ValueError("The message field is missing") pb_kwargs = { PBehavior.NAME: name, PBehavior.FILTER: filter, PBehavior.AUTHOR: author, PBehavior.TSTART: tstart, PBehavior.TSTOP: tstop, PBehavior.RRULE: rrule, PBehavior.ENABLED: enabled, PBehavior.COMMENTS: comments, PBehavior.CONNECTOR: connector, PBehavior.CONNECTOR_NAME: connector_name, PBehavior.TYPE: type_, PBehavior.REASON: reason, PBehavior.TIMEZONE: timezone, PBehavior.EXDATE: exdate, PBehavior.EIDS: [] } data = PBehavior(**pb_kwargs) if not data.comments or not isinstance(data.comments, list): data.update(comments=[]) else: for comment in data.comments: comment.update({'_id': str(uuid4())}) result = self.pb_storage.put_element(element=data.to_dict()) return result
def create(self, name, filter, author, tstart, tstop, rrule='', enabled=True, comments=None, connector='canopsis', connector_name='canopsis', type_=PBehavior.DEFAULT_TYPE, reason=''): """ Method creates pbehavior record :param str name: filtering options :param dict filter: a mongo filter that match entities from canopsis context :param str author: the name of the user/app that has generated the pbehavior :param timestamp tstart: timestamp that correspond to the start of the pbehavior :param timestamp tstop: timestamp that correspond to the end of the pbehavior :param str rrule: reccurent rule that is compliant with rrule spec :param bool enabled: boolean to know if pbhevior is enabled or disabled :param list of dict comments: a list of comments made by users :param str connector: a string representing the type of connector that has generated the pbehavior :param str connector_name: a string representing the name of connector that has generated the pbehavior :param str type_: associated type_ for this pbh :param str reason: associated reason for this pbh :raises ValueError: invalid RRULE :return: created element eid :rtype: str """ if enabled in [True, "True", "true"]: enabled = True elif enabled in [False, "False", "false"]: enabled = False else: raise ValueError("The enabled value does not match a boolean") check_valid_rrule(rrule) if comments is not None: for comment in comments: if "author" in comment: if not isinstance(comment["author"], string_types): raise ValueError("The author field must be an string") else: raise ValueError("The author field is missing") if "message" in comment: if not isinstance(comment["message"], string_types): raise ValueError("The message field must be an string") else: raise ValueError("The message field is missing") pb_kwargs = { 'name': name, 'filter': filter, 'author': author, 'tstart': tstart, 'tstop': tstop, 'rrule': rrule, 'enabled': enabled, 'comments': comments, 'connector': connector, 'connector_name': connector_name, PBehavior.TYPE: type_, 'reason': reason } if PBehavior.EIDS not in pb_kwargs: pb_kwargs[PBehavior.EIDS] = [] data = PBehavior(**pb_kwargs) if not data.comments or not isinstance(data.comments, list): data.update(comments=[]) else: for comment in data.comments: comment.update({'_id': str(uuid4())}) result = self.pb_storage.put_element(element=data.to_dict()) return result
def create(self, name, filter, author, tstart, tstop, rrule='', enabled=True, comments=None, connector='canopsis', connector_name='canopsis', type_=PBehavior.DEFAULT_TYPE, reason='', timezone=None, exdate=None): """ Method creates pbehavior record :param str name: filtering options :param dict filter: a mongo filter that match entities from canopsis context :param str author: the name of the user/app that has generated the pbehavior :param timestamp tstart: timestamp that correspond to the start of the pbehavior :param timestamp tstop: timestamp that correspond to the end of the pbehavior :param str rrule: reccurent rule that is compliant with rrule spec :param bool enabled: boolean to know if pbhevior is enabled or disabled :param list of dict comments: a list of comments made by users :param str connector: a string representing the type of connector that has generated the pbehavior :param str connector_name: a string representing the name of connector that has generated the pbehavior :param str type_: associated type_ for this pbh :param str reason: associated reason for this pbh :param str timezone: the timezone of the new pbehabior. If no timezone are given, use the default one. See the pbehavior documentation for more information. :param list of str| str exdate: a list of string representation of a date following this pattern "YYYY/MM/DD HH:MM:00 TIMEZONE". The hour use the 24 hours clock system and the timezone is the name of the timezone. The month, the day of the month, the hour, the minute and second are zero-padded. :raises ValueError: invalid RRULE :raises pytz.UnknownTimeZoneError: invalid timezone :return: created element eid :rtype: str """ if timezone is None: timezone = self.default_tz if exdate is None: exdate = [] # this line allow us to raise an exception pytz.UnknownTimeZoneError, # if the timezone defined in the pbehabior configuration file is wrong pytz.timezone(timezone) if enabled in [True, "True", "true"]: enabled = True elif enabled in [False, "False", "false"]: enabled = False else: raise ValueError("The enabled value does not match a boolean") if not isinstance(exdate, list): exdate = [exdate] check_valid_rrule(rrule) if comments is not None: for comment in comments: if "author" in comment: if not isinstance(comment["author"], string_types): raise ValueError("The author field must be an string") else: raise ValueError("The author field is missing") if "message" in comment: if not isinstance(comment["message"], string_types): raise ValueError("The message field must be an string") else: raise ValueError("The message field is missing") pb_kwargs = { PBehavior.NAME: name, PBehavior.FILTER: filter, PBehavior.AUTHOR: author, PBehavior.TSTART: tstart, PBehavior.TSTOP: tstop, PBehavior.RRULE: rrule, PBehavior.ENABLED: enabled, PBehavior.COMMENTS: comments, PBehavior.CONNECTOR: connector, PBehavior.CONNECTOR_NAME: connector_name, PBehavior.TYPE: type_, PBehavior.REASON: reason, PBehavior.TIMEZONE: timezone, PBehavior.EXDATE: exdate, PBehavior.EIDS: [] } data = PBehavior(**pb_kwargs) if not data.comments or not isinstance(data.comments, list): data.update(comments=[]) else: for comment in data.comments: comment.update({'_id': str(uuid4())}) result = self.pb_storage.put_element(element=data.to_dict()) return result
def check_values(data): """ Check if the values present in data respect the specification. If the values are correct do nothing. If not, raises an error. :param dict data: the data. :raises ValueError: a value is invalid. """ # check str values for k in ["_id", "name", "author", "rrule", "component", "connector", "connector_name", 'type_', 'reason']: check(data, k, string_types) # check int values for k in ["tstart", "tstop"]: check(data, k, int) if 'tstart' in data and 'tstop' in data: if data['tstart'] >= data['tstop'] and \ data['tstart'] is not None and data['tstop'] is not None: raise ValueError('tstop cannot be inferior or equal to tstart') # check dict values for k in ["comments"]: if "comments" not in data: continue if data["comments"] is None: continue check(data, k, list) for elt in data["comments"]: if not isinstance(elt, dict): raise ValueError("The list {0} store only {1} not {2}" .format(k, dict, type(elt))) if "filter" in data and isinstance(data["filter"], string_types): try: data["filter"] = loads(data["filter"]) except ValueError: raise ValueError("Cant decode mfilter parameter: {}" .format(data["filter"])) if 'rrule' in data: check_valid_rrule(data['rrule']) if PBehavior.EXDATE in data: if isinstance(data[PBehavior.EXDATE], list): for date in data[PBehavior.EXDATE]: if not isinstance(date, int): raise ValueError("The date inside exdate must be an int.") else: raise ValueError("Exdate must be a list.") # useful when enabled doesn't exist in document if ("enabled" not in data or data["enabled"] is None or isinstance(data['enabled'], bool)): return data["enabled"] = cfg_to_bool(data["enabled"])
def test_check_invalid_rrule(self): with self.assertRaises(ValueError): check_valid_rrule(self.INVALID_RRULE)
def test_check_valid_rrule(self): res = check_valid_rrule(self.VALID_RRULE) self.assertTrue(res)
def check_values(data): """ Check if the values present in data respect the specification. If the values are correct do nothing. If not, raises an error. :param dict data: the data. :raises ValueError: a value is invalid. """ # check str values for k in ["name", "author", "rrule", "component", "connector", "connector_name", 'type_', 'reason']: check(data, k, string_types) # check int values for k in ["tstart", "tstop"]: check(data, k, int) if 'tstart' in data and 'tstop' in data: if data['tstart'] >= data['tstop'] and \ data['tstart'] is not None and data['tstop'] is not None: raise ValueError('tstop cannot be inferior or equal to tstart') # check dict values for k in ["comments"]: if "comments" not in data: continue if data["comments"] is None: continue check(data, k, list) for elt in data["comments"]: if not isinstance(elt, dict): raise ValueError("The list {0} store only {1} not {2}" .format(k, dict, type(elt))) if "filter" in data and isinstance(data["filter"], string_types): try: data["filter"] = loads(data["filter"]) except ValueError: raise ValueError("Cant decode mfilter parameter: {}" .format(data["filter"])) if 'rrule' in data: check_valid_rrule(data['rrule']) if PBehavior.EXDATE in data: if isinstance(data[PBehavior.EXDATE], list): for date in data[PBehavior.EXDATE]: if not isinstance(date, int): raise ValueError("The date inside exdate must be an int.") else: raise ValueError("Exdate must be a list of string.") # useful when enabled doesn't exist in document if ("enabled" not in data or data["enabled"] is None or isinstance(data['enabled'], bool)): return data["enabled"] = cfg_to_bool(data["enabled"])