mirror of
https://github.com/saltstack/salt.git
synced 2025-04-16 09:40:20 +00:00
Adding parameter doc strings. Adding changelog. Fixing remove function to use the w parameter when deleting objects.
This commit is contained in:
parent
18327eb219
commit
30a5a0f370
3 changed files with 407 additions and 20 deletions
1
changelog/62900.fixed
Normal file
1
changelog/62900.fixed
Normal file
|
@ -0,0 +1 @@
|
|||
Updating various MongoDB module functions to work with latest version of pymongo.
|
|
@ -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,10 +1133,12 @@ def remove(
|
|||
deleted_count = 0
|
||||
if isinstance(query, list):
|
||||
for _query in query:
|
||||
res = col.delete_many(_query)
|
||||
for count in range(0, w):
|
||||
res = col.delete_one(_query)
|
||||
deleted_count += res.deleted_count
|
||||
else:
|
||||
res = col.delete_many(query)
|
||||
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:
|
||||
|
|
|
@ -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:
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue