| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2020, Rhys Campbell (@rhysmeister) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
--- |
| 14 |
module: mongodb_index |
| 15 |
|
| 16 |
short_description: Creates or drops indexes on MongoDB collections. |
| 17 |
|
| 18 |
description: |
| 19 |
- Creates or drops indexes on MongoDB collections. |
| 20 |
- Supports multiple index options, i.e. unique, sparse and partial. |
| 21 |
- Validates existence of indexes by name only. |
| 22 |
|
| 23 |
author: Rhys Campbell (@rhysmeister) |
| 24 |
version_added: "1.0.0" |
| 25 |
|
| 26 |
extends_documentation_fragment: |
| 27 |
- community.mongodb.login_options |
| 28 |
- community.mongodb.ssl_options |
| 29 |
|
| 30 |
options: |
| 31 |
indexes: |
| 32 |
description: |
| 33 |
- List of indexes to create or drop |
| 34 |
type: list |
| 35 |
elements: raw |
| 36 |
required: yes |
| 37 |
replica_set: |
| 38 |
description: |
| 39 |
- Replica set to connect to (automatically connects to primary for writes). |
| 40 |
type: str |
| 41 |
notes: |
| 42 |
- Requires the pymongo Python package on the remote host, version 4+. |
| 43 |
|
| 44 |
requirements: |
| 45 |
- pymongo |
| 46 |
''' |
| 47 |
|
| 48 |
EXAMPLES = r''' |
| 49 |
- name: Create a single index on a collection |
| 50 |
community.mongodb.mongodb_index: |
| 51 |
login_user: admin |
| 52 |
login_password: secret |
| 53 |
indexes: |
| 54 |
- database: mydb |
| 55 |
collection: test |
| 56 |
keys: |
| 57 |
- username: 1 |
| 58 |
last_login: -1 |
| 59 |
options: |
| 60 |
name: myindex |
| 61 |
state: present |
| 62 |
|
| 63 |
- name: Drop an index on a collection |
| 64 |
community.mongodb.mongodb_index: |
| 65 |
login_user: admin |
| 66 |
login_password: secret |
| 67 |
indexes: |
| 68 |
- database: mydb |
| 69 |
collection: test |
| 70 |
options: |
| 71 |
name: myindex |
| 72 |
state: absent |
| 73 |
|
| 74 |
- name: Create multiple indexes |
| 75 |
community.mongodb.mongodb_index: |
| 76 |
login_user: admin |
| 77 |
login_password: secret |
| 78 |
indexes: |
| 79 |
- database: mydb |
| 80 |
collection: test |
| 81 |
keys: |
| 82 |
- username: 1 |
| 83 |
last_login: -1 |
| 84 |
options: |
| 85 |
name: myindex |
| 86 |
state: present |
| 87 |
- database: mydb |
| 88 |
collection: test |
| 89 |
keys: |
| 90 |
- email: 1 |
| 91 |
last_login: -1 |
| 92 |
options: |
| 93 |
name: myindex2 |
| 94 |
state: present |
| 95 |
|
| 96 |
- name: Add a unique index |
| 97 |
community.mongodb.mongodb_index: |
| 98 |
login_port: 27017 |
| 99 |
login_user: admin |
| 100 |
login_password: secret |
| 101 |
login_database: "admin" |
| 102 |
indexes: |
| 103 |
- database: "test" |
| 104 |
collection: "rhys" |
| 105 |
keys: |
| 106 |
username: 1 |
| 107 |
options: |
| 108 |
name: myuniqueindex |
| 109 |
unique: true |
| 110 |
state: present |
| 111 |
|
| 112 |
- name: Add a ttl index |
| 113 |
community.mongodb.mongodb_index: |
| 114 |
login_port: 27017 |
| 115 |
login_user: admin |
| 116 |
login_password: secret |
| 117 |
login_database: "admin" |
| 118 |
indexes: |
| 119 |
- database: "test" |
| 120 |
collection: "rhys" |
| 121 |
keys: |
| 122 |
created: 1 |
| 123 |
options: |
| 124 |
name: myttlindex |
| 125 |
expireAfterSeconds: 3600 |
| 126 |
state: present |
| 127 |
|
| 128 |
- name: Add a sparse index |
| 129 |
community.mongodb.mongodb_index: |
| 130 |
login_port: 27017 |
| 131 |
login_user: admin |
| 132 |
login_password: secret |
| 133 |
login_database: "admin" |
| 134 |
indexes: |
| 135 |
- database: "test" |
| 136 |
collection: "rhys" |
| 137 |
keys: |
| 138 |
last_login: -1 |
| 139 |
options: |
| 140 |
name: mysparseindex |
| 141 |
sparse: true |
| 142 |
state: present |
| 143 |
|
| 144 |
- name: Add a partial index |
| 145 |
community.mongodb.mongodb_index: |
| 146 |
login_port: 27017 |
| 147 |
login_user: admin |
| 148 |
login_password: secret |
| 149 |
login_database: "admin" |
| 150 |
indexes: |
| 151 |
- database: "test" |
| 152 |
collection: "rhys" |
| 153 |
keys: |
| 154 |
last_login: -1 |
| 155 |
options: |
| 156 |
name: mypartialindex |
| 157 |
partialFilterExpression: |
| 158 |
rating: |
| 159 |
$gt: 5 |
| 160 |
state: present |
| 161 |
|
| 162 |
- name: Add a index in the background (background option is deprecated from 4.2+) |
| 163 |
community.mongodb.mongodb_index: |
| 164 |
login_port: 27017 |
| 165 |
login_user: admin |
| 166 |
login_password: secret |
| 167 |
login_database: "admin" |
| 168 |
indexes: |
| 169 |
- database: "test" |
| 170 |
collection: "rhys" |
| 171 |
options: |
| 172 |
name: idxbackground |
| 173 |
keys: |
| 174 |
username: -1 |
| 175 |
backgroud: true |
| 176 |
state: present |
| 177 |
|
| 178 |
- name: Check creating 5 index all with multiple options specified |
| 179 |
community.mongodb.mongodb_index: |
| 180 |
login_port: 27017 |
| 181 |
login_user: admin |
| 182 |
login_password: secret |
| 183 |
login_database: "admin" |
| 184 |
indexes: |
| 185 |
- database: "test" |
| 186 |
collection: "indextest" |
| 187 |
options: |
| 188 |
name: "idx_unq_username" |
| 189 |
unique: true |
| 190 |
keys: |
| 191 |
username: -1 |
| 192 |
state: present |
| 193 |
- database: "test" |
| 194 |
collection: "indextest" |
| 195 |
options: |
| 196 |
name: "idx_last_login" |
| 197 |
sparse: true |
| 198 |
keys: |
| 199 |
last_login: -1 |
| 200 |
state: present |
| 201 |
- database: "test" |
| 202 |
collection: "indextest" |
| 203 |
options: |
| 204 |
name: "myindex" |
| 205 |
keys: |
| 206 |
first_name: 1 |
| 207 |
last_name: -1 |
| 208 |
city: 1 |
| 209 |
state: present |
| 210 |
- database: "test" |
| 211 |
collection: partialtest |
| 212 |
options: |
| 213 |
name: "idx_partialtest" |
| 214 |
partialFilterExpression: |
| 215 |
rating: |
| 216 |
$gt: 5 |
| 217 |
keys: |
| 218 |
rating: -1 |
| 219 |
title: 1 |
| 220 |
state: present |
| 221 |
- database: "test" |
| 222 |
collection: "wideindex" |
| 223 |
options: |
| 224 |
name: "mywideindex" |
| 225 |
keys: |
| 226 |
email: -1 |
| 227 |
username: 1 |
| 228 |
first_name: 1 |
| 229 |
last_name: 1 |
| 230 |
dob: -1 |
| 231 |
city: 1 |
| 232 |
last_login: -1 |
| 233 |
review_count: 1 |
| 234 |
rating_count: 1 |
| 235 |
last_post: -1 |
| 236 |
state: present |
| 237 |
''' |
| 238 |
|
| 239 |
RETURN = r''' |
| 240 |
indexes_created: |
| 241 |
description: List of indexes created. |
| 242 |
returned: always |
| 243 |
type: list |
| 244 |
sample: ["myindex", "myindex2"] |
| 245 |
indexes_dropped: |
| 246 |
description: List of indexes dropped. |
| 247 |
returned: always |
| 248 |
type: list |
| 249 |
sample: ["myindex", "myindex2"] |
| 250 |
changed: |
| 251 |
description: Indicates the module has changed something. |
| 252 |
returned: When the module has changed something. |
| 253 |
type: bool |
| 254 |
failed: |
| 255 |
description: Indicates the module has failed. |
| 256 |
returned: When the module has encountered an error. |
| 257 |
type: bool |
| 258 |
''' |
| 259 |
|
| 260 |
from ansible.module_utils.basic import AnsibleModule |
| 261 |
from ansible_collections.community.mongodb.plugins.module_utils.mongodb_common import ( |
| 262 |
missing_required_lib, |
| 263 |
mongodb_common_argument_spec, |
| 264 |
PYMONGO_IMP_ERR, |
| 265 |
pymongo_found, |
| 266 |
index_exists, |
| 267 |
create_index, |
| 268 |
drop_index, |
| 269 |
mongo_auth, |
| 270 |
get_mongodb_client, |
| 271 |
) |
| 272 |
|
| 273 |
|
| 274 |
def validate_module(module): |
| 275 |
''' |
| 276 |
Runs validation rules specific the mongodb_index module |
| 277 |
''' |
| 278 |
required_index_keys = [ |
| 279 |
"database", |
| 280 |
"collection", |
| 281 |
"options", |
| 282 |
"state", |
| 283 |
] |
| 284 |
indexes = module.params['indexes'] |
| 285 |
|
| 286 |
if len(indexes) == 0: |
| 287 |
module.fail_json(msg="One or more indexes must be specified") |
| 288 |
if not all(isinstance(i, dict) for i in indexes): |
| 289 |
module.fail_json(msg="Indexes must be supplied as dictionaries") |
| 290 |
|
| 291 |
# Ensure keys are present in index spec |
| 292 |
for k in required_index_keys: |
| 293 |
for i in indexes: |
| 294 |
if k not in i.keys(): |
| 295 |
module.fail_json(msg="Missing required index key {0}".format(k)) |
| 296 |
|
| 297 |
# Check index subkeys look correct |
| 298 |
for i in indexes: |
| 299 |
if not isinstance(i["database"], str): |
| 300 |
module.fail_json(msg="database key should be str") |
| 301 |
elif not isinstance(i["collection"], str): |
| 302 |
module.fail_json(msg="collection key should be str") |
| 303 |
elif i["state"] == "present" and "keys" not in i.keys(): |
| 304 |
module.fail_json(msg="keys must be supplied when state is present") |
| 305 |
elif i["state"] == "present" and not isinstance(i["keys"], dict): |
| 306 |
module.fail_json(msg="keys key should be dict") |
| 307 |
elif not isinstance(i["options"], dict): |
| 308 |
module.fail_json(msg="options key should be dict") |
| 309 |
elif "name" not in i["options"]: |
| 310 |
module.fail_json(msg="The options dict must contain a name field") |
| 311 |
elif i["state"] not in ["present", "absent"]: |
| 312 |
module.fail_json(msg="state must be one of present or absent") |
| 313 |
|
| 314 |
|
| 315 |
# ================ |
| 316 |
# Module execution |
| 317 |
# |
| 318 |
def main(): |
| 319 |
argument_spec = mongodb_common_argument_spec() |
| 320 |
argument_spec.update( |
| 321 |
indexes=dict(type='list', elements='raw', required=True), |
| 322 |
replica_set=dict(type='str'), |
| 323 |
) |
| 324 |
module = AnsibleModule( |
| 325 |
argument_spec=argument_spec, |
| 326 |
supports_check_mode=True, |
| 327 |
required_together=[['login_user', 'login_password']], |
| 328 |
) |
| 329 |
|
| 330 |
if not pymongo_found: |
| 331 |
module.fail_json(msg=missing_required_lib('pymongo'), |
| 332 |
exception=PYMONGO_IMP_ERR) |
| 333 |
|
| 334 |
validate_module(module) |
| 335 |
|
| 336 |
indexes = module.params['indexes'] |
| 337 |
|
| 338 |
client = get_mongodb_client(module) |
| 339 |
client = mongo_auth(module, client) |
| 340 |
|
| 341 |
# Pre flight checks done |
| 342 |
indexes_created = [] |
| 343 |
indexes_dropped = [] |
| 344 |
changed = None |
| 345 |
for i in indexes: |
| 346 |
try: |
| 347 |
idx = index_exists(client, i["database"], i["collection"], i["options"]["name"]) |
| 348 |
except Exception as excep: |
| 349 |
module.fail_json(msg="Could not determine index status: {0}".format(str(excep))) |
| 350 |
if module.check_mode: |
| 351 |
if idx: |
| 352 |
if i["state"] == "present": |
| 353 |
changed = False |
| 354 |
elif i["state"] == "absent": |
| 355 |
indexes_dropped.append("{0}.{1}.{2}".format(i["database"], |
| 356 |
i["collection"], |
| 357 |
i["options"]["name"])) |
| 358 |
changed = True |
| 359 |
else: |
| 360 |
if i["state"] == "present": |
| 361 |
indexes_created.append("{0}.{1}.{2}".format(i["database"], |
| 362 |
i["collection"], |
| 363 |
i["options"]["name"])) |
| 364 |
changed = True |
| 365 |
elif i["state"] == "absent": |
| 366 |
changed = False |
| 367 |
else: |
| 368 |
if idx: |
| 369 |
if i["state"] == "present": |
| 370 |
changed = False |
| 371 |
elif i["state"] == "absent": |
| 372 |
try: |
| 373 |
drop_index(client, i["database"], i["collection"], |
| 374 |
i["options"]["name"]) |
| 375 |
indexes_dropped.append("{0}.{1}.{2}".format(i["database"], |
| 376 |
i["collection"], |
| 377 |
i["options"]["name"])) |
| 378 |
changed = True |
| 379 |
except Exception as excep: |
| 380 |
module.fail_json(msg="Error dropping index: {0}".format(str(excep))) |
| 381 |
|
| 382 |
else: |
| 383 |
if i["state"] == "present": |
| 384 |
try: |
| 385 |
create_index(client=client, |
| 386 |
database=i["database"], |
| 387 |
collection=i["collection"], |
| 388 |
keys=i["keys"], |
| 389 |
options=i["options"]) |
| 390 |
indexes_created.append("{0}.{1}.{2}".format(i["database"], |
| 391 |
i["collection"], |
| 392 |
i["options"]["name"])) |
| 393 |
changed = True |
| 394 |
except Exception as excep: |
| 395 |
module.fail_json(msg="Error creating index: {0}".format(str(excep))) |
| 396 |
elif i["state"] == "absent": |
| 397 |
changed = False |
| 398 |
|
| 399 |
module.exit_json(changed=changed, |
| 400 |
indexes_created=indexes_created, |
| 401 |
indexes_dropped=indexes_dropped) |
| 402 |
|
| 403 |
|
| 404 |
if __name__ == '__main__': |
| 405 |
main() |