Skip to content

Commit d09acc2

Browse files
authored
feat: delete checkpoint when input is removed (#6)
This PR removes a relevant checkpoint from the KVStore when an input is deleted. Additionally it introduces a basic bash script to update local environment for the testing purposes.
1 parent 15d0f3a commit d09acc2

File tree

5 files changed

+65
-9
lines changed

5 files changed

+65
-9
lines changed

globalConfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
"services": [
6363
{
6464
"name": "example",
65+
"restHandlerModule": "example_delete_checkpoint_rh",
66+
"restHandlerClass": "DeleteCheckpointRestHandler",
6567
"entity": [
6668
{
6769
"type": "text",
@@ -178,7 +180,7 @@
178180
"meta": {
179181
"name": "Splunk_TA_Example",
180182
"restRoot": "Splunk_TA_Example",
181-
"version": "0.0.1+81ad77f",
183+
"version": "0.0.1+15d0f3a",
182184
"displayName": "Splunk Add-on for Example",
183185
"schemaVersion": "0.0.9",
184186
"supportedThemes": [
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import traceback
2+
3+
import import_declare_test
4+
5+
from solnlib import log
6+
from solnlib.modular_input import checkpointer
7+
from splunktaucclib.rest_handler.admin_external import AdminExternalHandler
8+
9+
import example_utils
10+
11+
12+
class DeleteCheckpointRestHandler(AdminExternalHandler):
13+
def __init__(self, *args, **kwargs):
14+
AdminExternalHandler.__init__(self, *args, **kwargs)
15+
16+
def handleList(self, confInfo):
17+
AdminExternalHandler.handleList(self, confInfo)
18+
19+
def handleEdit(self, confInfo):
20+
AdminExternalHandler.handleEdit(self, confInfo)
21+
22+
def handleCreate(self, confInfo):
23+
AdminExternalHandler.handleCreate(self, confInfo)
24+
25+
def handleRemove(self, confInfo):
26+
log_filename = "example_delete_checkpoint"
27+
logger = log.Logs().get_logger(log_filename)
28+
session_key = self.getSessionKey()
29+
input_name = str(self.callerArgs.id)
30+
checkpointer_key_name = example_utils.get_example_collection_key_name(
31+
input_name
32+
)
33+
logger.info(f"Deleting the checkpoint for input '{input_name}'")
34+
try:
35+
kvstore_checkpointer = checkpointer.KVStoreCheckpointer(
36+
"example_checkpointer",
37+
session_key,
38+
example_utils.ADDON_NAME,
39+
)
40+
kvstore_checkpointer.delete(checkpointer_key_name)
41+
except Exception as e:
42+
log.log_exception(
43+
logger,
44+
e,
45+
"Checkpoint Error",
46+
msg_before=f"Error while deleting checkpoint for {input_name} input. {traceback.format_exc()}",
47+
)
48+
AdminExternalHandler.handleRemove(self, confInfo)

package/bin/example_helper.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@
1414
from splunklib import modularinput as smi
1515

1616

17-
ADDON_NAME = "Splunk_TA_Example"
18-
19-
2017
def logger_for_input(input_name: str) -> logging.Logger:
21-
return log.Logs().get_logger(f"{ADDON_NAME.lower()}_{input_name}")
18+
return log.Logs().get_logger(f"{example_utils.ADDON_NAME.lower()}_{input_name}")
2219

2320

2421
def get_account_api_key(session_key: str, account_name: str):
2522
cfm = conf_manager.ConfManager(
2623
session_key,
27-
ADDON_NAME,
28-
realm=f"__REST_CREDENTIAL__#{ADDON_NAME}#configs/conf-splunk_ta_example_account",
24+
example_utils.ADDON_NAME,
25+
realm=f"__REST_CREDENTIAL__#{example_utils.ADDON_NAME}#configs/conf-splunk_ta_example_account",
2926
)
3027
account_conf_file = cfm.get_conf("splunk_ta_example_account")
3128
return account_conf_file.get(account_name).get("api_key")
@@ -81,12 +78,12 @@ def stream_events(inputs: smi.InputDefinition, event_writer: smi.EventWriter):
8178
kvstore_checkpointer = checkpointer.KVStoreCheckpointer(
8279
"example_checkpointer",
8380
session_key,
84-
ADDON_NAME,
81+
example_utils.ADDON_NAME,
8582
)
8683
log_level = conf_manager.get_log_level(
8784
logger=logger,
8885
session_key=session_key,
89-
app_name=ADDON_NAME,
86+
app_name=example_utils.ADDON_NAME,
9087
conf_name="splunk_ta_example_settings",
9188
)
9289
logger.setLevel(log_level)

package/bin/example_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
ADDON_NAME = "Splunk_TA_Example"
2+
3+
14
def get_example_collection_key_name(input_name: str) -> str:
25
# `input_name` is a string like "example://<input_name>".
36
return input_name.split("/")[-1]

scripts/local_testing_setup.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
curl -k -X POST -u admin:Chang3d! --header "Content-Type: application/json" https://localhost:8089/servicesNS/-/Splunk_TA_Example/Splunk_TA_Example_account -d name=api_key -d api_key=super-secret-api-token
2+
3+
for i in $(seq 1 4);
4+
do
5+
curl -k -X POST -u admin:Chang3d! --header "Content-Type: application/json" https://localhost:8089/servicesNS/-/Splunk_TA_Example/Splunk_TA_Example_example -d name=test_input_$i -d interval=20 -d index=main -d account=api_key
6+
done

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy