| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# Copyright (c) 2020 T-Systems MMS |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# |
| 7 |
# This module is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This software is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this software. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
from __future__ import absolute_import, division, print_function |
| 21 |
|
| 22 |
__metaclass__ = type |
| 23 |
|
| 24 |
DOCUMENTATION = ''' |
| 25 |
--- |
| 26 |
module: mongodb_atlas_whitelist |
| 27 |
short_description: Manage IP whitelists in Atlas |
| 28 |
description: |
| 29 |
- The mongodb_atlas_whitelist module manages a Atlas project's IP whitelist. |
| 30 |
- L(API Documentation,https://docs.atlas.mongodb.com/reference/api/whitelist/) |
| 31 |
author: "Martin Schurz (@schurzi)" |
| 32 |
extends_documentation_fragment: community.mongodb.atlas_options |
| 33 |
options: |
| 34 |
cidr_block: |
| 35 |
description: |
| 36 |
- Whitelist entry in Classless Inter-Domain Routing (CIDR) notation. |
| 37 |
type: str |
| 38 |
required: True |
| 39 |
aliases: [ "cidrBlock" ] |
| 40 |
comment: |
| 41 |
description: |
| 42 |
- Optional Comment associated with the whitelist entry. |
| 43 |
type: str |
| 44 |
default: "created by Ansible" |
| 45 |
''' |
| 46 |
|
| 47 |
EXAMPLES = ''' |
| 48 |
- name: test whitelist |
| 49 |
community.mongodb.mongodb_atlas_whitelist: |
| 50 |
api_username: "API_user" |
| 51 |
api_password: "API_passwort_or_token" |
| 52 |
group_id: "GROUP_ID" |
| 53 |
cidr_block: "192.168.0.0/24" |
| 54 |
comment: "test" |
| 55 |
''' |
| 56 |
|
| 57 |
from ansible.module_utils.basic import AnsibleModule |
| 58 |
from ansible_collections.community.mongodb.plugins.module_utils.mongodb_atlas import ( |
| 59 |
AtlasAPIObject, |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
# =========================================== |
| 64 |
# Module execution. |
| 65 |
# |
| 66 |
def main(): |
| 67 |
# add our own arguments |
| 68 |
argument_spec = dict( |
| 69 |
state=dict(default="present", choices=["absent", "present"]), |
| 70 |
api_username=dict(required=True, aliases=['apiUsername']), |
| 71 |
api_password=dict(required=True, no_log=True, aliases=['apiPassword']), |
| 72 |
group_id=dict(required=True, aliases=['groupId']), |
| 73 |
cidr_block=dict(required=True, aliases=["cidrBlock"]), |
| 74 |
comment=dict(default="created by Ansible"), |
| 75 |
) |
| 76 |
|
| 77 |
# Define the main module |
| 78 |
module = AnsibleModule( |
| 79 |
argument_spec=argument_spec, supports_check_mode=True |
| 80 |
) |
| 81 |
|
| 82 |
data = { |
| 83 |
"cidrBlock": module.params["cidr_block"], |
| 84 |
"comment": module.params["comment"], |
| 85 |
} |
| 86 |
|
| 87 |
try: |
| 88 |
atlas = AtlasAPIObject( |
| 89 |
module=module, |
| 90 |
path="/whitelist", |
| 91 |
object_name="cidrBlock", |
| 92 |
group_id=module.params["group_id"], |
| 93 |
data=data, |
| 94 |
data_is_array=True, |
| 95 |
) |
| 96 |
except Exception as e: |
| 97 |
module.fail_json( |
| 98 |
msg="unable to connect to Atlas API. Exception message: %s" % e |
| 99 |
) |
| 100 |
|
| 101 |
changed, diff = atlas.update(module.params["state"]) |
| 102 |
module.exit_json( |
| 103 |
changed=changed, |
| 104 |
data=atlas.data, |
| 105 |
diff=diff, |
| 106 |
) |
| 107 |
|
| 108 |
|
| 109 |
# import module snippets |
| 110 |
if __name__ == "__main__": |
| 111 |
main() |