def main():
    # add our own arguments
    argument_spec = dict(
        state=dict(default="present", choices=["absent", "present"]),
        apiUsername=dict(required=True),
        apiPassword=dict(required=True, no_log=True),
        groupId=dict(required=True),
        databaseName=dict(default="admin", choices=["admin", "$external"]),
        username=dict(required=True),
        password=dict(required=True, no_log=True),
        roles=dict(
            required=True,
            type="list",
            elements="dict",
            options=dict(
                databaseName=dict(required=True),
                roleName=dict(required=True),
            ),
        ),
    )

    # Define the main module
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)

    data = {
        "databaseName": module.params["databaseName"],
        "username": module.params["username"],
        "password": module.params["password"],
        "roles": module.params["roles"],
    }

    try:
        atlas = AtlasAPIObject(
            module=module,
            path="/databaseUsers",
            object_name="username",
            groupId=module.params["groupId"],
            data=data,
        )
    except Exception as e:
        module.fail_json(
            msg="unable to connect to Atlas API. Exception message: %s" % e)

    changed, diff = atlas.update(module.params["state"])
    module.exit_json(
        changed=changed,
        data=atlas.data,
        diff=diff,
    )
def main():
    # add our own arguments
    argument_spec = dict(
        state=dict(default="present", choices=["absent", "present"]),
        apiUsername=dict(required=True),
        apiPassword=dict(required=True, no_log=True),
        groupId=dict(required=True),
        cidrBlock=dict(required=True),
        comment=dict(default="created by Ansible"),
    )

    # Define the main module
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)

    data = {
        "cidrBlock": module.params["cidrBlock"],
        "comment": module.params["comment"],
    }

    try:
        atlas = AtlasAPIObject(
            module=module,
            path="/whitelist",
            object_name="cidrBlock",
            groupId=module.params["groupId"],
            data=data,
            data_is_array=True,
        )
    except Exception as e:
        module.fail_json(
            msg="unable to connect to Atlas API. Exception message: %s" % e)

    changed, diff = atlas.update(module.params["state"])
    module.exit_json(
        changed=changed,
        data=atlas.data,
        diff=diff,
    )
Exemple #3
0
def main():
    # add our own arguments
    argument_spec = dict(
        state=dict(default="present", choices=["absent", "present"]),
        apiUsername=dict(required=True),
        apiPassword=dict(required=True, no_log=True),
        groupId=dict(required=True),
        name=dict(required=True),
        mongoDBMajorVersion=dict(choices=["3.6", "4.0", "4.2", "4.4"]),
        clusterType=dict(
            default="REPLICASET", choices=["REPLICASET", "SHARDED"]
        ),
        replicationFactor=dict(default=3, type="int", choices=[3, 5, 7]),
        autoScaling=dict(
            type="dict",
            options=dict(
                diskGBEnabled=dict(type="bool"),
            ),
        ),
        providerSettings=dict(
            type="dict",
            required=True,
            options=dict(
                providerName=dict(required=True),
                regionName=dict(required=True),
                instanceSizeName=dict(required=True),
            ),
        ),
        diskSizeGB=dict(type="int"),
        providerBackupEnabled=dict(type="bool"),
        pitEnabled=dict(type="bool"),
    )

    # Define the main module
    module = AnsibleModule(
        argument_spec=argument_spec, supports_check_mode=True
    )

    data = {
        "name": module.params["name"],
        "clusterType": module.params["clusterType"],
        "replicationFactor": module.params["replicationFactor"],
        "providerSettings": module.params["providerSettings"],
    }

    # handle optional options
    optional_vars = [
        "mongoDBMajorVersion",
        "autoScaling",
        "diskSizeGB",
        "providerBackupEnabled",
        "pitEnabled",
    ]

    for var in optional_vars:
        if var in module.params:
            data.update({var: module.params[var]})

    try:
        atlas = AtlasAPIObject(
            module=module,
            path="/clusters",
            object_name="name",
            groupId=module.params["groupId"],
            data=data,
        )
    except Exception as e:
        module.fail_json(
            msg="unable to connect to Atlas API. Exception message: %s" % e
        )

    changed, diff = atlas.update(module.params["state"])
    module.exit_json(
        changed=changed,
        data=atlas.data,
        diff=diff,
    )