Datastore Module

datastore.cache

TcEx Framework Module for working with Datastore in the ThreatConnect Platform.

class tcex.datastore.cache.Cache(tcex, domain, data_type, ttl_seconds=None, mapping=None)[source]

Bases: object

TcEx Cache Class.

_cache_date(ttl_seconds)[source]

Return the cache date.

add(rid, data, ttl_seconds=None, raise_on_error=True)[source]

Write cache data to the data store.

Parameters:
  • rid (str) – The record identifier.
  • data (dict) – The record data.
  • ttl_seconds (int, optional) – Defaults to None. Number of seconds the cache is valid.
  • raise_on_error (bool) – If True and not r.ok this method will raise a RunTimeError.
Returns:

Python request response.

Return type:

object

delete(rid, raise_on_error=True)[source]

Write cache data to the data store.

Parameters:
  • rid (str) – The record identifier.
  • raise_on_error (bool) – If True and not r.ok this method will raise a RunTimeError.
Returns:

Python request response.

Return type:

object

get(rid, data_callback=None, raise_on_error=True)[source]

Get cached data from the data store.

Parameters:
  • rid (str) – The record identifier.
  • data_callback (callable) – A method that will return the data.
  • raise_on_error (bool) – If True and not r.ok this method will raise a RunTimeError.
Returns:

Python request response.

Return type:

object

update(rid, data, ttl_seconds=None, raise_on_error=True)[source]

Write updated cache data to the DataStore.

Parameters:
  • rid (str) – The record identifier.
  • data (dict) – The record data.
  • ttl_seconds (int, optional) – Defaults to None. Number of seconds the cache is valid.
  • raise_on_error (bool) – If True and not r.ok this method will raise a RunTimeError.
Returns:

Python request response.

Return type:

object

datastore.datastore

TcEx Framework Module for working with DataStore in the ThreatConnect Platform.

class tcex.datastore.datastore.DataStore(tcex, domain, data_type, mapping=None)[source]

Bases: object

TcEx DataStore Class.

domain

This can be either “system”, “organization”, or “local”. Any string not defined as these 3 will return a “Resource Not Found” error response.

dataType

This is a free form index type name as allowed by ElasticSearch. The ThreatConnect API will use this resource verbatim for ElasticSearch calls.

searchCommand

The URI string after the typeName is assumed to be ElasticSearch relevant information and is passed directly to ElasticSearch.

_create_index()[source]

Create index if it doesn’t exist.

_token_available()[source]

Raise an error if token is not available.

This check needs to be made after args are parsed to ensure token module has initialized.

_update_mappings()[source]

Update the mappings for the current index.

add(rid, data, raise_on_error=True)[source]

Write data to the DataStore. Alias for post() method.

Parameters:
  • rid (str) – The record identifier.
  • data (dict) – The record data.
  • raise_on_error (bool) – If True and not r.ok this method will raise a RunTimeError.
Returns:

Python request response.

Return type:

object

delete(rid, raise_on_error=True)[source]

Delete a record from the index using provide Id.

Parameters:
  • rid (str) – The record identifier.
  • raise_on_error (bool) – If True and not r.ok this method will raise a RunTimeError.
Returns:

Python request response.

Return type:

object

get(rid=None, data=None, raise_on_error=True)[source]

Get data from the DataStore.

Parameters:
  • rid (str, Optional) – Defaults to None. The record identifier.
  • data (dict, Optional) – Defaults to None. A search query
  • raise_on_error (bool) – If True and not r.ok this method will raise a RunTimeError.
Returns:

Python request response.

Return type:

object

index_exists

Check to see if index exists.

post(rid, data, raise_on_error=True)[source]

Write data to the DataStore.

Parameters:
  • rid (str) – The record identifier. If a value of None is passed an identifier elastic search will automatically generate a id.
  • data (dict) – The record data.
  • raise_on_error (bool) – If True and not r.ok this method will raise a RunTimeError.
Returns:

Python request response.

Return type:

object

put(rid, data, raise_on_error=True)[source]

Update the data for the provided Id.

Parameters:
  • rid (str) – The record identifier.
  • data (dict) – A search query
  • raise_on_error (bool) – If True and not r.ok this method will raise a RunTimeError.
Returns:

Python request response.

Return type:

object

update(rid, data, raise_on_error=True)[source]

Update the for the provided Id. Alias for put() method.

Parameters:
  • rid (str) – The record identifier.
  • data (dict) – The record data.
  • raise_on_error (bool) – If True and not r.ok this method will raise a RunTimeError.
Returns:

Python request response.

Return type:

object