Academy/VCAP — VCF Automation (3V0-21.25)/Lab: Build API Client with Token Refresh & Async Polling
This lab targets VCF 9.0

Lab: Build API Client with Token Refresh & Async Polling

VCF 9.0Intermediatevcap-advanced⏱ 120 min

Objectives

  • Create Python API client library that handles OAuth2 authentication, token refresh, pagination, rate limiting, and async operation polling.

Prerequisites

VCF lab environment deployed and operational

Lab Environment

Standard VCF lab environment for Advanced VCF 9.0 Automation

Tasks

Task 1 Lab: Build API Client with Token Refresh & Async Polling

Create Python API client library that handles OAuth2 authentication, token refresh, pagination, rate limiting, and async operation polling.

Step 1

Create VCFAutomationClient class with authenticate() method: for a VCF Automation 9.0 service account, obtain the token through the device authorization grant (/tm/oauth/tenant/<org>/device_authorization, approve the user code, then /tm/oauth/tenant/<org>/token with grant_type device_code) (VCF 9.0.1 docs p.6096-6097)

Step 2

Implement auto token refresh in get_headers(): exchange the refresh token at /tm/oauth/tenant/<org>/token before expires_in elapses, and store the NEW refresh_token from every response — service-account API tokens rotate on every use (RFC 6749 §6; VCF 9.0.1 docs p.5861)

Step 3

Add api_request() wrapper that handles HTTP 429 with exponential backoff

Step 4

Implement paginate() method for bulk listing

Step 5

Add submit_deployment() method that returns request ID

Step 6

Implement poll_request_status() with 10-minute timeout and 5s poll interval

Step 7

Test with real VCF Automation instance: deploy blueprint, poll until completion

Step 8

Verify token refresh happens before expiry (e.g. at the 55-minute mark for expires_in 3600) and that the client persists and uses the rotated refresh_token on the next refresh

Validation Gate

Check: Verify lab completion

Expected: Lab exercise completed successfully

Common Errors

Not handling API pagination for large result sets
Fix: VCF Automation API returns paginated results (default 20 items per page). Code that reads only the first page misses resources. Implement pagination loop: check totalElements vs current page, request next page until all results collected.
Synchronous polling for long-running operations
Fix: Deployment creation returns a request ID, not the final result. Polling too frequently wastes API calls; too infrequently delays workflow. Implement exponential backoff: poll at 5s, 10s, 20s, 30s intervals. Check request status for 'SUCCESSFUL' or 'FAILED'.
Ignoring API rate limits
Fix: VCF Automation API has rate limits. Rapid sequential calls (e.g., deploying 50 VMs in a loop) may hit rate limits causing HTTP 429 errors. Implement rate limiting in your client: max N requests per minute, with retry-after header respect.

Final Validation

Lab completed successfully

✓ All steps completed → No errors observed

Cleanup / Restore

• Revert to snapshot if needed

Design Reflection (VCDX)

API integration skills demonstrate automation maturity. VCDX designs with programmatic management show scalability beyond UI-driven operations.

⚠ Known Pitfalls

Ignoring API pagination — your script reports 20 VMs when there are actually 200.
Polling synchronously without backoff — wastes API calls and may trigger rate limiting.
Was this page useful?
Type to search. ↑ ↓ to move, Enter to open, Esc to close.