Skip to content

backend_confs

BackendConf(url, username, password)

Base Class to fetch configurations from rest endpoint. The classes need management url & session_id of the splunk to fetch the configurations.

Parameters:

Name Type Description Default
url

management url of the Splunk instance.

required
username

username of the Splunk instance

required
password

password of the Splunk instance

required
Source code in pytest_splunk_addon_ui_smartx/backend_confs.py
30
31
32
33
34
35
36
37
38
def __init__(self, url, username, password):
    """
    :param url: management url of the Splunk instance.
    :param username: username of the Splunk instance
    :param password: password of the Splunk instance
    """
    self.url = url
    self.username = username
    self.password = password

parse_conf(json_res, single_stanza=False)

Parse the json result in to the configuration dictionary :param json_res: the json_res got from the request :returns: dictionary

Source code in pytest_splunk_addon_ui_smartx/backend_confs.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def parse_conf(self, json_res, single_stanza=False):
    """
    Parse the json result in to the configuration dictionary
        :param json_res: the json_res got from the request
        :returns: dictionary
    """
    stanzas_map = dict()
    for each_stanzas in json_res["entry"]:
        stanza_name = each_stanzas["name"]
        stanzas_map[stanza_name] = dict()

        for each_param, param_value in list(each_stanzas["content"].items()):
            if each_param.startswith("eai:"):
                continue
            stanzas_map[stanza_name][each_param] = param_value

    if single_stanza:
        return stanzas_map[list(stanzas_map.keys())[0]]
    return stanzas_map

rest_call(url)

rest call to the splunk rest-endpoint :param url: url to call :returns: json result of the request

Source code in pytest_splunk_addon_ui_smartx/backend_confs.py
40
41
42
43
44
45
46
47
48
49
50
51
@backend_retry(3)
def rest_call(self, url):
    """
    rest call to the splunk rest-endpoint
        :param url: url to call
        :returns: json result of the request
    """
    res = requests.get(url, auth=(self.username, self.password), verify=False)
    assert res.status_code == 200, "url={}, status_code={}, error_msg={}".format(
        url, res.status_code, res.text
    )
    return res.json()

rest_call_delete(url)

rest call to the splunk rest-endpoint :param url: url to call :returns: json result of the request

Source code in pytest_splunk_addon_ui_smartx/backend_confs.py
70
71
72
73
74
75
76
77
78
79
def rest_call_delete(self, url):
    """
    rest call to the splunk rest-endpoint
        :param url: url to call
        :returns: json result of the request
    """
    res = requests.delete(url, auth=(self.username, self.password), verify=False)
    assert (
        res.status_code == 200 or res.status_code == 201
    ), "url={}, status_code={}, error_msg={}".format(url, res.status_code, res.text)

rest_call_post(url, kwargs)

rest call to the splunk rest-endpoint :param url: url to call :returns: json result of the request

:param kwargs: body of request method
:returns: json result of the request
Source code in pytest_splunk_addon_ui_smartx/backend_confs.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def rest_call_post(self, url, kwargs):
    """
    rest call to the splunk rest-endpoint
        :param url: url to call
        :returns: json result of the request

        :param kwargs: body of request method
        :returns: json result of the request
    """
    res = requests.post(
        url, kwargs, auth=(self.username, self.password), verify=False
    )
    assert (
        res.status_code == 200 or res.status_code == 201
    ), "url={}, status_code={}, error_msg={}".format(url, res.status_code, res.text)
    return res.json()

ListBackendConf(url, username, password)

Bases: BackendConf

For the configuration which can have more than one stanzas. The list will be fetched from endpoint/ and a specific stanza will be fetched from endpoint/{stanza_name}

Parameters:

Name Type Description Default
url

management url of the Splunk instance.

required
username

username of the Splunk instance

required
password

password of the Splunk instance

required
Source code in pytest_splunk_addon_ui_smartx/backend_confs.py
30
31
32
33
34
35
36
37
38
def __init__(self, url, username, password):
    """
    :param url: management url of the Splunk instance.
    :param username: username of the Splunk instance
    :param password: password of the Splunk instance
    """
    self.url = url
    self.username = username
    self.password = password

delete_all_stanzas(query=None)

Delete all stanza from the configuration. :query: query params for filter the stanza :returns: json result of the request

Source code in pytest_splunk_addon_ui_smartx/backend_confs.py
146
147
148
149
150
151
152
153
154
def delete_all_stanzas(self, query=None):
    """
    Delete all stanza from the configuration.
        :query: query params for filter the stanza
        :returns: json result of the request
    """
    all_stanzas = list(self.get_all_stanzas(query).keys())
    for stanza in all_stanzas:
        self.delete_stanza(stanza)

delete_stanza(stanza)

Delete a specific stanza of the configuration. :param stanza: stanza to delete :returns: json result of the request

Source code in pytest_splunk_addon_ui_smartx/backend_confs.py
156
157
158
159
160
161
162
163
def delete_stanza(self, stanza):
    """
    Delete a specific stanza of the configuration.
        :param stanza: stanza to delete
        :returns: json result of the request
    """
    url = "{}/{}".format(self.url, urllib.parse.quote_plus(stanza))
    self.rest_call_delete(url)

get_all_stanzas(query=None)

Get list of all stanzas of the configuration :query: query params for filter the stanza :returns: dictionary {stanza: {param: value, … }, … }

Source code in pytest_splunk_addon_ui_smartx/backend_confs.py
108
109
110
111
112
113
114
115
116
117
118
def get_all_stanzas(self, query=None):
    """
    Get list of all stanzas of the configuration
        :query: query params for filter the stanza
        :returns: dictionary {stanza: {param: value, ... }, ... }
    """
    url = self.url + "?count=0&output_mode=json"
    if query:
        url = url + "&" + query
    res = self.rest_call(url)
    return self.parse_conf(res)

get_stanza(stanza, decrypt=False)

Get a specific stanza of the configuration. :param stanza: stanza to fetch :returns: dictionary {param: value, … }

Source code in pytest_splunk_addon_ui_smartx/backend_confs.py
120
121
122
123
124
125
126
127
128
129
130
131
132
def get_stanza(self, stanza, decrypt=False):
    """
    Get a specific stanza of the configuration.
        :param stanza: stanza to fetch
        :returns: dictionary {param: value, ... }
    """
    url = "{}/{}?count=0&output_mode=json".format(
        self.url, urllib.parse.quote_plus(stanza)
    )
    if decrypt:
        url = "{}&--cred--=1".format(url)
    res = self.rest_call(url)
    return self.parse_conf(res, single_stanza=True)

get_stanza_value(stanza, param, decrypt=False)

Get value of a specific parameter from a stanza :param stanza: str The Stanza we are interested in :param param: the parameter to fetch :returns: str value

Source code in pytest_splunk_addon_ui_smartx/backend_confs.py
165
166
167
168
169
170
171
172
173
def get_stanza_value(self, stanza, param, decrypt=False):
    """
    Get value of a specific parameter from a stanza
        :param stanza: str The Stanza we are interested in
        :param param: the parameter to fetch
        :returns: str value
    """
    stanza_map = self.get_stanza(stanza, decrypt)
    return stanza_map[param]

post_stanza(url, kwargs)

Create a specific stanza of the configuration. :param url: url to call :returns: json result of the request

:param kwargs: body of request method
:returns: json result of the request
Source code in pytest_splunk_addon_ui_smartx/backend_confs.py
134
135
136
137
138
139
140
141
142
143
144
def post_stanza(self, url, kwargs):
    """
    Create a specific stanza of the configuration.
        :param url: url to call
        :returns: json result of the request

        :param kwargs: body of request method
        :returns: json result of the request
    """
    kwargs["output_mode"] = "json"
    return self.rest_call_post(url, kwargs)

SingleBackendConf(url, username, password)

Bases: BackendConf

For the configurations which can only have one stanza. for example, logging.

Parameters:

Name Type Description Default
url

management url of the Splunk instance.

required
username

username of the Splunk instance

required
password

password of the Splunk instance

required
Source code in pytest_splunk_addon_ui_smartx/backend_confs.py
30
31
32
33
34
35
36
37
38
def __init__(self, url, username, password):
    """
    :param url: management url of the Splunk instance.
    :param username: username of the Splunk instance
    :param password: password of the Splunk instance
    """
    self.url = url
    self.username = username
    self.password = password

get_parameter(param, decrypt=False)

Get value of a specific parameter from the stanza :param param: the parameter to fetch :returns: str value

Source code in pytest_splunk_addon_ui_smartx/backend_confs.py
192
193
194
195
196
197
198
199
def get_parameter(self, param, decrypt=False):
    """
    Get value of a specific parameter from the stanza
        :param param: the parameter to fetch
        :returns: str value
    """
    stanza_map = self.get_stanza(decrypt)
    return stanza_map[param]

get_stanza(decrypt=False)

Get the values of the Stanza from the configuration :returns: dictionary {param: value, … }

Source code in pytest_splunk_addon_ui_smartx/backend_confs.py
181
182
183
184
185
186
187
188
189
190
def get_stanza(self, decrypt=False):
    """
    Get the values of the Stanza from the configuration
        :returns: dictionary {param: value, ... }
    """
    url = self.url + "?output_mode=json"
    if decrypt:
        url = "{}&--cred--=1".format(url)
    res = self.rest_call(url)
    return self.parse_conf(res, single_stanza=True)

update_parameters(kwargs)

Updates the values of the stanza in the configuration :param kwargs: body of request method :returns: json result of the request

Source code in pytest_splunk_addon_ui_smartx/backend_confs.py
201
202
203
204
205
206
207
208
def update_parameters(self, kwargs):
    """
    Updates the values of the stanza in the configuration
        :param kwargs: body of request method
        :returns: json result of the request
    """
    kwargs["output_mode"] = "json"
    return self.rest_call_post(self.url, kwargs)