|
26 | 26 | # Entity state, are written so that they may be used in a fluent style. |
27 | 27 | # |
28 | 28 |
|
29 | | -"""This module provides a client interface for the `Splunk REST API |
30 | | -<http://docs.splunk.com/Documentation/Splunk/latest/RESTAPI/RESTcontents>`_.""" |
| 29 | +"""A Pythonic interface to the Splunk REST API. |
| 30 | +
|
| 31 | +``splunklib.client`` wraps a Pythonic layer around the wire level |
| 32 | +binding of ``splunklib.binding``. The core of the library is the |
| 33 | +``Service`` class that encapsulates a connection to the server, and |
| 34 | +provides access to the various aspects of the REST API. Typically you |
| 35 | +would create a server with the :func:`connect` function, as in:: |
| 36 | +
|
| 37 | + import splunklib.client as client |
| 38 | + s = client.connect(host='localhost', port=8089, |
| 39 | + username='admin', password='...') |
| 40 | + assert isinstance(s, client.Service) |
| 41 | + |
| 42 | +``Service``s have fields for the various API functionality (``apps``, |
| 43 | +``saved_searches``, etc.). All these fields are ``Collection``s. For |
| 44 | +example,:: |
| 45 | +
|
| 46 | + a = s.apps |
| 47 | + my_app = a.create('my_app') |
| 48 | + my_app = a['my_app'] |
| 49 | + a.delete('my_app') |
| 50 | +
|
| 51 | +The individual applications, or elements of the other ``Collection``s, |
| 52 | +are subclasses of ``Entity``. An ``Entity`` has fields giving its |
| 53 | +attributes, and methods specific to each kind of entity.:: |
| 54 | +
|
| 55 | + print my_app['author'] # or print my_app.author |
| 56 | + my_app.package() # Create a compressed package of this application |
| 57 | +""" |
31 | 58 |
|
32 | 59 | # UNDONE: Add Collection.refresh and list caching |
33 | 60 | # UNDONE: Resolve conflict between Collection.delete and name of REST method |
|
48 | 75 | "connect", |
49 | 76 | "NotSupportedError", |
50 | 77 | "OperationError", |
| 78 | + "IncomparableException", |
51 | 79 | "Service" |
52 | 80 | ] |
53 | 81 |
|
@@ -150,22 +178,38 @@ def _parse_atom_metadata(content): |
150 | 178 |
|
151 | 179 | # kwargs: scheme, host, port, app, owner, username, password |
152 | 180 | def connect(**kwargs): |
153 | | - """Establishes an authenticated connection to a Splunk :class:`Service` |
154 | | - instance. |
| 181 | + """Connect and log in to a Splunk instance. |
155 | 182 |
|
156 | | - :param `host`: The host name (the default is *localhost*). |
157 | | - :param `port`: The port number (the default is *8089*). |
158 | | - :param `scheme`: The scheme for accessing the service (the default is |
159 | | - *https*). |
| 183 | + This is a shorthand for ``Service(...).login()``. :func:`connect` makes one round trip to the server (for logging in). |
| 184 | +
|
| 185 | + :param `host`: The host name (default: ``"localhost"``). |
| 186 | + :type host: string |
| 187 | + :param `port`: The port number (default: 8089). |
| 188 | + :type port: integer |
| 189 | + :param `scheme`: The scheme for accessing the service (default: ``"https"``) |
| 190 | + :type scheme: ``"https"`` |
160 | 191 | :param `owner`: The owner namespace (optional). |
| 192 | + :type owner: string |
161 | 193 | :param `app`: The app context (optional). |
| 194 | + :type app: string |
162 | 195 | :param `token`: The current session token (optional). Session tokens can be |
163 | 196 | shared across multiple service instances. |
| 197 | + :type token: string |
164 | 198 | :param `username`: The Splunk account username, which is used to |
165 | 199 | authenticate the Splunk instance. |
| 200 | + :type username: string |
166 | 201 | :param `password`: The password, which is used to authenticate the Splunk |
167 | 202 | instance. |
168 | | - :return: An initialized :class:`Service` instance. |
| 203 | + :type password: string |
| 204 | + :return: An initialized connection |
| 205 | + :rtype: :class:`Service` |
| 206 | +
|
| 207 | + **Example**: |
| 208 | +
|
| 209 | + import splunklib.client as client |
| 210 | + s = client.connect(...) |
| 211 | + a = s.apps["my_app"] |
| 212 | + ... |
169 | 213 | """ |
170 | 214 | return Service(**kwargs).login() |
171 | 215 |
|
@@ -699,9 +743,6 @@ def __init__(self, service, path, item=Entity): |
699 | 743 | Endpoint.__init__(self, service, path) |
700 | 744 | self.item = item # Item accessor |
701 | 745 |
|
702 | | - def __call__(self, **kwargs): |
703 | | - return self.list(**kwargs) |
704 | | - |
705 | 746 | def __contains__(self, name): |
706 | 747 | for item in self.list(): |
707 | 748 | if item.name == name: return True |
@@ -978,8 +1019,7 @@ def __getitem__(self, key): |
978 | 1019 | try: |
979 | 1020 | path = self.service._abspath(PATH_CONF % key) |
980 | 1021 | response = self.get(path) |
981 | | - entries = self._load_list(response) |
982 | | - return entries |
| 1022 | + return ConfigurationFile(self.service, path, state={'title': key}) |
983 | 1023 | except HTTPError as he: |
984 | 1024 | if he.status == 404: # No entity matching key |
985 | 1025 | raise KeyError(key) |
@@ -1169,10 +1209,6 @@ def __init__(self, service, kindmap=None): |
1169 | 1209 | Collection.__init__(self, service, PATH_INPUTS) |
1170 | 1210 | self._kindmap = kindmap if kindmap is not None else INPUT_KINDMAP |
1171 | 1211 |
|
1172 | | - # args: kind* |
1173 | | - def __call__(self, *args): |
1174 | | - return self.list(*args) |
1175 | | - |
1176 | 1212 | def __getitem__(self, key): |
1177 | 1213 | if isinstance(key, tuple) and len(key) == 2: |
1178 | 1214 | kind, key = key |
@@ -1586,7 +1622,6 @@ def oneshot(self, query, **params): |
1586 | 1622 | else: |
1587 | 1623 | raise |
1588 | 1624 |
|
1589 | | - |
1590 | 1625 | def list(self, count=0, **kwargs): |
1591 | 1626 | return Collection.list(self, count, **kwargs) |
1592 | 1627 |
|
|
0 commit comments