IBM Cloud Pak for Security

 View Only
Expand all | Collapse all

Bulk task closure of particular case in cp4s using script

  • 1.  Bulk task closure of particular case in cp4s using script

    Posted Wed October 16, 2024 07:31 AM

    Hi All,

    I am trying to do bulk closure of all open incident from respective cases in one click using automation.

    Approach: I am query to rest api to get all open task of case id:123  once we have that using anothe task rest api i am trying to close task in bulk but i am getting error

    Rest API: /orgs/{org_id}/tasks

    list_of_id is list of open task ids.

    data={'id':list_of_id, 'status':'C'}

     Method=Put

    "{"success":false,"title":null,"message":"Unable to process the supplied JSON. The invalid path is /. The error occurred at line #1 and column #1.","hints":[],"error_code":"generic"}"

    Can anyone help me how to achieve that. or any one knows how we can create for loop in cp4s playbook & send taskid one by one to function "close task". please help & suggest



    ------------------------------
    Kavita K
    ------------------------------


  • 2.  RE: Bulk task closure of particular case in cp4s using script

    Posted Sun January 26, 2025 06:28 AM

    Hi Kavita,

    The error you're encountering indicates an issue with the JSON payload structure you're sending to the REST API. Here's a step-by-step solution to address the problem and achieve bulk task closure:

    The REST API does not seem to support bulk updates directly. Typically, such APIs require individual task updates. Instead of passing a list of IDs in one request, you may need to loop through each task ID and send individual requests.

    Here's a Python script to achieve that:

    import requests

    # Base URL and Authorization details
    base_url = "https://<your-cp4s-url>/orgs/<org_id>/tasks"
    headers = {
        "Authorization": "Bearer <your_access_token>",
        "Content-Type": "application/json"
    }

    # List of open task IDs to be closed
    list_of_ids = ["task_id_1", "task_id_2", "task_id_3"]  

    # Loop through each task ID and send individual PUT requests
    for task_id in list_of_ids:
        url = f"{base_url}/{task_id}"
        payload = {"status": "C"}  # Status 'C' for closed
        response = requests.put(url, json=payload, headers=headers)
        
        if response.status_code == 200:
            print(f"Task {task_id} closed successfully.")
        else:
            print(f"Failed to close task {task_id}. Error: {response.text}")



    ------------------------------
    Peter Rafai
    ------------------------------