Source code for saltext.splunk.states.splunk_search
"""Statefully manage Splunk searches.Usage-----This state is used to ensure presence of splunk searches... code-block:: yaml server-warning-message: splunk_search.present: - name: This is the splunk search name - search: index=main sourcetype="""def__virtual__():if"splunk_search.get"in__salt__:return"splunk_search"return(False,"splunk module could not be loaded")
[docs]defpresent(name,profile="splunk",**kwargs):""" Ensure a search is present .. code-block:: yaml API Error Search: splunk_search.present: search: index=main sourcetype=blah template: alert_5min The following parameters are required: name This is the name of the search in splunk """ret={"name":name,"changes":{},"result":None,"comment":""}target=__salt__["splunk_search.get"](name,profile=profile)iftarget:if__opts__["test"]:ret["comment"]=f"Would update {name}"returnret# found a search... updatingresult=__salt__["splunk_search.update"](name,profile=profile,**kwargs)ifnotresult:# no updateret["result"]=Trueret["comment"]="No changes"else:(newvalues,diffs)=resultold_content=dict(target.content)old_changes={}forxinnewvalues:old_changes[x]=old_content.get(x,None)ret["result"]=Trueret["changes"]["diff"]=diffsret["changes"]["old"]=old_changesret["changes"]["new"]=newvalueselse:if__opts__["test"]:ret["comment"]=f"Would create {name}"returnret# creating a new searchresult=__salt__["splunk_search.create"](name,profile=profile,**kwargs)ifresult:ret["result"]=Trueret["changes"]["old"]=Falseret["changes"]["new"]=kwargselse:ret["result"]=Falseret["comment"]=f"Failed to create {name}"returnret
[docs]defabsent(name,profile="splunk"):""" Ensure a search is absent .. code-block:: yaml API Error Search: splunk_search.absent The following parameters are required: name This is the name of the search in splunk """ret={"name":name,"changes":{},"result":True,"comment":f"{name} is absent.",}target=__salt__["splunk_search.get"](name,profile=profile)iftarget:if__opts__["test"]:ret={}ret["name"]=nameret["comment"]=f"Would delete {name}"ret["result"]=Nonereturnretresult=__salt__["splunk_search.delete"](name,profile=profile)ifresult:ret["comment"]=f"{name} was deleted"else:ret["comment"]=f"Failed to delete {name}"ret["result"]=Falsereturnret