Skip to content

Commit 2ee73c1

Browse files
artemrysArtem Rys
authored andcommitted
feat: add ServerInfo's from_server_uri and to_dict
This commit introduces 2 new methods for ServerInfo class, 2 methods that are missing from splunktalib's implementation of ServerInfo. After this commit is merged, splunktalib's implementation can be deprecated, because solnlib's implementation supports everything and even more.
1 parent 79237e1 commit 2ee73c1

File tree

3 files changed

+104
-41
lines changed

3 files changed

+104
-41
lines changed

solnlib/server_info.py

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
"""This module contains Splunk server info related functionalities."""
1818

1919
import json
20+
from typing import Any, Dict, Optional
2021

2122
from splunklib import binding
2223

23-
from . import splunk_rest_client as rest_client
24-
from . import utils
25-
from .utils import retry
24+
from solnlib import splunk_rest_client as rest_client
25+
from solnlib import utils
2626

2727
__all__ = ["ServerInfo", "ServerInfoException"]
2828

@@ -42,10 +42,10 @@ class ServerInfo:
4242
def __init__(
4343
self,
4444
session_key: str,
45-
scheme: str = None,
46-
host: str = None,
47-
port: int = None,
48-
**context: dict
45+
scheme: Optional[str] = None,
46+
host: Optional[str] = None,
47+
port: Optional[int] = None,
48+
**context: Any
4949
):
5050
"""Initializes ServerInfo.
5151
@@ -60,23 +60,75 @@ def __init__(
6060
session_key, "-", scheme=scheme, host=host, port=port, **context
6161
)
6262

63-
@retry(exceptions=[binding.HTTPError])
63+
@classmethod
64+
def from_server_uri(
65+
cls, server_uri: str, session_key: str, **context: Any
66+
) -> "ServerInfo":
67+
"""Creates ServerInfo class using server_uri and session_key.
68+
69+
Note: splunktalib uses these parameters to create it's ServerInfo class,
70+
so this method should ease the transition from splunktalib to solnlib.
71+
72+
Arguments:
73+
server_uri: splunkd URI.
74+
session_key: Splunk access token.
75+
context: Other configurations for Splunk rest client.
76+
77+
Returns:
78+
An instance of `ServerInfo`.
79+
80+
Raises:
81+
ValueError: server_uri is in the wrong format.
82+
"""
83+
scheme, host, port = utils.extract_http_scheme_host_port(server_uri)
84+
return ServerInfo(
85+
session_key,
86+
scheme=scheme,
87+
host=host,
88+
port=port,
89+
**context,
90+
)
91+
92+
def to_dict(self) -> Dict:
93+
"""Returns server information in a form dictionary.
94+
95+
Note: This method is implemented here to have compatibility with splunktalib's
96+
analogue.
97+
98+
Returns:
99+
Server information in a dictionary format.
100+
"""
101+
return self._server_info()
102+
103+
@utils.retry(exceptions=[binding.HTTPError])
64104
def _server_info(self):
65105
return self._rest_client.info
66106

67107
@property
68108
def server_name(self) -> str:
69-
"""Get server name."""
109+
"""Get server name.
110+
111+
Returns:
112+
Server name.
113+
"""
70114
return self._server_info()["serverName"]
71115

72116
@property
73117
def guid(self) -> str:
74-
"""Get guid for the server."""
118+
"""Get guid for the server.
119+
120+
Returns:
121+
Server GUID.
122+
"""
75123
return self._server_info()["guid"]
76124

77125
@property
78126
def version(self) -> str:
79-
"""Get Splunk server version."""
127+
"""Get Splunk server version.
128+
129+
Returns:
130+
Splunk version.
131+
"""
80132
return self._server_info()["version"]
81133

82134
def is_captain(self) -> bool:
@@ -133,7 +185,7 @@ def is_shc_member(self) -> bool:
133185

134186
return False
135187

136-
@retry(exceptions=[binding.HTTPError])
188+
@utils.retry(exceptions=[binding.HTTPError])
137189
def get_shc_members(self) -> list:
138190
"""Get SHC members.
139191
@@ -162,7 +214,7 @@ def get_shc_members(self) -> list:
162214

163215
return members
164216

165-
@retry(exceptions=[binding.HTTPError])
217+
@utils.retry(exceptions=[binding.HTTPError])
166218
def is_captain_ready(self) -> bool:
167219
"""Check if captain is ready.
168220
@@ -173,7 +225,8 @@ def is_captain_ready(self) -> bool:
173225
True if captain is ready else False.
174226
175227
Examples:
176-
>>> serverinfo = solnlib.server_info.ServerInfo(session_key)
228+
>>> from solnlib import server_info
229+
>>> serverinfo = server_info.ServerInfo(session_key)
177230
>>> while 1:
178231
>>> if serverinfo.is_captain_ready():
179232
>>> break
@@ -189,7 +242,7 @@ def is_captain_ready(self) -> bool:
189242
cap_info["maintenance_mode"]
190243
)
191244

192-
@retry(exceptions=[binding.HTTPError])
245+
@utils.retry(exceptions=[binding.HTTPError])
193246
def captain_info(self) -> dict:
194247
"""Get captain information.
195248

tests/integration/test_server_info.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,33 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
import unittest
17-
1816
import context
17+
import pytest
1918

2019
from solnlib import server_info
2120

2221

23-
class ServerInfoTest(unittest.TestCase):
24-
def test_methods(self):
25-
# This test does not check for guid, version and SHC related methods.
26-
session_key = context.get_session_key()
27-
si = server_info.ServerInfo(
28-
session_key, context.scheme, context.host, context.port
29-
)
30-
self.assertEqual("custom-servername", si.server_name)
31-
self.assertFalse(si.is_search_head())
32-
self.assertFalse(si.is_shc_member())
33-
with self.assertRaises(server_info.ServerInfoException):
34-
si.get_shc_members()
35-
with self.assertRaises(server_info.ServerInfoException):
36-
si.captain_info()
37-
with self.assertRaises(server_info.ServerInfoException):
38-
si.is_captain_ready()
39-
self.assertFalse(si.is_captain())
40-
self.assertFalse(si.is_cloud_instance())
22+
def test_server_info_methods():
23+
# This test does not check for guid, version and SHC related methods.
24+
session_key = context.get_session_key()
25+
si = server_info.ServerInfo(session_key, context.scheme, context.host, context.port)
26+
assert "custom-servername" == si.server_name
27+
assert si.is_search_head() is False
28+
assert si.is_shc_member() is False
29+
with pytest.raises(server_info.ServerInfoException):
30+
si.get_shc_members()
31+
with pytest.raises(server_info.ServerInfoException):
32+
si.captain_info()
33+
with pytest.raises(server_info.ServerInfoException):
34+
si.is_captain_ready()
35+
assert si.is_captain() is False
36+
assert si.is_cloud_instance() is False
37+
38+
39+
def test_from_server_uri():
40+
session_key = context.get_session_key()
41+
si = server_info.ServerInfo.from_server_uri(
42+
f"{context.scheme}://{context.host}:{context.port}", session_key
43+
)
44+
# Run 1 small test to check that .from_server_uri is working.
45+
assert "custom-servername" == si.server_name

tests/unit/test_server_info.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,24 @@
1414
# limitations under the License.
1515
#
1616
import common
17+
import pytest
1718
from splunklib import binding
1819

1920
from solnlib import server_info
2021

2122

22-
class TestServerInfo:
23-
def test_version(self, monkeypatch):
24-
common.mock_splunkhome(monkeypatch)
25-
common.mock_serverinfo(monkeypatch)
23+
def test_from_server_uri():
24+
server_info.ServerInfo.from_server_uri("https://localhost:8089", common.SESSION_KEY)
2625

27-
si = server_info.ServerInfo(common.SESSION_KEY)
28-
assert si.version == "6.3.1511.2"
2926

27+
def test_from_server_uri_when_invalid_server_uri():
28+
with pytest.raises(ValueError):
29+
server_info.ServerInfo.from_server_uri(
30+
"no-schema://localhost:99999", common.SESSION_KEY
31+
)
32+
33+
34+
class TestServerInfo:
3035
def test_get_shc_members(self, monkeypatch):
3136
def _mock_get(self, path_segment, owner=None, app=None, sharing=None, **query):
3237
return common.make_response_record(

0 commit comments

Comments
 (0)