Skip to content

Commit ae91b01

Browse files
committed
Added Hosts API support
1 parent ef26d74 commit ae91b01

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

tenable/sc/hosts.py

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
'''
2+
Hosts
3+
=====
4+
5+
The following methods allow for interaction with the Tenable Security Center
6+
:sc-api:`Hosts <Hosts.htm>` API. These items are typically seen under the
7+
**Hosts** section of Tenable Security Center.
8+
9+
Methods available on ``sc.hosts``:
10+
11+
.. rst-class:: hide-signature
12+
.. autoclass:: HostsAPI
13+
:members:
14+
'''
15+
from typing import List, Union, Dict, Optional, Tuple
16+
from .base import SCEndpoint, SCResultsIterator
17+
from tenable.errors import UnexpectedValueError
18+
19+
20+
class HostsResultsIterator(SCResultsIterator):
21+
pass
22+
23+
24+
class HostsAPI(SCEndpoint):
25+
_path = 'hosts'
26+
_box = True
27+
28+
def list(self,
29+
fields: List[str],
30+
limit: int = 10000,
31+
offset: int = 0,
32+
pages: Optional[int] = None,
33+
pagination: bool = True,
34+
return_json: bool = False,
35+
) -> Union[HostsResultsIterator, Dict]:
36+
"""
37+
Retreive the list of hosts from the system.
38+
39+
Args:
40+
fields (list[str], optional):
41+
What fields should be returned in the response.
42+
limit (int, 1000):
43+
How many hosts should be returned?
44+
offset (int, 0):
45+
At what index should
46+
pages (int, optional):
47+
The maximum number of pages to return for the iterator.
48+
pagination (bool, False):
49+
Should pagination be used?
50+
return_json (bool, False):
51+
Should we return the json response instead of an iterator?
52+
53+
Response:
54+
The response will be either the HostResultsIterator to handle
55+
pagination of the data (preferred) or the raw response from the
56+
api (if return_json is set to `True`).
57+
58+
Examples:
59+
60+
>>> for host in sc.hosts.list():
61+
... print(host)
62+
"""
63+
params = {
64+
'fields': ','.join(fields),
65+
'limit': limit,
66+
'startOffset': offset,
67+
'endOffset': limit + offset,
68+
'pagination': str(pagination).lower(),
69+
}
70+
if return_json:
71+
return self._get(params=params).response
72+
return HostsResultsIterator(self._api,
73+
_resource='hosts'
74+
_params=params,
75+
_limit=limit,
76+
_offset=offset
77+
_pages_total=pages
78+
)
79+
80+
def search(self,
81+
filters: List[Tuple[str, str, str]],
82+
fields: List[str],
83+
limit: int = 10000,
84+
offset: int = 0,
85+
pages: Optional[int] = None,
86+
pagination: bool = True,
87+
return_json: bool = False,
88+
) -> Union[HostsResultsIterator, Dict]:
89+
"""
90+
Retreive the list of hosts from the system.
91+
92+
Args:
93+
fields (list[str], optional):
94+
What fields should be returned in the response.
95+
limit (int, 1000):
96+
How many hosts should be returned?
97+
offset (int, 0):
98+
At what index should
99+
pages (int, optional):
100+
The maximum number of pages to return for the iterator.
101+
pagination (bool, False):
102+
Should pagination be used?
103+
return_json (bool, False):
104+
Should we return the json response instead of an iterator?
105+
106+
Response:
107+
The response will be either the HostResultsIterator to handle
108+
pagination of the data (preferred) or the raw response from the
109+
api (if return_json is set to `True`).
110+
111+
Examples:
112+
113+
>>> for host in sc.hosts.list():
114+
... print(host)
115+
"""
116+
params = {
117+
'fields': ','.join(fields),
118+
'limit': limit,
119+
'startOffset': offset,
120+
'endOffset': limit + offset,
121+
'pagination': str(pagination).lower(),
122+
}
123+
if return_json:
124+
return self._get(params=params).response
125+
return HostsResultsIterator(self._api,
126+
_resource='hosts'
127+
_params=params,
128+
_limit=limit,
129+
_offset=offset
130+
_pages_total=pages
131+
)

0 commit comments

Comments
 (0)