Skip to content

Commit 3144a61

Browse files
committed
initial support for new Cb Defense policy management API
1 parent b05c160 commit 3144a61

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

src/cbapi/defense/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
from __future__ import absolute_import
44

55
from .rest_api import CbDefenseAPI
6-
from .models import Device, Event
6+
from .models import Device, Event, Policy

src/cbapi/defense/models.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from ..models import MutableBaseModel, CreatableModelMixin, NewBaseModel
22

3+
from copy import deepcopy
34
import logging
45
import json
6+
57
from ..errors import ServerError
68

79
log = logging.getLogger(__name__)
@@ -10,7 +12,35 @@
1012
class DefenseMutableModel(MutableBaseModel):
1113
_change_object_http_method = "PATCH"
1214

15+
def _parse(self, obj):
16+
if type(obj) == dict and self.info_key in obj:
17+
return obj[self.info_key]
18+
1319
def _update_object(self):
20+
if self._change_object_http_method != "PATCH":
21+
return self._update_entire_object()
22+
else:
23+
return self._patch_object()
24+
25+
def _update_entire_object(self):
26+
if self.__class__.primary_key in self._dirty_attributes.keys() or self._model_unique_id is None:
27+
new_object_info = deepcopy(self._info)
28+
try:
29+
if not self._new_object_needs_primary_key:
30+
del(new_object_info[self.__class__.primary_key])
31+
except Exception:
32+
pass
33+
log.debug("Creating a new {0:s} object".format(self.__class__.__name__))
34+
ret = self._cb.api_json_request(self.__class__._new_object_http_method, self.urlobject,
35+
data={self.info_key: new_object_info})
36+
else:
37+
log.debug("Updating {0:s} with unique ID {1:s}".format(self.__class__.__name__, str(self._model_unique_id)))
38+
ret = self._cb.api_json_request(self.__class__._change_object_http_method,
39+
self._build_api_request_uri(), data={self.info_key: self._info})
40+
41+
return self._refresh_if_needed(ret)
42+
43+
def _patch_object(self):
1444
if self.__class__.primary_key in self._dirty_attributes.keys() or self._model_unique_id is None:
1545
log.debug("Creating a new {0:s} object".format(self.__class__.__name__))
1646
ret = self._cb.api_json_request(self.__class__._new_object_http_method, self.urlobject,
@@ -71,10 +101,6 @@ class Device(DefenseMutableModel):
71101
def __init__(self, cb, model_unique_id, initial_data=None):
72102
super(Device, self).__init__(cb, model_unique_id, initial_data)
73103

74-
def _parse(self, obj):
75-
if type(obj) == dict and "deviceInfo" in obj:
76-
return obj["deviceInfo"]
77-
78104
def lr_session(self):
79105
"""
80106
Retrieve a Live Response session object for this Device.
@@ -90,11 +116,20 @@ def lr_session(self):
90116
class Event(NewBaseModel):
91117
urlobject = "/integrationServices/v3/event"
92118
primary_key = "eventId"
119+
info_key = "eventInfo"
120+
121+
def _parse(self, obj):
122+
if type(obj) == dict and self.info_key in obj:
123+
return obj[self.info_key]
93124

94125
def __init__(self, cb, model_unique_id, initial_data=None):
95126
super(Event, self).__init__(cb, model_unique_id, initial_data)
96127

97-
def _parse(self, obj):
98-
if type(obj) == dict and "eventInfo" in obj:
99-
return obj["eventInfo"]
128+
129+
class Policy(DefenseMutableModel, CreatableModelMixin):
130+
urlobject = "/integrationServices/v3/policy"
131+
info_key = "policyInfo"
132+
swagger_meta_file = "defense/models/policyInfo.yaml"
133+
_change_object_http_method = "PUT"
134+
100135

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
type: object
2+
required:
3+
- description
4+
- name
5+
- policy
6+
- priorityLevel
7+
- version
8+
properties:
9+
description:
10+
type: string
11+
id:
12+
type: integer
13+
latestRevision:
14+
type: integer
15+
name:
16+
type: string
17+
policy:
18+
type: object
19+
priorityLevel:
20+
type: string
21+
systemPolicy:
22+
type: boolean
23+
version:
24+
type: integer
25+

0 commit comments

Comments
 (0)