| 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_cluster |
| 27 |
short_description: Manage database clusters in Atlas |
| 28 |
description: |
| 29 |
- The clusters module provides access to your cluster configurations. |
| 30 |
- The module lets you create, edit and delete clusters. |
| 31 |
- L(API Documentation,https://docs.atlas.mongodb.com/reference/api/clusters/) |
| 32 |
author: "Martin Schurz (@schurzi)" |
| 33 |
extends_documentation_fragment: community.mongodb.atlas_options |
| 34 |
options: |
| 35 |
name: |
| 36 |
description: |
| 37 |
- Name of the cluster as it appears in Atlas. Once the cluster is created, its name cannot be changed. |
| 38 |
type: str |
| 39 |
required: True |
| 40 |
mongo_db_major_version: |
| 41 |
description: |
| 42 |
- Version of the cluster to deploy. |
| 43 |
- Atlas always deploys the cluster with the latest stable release of the specified version. |
| 44 |
- You can upgrade to a newer version of MongoDB when you modify a cluster. |
| 45 |
choices: [ "4.2", "4.4", "5.0", "6.0", "7.0" ] |
| 46 |
type: str |
| 47 |
aliases: [ "mongoDBMajorVersion" ] |
| 48 |
cluster_type: |
| 49 |
description: |
| 50 |
- Type of the cluster that you want to create. |
| 51 |
choices: [ "REPLICASET", "SHARDED" ] |
| 52 |
default: "REPLICASET" |
| 53 |
type: str |
| 54 |
aliases: [ "clusterType" ] |
| 55 |
replication_factor: |
| 56 |
description: |
| 57 |
- Number of replica set members. Each member keeps a copy of your databases, providing high availability and data redundancy. |
| 58 |
choices: [ 3, 5, 7 ] |
| 59 |
default: 3 |
| 60 |
type: int |
| 61 |
aliases: [ "replicationFactor" ] |
| 62 |
auto_scaling: |
| 63 |
description: |
| 64 |
- Configure your cluster to automatically scale its storage and cluster tier. |
| 65 |
suboptions: |
| 66 |
disk_gb_enabled: |
| 67 |
type: bool |
| 68 |
description: |
| 69 |
- Specifies whether disk auto-scaling is enabled. The default is true. |
| 70 |
aliases: [ "diskGBEnabled" ] |
| 71 |
required: False |
| 72 |
type: dict |
| 73 |
aliases: [ "autoScaling" ] |
| 74 |
provider_settings: |
| 75 |
description: |
| 76 |
- Configuration for the provisioned servers on which MongoDB runs. |
| 77 |
- The available options are specific to the cloud service provider. |
| 78 |
suboptions: |
| 79 |
provider_name: |
| 80 |
required: True |
| 81 |
type: str |
| 82 |
description: |
| 83 |
- Cloud service provider on which the servers are provisioned. |
| 84 |
aliases: [ "providerName" ] |
| 85 |
region_name: |
| 86 |
required: True |
| 87 |
type: str |
| 88 |
description: |
| 89 |
- Physical location of your MongoDB cluster. |
| 90 |
aliases: [ "regionName" ] |
| 91 |
instance_size_name: |
| 92 |
required: True |
| 93 |
type: str |
| 94 |
description: |
| 95 |
- Atlas provides different cluster tiers, each with a default storage capacity and RAM size. |
| 96 |
- The cluster you select is used for all the data-bearing servers in your cluster tier. |
| 97 |
aliases: [ "instanceSizeName" ] |
| 98 |
required: True |
| 99 |
type: dict |
| 100 |
aliases: [ "providerSettings" ] |
| 101 |
disk_size_gb: |
| 102 |
description: |
| 103 |
- Capacity, in gigabytes, of the host's root volume. Increase this number to add capacity, |
| 104 |
up to a maximum possible value of 4096 (i.e., 4 TB). This value must be a positive integer. |
| 105 |
type: int |
| 106 |
aliases: [ "diskSizeGB" ] |
| 107 |
provider_backup_enabled: |
| 108 |
description: |
| 109 |
- Flag that indicates if the cluster uses Cloud Backups for backups. |
| 110 |
type: bool |
| 111 |
aliases: [ "providerBackupEnabled" ] |
| 112 |
pit_enabled: |
| 113 |
description: |
| 114 |
- Flag that indicates the cluster uses continuous cloud backups. |
| 115 |
type: bool |
| 116 |
aliases: [ "pitEnabled" ] |
| 117 |
''' |
| 118 |
|
| 119 |
EXAMPLES = ''' |
| 120 |
- name: test cluster |
| 121 |
community.mongodb.mongodb_atlas_cluster: |
| 122 |
api_username: "API_user" |
| 123 |
api_password: "API_passwort_or_token" |
| 124 |
group_id: "GROUP_ID" |
| 125 |
name: "testcluster" |
| 126 |
mongo_db_major_version: "4.0" |
| 127 |
cluster_type: "REPLICASET" |
| 128 |
provider_settings: |
| 129 |
provider_name: "GCP" |
| 130 |
region_name: "EUROPE_WEST_3" |
| 131 |
instance_size_name: "M10" |
| 132 |
... |
| 133 |
''' |
| 134 |
|
| 135 |
from ansible.module_utils.basic import AnsibleModule |
| 136 |
from ansible_collections.community.mongodb.plugins.module_utils.mongodb_atlas import ( |
| 137 |
AtlasAPIObject, |
| 138 |
) |
| 139 |
|
| 140 |
|
| 141 |
# =========================================== |
| 142 |
# Module execution. |
| 143 |
# |
| 144 |
def main(): |
| 145 |
# add our own arguments |
| 146 |
argument_spec = dict( |
| 147 |
state=dict(default="present", choices=["absent", "present"]), |
| 148 |
api_username=dict(required=True, aliases=['apiUsername']), |
| 149 |
api_password=dict(required=True, no_log=True, aliases=['apiPassword']), |
| 150 |
group_id=dict(required=True, aliases=['groupId']), |
| 151 |
name=dict(required=True), |
| 152 |
mongo_db_major_version=dict( |
| 153 |
choices=["4.2", "4.4", "5.0", "6.0", "7.0"], |
| 154 |
aliases=["mongoDBMajorVersion"] |
| 155 |
), |
| 156 |
cluster_type=dict( |
| 157 |
default="REPLICASET", choices=["REPLICASET", "SHARDED"], |
| 158 |
aliases=["clusterType"] |
| 159 |
), |
| 160 |
replication_factor=dict(default=3, type="int", choices=[3, 5, 7], aliases=["replicationFactor"]), |
| 161 |
auto_scaling=dict( |
| 162 |
type="dict", |
| 163 |
options=dict( |
| 164 |
disk_gb_enabled=dict(type="bool", aliases=["diskGBEnabled"]), |
| 165 |
), |
| 166 |
aliases=["autoScaling"] |
| 167 |
), |
| 168 |
provider_settings=dict( |
| 169 |
type="dict", |
| 170 |
required=True, |
| 171 |
options=dict( |
| 172 |
provider_name=dict(required=True, aliases=["providerName"]), |
| 173 |
region_name=dict(required=True, aliases=["regionName"]), |
| 174 |
instance_size_name=dict(required=True, aliases=["instanceSizeName"]), |
| 175 |
), |
| 176 |
aliases=["providerSettings"] |
| 177 |
), |
| 178 |
disk_size_gb=dict(type="int", aliases=["diskSizeGB"]), |
| 179 |
provider_backup_enabled=dict(type="bool", aliases=["providerBackupEnabled"]), |
| 180 |
pit_enabled=dict(type="bool", aliases=["pitEnabled"]), |
| 181 |
) |
| 182 |
|
| 183 |
# Define the main module |
| 184 |
module = AnsibleModule( |
| 185 |
argument_spec=argument_spec, supports_check_mode=True |
| 186 |
) |
| 187 |
|
| 188 |
data = { |
| 189 |
"name": module.params["name"], |
| 190 |
"clusterType": module.params["cluster_type"], |
| 191 |
"replicationFactor": module.params["replication_factor"], |
| 192 |
"providerSettings": { |
| 193 |
"providerName": module.params["provider_settings"]["provider_name"], |
| 194 |
"regionName": module.params["provider_settings"]["region_name"], |
| 195 |
"instanceSizeName": module.params["provider_settings"]["instance_size_name"], |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
# handle optional options |
| 200 |
optional_vars = { |
| 201 |
"mongo_db_major_version": "mongoDBMajorVersion", |
| 202 |
"auto_scaling": "autoScaling", |
| 203 |
"disk_size_gb": "diskSizeGB", |
| 204 |
"provider_backup_enabled": "providerBackupEnabled", |
| 205 |
"pit_enabled": "pitEnabled", |
| 206 |
} |
| 207 |
|
| 208 |
for key in optional_vars: |
| 209 |
if module.params[key] is not None: |
| 210 |
if key == "auto_scaling": |
| 211 |
data.update({optional_vars[key]: {"diskGBEnabled": module.params[key]["disk_gb_enabled"]}}) |
| 212 |
else: |
| 213 |
data.update({optional_vars[key]: module.params[key]}) |
| 214 |
|
| 215 |
try: |
| 216 |
atlas = AtlasAPIObject( |
| 217 |
module=module, |
| 218 |
path="/clusters", |
| 219 |
object_name="name", |
| 220 |
group_id=module.params["group_id"], |
| 221 |
data=data, |
| 222 |
) |
| 223 |
except Exception as e: |
| 224 |
module.fail_json( |
| 225 |
msg="unable to connect to Atlas API. Exception message: %s" % e |
| 226 |
) |
| 227 |
|
| 228 |
changed, diff = atlas.update(module.params["state"]) |
| 229 |
module.exit_json( |
| 230 |
changed=changed, |
| 231 |
data=atlas.data, |
| 232 |
diff=diff, |
| 233 |
) |
| 234 |
|
| 235 |
|
| 236 |
# import module snippets |
| 237 |
if __name__ == "__main__": |
| 238 |
main() |