DuploCloud Client

0.4.3 · active · verified Wed Apr 15

The `duplocloud-client` library provides a Python SDK for programmatically interacting with DuploCloud portals, enabling management of infrastructure, tenants, services, and various cloud resources. It serves as the underlying library for the `duploctl` command-line interface. Currently at version 0.4.3, the library maintains a frequent release cadence with minor updates and fixes, often coinciding with new feature introductions in the DuploCloud platform.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `DuploClient` using environment variables for authentication and then attempts to list tenants. Ensure `DUPLO_HOST`, `DUPLO_TOKEN`, and optionally `DUPLO_TENANT` are set in your environment. The example also shows how to use the `DuploCtl.from_env()` for a CLI-like callable interface to interact with resources.

import os
from duplocloud.client import DuploClient

# Ensure DUPLO_HOST and DUPLO_TOKEN are set in your environment
duplo_host = os.environ.get('DUPLO_HOST', 'https://example.duplocloud.net') # Replace with your DuploCloud portal URL
duplo_token = os.environ.get('DUPLO_TOKEN', '') # Replace with your DuploCloud API token
duplo_tenant = os.environ.get('DUPLO_TENANT', 'default') # Optional: your target tenant

if not duplo_token:
    print("Error: DUPLO_TOKEN environment variable not set.")
    exit(1)

if not duplo_host:
    print("Error: DUPLO_HOST environment variable not set.")
    exit(1)

try:
    # Initialize the client
    client = DuploClient(host=duplo_host, token=duplo_token, tenant_name=duplo_tenant)
    print(f"Successfully initialized DuploClient for host: {duplo_host}, tenant: {duplo_tenant}")

    # Example: List tenants (assuming the token has appropriate permissions)
    # The client often exposes resources as attributes or methods following CLI patterns.
    # Direct programmatic access might vary, but a common pattern is to access resource managers.
    # For this example, we'll assume a 'tenant' resource is accessible for listing.
    # Note: Specific resource methods might require exploration of the DuploClient object.
    print("\nAttempting to list tenants...")
    try:
        # This is an illustrative example. Actual resource access might be via client.get('tenant')
        # or client.tenant.list(). Refer to DuploCloud's SDK documentation for exact methods.
        # The `duploctl` CLI uses `duplocloud.controller.DuploCtl.from_env()` for a callable interface.
        from duplocloud.controller import DuploCtl
        duplo_cli_callable, _ = DuploCtl.from_env() # Reads from env vars for convenience
        tenants = duplo_cli_callable("tenant", "list")
        print("Tenants:")
        for tenant in tenants:
            print(f"  - {tenant.get('Name')} (ID: {tenant.get('TenantId')})")
    except Exception as e:
        print(f"Could not list tenants (this might be due to permissions or API structure): {e}")

except Exception as e:
    print(f"An error occurred during client initialization or operation: {e}")

view raw JSON →