laceworksdk.api.crud_endpoint

  1# -*- coding: utf-8 -*-
  2
  3from laceworksdk.api.base_endpoint import BaseEndpoint
  4
  5
  6class CrudEndpoint(BaseEndpoint):
  7    """
  8    A class used to implement CRUD create/read/update/delete functionality for Lacework API Endpoints
  9    """
 10
 11    def __init__(self,
 12                 session,
 13                 object_type,
 14                 endpoint_root="/api/v2"):
 15        """
 16        :param session: An instance of the HttpSession class.
 17        :param object_type: The Lacework object type to use.
 18        :param endpoint_root: The URL endpoint root to use.
 19        """
 20
 21        super().__init__(session, object_type, endpoint_root)
 22
 23    def create(self, params=None, **request_params):
 24        """
 25        A method to create a new object.
 26
 27        :param request_params: Request parameters.
 28
 29        :return response json
 30        """
 31
 32        json = self.build_dict_from_items(
 33            request_params
 34        )
 35
 36        response = self._session.post(self.build_url(), json=json, params=params)
 37
 38        return response.json()
 39
 40    def get(self, id=None, resource=None, **request_params):
 41        """
 42        A method to get objects.
 43
 44        :param guid: A string representing the object ID.
 45        :param type: A string representing the object resource type.
 46        :param request_params: A dictionary of parameters to add to the request.
 47
 48        :return response json
 49        """
 50
 51        params = self.build_dict_from_items(
 52            request_params
 53        )
 54
 55        response = self._session.get(self.build_url(id=id, resource=resource), params=params)
 56
 57        return response.json()
 58
 59    def search(self, json=None, **kwargs):
 60        """
 61        A method to search objects.
 62
 63        :param json: A dictionary containing the desired search parameters.
 64            (filters, returns)
 65
 66        :return response json
 67        """
 68
 69        response = self._session.post(self.build_url(action="search"), json=json)
 70
 71        return response.json()
 72
 73    def update(self, id=None, params=None, **request_params):
 74        """
 75        A method to update an object.
 76
 77        :param guid: A string representing the object ID.
 78        :param request_params: Request parameters.
 79
 80        :return response json
 81        """
 82
 83        json = self.build_dict_from_items(
 84            request_params
 85        )
 86
 87        response = self._session.patch(self.build_url(id=id), json=json, params=params)
 88
 89        return response.json()
 90
 91    def delete(self, id, params=None):
 92        """
 93        A method to delete an object.
 94
 95        :param guid: A string representing the alert channel GUID.
 96
 97        :return response json
 98        """
 99
100        response = self._session.delete(self.build_url(id=id), params=params)
101
102        return response
103
104    def _format_filters(self,
105                        filters):
106        """
107        A method to properly format the filters object.
108
109        :param filters: A dict of filters to be properly formatted (boolean conversion).
110
111        :return json
112        """
113
114        if "enabled" in filters.keys():
115            filters["enabled"] = int(bool(filters["enabled"]))
116
117        return filters
class CrudEndpoint(laceworksdk.api.base_endpoint.BaseEndpoint):
  7class CrudEndpoint(BaseEndpoint):
  8    """
  9    A class used to implement CRUD create/read/update/delete functionality for Lacework API Endpoints
 10    """
 11
 12    def __init__(self,
 13                 session,
 14                 object_type,
 15                 endpoint_root="/api/v2"):
 16        """
 17        :param session: An instance of the HttpSession class.
 18        :param object_type: The Lacework object type to use.
 19        :param endpoint_root: The URL endpoint root to use.
 20        """
 21
 22        super().__init__(session, object_type, endpoint_root)
 23
 24    def create(self, params=None, **request_params):
 25        """
 26        A method to create a new object.
 27
 28        :param request_params: Request parameters.
 29
 30        :return response json
 31        """
 32
 33        json = self.build_dict_from_items(
 34            request_params
 35        )
 36
 37        response = self._session.post(self.build_url(), json=json, params=params)
 38
 39        return response.json()
 40
 41    def get(self, id=None, resource=None, **request_params):
 42        """
 43        A method to get objects.
 44
 45        :param guid: A string representing the object ID.
 46        :param type: A string representing the object resource type.
 47        :param request_params: A dictionary of parameters to add to the request.
 48
 49        :return response json
 50        """
 51
 52        params = self.build_dict_from_items(
 53            request_params
 54        )
 55
 56        response = self._session.get(self.build_url(id=id, resource=resource), params=params)
 57
 58        return response.json()
 59
 60    def search(self, json=None, **kwargs):
 61        """
 62        A method to search objects.
 63
 64        :param json: A dictionary containing the desired search parameters.
 65            (filters, returns)
 66
 67        :return response json
 68        """
 69
 70        response = self._session.post(self.build_url(action="search"), json=json)
 71
 72        return response.json()
 73
 74    def update(self, id=None, params=None, **request_params):
 75        """
 76        A method to update an object.
 77
 78        :param guid: A string representing the object ID.
 79        :param request_params: Request parameters.
 80
 81        :return response json
 82        """
 83
 84        json = self.build_dict_from_items(
 85            request_params
 86        )
 87
 88        response = self._session.patch(self.build_url(id=id), json=json, params=params)
 89
 90        return response.json()
 91
 92    def delete(self, id, params=None):
 93        """
 94        A method to delete an object.
 95
 96        :param guid: A string representing the alert channel GUID.
 97
 98        :return response json
 99        """
100
101        response = self._session.delete(self.build_url(id=id), params=params)
102
103        return response
104
105    def _format_filters(self,
106                        filters):
107        """
108        A method to properly format the filters object.
109
110        :param filters: A dict of filters to be properly formatted (boolean conversion).
111
112        :return json
113        """
114
115        if "enabled" in filters.keys():
116            filters["enabled"] = int(bool(filters["enabled"]))
117
118        return filters

A class used to implement CRUD create/read/update/delete functionality for Lacework API Endpoints

CrudEndpoint(session, object_type, endpoint_root='/api/v2')
12    def __init__(self,
13                 session,
14                 object_type,
15                 endpoint_root="/api/v2"):
16        """
17        :param session: An instance of the HttpSession class.
18        :param object_type: The Lacework object type to use.
19        :param endpoint_root: The URL endpoint root to use.
20        """
21
22        super().__init__(session, object_type, endpoint_root)
Parameters
  • session: An instance of the HttpSession class.
  • object_type: The Lacework object type to use.
  • endpoint_root: The URL endpoint root to use.
def create(self, params=None, **request_params):
24    def create(self, params=None, **request_params):
25        """
26        A method to create a new object.
27
28        :param request_params: Request parameters.
29
30        :return response json
31        """
32
33        json = self.build_dict_from_items(
34            request_params
35        )
36
37        response = self._session.post(self.build_url(), json=json, params=params)
38
39        return response.json()

A method to create a new object.

Parameters
  • request_params: Request parameters.

:return response json

def get(self, id=None, resource=None, **request_params):
41    def get(self, id=None, resource=None, **request_params):
42        """
43        A method to get objects.
44
45        :param guid: A string representing the object ID.
46        :param type: A string representing the object resource type.
47        :param request_params: A dictionary of parameters to add to the request.
48
49        :return response json
50        """
51
52        params = self.build_dict_from_items(
53            request_params
54        )
55
56        response = self._session.get(self.build_url(id=id, resource=resource), params=params)
57
58        return response.json()

A method to get objects.

Parameters
  • guid: A string representing the object ID.
  • type: A string representing the object resource type.
  • request_params: A dictionary of parameters to add to the request.

:return response json

def search(self, json=None, **kwargs):
60    def search(self, json=None, **kwargs):
61        """
62        A method to search objects.
63
64        :param json: A dictionary containing the desired search parameters.
65            (filters, returns)
66
67        :return response json
68        """
69
70        response = self._session.post(self.build_url(action="search"), json=json)
71
72        return response.json()

A method to search objects.

Parameters
  • json: A dictionary containing the desired search parameters. (filters, returns)

:return response json

def update(self, id=None, params=None, **request_params):
74    def update(self, id=None, params=None, **request_params):
75        """
76        A method to update an object.
77
78        :param guid: A string representing the object ID.
79        :param request_params: Request parameters.
80
81        :return response json
82        """
83
84        json = self.build_dict_from_items(
85            request_params
86        )
87
88        response = self._session.patch(self.build_url(id=id), json=json, params=params)
89
90        return response.json()

A method to update an object.

Parameters
  • guid: A string representing the object ID.
  • request_params: Request parameters.

:return response json

def delete(self, id, params=None):
 92    def delete(self, id, params=None):
 93        """
 94        A method to delete an object.
 95
 96        :param guid: A string representing the alert channel GUID.
 97
 98        :return response json
 99        """
100
101        response = self._session.delete(self.build_url(id=id), params=params)
102
103        return response

A method to delete an object.

Parameters
  • guid: A string representing the alert channel GUID.

:return response json