"""Library for interacting with PagerDuty API.. versionadded:: 2014.7.0:configuration: This module can be used by specifying the name of a configuration profile in the minion config, minion pillar, or master config. For example: .. code-block:: yaml my-pagerduty-account: pagerduty.subdomain: mysubdomain pagerduty.api_key: F3Rbyjbve43rfFWf2214"""importloggingimportsalt.utils.httpimportsalt.utils.jsonfromsalt.versionimport__version__log=logging.getLogger(__name__)
[docs]defquery(method="GET",profile_dict=None,url=None,path="api/v1",action=None,api_key=None,service=None,params=None,data=None,subdomain=None,client_url=None,description=None,opts=None,verify_ssl=True,):""" Query the PagerDuty API """user_agent=f"SaltStack {__version__}"ifoptsisNone:opts={}ifisinstance(profile_dict,dict):creds=profile_dictelse:creds={}ifapi_keyisnotNone:creds["pagerduty.api_key"]=api_keyifserviceisnotNone:creds["pagerduty.service"]=serviceifsubdomainisnotNone:creds["pagerduty.subdomain"]=subdomainifclient_urlisNone:client_url=f"https://{creds['pagerduty.subdomain']}.pagerduty.com"ifurlisNone:url=f"https://{creds['pagerduty.subdomain']}.pagerduty.com/{path}/{action}"ifparamsisNone:params={}ifdataisNone:data={}data["client"]=user_agent# pagerduty.service is not documented. While it makes sense to have in# some cases, don't force it when it is not defined.if"pagerduty.service"incredsandcreds["pagerduty.service"]isnotNone:data["service_key"]=creds["pagerduty.service"]data["client_url"]=client_urlif"event_type"notindata:data["event_type"]="trigger"if"description"notindata:ifnotdescription:data["description"]="SaltStack Event Triggered"else:data["description"]=descriptionheaders={"User-Agent":user_agent,"Authorization":f"Token token={creds['pagerduty.api_key']}",}ifmethod=="GET":data={}else:headers["Content-type"]="application/json"result=salt.utils.http.query(url,method,params=params,header_dict=headers,data=salt.utils.json.dumps(data),decode=False,text=True,opts=opts,verify_ssl=verify_ssl,)returnresult["text"]
[docs]deflist_items(action,key,profile_dict=None,api_key=None,opts=None):""" List items belonging to an API call. Used for list_services() and list_incidents() """items=salt.utils.json.loads(query(profile_dict=profile_dict,api_key=api_key,action=action,opts=opts))ret={}foriteminitems[action]:ret[item[key]]=itemreturnret