From 30a5a0f370c39f47e10949328bcd5f64a2f56f78 Mon Sep 17 00:00:00 2001 From: "Gareth J. Greenaway" Date: Mon, 17 Oct 2022 16:23:17 -0700 Subject: [PATCH] Adding parameter doc strings. Adding changelog. Fixing remove function to use the w parameter when deleting objects. --- changelog/62900.fixed | 1 + salt/modules/mongodb.py | 400 ++++++++++++++++++++- tests/pytests/unit/modules/test_mongodb.py | 26 +- 3 files changed, 407 insertions(+), 20 deletions(-) create mode 100644 changelog/62900.fixed diff --git a/changelog/62900.fixed b/changelog/62900.fixed new file mode 100644 index 00000000000..437c1edcef2 --- /dev/null +++ b/changelog/62900.fixed @@ -0,0 +1 @@ +Updating various MongoDB module functions to work with latest version of pymongo. diff --git a/salt/modules/mongodb.py b/salt/modules/mongodb.py index 0e545cac885..3070f429200 100644 --- a/salt/modules/mongodb.py +++ b/salt/modules/mongodb.py @@ -92,6 +92,21 @@ def db_list(user=None, password=None, host=None, port=None, authdb=None): """ List all MongoDB databases + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Example: .. code-block:: bash @@ -114,6 +129,24 @@ def db_exists(name, user=None, password=None, host=None, port=None, authdb=None) """ Checks if a database exists in MongoDB + name + The name of the database to check for. + + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Example: .. code-block:: bash @@ -132,6 +165,24 @@ def db_remove(name, user=None, password=None, host=None, port=None, authdb=None) """ Remove a MongoDB database + name + The name of the database to remove. + + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Example: .. code-block:: bash @@ -162,6 +213,21 @@ def version( """ Get MongoDB instance version + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Example: .. code-block:: bash @@ -188,6 +254,27 @@ def user_find( """ Get single user from MongoDB + name + The name of the user to find. + + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + database + The MongoDB database to use when looking for the user. Default is ``admin``. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Example: .. code-block:: bash @@ -214,6 +301,24 @@ def user_list( """ List users of a MongoDB database + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + database + The MongoDB database to use when listing users. Default is ``admin``. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Example: .. code-block:: bash @@ -252,6 +357,24 @@ def user_exists( """ Checks if a user exists in MongoDB + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + database + The MongoDB database to use when checking if the user exists. Default is ``admin``. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Example: .. code-block:: bash @@ -284,6 +407,33 @@ def user_create( """ Create a MongoDB user + name + The name of the user to create. + + passwd + The password for the user that is being created. + + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + database + The MongoDB database to use when checking if the user exists. Default is ``admin``. + + authdb + The MongoDB database to use for authentication. Default is None. + + roles + The roles that should be associated with the user. Default is None. + CLI Example: .. code-block:: bash @@ -315,6 +465,24 @@ def user_remove( """ Remove a MongoDB user + name + The name of the user that should be removed. + + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Example: .. code-block:: bash @@ -328,9 +496,9 @@ def user_remove( try: log.info("Removing user %s", name) mdb = pymongo.database.Database(conn, database) - ret = mdb.command("dropUser", name) + mdb.command("dropUser", name) except pymongo.errors.PyMongoError as err: - log.error("Creating database %s failed with error: %s", name, err) + log.error("Removing user %s failed with error: %s", name, err) return str(err) return True @@ -342,6 +510,30 @@ def user_roles_exists( """ Checks if a user of a MongoDB database has specified roles + name + The name of the user to check for the specified roles. + + roles + The roles to check are associated with the specified user. + + database + The database to check has the specified roles for the specified user. + + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Examples: .. code-block:: bash @@ -381,6 +573,30 @@ def user_grant_roles( """ Grant one or many roles to a MongoDB user + name + The user to grant the specified roles to. + + roles + The roles to grant to the specified user. + + database + The database to great the roles against for the specified user. + + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Examples: .. code-block:: bash @@ -403,7 +619,7 @@ def user_grant_roles( try: log.info("Granting roles %s to user %s", roles, name) mdb = pymongo.database.Database(conn, database) - ret = mdb.command("grantRolesToUser", name, roles=roles) + mdb.command("grantRolesToUser", name, roles=roles) except pymongo.errors.PyMongoError as err: log.error( "Granting roles %s to user %s failed with error: %s", roles, name, err @@ -419,6 +635,27 @@ def user_revoke_roles( """ Revoke one or many roles to a MongoDB user + user + The user to connect to MongoDB as. Default is None. + + roles + The roles to revoke from the specified user. + + database + The database to revoke the roles from for the specified user. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Examples: .. code-block:: bash @@ -461,8 +698,28 @@ def collection_create( authdb=None, ): """ + .. versionadded:: 3006 + Create a collection in the specified database. + collection + The name of the collection to create. + + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Example: .. code-block:: bash @@ -496,8 +753,28 @@ def collection_drop( authdb=None, ): """ + .. versionadded:: 3006 + Drop a collection in the specified database. + collection + The name of the collection to drop. + + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Example: .. code-block:: bash @@ -530,8 +807,25 @@ def collections_list( authdb=None, ): """ + .. versionadded:: 3006 + List the collections available in the specified database. + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Example: .. code-block:: bash @@ -565,6 +859,27 @@ def insert( """ Insert an object or list of objects into a collection + objects + The objects to insert into the collection, should be provided as a list. + + collection + The collection to insert the objects into. + + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Example: .. code-block:: bash @@ -608,6 +923,27 @@ def update_one( .. versionadded:: 2016.11.0 + objects + The objects to update in the collection, should be provided as a list. + + collection + The collection to insert the objects into. + + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Example: .. code-block:: bash @@ -677,6 +1013,27 @@ def find( """ Find an object or list of objects in a collection + collection + The collection to find the objects in. + + query + The query to use when locating objects in the collection. + + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Example: .. code-block:: bash @@ -726,6 +1083,33 @@ def remove( """ Remove an object or list of objects from a collection + collection + The collection to remove objects from based on the query. + + query + Query to determine which objects to remove. + + user + The user to connect to MongoDB as. Default is None. + + password + The password to use to connect to MongoDB as. Default is None. + + host + The host where MongoDB is running. Default is None. + + port + The host where MongoDB is running. Default is None. + + database + The database where the collection is. + + w + The number of matches to remove from the collection. + + authdb + The MongoDB database to use for authentication. Default is None. + CLI Example: .. code-block:: bash @@ -749,11 +1133,13 @@ def remove( deleted_count = 0 if isinstance(query, list): for _query in query: - res = col.delete_many(_query) - deleted_count += res.deleted_count + for count in range(0, w): + res = col.delete_one(_query) + deleted_count += res.deleted_count else: - res = col.delete_many(query) - deleted_count += res.deleted_count + for count in range(0, w): + res = col.delete_one(query) + deleted_count += res.deleted_count return "{} objects removed".format(deleted_count) except pymongo.errors.PyMongoError as err: log.error("Removing objects failed with error: %s", _get_error_message(err)) diff --git a/tests/pytests/unit/modules/test_mongodb.py b/tests/pytests/unit/modules/test_mongodb.py index 5dcda2b7931..51249fe6177 100644 --- a/tests/pytests/unit/modules/test_mongodb.py +++ b/tests/pytests/unit/modules/test_mongodb.py @@ -83,7 +83,7 @@ class MockPyMongoCollection: def insert_many(self, *args, **kwards): return True - def delete_many(self, *args, **kwards): + def delete_one(self, *args, **kwards): return True @@ -626,15 +626,15 @@ def test_remove(): ) # Assume we delete one entry each time - collection_delete_many_mock = MockDeleteResult() - collection_delete_many_mock.deleted_count = 1 - collection_delete_many_mock.raw_result = {"n": 1, "ok": 1.0} - collection_delete_many_mock.acknowledged = True + collection_delete_one_mock = MockDeleteResult() + collection_delete_one_mock.deleted_count = 1 + collection_delete_one_mock.raw_result = {"n": 1, "ok": 1.0} + collection_delete_one_mock.acknowledged = True patch_pymongo_collection_remove = patch.object( MockPyMongoCollection, - "delete_many", - MagicMock(return_value=collection_delete_many_mock), + "delete_one", + MagicMock(return_value=collection_delete_one_mock), ) with patch_pymongo_collection_remove, patch_salt_dict: @@ -653,15 +653,15 @@ def test_remove(): ) # Assume we delete one entry each time - collection_delete_many_mock = MockDeleteResult() - collection_delete_many_mock.deleted_count = 1 - collection_delete_many_mock.raw_result = {"n": 1, "ok": 1.0} - collection_delete_many_mock.acknowledged = True + collection_delete_one_mock = MockDeleteResult() + collection_delete_one_mock.deleted_count = 1 + collection_delete_one_mock.raw_result = {"n": 1, "ok": 1.0} + collection_delete_one_mock.acknowledged = True patch_pymongo_collection_remove = patch.object( MockPyMongoCollection, - "delete_many", - MagicMock(return_value=collection_delete_many_mock), + "delete_one", + MagicMock(return_value=collection_delete_one_mock), ) with patch_pymongo_collection_remove, patch_salt_dict: