Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mongodb_role module #45488

Closed
wants to merge 19 commits into from

Conversation

jkramarz
Copy link
Contributor

SUMMARY

mongodb_role module introduced in this pull request provides management capabilities for user-defined roles in MongoDB, enabling users to define custom privileges.

ISSUE TYPE
  • New Module Pull Request
COMPONENT NAME

mongodb_role

ANSIBLE VERSION
ansible 2.6.3
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/jkramarz/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.15 (default, May 16 2018, 17:50:09) [GCC 8.1.1 20180502 (Red Hat 8.1.1-1)]

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 community_review In order to be merged, this PR must follow the community review workflow. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. new_module This PR includes a new module. new_plugin This PR includes a new plugin. support:community This issue/PR relates to code supported by the Ansible community. labels Sep 11, 2018
@webknjaz webknjaz removed the needs_triage Needs a first human triage before being processed. label Sep 11, 2018
@webknjaz
Copy link
Member

@jkramarz you'll need to fix linter errors to get this merged

Also, if you wish to be a maintainer, self-add to:
https://github.com/ansible/ansible/blob/devel/.github/BOTMETA.yml#L143-L149

@ansibot

This comment has been minimized.

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Sep 11, 2018
@ansibot

This comment has been minimized.

@ansibot ansibot added ci_verified Changes made in this PR are causing tests to fail. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Sep 11, 2018
def role_find(module, client, db_name, role, authentication_restrictions_supported):
db = client[db_name]

if authentication_restrictions_supported:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactor:

additional_kwargs = (
    {'showAuthenticationRestrictions': True}
    if authentication_restrictions_supported
    else {}
)
result = db.command(
    'rolesInfo',
    role,
    showPrivileges=True,
    **additional_kwargs
)

return creds


def check_authentication_restrictions_support(client):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better name: is_authentication_restrictions_supported

try:
with open(config_file) as f:
config.readfp(f)
creds = dict(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can probably move this statement outside of with-block

try:
with open(config_file) as f:
config.readfp(f)
creds = dict(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, please use dict literal instead of constuctor ({'key': 'val'} instead of dict(key='val'))

config = configparser.RawConfigParser()

try:
with open(config_file) as f:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrap these pieces of code into two try/except statements to be precise with what you're trapping.

  1. one around with-block (with readfp call) catching IOError
  2. the other one around creds dict constuction catching configparser.NoOptionError

# MongoDB module specific support methods.
#

def check_compatibility(module, client):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a hanging PR implementing it in a nicer way: https://github.com/ansible/ansible/pull/44110/files#diff-d4702ee9f9f2977f8c82cb5b4e8b0e26R221. You may want to try borrowing some ideas from there.

db = client[db_name]

if authentication_restrictions_supported:
result = db.command('createRole',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This snippet looks just like one in role_find. It probably deserves being moved into a reusable function.

)

if result['ok'] != 1:
module.fail_json(msg='rolesInfo failed', exception=traceback.format_exc())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message is not very informative. Maybe there's some data you'd like to add to the returned result?

roles=roles
)

if result['ok'] != 1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this is also very boilerplate, which you could deduplicate.

and sorted(current['privileges']) == sorted(privileges) \
and sorted(current_restrictions) == sorted(authentication_restrictions)
else:
return sorted(current['roles']) == sorted(roles) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This thing is also repeated.


def main():
module = AnsibleModule(
argument_spec=dict(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rewrite dict creation using literals (with curly braces)

login_user=dict(default=None),
login_password=dict(default=None, no_log=True),
login_host=dict(default='localhost'),
login_port=dict(default='27017'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use type='int' here and you won't have to do manual conversion or checking manually

@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Sep 28, 2018
@dagwieers dagwieers added the mongodb MongoDB community label Jan 28, 2019
@dagwieers dagwieers added the database Database category label Feb 13, 2019
@gundalow
Copy link
Contributor

@jkramarz Hi, would you be able to take a look at the suggestions above?

@dagwieers dagwieers added the botmeta This PR modifies the BOTMETA.yml and this requires special attention! label Feb 21, 2019
@SPR0STO
Copy link

SPR0STO commented May 14, 2019

Hello
Extremely interested in the ability to manipulate MongoDB roles
When will the module be implemented?

@@ -141,6 +141,7 @@ files:
$modules/crypto/: Spredzy
$modules/crypto/acme/: resmo felixfontein
$modules/database/influxdb/: kamsz
$modules/database/mongodb/mongodb_role.py: jkramarz
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed as you are listed in the module's author: list

short_description: Adds or removes a role from a MongoDB database.
description:
- Adds or removes a role from a MongoDB database.
version_added: "2.8"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
version_added: "2.8"
version_added: `2.10`

@ansibot ansibot added needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed core_review In order to be merged, this PR must follow the core review workflow. labels Mar 29, 2020
@ansibot ansibot added collection Related to Ansible Collections work collection:community.mongodb and removed support:community This issue/PR relates to code supported by the Ansible community. labels Apr 29, 2020
@gundalow
Copy link
Contributor

gundalow commented Jul 2, 2020

@jkramarz Hi, the mongodb modules have moved to https://github.com/ansible-collections/community.mongodb could you please raise the PR there

@emanuelflp
Copy link

Hello guys. Can I continue with this PR? If yes, I will create the PR in the new repo.
@gundalow @jkramarz @webknjaz

@gundalow
Copy link
Contributor

@emanuelflp if the PR doesn't exist in the new repo yet, then yes, please proceed. Thanks!

@gundalow gundalow closed this Jul 15, 2020
@ansible ansible locked and limited conversation to collaborators Aug 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 botmeta This PR modifies the BOTMETA.yml and this requires special attention! collection:community.mongodb collection Related to Ansible Collections work database Database category module This issue/PR relates to a module. mongodb MongoDB community needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. new_module This PR includes a new module. new_plugin This PR includes a new plugin. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. support:core This issue/PR relates to code supported by the Ansible Engineering Team. test This PR relates to tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants