DuploCloud Client
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
- breaking Version 0.4.0 introduced a significant architectural change with a new decorator-based scope system (`@Resource`). This release removed previous deep inheritance hierarchies like `DuploTenantResourceV2` and `DuploTenantResourceV3`. Code relying on these older class structures will break and needs to be refactored to use the new decorator pattern.
- gotcha Prior to version 0.4.1, the `aws_secret find` command/method could incorrectly return 'Resource not found' even when the secret existed, especially if the full prefixed name was not initially provided. This required manual prefixing or retrying with different names.
- gotcha As of version 0.4.3, the `apply()` method now specifically catches `DuploNotFound` errors (HTTP 404) instead of the broader `DuploError`. If your code had a generic `except DuploError` block for 404 scenarios within `apply()`, it might now need to explicitly catch `DuploNotFound` or adjust its error handling for more granular control.
Install
-
pip install duplocloud-client
Imports
- DuploClient
from duplocloud.client import DuploClient
- DuploCtl
from duplocloud.controller import DuploCtl
Quickstart
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}")