From 73a8c7e3e994c7115c561a34e610d5768d43616c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9stor=20Salceda?= Date: Tue, 10 Apr 2018 17:32:45 +0200 Subject: [PATCH 1/3] Add support for sampling parameter when retrieving policy events When sampling parameter is present in context and has a value, use it for building the URL --- sdcclient/_client.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sdcclient/_client.py b/sdcclient/_client.py index 47b3ccf6..bb97fd49 100644 --- a/sdcclient/_client.py +++ b/sdcclient/_client.py @@ -1939,7 +1939,11 @@ def set_user_falco_rules(self, rules_content): return self._set_falco_rules("user", rules_content) def _get_policy_events_int(self, ctx): - res = requests.get(self.url + '/api/policyEvents?from={:d}&to={:d}&offset={}&limit={}'.format(int(ctx['from']), int(ctx['to']), ctx['offset'], ctx['limit']), headers=self.hdrs, verify=self.ssl_verify) + policy_events_url = self.url + '/api/policyEvents?from={:d}&to={:d}&offset={}&limit={}'.format(int(ctx['from']), int(ctx['to']), ctx['offset'], ctx['limit']) + if 'sampling' in ctx: + policy_events_url += '&sampling={:d}'.format(int(ctx['sampling'])) + + res = requests.get(policy_events_url, headers=self.hdrs, verify=self.ssl_verify) if not self._checkResponse(res): return [False, self.lasterr] From ef9ad1c1fa5a1ef5214000bbb60e18b2413252f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9stor=20Salceda?= Date: Tue, 10 Apr 2018 17:44:55 +0200 Subject: [PATCH 2/3] Add a sampling parameter to get_policy_events_duration method With sampling support I'm able to keep consistency with UI policy event timeline. --- sdcclient/_client.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sdcclient/_client.py b/sdcclient/_client.py index bb97fd49..f0ceb723 100644 --- a/sdcclient/_client.py +++ b/sdcclient/_client.py @@ -1978,13 +1978,14 @@ def get_policy_events_range(self, from_sec, to_sec): return self._get_policy_events_int(ctx) - def get_policy_events_duration(self, duration_sec): + def get_policy_events_duration(self, duration_sec, sampling=None): '''**Description** Fetch all policy events that occurred in the last duration_sec seconds. This method is used in conjunction with :func:`~sdcclient.SdSecureClient.get_more_policy_events` to provide paginated access to policy events. **Arguments** - duration_sec: Fetch all policy events that have occurred in the last *duration_sec* seconds. + - sampling: Sample all policy events using *sampling* interval. **Success Return Value** An array containing: @@ -2005,6 +2006,9 @@ def get_policy_events_duration(self, duration_sec): "offset": 0, "limit": 1000} + if sampling is not None: + ctx["sampling"] = sampling + return self._get_policy_events_int(ctx) def get_more_policy_events(self, ctx): From d95113125dbae36b907ff5fa07b221e20cc9db97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9stor=20Salceda?= Date: Tue, 10 Apr 2018 17:56:23 +0200 Subject: [PATCH 3/3] Add a sampling parameter to get_policy_events_range method This is the same case that get_policy_events_duration: With sampling I'm able to keep consistency with UI policy event timeline. --- sdcclient/_client.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sdcclient/_client.py b/sdcclient/_client.py index f0ceb723..f013c4f2 100644 --- a/sdcclient/_client.py +++ b/sdcclient/_client.py @@ -1952,7 +1952,7 @@ def _get_policy_events_int(self, ctx): return [True, {"ctx": ctx, "data": res.json()}] - def get_policy_events_range(self, from_sec, to_sec): + def get_policy_events_range(self, from_sec, to_sec, sampling=None): '''**Description** Fetch all policy events that occurred in the time range [from_sec:to_sec]. This method is used in conjunction with :func:`~sdcclient.SdSecureClient.get_more_policy_events` to provide paginated access to policy events. @@ -1960,6 +1960,7 @@ def get_policy_events_range(self, from_sec, to_sec): **Arguments** - from_sec: the start of the timerange for which to get events - end_sec: the end of the timerange for which to get events + - sampling: sample all policy events using *sampling* interval. **Success Return Value** An array containing: @@ -1976,6 +1977,9 @@ def get_policy_events_range(self, from_sec, to_sec): "offset": 0, "limit": 1000} + if sampling is not None: + ctx["sampling"] = sampling + return self._get_policy_events_int(ctx) def get_policy_events_duration(self, duration_sec, sampling=None):