← Engineering articles

Practical guide and proposed lab · Controller and fabric

Cisco Catalyst Center API Task Polling with Python

Poll Cisco Catalyst Center API tasks with Python timeouts, validate isError and endTime, and keep task completion separate from device readback evidence.

An API request moving through a task queue — conceptual illustration

What you’ll learn

  • An accepted request is not a successful device change.
  • Bound polling and preserve the task identifier.
  • Verify the affected resource after task completion.

A Cisco Catalyst Center API request can return a task ID before the requested work finishes. Poll the documented task resource with a deadline, distinguish failed, pending and completed states, and independently verify the affected resource before reporting a successful device change.

Scope and evidence: Python 3.10+ and the task-style envelope documented for Catalyst Center 2.3.7.7. Check the installed release’s Developer Toolkit before adapting an endpoint or field type. The responses below are synthetic minimal examples, not controller captures. Offline fixtures validate local state handling only; no live controller, device configuration or service result is claimed.

Why Does the Catalyst Center API Return a Task ID?

An asynchronous API accepts work that may outlive the initial HTTP request. A documented 202 Accepted response can identify the resulting task, but acceptance does not establish task success or device convergence. Preserve the task ID, operation, intended target and request time in a secure change record. Do not resubmit a configuration write merely because polling times out.

For the documented task-based workflow, a synthetic minimal acceptance envelope is:

{"response": {"taskId": "synthetic-task-001", "url": "dna/intent/api/v1/task/synthetic-task-001"}, "version": "1.0"}

The identifier is deliberately synthetic and is not a valid live job. Other APIs and releases can use different response contracts. A task URL is a reference to validate, not permission to follow an arbitrary destination. Cisco’s 2.3.7.7 asynchronous API documentation describes this workflow.

Catalyst Center asynchronous workflow records task acceptance, polls the task resource, separates pending, failed and completed states, then verifies the affected resource independently
Conceptual task workflow. Offline fixtures validate reporting; no live controller or device outcome is represented. Scroll horizontally on small screens to read all labels. Open full-size diagram.

How to Check Cisco Catalyst Center API Task Status

In the installed release’s API catalog, confirm the task resource and response schema for the operation you submitted. The referenced task-style workflow uses a GET such as /dna/intent/api/v1/task/{taskId}. Use the trusted controller origin, TLS certificate verification and an authorized X-Auth-Token; a token obtained for the write still needs to be supplied for the read request.

Treat HTTP transport and remote task state as separate results. A GET returning HTTP 200 can contain a task failure. HTTP 401/403 indicates an authentication/authorization boundary; 404 requires checking the ID, resource path, controller and task retention behavior for that release. Do not call it a failed device change without task or readback evidence. For 429 or transient server failures, honor the documented retry policy within a total observation budget; never restart the original write automatically.

The adapter used below is fetch(task_id, timeout_seconds). It must perform only a bounded, TLS-verified, authenticated read against the preapproved controller. Reject redirects and unexpected content, check HTTP status, decode JSON, and retain sanitized transport errors. A socket read timeout alone is not a complete end-to-end deadline; the adapter must respect the supplied remaining budget. Keep tokens, credentials and sensitive task progress out of general logs.

Understanding isError, endTime, and Task Responses

These are synthetic minimal task envelopes illustrating the fields inspected by the classifier. They omit unrelated response fields; they are not a complete schema or real execution history.

Pending Task Example

{"response": {"isError": false}}

No terminal timestamp has been observed. isError: false alone must not become a completed configuration status in this conservative reporting contract.

Completed Task Example

{"response": {"isError": false, "endTime": 1700000000000}}

A positive numeric endTime and no reported error produce completed_requires_readback. The timestamp is synthetic epoch milliseconds. The next stage still needs its own resource/service verification.

Failed Task Example

{"response": {"isError": true, "endTime": 1700000000000, "failureReason": "Synthetic task failure"}}

Reported failure takes precedence over the presence of an endTime. Capture the actual release’s error/progress fields securely for diagnosis. Unknown envelopes, missing/incorrect isError types and invalid timestamps remain unknown_schema; they are not silently accepted. This interpretation is deliberately limited to the documented task-style contract.

Python Example: Poll API Task Status with a Timeout

This Python example never submits a write. Its fetch callback is supplied separately, keeping controller transport and local polling logic independently testable. It validates task IDs, finite timing inputs and observed timestamp types. Exceptions from the adapter remain execution failures at the caller boundary; they are not transformed into task completion.

import math
import re
import time

def poll_task(fetch, task_id, deadline_seconds=30, interval=2,
              clock=time.monotonic, sleep=time.sleep):
    for value in (deadline_seconds, interval):
        if isinstance(value, bool) or not isinstance(value, (int, float)):
            raise ValueError("numeric polling limits required")
        if not math.isfinite(value) or value <= 0:
            raise ValueError("positive finite polling limits required")
    if not isinstance(task_id, str) or not re.fullmatch(r"[A-Za-z0-9-]{1,128}", task_id):
        raise ValueError("invalid task ID")
    deadline = clock() + deadline_seconds
    while clock() < deadline:
        remaining = deadline - clock()
        if remaining <= 0:
            break
        payload = fetch(task_id, min(5, remaining))
        if clock() >= deadline:
            break
        task = payload.get("response") if isinstance(payload, dict) else None
        if not isinstance(task, dict) or not isinstance(task.get("isError"), bool):
            return {"state": "unknown_schema", "task_id": task_id}
        if task["isError"]:
            return {"state": "failed", "task_id": task_id}
        end = task.get("endTime")
        if end is not None:
            if isinstance(end, bool) or not isinstance(end, (int, float)):
                return {"state": "unknown_schema", "task_id": task_id}
            if not math.isfinite(end) or end < 0:
                return {"state": "unknown_schema", "task_id": task_id}
        if end is not None and end > 0:
            return {"state": "completed_requires_readback", "task_id": task_id}
        sleep(min(interval, max(0, deadline - clock())))
    return {"state": "unresolved_deadline", "task_id": task_id}

The deadline stops local observation. It does not cancel the task or reverse a remote change. The fetch callback may still overrun if its transport implementation ignores the budget; the loop cannot preempt an arbitrary blocking adapter. Retain the task ID for later reconciliation.

Offline Mock Test with a Deterministic Clock

Append this to the same file and run it locally. It uses only constructed responses and a simulated clock; no requests, credentials or controller are involved.

if __name__ == "__main__":
    elapsed = [0.0]
    def clock():
        return elapsed[0]
    def sleep(seconds):
        elapsed[0] += seconds
    samples = iter([
        {"response": {"isError": False}},
        {"response": {"isError": False, "endTime": 1700000000000}},
    ])
    def offline_fetch(task_id, timeout):
        return next(samples)
    result = poll_task(offline_fetch, "synthetic-task-001", clock=clock, sleep=sleep)
    assert result["state"] == "completed_requires_readback"
    print(result["state"])

Expected output is completed_requires_readback. This mock test demonstrates only the local decision. No supported Catalyst Center appliance, controller-side operation, device readback or production change has been tested here.

Common Catalyst Center API Task Polling Errors

Symptom Boundary to check Next action
Request accepted, task later reports failure Remote execution Preserve the ID and inspect actual failure/progress fields
HTTP authentication or permission error Read adapter identity and scope Correct authorized credentials/role without repeating the write
Unfamiliar JSON or timestamp type Versioned response contract Compare installed-release documentation; report unknown schema
Deadline expires with no terminal state Observation budget Record unresolved work and reconcile later
Rate limit or transient server error Read retry policy Apply bounded documented retry handling at the worker boundary
Task completes but service is wrong Resource/device state Collect independent readback and application evidence

Do not poll without a delay or use an unbounded loop. Keep per-task history small and rate-limited. A dashboard should distinguish accepted, pending, failed, completed-pending-readback and unresolved states rather than collapse them into one success badge. The Catalyst Center rate-limit guide covers adjacent retry behavior; use the controller’s actual guidance before choosing a retry interval.

How to Verify Device Configuration After Task Completion

Choose an operation-specific verification contract before submitting the original authorized write. Read the affected resource independently, confirm its identity, compare the intended fields and record the readback timestamp and errors. If the change concerns a network service, also verify the relevant operational/device state and an approved end-to-end transaction. An inventory refresh, task status and actual device CLI can describe different stages of convergence.

Do not copy completed_requires_readback into a success report while skipping that check. If readback disagrees, preserve both pieces of evidence and investigate the actual controller/device boundary; do not repeat the write blindly. Rollback is the original change’s approved recovery procedure, not a generic task-polling API call. This helper provides no controller write or rollback operation.

Readback Evidence Checklist

  • Same intended resource/device identity and expected configuration fields.
  • Independent collection time, platform/version and any readback failures.
  • Required operational/service evidence and owner acceptance.
  • Task ID linked to the original authorized change and recovery plan.

The Netmiko, NETCONF and RESTCONF comparison helps select an appropriate readback interface. The switch migration checks illustrate an operation-specific forwarding evidence contract.

Frequently Asked Questions

Does HTTP 202 Mean the Catalyst Center Task Succeeded?

No. It can mean that asynchronous work was accepted. Check the versioned task resource and then the affected resource before reporting success.

Should a Polling Timeout Resubmit the Original API Write?

No automatic resubmission is justified by a local observation deadline. The remote job may still run. Keep the identifier and reconcile the existing operation through its owner.

Do Offline Fixtures Prove Compatibility with My Controller?

No. They validate local code decisions for synthetic envelopes. A supported controller environment, release-specific API review and separately authorized live validation are still required.

References: Cisco task-style asynchronous workflow, 2.3.7.7 and the Catalyst Center API catalog.

Discuss a related technical problem

Need reliable controller job status in a dashboard?

I can assess a Python/Flask integration that tracks task identifiers, timeouts and evidence. A supported controller lab and version-specific API review are prerequisites for live validation.

Discuss your project Review Imran Sarwar’s experience

Remote English-language inquiries: USA, UK, Canada, Europe and beyond. UAE-based (UTC+4); working overlap is agreed per project.