Charmed MongoDB

Channel Revision Published Runs on
6/stable 164 26 Mar 2024
Ubuntu 22.04
6/candidate 164 26 Mar 2024
Ubuntu 22.04
6/beta 164 26 Mar 2024
Ubuntu 22.04
6/edge 170 17 Apr 2024
Ubuntu 22.04
5/stable 117 20 Apr 2023
Ubuntu 22.04
5/candidate 117 20 Apr 2023
Ubuntu 22.04
5/edge 139 21 Nov 2023
Ubuntu 22.04
3.6/stable 100 28 Apr 2023
Ubuntu 20.04 Ubuntu 18.04
3.6/candidate 100 13 Apr 2023
Ubuntu 20.04 Ubuntu 18.04
3.6/edge 100 03 Feb 2023
Ubuntu 20.04 Ubuntu 18.04
juju deploy mongodb --channel 6/stable
Show information

Platform:

Ubuntu
22.04

charms.mongodb.v1.mongodb

Code for interactions with MongoDB.


class MongoDBConfiguration

Class for MongoDB configuration.

Description

— replset: name of replica set, needed for connection URI. — database: database name. — username: username. — password: password. — hosts: full list of hosts to connect to, needed for the URI.

  • tls_external: indicator for use of internal TLS connection.
  • tls_internal: indicator for use of external TLS connection.

Methods

MongoDBConfiguration. uri( self )

Description

Return URI concatenated from fields. None

class NotReadyError

Description

Raised when not all replica set members healthy or finished initial sync. None

class MongoDBConnection

In this class we create connection object to MongoDB.

Description

Real connection is created on the first call to MongoDB. Delayed connectivity allows to firstly check database readiness and reuse the same connection for an actual query later in the code.

Connection is automatically closed when object destroyed. Automatic close allows to have more clean code.

Note that connection when used may lead to the following pymongo errors: ConfigurationError, ConfigurationError, OperationFailure. It is suggested that the following pattern be adopted when using MongoDBConnection:

with MongoDBConnection(self._mongodb_config) as mongo: try: mongo.<some operation from this class> except ConfigurationError, ConfigurationError, OperationFailure: <error handling as needed>

Methods

MongoDBConnection. __init__( self , config: MongoDBConfiguration , uri , direct )

A MongoDB client interface.

Arguments

config

MongoDB Configuration object.

uri

allow using custom MongoDB URI, needed for replSet init.

direct

force a direct connection to a specific host, avoiding reading replica set configuration and reconnection.

MongoDBConnection. __enter__( self )

Description

Return a reference to the new connection. None

MongoDBConnection. __exit__( self , object_type , value , traceback )

Description

Disconnect from MongoDB client. None

MongoDBConnection. is_ready( self )

Is the MongoDB server ready for services requests.

Description

Returns: True if services is ready False otherwise. Retries over a period of 60 seconds times to allow server time to start up.

Raises: ConfigurationError, ConfigurationError, OperationFailure

MongoDBConnection. init_replset( self )

Create replica set config the first time.

Description

Raises: ConfigurationError, ConfigurationError, OperationFailure

MongoDBConnection. get_replset_status( self )

Get a replica set status as a dict.

Description

Returns: A set of the replica set members along with their status.

Raises: ConfigurationError, ConfigurationError, OperationFailure

MongoDBConnection. get_replset_members( self )

Get a replica set members.

Description

Returns: A set of the replica set members as reported by mongod.

Raises: ConfigurationError, ConfigurationError, OperationFailure

MongoDBConnection. add_replset_member( self , hostname: str )

Add a new member to replica set config inside MongoDB.

Description

Raises: ConfigurationError, ConfigurationError, OperationFailure, NotReadyError

MongoDBConnection. remove_replset_member( self , hostname: str )

Remove member from replica set config inside MongoDB.

Description

Raises: ConfigurationError, ConfigurationError, OperationFailure, NotReadyError

MongoDBConnection. create_user( self , config: MongoDBConfiguration )

Create user.

Description

Grant read and write privileges for specified database.

MongoDBConnection. update_user( self , config: MongoDBConfiguration )

Description

Update grants on database. None

MongoDBConnection. set_user_password( self , username , password: str )

Description

Update the password. None

MongoDBConnection. create_role( self , role_name: str , privileges: dict , roles: dict )

Creates a new role.

Arguments

role_name

name of the role to be added.

privileges

privileges to be associated with the role.

roles

List of roles from which this role inherits privileges.

MongoDBConnection. drop_user( self , username: str )

Description

Drop user. None

MongoDBConnection. get_users( self )

Description

Add a new member to replica set config inside MongoDB. None

MongoDBConnection. get_databases( self )

Description

Return list of all non-default databases. None

MongoDBConnection. drop_database( self , database: str )

Description

Drop a non-default database. None

MongoDBConnection. primary( self )

Description

Returns primary replica host. None