Commit bab0bbcf authored by Lucas Seguinot's avatar Lucas Seguinot

Merge branch 'feat-pbh_exdate' into 'develop'

Feat pbh exdate

See merge request !614
parents 5f00aebe b9150d1a
Pipeline #4509 passed with stage
in 1 minute and 59 seconds
......@@ -36,13 +36,15 @@ class PBehavior(object):
TYPE = 'type_'
REASON = 'reason'
SOURCE = 'source'
EXDATE = 'exdate'
TIMEZONE = 'timezone'
def __init__(self, _id, name, filter_, tstart, tstop, rrule, author,
connector=DEFAULT_CONNECTOR_VALUE,
connector_name=DEFAULT_CONNECTOR_NAME_VALUE,
comments=None, eids=None, type_=None, reason=None,
enabled=True, source=None,
*args, **kwargs):
enabled=True, source=None, exdate=None,
timezone="UTC", *args, **kwargs):
"""
:param str _id: pbehavior id
:param str name: pbehavior name
......@@ -59,6 +61,8 @@ class PBehavior(object):
:param str reason: explanation on pbehavior creation
:param bool enabled: allow this pbehavior to be used. This is NOT the same as the is_active property.
:param str source: if None, pbehavior was created from canopsis. if anything else, it was created from an external data like an event from Nagios or so.
:param list int exdate: a list of exclusion date as a timestamp
:param str timezone: a timezone name
"""
if filter_ is None:
filter_ = {}
......@@ -88,6 +92,15 @@ class PBehavior(object):
if source is not None and not isinstance(source, string_types):
raise TypeError('source must be None or a string, got {}'.format(type(source)))
if exdate is None:
exdate = []
elif not isinstance(exdate, list):
raise TypeError('exdate must be None or a list, got {}'.format(type(source)))
else:
for date in exdate:
if not isinstance(date, int):
raise TypeError('The date inside exdate must be an int, got {}'.format(type(source)))
self._id = _id
self.enabled = enabled
self.name = name
......@@ -103,6 +116,8 @@ class PBehavior(object):
self.type_ = type_
self.reason = reason
self.source = source
self.exdate = exdate
self.timezone = timezone
if args not in [(), None] or kwargs not in [{}, None]:
print('Ignored values on creation: {} // {}'.format(args, kwargs))
......@@ -171,7 +186,9 @@ class PBehavior(object):
self.ENABLED: self.enabled,
self.TYPE: self.type_,
self.REASON: self.reason,
self.SOURCE: self.source
self.SOURCE: self.source,
self.EXDATE: self.exdate,
self.TIMEZONE: self.timezone
}
return dictionnary
......@@ -3,6 +3,11 @@ Utils for pbehaviors.
"""
from dateutil.rrule import rrulestr
from dateutil import tz
from datetime import datetime
EXDATE_DATE_FORMAT = "%Y/%m/%d %H:%M:%S"
def check_valid_rrule(rrule):
......
......@@ -40,7 +40,7 @@ from canopsis.webcore.utils import gen_json, gen_json_error, HTTP_ERROR
VALID_PBEHAVIOR_PARAMS = [
'name', 'filter_', 'author', 'tstart', 'tstop', 'rrule',
'enabled', 'comments', 'connector', 'connector_name', 'type_', 'reason',
'timezone'
'timezone', 'exdate'
]
......@@ -108,6 +108,13 @@ def check_values(data):
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
......@@ -135,7 +142,8 @@ class RouteHandlerPBehavior(object):
tstart, tstop, rrule=None,
enabled=True, comments=None,
connector='canopsis', connector_name='canopsis',
type_=PBehavior.DEFAULT_TYPE, reason='', timezone=None):
type_=PBehavior.DEFAULT_TYPE, reason='', timezone=None,
exdate=None):
"""
Create a pbehavior.
......@@ -152,6 +160,9 @@ class RouteHandlerPBehavior(object):
:param str type_: an associated type_
:param str reason: a reason to apply this behavior
"""
if exdate is None:
exdate = []
data = {
PBehavior.NAME: name,
PBehavior.FILTER: filter_,
......@@ -165,7 +176,8 @@ class RouteHandlerPBehavior(object):
PBehavior.CONNECTOR_NAME: connector_name,
PBehavior.TYPE: type_,
PBehavior.REASON: reason,
PBehavior.TIMEZONE: timezone
PBehavior.TIMEZONE: timezone,
PBehavior.EXDATE: exdate
}
check_values(data)
......@@ -183,7 +195,8 @@ class RouteHandlerPBehavior(object):
connector_name=connector_name,
type_=type_,
reason=reason,
timezone=timezone
timezone=timezone,
exdate=exdate
)
return result
......@@ -217,12 +230,15 @@ class RouteHandlerPBehavior(object):
def update(self, _id, name=None, filter_=None, tstart=None, tstop=None,
rrule=None, enabled=None, comments=None, connector=None,
connector_name=None, author=None, type_=None, reason=None,
timezone=None):
timezone=None, exdate=None):
"""
Update pbehavior fields. Fields to None will **not** be updated.
:param str _id: pbehavior id
"""
if exdate is None:
exdate = []
params = {
PBehavior.NAME: name,
PBehavior.FILTER: filter_,
......@@ -236,7 +252,8 @@ class RouteHandlerPBehavior(object):
PBehavior.CONNECTOR_NAME: connector_name,
PBehavior.TYPE: type_,
PBehavior.REASON: reason,
PBehavior.TIMEZONE: timezone
PBehavior.TIMEZONE: timezone,
PBehavior.EXDATE: exdate
}
check_values(params)
......@@ -309,7 +326,7 @@ def exports(ws):
'tstart', 'tstop', 'rrule',
'enabled', 'comments',
'connector', 'connector_name',
'type_', 'reason', 'timezone'
'type_', 'reason', 'timezone', 'exdate'
]
)
def create(
......@@ -317,7 +334,8 @@ def exports(ws):
tstart, tstop, rrule=None,
enabled=True, comments=None,
connector='canopsis', connector_name='canopsis',
type_=PBehavior.DEFAULT_TYPE, reason='', timezone=None
type_=PBehavior.DEFAULT_TYPE, reason='', timezone=None,
exdate=None
):
"""
Create a pbehavior.
......@@ -325,7 +343,7 @@ def exports(ws):
return rhpb.create(
name, filter, author, tstart, tstop, rrule,
enabled, comments, connector, connector_name, type_, reason,
timezone
timezone, exdate
)
@ws.application.post('/api/v2/pbehavior')
......@@ -399,7 +417,7 @@ def exports(ws):
'name', 'filter',
'tstart', 'tstop', 'rrule',
'enabled',
'timezone'
'timezone', 'exdate'
]
)
def update(
......@@ -408,7 +426,7 @@ def exports(ws):
tstart=None, tstop=None, rrule=None,
enabled=None, comments=None,
connector=None, connector_name=None,
author=None, type_=None, reason=None, timezone=None
author=None, type_=None, reason=None, timezone=None, exdate=None
):
"""
Update a pbehavior.
......@@ -427,7 +445,8 @@ def exports(ws):
author=author,
type_=type_,
reason=reason,
timezone=timezone
timezone=timezone,
exdate=exdate
)
@route(
......
......@@ -26,7 +26,9 @@ db_pbehavior = {
"tstart": 0,
"tstop": 2147483647,
"eids": ["Tanya/Adams"],
"source": "nagioslike"
"source": "nagioslike",
"exdate": [],
"timezone": "Europe/Paris"
}
......
......@@ -376,7 +376,8 @@ class TestManager(BaseTest):
PBehavior.TYPE: 'maintenance',
"rrule": 'FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR,SA,SU',
'tstart': now - hour,
'tstop': now + hour
'tstop': now + hour,
'timezone': "UTC"
}
)
......@@ -499,7 +500,8 @@ class TestManager(BaseTest):
pbehavior = {
"rrule": "FREQ=WEEKLY;BYDAY=FR",
"tstart": 1529085600,
"tstop": 1529294400,
"tstop": 1529294400,
"timezone": "UTC"
}
for i, ts in enumerate(timestamps):
......
......@@ -48,23 +48,24 @@ JSON :
Les attributs du corps sont les suivants :
| Name | type | nullable | Description |
|-----------------|---------|----------|------------------------------------------------|
| connector | string | No | Identifiant du connecteur d'entité |
| name | string | No | Display name du pbehavior |
| author | string | No | Nom de l'auteur |
| enabled | boolean | No | Si le pbehavior est déclenché ou non |
| reason | string | yes | motif d'administration (optionnel) |
| comments | array | yes | Commentaires (option) |
| filter | string | No | filtre d'entité (JSON) |
| type\_ | string | No | type de Pbehavior |
| connector\_name | string | No | Display name du connector |
| rrule | string | yes | Rrule (récurrence) |
| tstart | integer | No | Timestamp de la date de départ |
| tstop | integer | No | Timestamp de la date de fin |
| \_id | string | No | identifiant du Pbehavior |
| eids | array | No | tableau du \_ids pour les entités impactées. |
| timezone | string | yes | La timezone à utiliser pour le calcul des dates |
| Name | type | nullable | Description |
|-----------------|---------|----------|-------------------------------------------------------------|
| connector | string | No | Identifiant du connecteur d'entité |
| name | string | No | Display name du pbehavior |
| author | string | No | Nom de l'auteur |
| enabled | boolean | No | Si le pbehavior est déclenché ou non |
| reason | string | Yes | motif d'administration (optionnel) |
| comments | array | Yes | Commentaires (option) |
| filter | string | No | filtre d'entité (JSON) |
| type\_ | string | No | type de Pbehavior |
| connector\_name | string | No | Display name du connector |
| rrule | string | Yes | Rrule (récurrence) |
| tstart | integer | No | Timestamp de la date de départ |
| tstop | integer | No | Timestamp de la date de fin |
| \_id | string | No | Indentifiant du Pbehavior |
| eids | array | No | tableau du \_ids pour les entités impactées. |
| timezone | string | Yes | La timezone à utiliser pour le calcul des dates |
| exdate | array | Yes | La liste des occurrences à ignorer sous forme de timestamps |
Réponse : UID de l'élément inséré
......@@ -106,31 +107,37 @@ Cette route répertorie les pbehaviors existant sur une entité, identifiée par
"isActive": true,
"eids": [
"580059AB4B100031"
]
],
"exdate" : [
1592471125
],
"timezone": "Europe/Paris"
}
]
```
Les attributs de réponse sont les suivants :
| Name | type | nullable | Description |
|-----------------|---------|----------|------------------------------------------------|
| connector | string | No | Identifier of the entity connector |
| name | string | No | Display name of the pbehavior |
| author | string | No | Author name |
| enabled | boolean | No | Should the pbehavior trigger or not |
| reason | string | yes | Administrative reason (optionnal) |
| comments | array | yes | Comments (option) |
| filter | string | No | Entities filter (json) |
| type\_ | string | No | Pbehavior type |
| connector\_name | string | No | Display name of the entity connector |
| rrule | string | yes | Rrule (recurrence) |
| tstart | integer | No | Timestamp of the start date |
| tstop | integer | No | Timestamp end date |
| \_id | string | No | Pbehavior identifier |
| eids | array | No | Array of \_ids for the impacted entities. |
| isActive | boolean | No | is the pbehavior currently active |
| timezone | string | yes | La timezone à utiliser pour le calcul des dates |
| Name | type | nullable | Description |
|-----------------|---------|----------|-------------------------------------------------------------|
| connector | string | No | Identifier of the entity connector |
| name | string | No | Display name of the pbehavior |
| author | string | No | Author name |
| enabled | boolean | No | Should the pbehavior trigger or not |
| reason | string | yes | Administrative reason (optionnal) |
| comments | array | yes | Comments (option) |
| filter | string | No | Entities filter (json) |
| type\_ | string | No | Pbehavior type |
| connector\_name | string | No | Display name of the entity connector |
| rrule | string | yes | Rrule (recurrence) |
| tstart | integer | No | Timestamp of the start date |
| tstop | integer | No | Timestamp end date |
| \_id | string | No | Pbehavior identifier |
| eids | array | No | Array of \_ids for the impacted entities. |
| isActive | boolean | No | Is the pbehavior currently active |
| timeszone | string | yes | La timezone à utiliser pour le calcul des dates |
| exdate | array | Yes | La liste des occurrences à ignorer sous forme de timestamps |
## Supprimer un pbehavior
......
......@@ -16,6 +16,7 @@ Un pbehavior se caractérise par les informations suivantes.
| `type_` | string | Optionnel. Type de pbehavior (pause, maintenance…). |
| `reason` | string | Optionnel. Raison pour laquelle ce pbehavior a été posé. |
| `timezone` | string | Timezone dans laquelle le pbehavior doit s'exécuter. |
| `exdate` | array | Yes | La liste des occurrences à ignorer sous forme de timestamps |
Un exemple d'évènement pbehavior brut :
```js
......@@ -37,7 +38,10 @@ Un exemple d'évènement pbehavior brut :
"connector" : string,
"connector_name" : string,
"author" : string,
"timezone" : string
"timezone" : string,
"exdate" : [
1592471125
]
}
```
......@@ -62,3 +66,8 @@ sous le champ `default_timezone`.
Si le fichier de configuration n'existe pas ou si le champ `default_timezone`
n'existe pas, la timezone `Europe/Paris` sera utilisée.
## Exdate
Il est possible d'empêcher l'exécution d'une occurrence d'un pbehavior, à l'aide
du champ exdate. Exdate est une liste de timestamp correspondant à au début
d'une occurence à empêcher.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment