#This is a sample code only for reference # # from __future__ import print_function import os import sys import logging import getopt import getpass import time import json # set up some logging SCRIPT_PATH = os.path.dirname(os.path.abspath(sys.argv[0])) LOG_PATH = os.path.join(SCRIPT_PATH, "bulk_close_incidents.log") logging.basicConfig(filename=LOG_PATH, format='%(asctime)s %(message)s', level=logging.INFO) try: import gzip import urllib import json import co3 as resilient if sys.version_info.major < 3: import ConfigParser as config_parser else: import config_parser except ImportError as err: logging.exception("ERROR:") exit(1) # Read configuration for connection to Resilient REST API. CONFIG_PATH = os.path.join(SCRIPT_PATH, "bulk_close_incidents.config") opts = dict() actions = dict() config = config_parser.SafeConfigParser() config.read(CONFIG_PATH) try: for opt in config.options("resilient"): opts[opt] = config.get("resilient", opt) except NoSectionError: logging.exception("Failed to read 'resilient' section in %s", config_path) exit(1) try: for action in config.options("bulk_close_incidents"): actions[action] = config.get("bulk_close_incidents", action) except NoSectionError: logging.exception("Failed to read 'bulk_close_incidents' section in %s", config_path) exit(1) def change_status(incident): incident["plan_status"] = "C" return incident def update_fields(incident): incident["resolution_id"] = "Duplicate" return incident def main(): # Initialize a connection to the Resilient REST API verify = False # IMPORTANT: Change to True if you want to use a cert if "cafile" in opts: verify = os.path.expanduser(opts["cafile"]) print("Org Name = {}".format(opts["org"])) resilient_client = resilient.SimpleClient(org_name=opts["org"], base_url=opts["resturl"], verify=verify) resilient_client.connect(opts["user"], opts["password"]) json_query = { "filters" : [ { "conditions" : [ { "method" : actions["method"], "field_name" : "name", "value" : actions["incident_name"], }, ], }, ], } result_incidents = resilient_client.post("/incidents/query", json_query) closed_incidents = [] for incident in result_incidents: current_incident = resilient_client.get_put("/incidents/{}".format(incident["id"]), update_fields) resilient_client.get_put("/incidents/{}".format(incident["id"]), change_status) closed_incidents.append(incident["id"]) print("Number of incidents closed: {}".format(len(closed_incidents))) print("These are the incidents that were closed.") print(closed_incidents) if __name__ == "__main__": try: main() except: logging.exception("Script failed!") exit(1)