Skip to content

Duplo Client

Duplo Client

This is the main Duplo client class. It is used to connect to Duplo and to retrieve resources from Duplo. All services have a reference to this when they are created.

Using injected client to load a service.
from duplocloud.client import DuploClient  
from duplocloud.resource import DuploResource  
from duplocloud.errors import DuploError  

class DuploSomeService(DuploResource):
  def __init__(self, duplo: DuploClient):
    super().__init__(duplo)
    self.tenent_svc = duplo.service('tenant')

Instantiation

These are a few static methods which instantiate new instances a few different ways. You can also just use the normal init constructor method to build it up manually.

__init__(host=None, token=None, tenant=None, tenant_id=None, home_dir=None, config_file=None, cache_dir=None, version=False, interactive=False, ctx=None, nocache=False, browser=None, isadmin=False, query=None, output='json', loglevel='WARN')

DuploClient Constructor

Creates an instance of a duplocloud client configured for a certain portal. All of the arguments are optional and can be set in the environment or in the config file. The types of each ofthe arguments are annotated types that are used by argparse to create the command line arguments.

Parameters:

Name Type Description Default
host HOST

The host of the Duplo instance.

None
token TOKEN

The token to use for authentication.

None
tenant TENANT

The tenant to use.

None
tenant_id TENANT_ID

The tenant id to use.

None
home_dir HOME_DIR

The home directory for the client.

None
config_file CONFIG

The config file for the client.

None
cache_dir CACHE_DIR

The cache directory for the client.

None
version VERSION

The version of the client.

False
interactive INTERACTIVE

The interactive mode for the client.

False
ctx CONTEXT

The context to use.

None
nocache NOCACHE

The nocache flag for the client.

False
browser BROWSER

The browser to use for interactive login.

None
isadmin ISADMIN

The admin flag for the client.

False
query QUERY

The query to use.

None
output OUTPUT

The output format for the client.

'json'
loglevel LOGLEVEL

The log level for the client.

'WARN'

Returns:

Name Type Description
duplo DuploClient

An instance of a DuploClient.

from_env() staticmethod

From Environment

Create a DuploClient from environment variables. This is the most common way to create a DuploClient.

New Client From Environment
duplo, args = DuploClient.from_env()

Returns:

Name Type Description
duplo DuploClient

An instance of a DuploClient.

from_args(*args) staticmethod

DuploClient from Environment

Create a DuploClient from an array of global client arguments.

Parameters:

Name Type Description Default
args str

An array of global client arguments aligning with the DuploClient constructor.

()

Returns:

Name Type Description
duplo DuploClient

An instance of DuploConfig.

from_creds(host, token, tenant) staticmethod

Create a DuploClient from credentials.

Parameters:

Name Type Description Default
host str

The host of the Duplo instance.

required
token str

The token to use for authentication.

required
tenant str

The tenant to use.

required

Returns:

Name Type Description
duplo DuploClient

The DuploClient.

Properties

These are the properties of the DuploClient class.

token: str property

Get Token

Returns the configured token. If interactive mode is enabled, an attempt will be made to get the token interactively. Ultimately, the token is required and if it is not set, an error will be raised. This is accessed as a lazy loaded property.

Returns:

Type Description
str

The token as a string.

host: str property

Get Host

Get the host from the Duplo config. This is accessed as a lazy loaded property. If the host is some kind of falsey value, it will attempt to use the context.

Returns:

Type Description
str

The host as a string.

settings property

Get Config

Get the Duplo config as a dict. This is accessed as a lazy loaded property.

Returns:

Type Description

The config as a dict.

context: dict property

Get Config Context

Get the current context from the Duplo config. This is accessed as a lazy loaded property.

Returns:

Type Description
dict

The context as a dict.

tenant: str property writable

Get Tenant

Get the tenant from the Duplo config. This is accessed as a lazy loaded property. If the tenant is some kind of falsey value, it will attempt to use the context.

Returns:

Type Description
str

The tenant as a string.

API Request Methods

The following methods are used to make requests to the Duplo API. They are simply the standard HTTP methods with scoped into the configured host and token.

get(path)

Get a Duplo resource.

This request is cached for 60 seconds.

Parameters:

Name Type Description Default
path str

The path to the resource.

required

Returns: The resource as a JSON object.

post(path, data={})

Post data to a Duplo resource.

Parameters:

Name Type Description Default
path str

The path to the resource.

required
data dict

The data to post.

{}

Returns: The response as a JSON object.

put(path, data={})

Put data to a Duplo resource.

Parameters:

Name Type Description Default
path str

The path to the resource.

required
data dict

The data to post.

{}

Returns: The response as a JSON object.

delete(path)

Delete a Duplo resource.

Parameters:

Name Type Description Default
path str

The path to the resource.

required

Returns: The response as a JSON object.

Injection

Duploctl is a plugin framework which takes advantage of the Python entrypoints system in the pyproject.toml file. This allows you to inject your own custom classes into the DuploClient class.

load(resource)

Load Resource

Load a resource class from the entry points.

Parameters:

Name Type Description Default
name

The name of the service.

required

Returns:

Type Description
T

The instantiated service with a reference to this client.

format(data)

Format data.

Parameters:

Name Type Description Default
data dict

The data to format.

required

Returns: The data as a string.