Arista CloudVision Portal REST API Client

1.4.2 · active · verified Thu Apr 16

cvprac is a Python client library designed to interact with the Arista CloudVision Portal (CVP) RESTful API. It simplifies API calls, handles session management, error retries, and abstracts CVP version-specific API changes, enabling easier automation and integration with CVP. The library is actively maintained, with a typical release cadence of several updates per year, and is currently at version 1.4.2.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to an Arista CloudVision Portal (CVP) instance using `cvprac.CvpClient`. It prioritizes API token authentication, suitable for both CloudVision as-a-Service (CVaaS) and on-prem deployments, falling back to username/password for older on-prem setups. It then fetches and prints basic CVP information. Ensure `CVP_HOST` and either `CVP_API_TOKEN` or `CVP_USERNAME`/`CVP_PASSWORD` environment variables are set.

import os
from cvprac.cvp_client import CvpClient

# Environment variables for CVP connection
CVP_HOST = os.environ.get('CVP_HOST', 'your_cvp_ip_or_hostname')
CVP_API_TOKEN = os.environ.get('CVP_API_TOKEN', 'your_api_token') # For CVaaS or On-prem token-based auth
CVP_USERNAME = os.environ.get('CVP_USERNAME', '') # For On-prem username/password auth (deprecated for CVaaS)
CVP_PASSWORD = os.environ.get('CVP_PASSWORD', '') # For On-prem username/password auth (deprecated for CVaaS)

def main():
    client = CvpClient()
    try:
        # Connect using API Token (recommended for CVaaS and newer on-prem)
        if CVP_API_TOKEN and CVP_HOST != 'your_cvp_ip_or_hostname':
            print(f"Attempting connection to {CVP_HOST} using API Token...")
            client.connect(
                nodes=[CVP_HOST],
                username=CVP_USERNAME, # Can be empty if only using api_token
                password=CVP_PASSWORD, # Can be empty if only using api_token
                api_token=CVP_API_TOKEN,
                is_cvaas=CVP_HOST.endswith('.arista.io') # Set based on hostname
            )
        # Fallback to username/password if no token provided (primarily for older on-prem)
        elif CVP_USERNAME and CVP_PASSWORD and CVP_HOST != 'your_cvp_ip_or_hostname':
            print(f"Attempting connection to {CVP_HOST} using username/password...")
            client.connect(
                nodes=[CVP_HOST],
                username=CVP_USERNAME,
                password=CVP_PASSWORD
            )
        else:
            print("Please set CVP_HOST and either CVP_API_TOKEN or CVP_USERNAME/CVP_PASSWORD environment variables or replace placeholders.")
            return

        print("Successfully connected to CVP.")
        cvp_info = client.api.get_cvp_info()
        print(f"CVP Version: {cvp_info.get('version')}")
        print(f"CVP Build: {cvp_info.get('build')}")

    except Exception as e:
        print(f"Error connecting or fetching info: {e}")
    finally:
        # It's good practice to logout to close the session
        try:
            if client and client.is_connected:
                client.logout()
                print("Logged out successfully.")
        except Exception as e:
            print(f"Error during logout: {e}")

if __name__ == '__main__':
    main()

view raw JSON →