SQLite

Models

class dna.utils.Service(**kwargs)[source]

Represents a service, as well any domains bound to it

Parameters
  • name (str) – the name of the service

  • image (str) – the name of the docker image containing this image

  • port (str) – the container port running the front-end of this service

  • domains (list[Domain]) – a list of all the domains bound to this service

to_json()[source]

Represent this Service as a JSON dictionary

Returns

a dictionary containing the name, image, port, and domains associated with this Service

class dna.utils.Domain(**kwargs)[source]

Represents a domain, storing the Service it’s bound to (if any)

Parameters
  • url (str) – the domain itself

  • service (Service) – the service connected to this domain

Variables

service_name – the name of the service the domain is bound to

Warning

Do not set service_name manually! It is a foreign key that depends on the service, which is the parameter you should set.

class dna.utils.ApiKey(**kwargs)[source]

Represents an API Key, for the optional Flask REST API utility

Parameters
  • key (str) – the randomly-generated API key

  • ip (str) – the IP address that requested the key

  • issued_at (int) – the timestamp the key was issued at

  • expires_in (int) – the number of seconds this key is active

is_expired()[source]

Check if this key is expired or will expire within 10 seconds

Returns

True if expired, False otherwise

Interface

class dna.utils.SQLite(rel='/', name='app')[source]

Various utilities to interface with SQLite

Parameters
  • rel (str) – the relative path to the location to store the database

  • name (str) – the name of the database file (minus the .db extension)

create_service(name, image, port)[source]

Create a new service with the given parameters

Parameters
  • name (str) – the name of the service

  • image (str) – the docker image holding the service

  • port (str) – the container port running the front-end of the

Returns

the created Service

add_domain_to_service(domain, service)[source]

Bind domain to service if it is not bound elsewhere

Parameters
  • domain (str or Domain) – the (url of the) domain to bind

  • service (str or Service) – the (name of the) service to bind to

Returns

a boolean representing whether domain was successfully bound to service

remove_domain_from_service(domain, service)[source]

Unbind domain from service if it is bound to it

Parameters
  • domain (str or Domain) – the (url of the) domain to unbind

  • service (str or Service) – the (name of the) service to unbind from

Returns

a boolean representing whether domain was successfully unbound from service

delete_service(service)[source]

Remove all records related to service, including the associated Service object and any associated Domain objects

Parameters

service (str or Service) – the (name of the) service to delete

get_services()[source]

Get all the services stored in this database

Returns

a list of Service objects

get_service_by_name(name)[source]

Get information on the service called name

Parameters

name (str) – the name to query on

Returns

the requested Service, if it exists (else None)

get_service_by_domain(domain)[source]

Get information on the service that domain is bound to

Parameters

domain (str or Domain) – the (url of the) domain to query on

Returns

the requested Service, if it exists (else None)

get_domains()[source]

Get all the domains stored in this database

Returns

a list of Domain objects

get_domain_by_url(url, create=False)[source]

Get information on the domain pointing to url

Parameters
  • url (str) – the url to query on

  • create (bool) – flag to create the domain if it doesn’t exist (defaults to False)

Returns

the requested Domain if it exists (else None)

get_active_keys()[source]

Get all the active API Keys in this DNA instance

Returns

a list of ApiKey objects

get_key_info(key)[source]

Get info for the API key represented by the given key

Parameters

key (str) – the key to query by

Returns

the relevant ApiKey object if it exists, else None

new_api_key(key, ip, expires_in=3600)[source]

Create a new API key

Parameters
  • key (str) – the randomly-generated key

  • ip (str) – the IP address that requested the key

  • expires_in (int) – the number of seconds the key may be used (defaults to 3600)

Returns

the new ApiKey object

check_api_key(key, ip)[source]

Check whether the given IP can request the given key, if the key is valid

Parameters
  • key (str) – the key to query by and check

  • ip (str) – the requesting IP address

Returns

True if the IP can call the key, False otherwise or if the key is invalid

revoke_api_key(key)[source]

Revoke the given key early

Parameters

key (str) – the key to query by and revoke

Returns

True if successful, False otherwise