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__

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 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 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: dict property

Get Config

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

Returns:

Name Type Description
settings dict

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

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

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

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

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

Load Resource

Load a resource class from the entry points.

Parameters:

Name Type Description Default
kind str

The name of the service.

required

Returns:

Name Type Description
kind T

The instantiated service with a reference to this client.

format

Format data.

Parameters:

Name Type Description Default
data dict

The data to format.

required

Returns: The data as a string.