class Resources: def __init__(self, access_token, from_email): self.schedules = None self.teams = None self.session = APISession(access_token, default_from=from_email) def get_schedules(self): if self.schedules is None: log.info("Retrieving all schedules on the account. This could take several minutes.") print("Retrieving all schedules on the account. This could take several minutes.") self.schedules = self.session.list_all('schedules') log.info("Retrieving a list of users for each schedule. This could take several minutes.") for schedule in self.schedules: schedule['details'] = self.session.rget(schedule.get('self')) return self.schedules def get_teams(self): if self.teams is None: log.info("Retrieving all teams on the account. This could take several minutes.") print("Retrieving all teams on the account. This could take several minutes.") self.teams = self.session.list_all('teams') log.info("Retrieving a list of users for each team. This could take several minutes.") for team in self.teams: team['users'] = self.session.list_all('users', params={'team_ids[]': team.get('id')}) return self.teams
def service_iter_selected(api_key, service_ids): api_session = APISession(api_key) all_services = [] for service_id in service_ids: service = api_session.rget('/services/%s' % service_id) all_services.append(service) return all_services
def is_ep_valid(api_token, ep_id): session = APISession(api_token) try: response = session.rget("/escalation_policies/" + ep_id) if response: return True return False except PDClientError: return False
def is_service_valid(api_token, service_id): session = APISession(api_token) try: response = session.rget("/services/" + service_id) if response: return True return False except PDClientError: return False
def get_ep(api_token): while True: ep_id = input("Enter a default Escalation Policy ID: ") try: session = APISession(api_token) ep_object = session.rget("/escalation_policies/%s" % ep_id) break except PDClientError as e: print(e.response) print(e.response.url) print(e.response.text) print("Some errors happened. Try again.\n") return ep_object
def get_service(api_token, service_id): try: session = APISession(api_token) service = session.rget("/services/%s" % service_id) if not service['last_incident_timestamp']: print("The selected service '%s' [%s] has no incident" % (service['summary'], service['id'])) service = None except PDClientError as e: # print(e.response) # print(e.response.url) # print(e.response.text) service = None return service
def get_integrations( pd_session: APISession, services: List[Dict[str, Any]], ) -> List[Dict[str, Any]]: """ Get integrations from services. """ all_integrations: List[Dict[str, Any]] = [] for service in services: s_id = service["id"] if service.get("integrations"): for integration in service["integrations"]: i_id = integration["id"] i = pd_session.rget(f"/services/{s_id}/integrations/{i_id}") all_integrations.append(i) return all_integrations
import json, yaml, os from pdpyras import APISession from pathlib import Path from datetime import datetime, timedelta api_token = Path( os.getenv('PD_API_KEY_PATH', '/run/secrets/pagerduty/PAGERDUTY_KEY')).read_text() policy_id = os.getenv('POLICY_ID', 'PA4586M') team_id = os.getenv('TEAM_ID', 'PASPK4G') alerts_db = 'alerts.json' pd = APISession(api_token) pd.rget('escalation_policies/' + policy_id) try: db = json.loads(Path(alerts_db).read_text()) db.sort(key=lambda i: datetime.strptime(i['created_at'] + '+00:00', '%Y-%m-%dT%H:%M:%SZ%z')) since = db[-1]['created_at'] except: db, since = [], datetime.utcnow() - timedelta(days=90) seen = [alert['id'] for alert in db] def status_reporter(db): def print_status(_, num, total): if num % 100 == 1: