MongoDBExporter

class resilient_exporters.exporters.MongoDBExporter(target_ip: str, target_port: int = 27017, username: str = None, password: str = None, default_db: str = None, default_collection: str = None, **kwargs)[source]

Exporter for MongoDB.

Parameters
  • target_ip (str)

  • target_port (int)

  • username (str)

  • password (str)

  • default_db (str)

  • default_collection (str)

  • **kwargs – the keyword arguments to pass down to parent’s class Exporter

Raises

InvalidConfigError – if it cannot retrieve the server information, which is likely due an invalid configuration of the target.

Example

import os
from resilient_exporters.exporters import MongoDBExporter

exporter = MongoDBExporter(target_ip="127.0.0.1",
                           username=os.environ["MONGO_USERNAME"],
                           password=os.environ["MONGO_PASSWORD"],
                           default_db="profiles",
                           default_db="scientists")

data = {"name": "Richard Feynman",
        "age": 69}
exporter.send(data)
property client

The MongoDB client. It cannot be replaced.

has_unsent_data() → bool

Assesses if there’s saved, unsent data.

Returns

True if there’s saved, unsent data. False otherwise.

Return type

bool

save_unsent_data(data: Any, kwargs: dict, exporter_name: str) → None

Saves the data in memory or disk, depending on the value of use_memory of the instance, so it can be sent later.

Parameters
  • data (Any) – the core data.

  • kwargs (dict) – the keyword arguments to pass to send for the given data.

  • exporter_name (str) – the name of the exporter.

Returns

None

send_unsent_data() → List[resilient_exporters.exporters.ExportResult]

Tries to send the previously saved, unsent data.

Returns

list of the results of the export jobs.

Return type

List[ExportResult]

transform(data: Any) → Any

Applies transform to the given data.

Parameters

data (Any) – the data to pass to transform.

Returns

the output of transform

Return type

Any

Raises

Exception – if the output of transform(data) is None.

property use_memory

The value use_memory of the instance.

When set to True, it loads the data into memory if it was previously using a file (previous value was False), or vice versa.

Parameters

new_val (bool) – new value for use_memory.

Returns

its value

Return type

bool

send(data: dict, db: str = None, collection: str = None) → resilient_exporters.exporters.ExportResult[source]

Inserts data into a collection. Reuses default database and collection names, if provided at initialisation.

Parameters
  • data (dict) – a dict representing the document to insert into the collection.

  • db (str) – name of the target database. If None, will use the default value. Default is None.

  • collection (str) – name of the target colleciton. If None, will use the default value. Default is None.

Returns

the result in the form (ObjectId, True) if successful,

(None, False) otherwise.

Return type

ExportResult

Raises

MissingConfigError – if it cannot find a database and/or collection in the arguments and default values.