def main():
    argument_spec = ecs_argument_spec()
    argument_spec.update(
        dict(db_instance_id=dict(type='str',
                                 aliases=['instance_id'],
                                 required=True),
             name_prefix=dict(type='str')))

    module = AnsibleModule(argument_spec=argument_spec)

    if HAS_FOOTMARK is False:
        module.fail_json("Package 'footmark' required for this module.")

    # Get values of variable
    db_instance_id = module.params['db_instance_id']
    name_prefix = module.params['name_prefix']
    result = []
    try:
        rds = rds_connect(module)
        for account in rds.describe_accounts(db_instance_id=db_instance_id):
            if name_prefix and not account.name.startswith(name_prefix):
                continue
            result.append(account.read())
        module.exit_json(changed=False, rds_accounts=result)
    except Exception as e:
        module.fail_json(
            msg="Unable to describe rds accounts, and got an error: {0}.".
            format(e))
def main():
    argument_spec = ecs_argument_spec()
    argument_spec.update(
        dict(db_instance_id=dict(type='str',
                                 aliases=['instance_id'],
                                 required=True),
             name_prefix=dict(type='str'),
             db_status=dict(type='str', aliases=['status'])))
    module = AnsibleModule(argument_spec=argument_spec)
    rds = rds_connect(module)
    name_prefix = module.params['name_prefix']
    db_status = module.params['db_status']

    if HAS_FOOTMARK is False:
        module.fail_json(msg="Package 'footmark' required for this module.")

    result = []
    try:
        for db in rds.describe_databases(**module.params):
            if name_prefix and not db.dbname.startswith(name_prefix):
                continue
            if db_status and db.dbstatus.lower() != db_status.lower():
                continue
            result.append(db.read())
        module.exit_json(changed=False, databases=result)
    except Exception as e:
        module.fail_json(
            msg="Unable to describe rds database, and got an error: {0}.".
            format(e))
示例#3
0
def main():
    argument_spec = ecs_argument_spec()
    argument_spec.update(
        dict(db_instance_id=dict(type='str',
                                 aliases=['instance_id'],
                                 required=True),
             backup_id=dict(type='str'),
             backup_status=dict(type='str', choice=['Success', 'Failed']),
             backup_mode=dict(type='str', choice=['Automated', 'Manual'])))
    module = AnsibleModule(argument_spec=argument_spec)

    if HAS_FOOTMARK is False:
        module.fail_json(msg="Package 'footmark' required for this module.")

    result = []
    backup_status = module.params['backup_status']
    backup_mode = module.params['backup_mode']

    try:
        rds = rds_connect(module)
        for backup in rds.describe_backups(**module.params):
            if backup_status and backup.status.lower() != backup_status.lower(
            ):
                continue
            if backup_mode and backup.mode.lower() != backup_status.lower():
                continue
            result.append(backup.read())

    except Exception as e:
        module.fail_json(
            msg="Unable to describe rds backup, and got an error: {0}.".format(
                e))

    module.exit_json(changed=True, backups=result)
示例#4
0
def main():
    argument_spec = ecs_argument_spec()
    argument_spec.update(dict(
        name_prefix=dict(type='str'),
        db_instance_ids=dict(type='list', elements='str'),
        tags=dict(type='dict')
    ))
    module = AnsibleModule(argument_spec=argument_spec)
    rds = rds_connect(module)
    name_prefix = module.params['name_prefix']
    db_instance_ids = module.params['db_instance_ids']

    if HAS_FOOTMARK is False:
        module.fail_json(msg="Package 'footmark' required for this module.")

    result = []
    try:
        for rds_instance in rds.describe_db_instances(**module.params):
            if name_prefix and not rds_instance.read()['name'].startswith(name_prefix):
                continue
            if db_instance_ids and rds_instance.read()['id'] not in db_instance_ids:
                continue
            result.append(rds_instance.get().read())
        module.exit_json(changed=False, instances=result)
    except Exception as e:
        module.fail_json(msg="Unable to describe rds db instance, and got an error: {0}.".format(e))
示例#5
0
def main():
    argument_spec = ecs_argument_spec()
    argument_spec.update(
        dict(instance_ids=dict(type='list', aliases=['db_instance_ids']),
             engine=dict(type='str'),
             dbinstance_type=dict(type='str'),
             instance_network_type=dict(type='str'),
             connection_mode=dict(type='str'),
             tags=dict(type='dict')))
    module = AnsibleModule(argument_spec=argument_spec)

    if HAS_FOOTMARK is False:
        module.fail_json(msg="Package 'footmark' required for this module.")

    result = []
    ids = []
    instance_ids = module.params['instance_ids']
    engine = module.params['engine']
    dbinstance_type = module.params['dbinstance_type']
    instance_network_type = module.params['instance_network_type']
    connection_mode = module.params['connection_mode']
    tags = module.params['tags']

    if instance_ids and (not isinstance(instance_ids, list)
                         or len(instance_ids)) < 1:
        module.fail_json(
            msg='instance_ids should be a list of db_instance id, aborting')

    try:
        rds = rds_connect(module)
        # list rds db_instance by instance ids
        if instance_ids:
            instance_id = ",".join(instance_ids)

            for rds_instance in rds.get_rds_instances(
                    instance_id=instance_id,
                    engine=engine,
                    dbinstance_type=dbinstance_type,
                    instance_network_type=instance_network_type,
                    connection_mode=connection_mode,
                    tags=tags):
                result.append(get_info(rds_instance))
                ids.append(rds_instance.dbinstance_id)

        # list all db_instance available in specified region
        else:
            for rds_instance in rds.get_rds_instances():
                result.append(get_info(rds_instance))
                ids.append(rds_instance.dbinstance_id)

    except Exception as e:
        module.fail_json(
            msg="Unable to describe rds db instance, and got an error: {0}.".
            format(e))

    module.exit_json(changed=False,
                     db_instance_ids=ids,
                     rds_db_instances=result,
                     total=len(result))
def main():
    argument_spec = ecs_argument_spec()
    argument_spec.update(
        dict(db_instance_id=dict(type='str', required=True),
             account_names=dict(type='list', aliases=['names'])))

    module = AnsibleModule(argument_spec=argument_spec)

    if HAS_FOOTMARK is False:
        module.fail_json("Package 'footmark' required for this module.")

    # Get values of variable
    db_instance_id = module.params['db_instance_id']
    names = module.params['account_names']
    result = []

    try:
        rds = rds_connect(module)

        if names and (not isinstance(names, list) or len(names)) < 1:
            module.fail_json(
                msg='account_name should be a list of account name, aborting')

        # fetch rds accounts by name
        if names:
            for name in names:
                rds_accounts = rds.list_account(db_instance_id=db_instance_id,
                                                account_name=name)
                if rds_accounts and len(rds_accounts) == 1:
                    result.append(get_info(rds_accounts[0]))

        # fetch all rds accounts
        else:
            names = []
            for account in rds.list_account(db_instance_id=db_instance_id):
                names.append(account.account_name)
                result.append(get_info(account))

        module.exit_json(changed=False,
                         account_names=names,
                         rds_accounts=result,
                         total=len(result))
    except Exception as e:
        module.fail_json(
            msg="Unable to describe rds accounts, and got an error: {0}.".
            format(e))
示例#7
0
def main():
    argument_spec = ecs_argument_spec()
    argument_spec.update(
        dict(state=dict(default='present', choices=['present', 'absent']),
             db_instance_id=dict(type='str',
                                 aliases=['instance_id'],
                                 required=True),
             db_name=dict(type='list', elements='str'),
             backup_id=dict(type='list', elements='str'),
             backup_method=dict(type='str', default='Physical'),
             backup_strategy=dict(type='str'),
             backup_type=dict(type='str', default='Auto')))

    module = AnsibleModule(argument_spec=argument_spec)
    rds = rds_connect(module)

    if HAS_FOOTMARK is False:
        module.fail_json(msg="Footmark required for this module")

    # Get values of variable
    state = module.params['state']
    backup_id = module.params['backup_id']
    db_name = module.params['db_name']
    if backup_id:
        module.params['backup_id'] = ','.join(backup_id)
    if db_name:
        module.params['db_name'] = ','.join(db_name)

    if state == 'absent':
        try:
            changed = rds.delete_backup(**module.params)
            module.exit_json(changed=changed, backup={})
        except Exception as e:
            module.fail_json(
                msg=str("Unable to delete backup error:{0}".format(e)))

    if state == 'present':
        try:
            create_backup = rds.create_backup(**module.params)
            module.exit_json(changed=True, backup=create_backup.read())
        except Exception as e:
            module.fail_json(
                msg=str("Unable to create backup error:{0}".format(e)))
示例#8
0
def main():
    argument_spec = ecs_argument_spec()
    argument_spec.update(dict(
        state=dict(default='present', choices=['present', 'absent']),
        db_name=dict(type='str'),
        db_instance_id=dict(type='str', required=True),
        account_name=dict(type='str', aliases=['name'], required=True),
        account_password=dict(type='str', aliases=['password']),
        account_privilege=dict(aliases=['privilege'], choices=['ReadOnly', 'ReadWrite']),
        description=dict(type='str'),
        account_type=dict(default='Normal', type='str', choices=['Normal', 'Super']),
    ))

    module = AnsibleModule(argument_spec=argument_spec)
    rds = rds_connect(module)

    if HAS_FOOTMARK is False:
        module.fail_json("Footmark required for this module")

    # Get values of variable
    state = module.params['state']
    db_instance_id = module.params['db_instance_id']
    account_name = module.params['account_name']
    account_password = module.params['account_password']
    account_privilege = module.params['account_privilege']
    description = module.params['description']
    account_type = module.params['account_type']
    db_name = module.params['db_name']

    account_list = []
    current_account = None
    changed = False

    try:
        current_account_list = rds.list_account(db_instance_id, account_name)
        if len(current_account_list) == 1:
            current_account = current_account_list[0]
    except Exception as e:
        module.fail_json(msg=str("Unable to describe accounts, error:{0}".format(e)))

    if state == "absent":
        if current_account:
            if db_name:
                try:
                    changed = current_account.revoke_privilege(db_instance_id, db_name)
                    current_account = rds.list_account(db_instance_id, account_name)[0]
                    module.exit_json(changed=True, account_name=account_name, account=get_info(current_account))
                except Exception as e:
                    module.fail_json(msg=str("Unable to revoke privilege error:{0}".format(e)))
            try:
                changed = current_account.delete(db_instance_id)
                module.exit_json(changed=True, account_name=account_name, account=get_info(current_account))
            except Exception as e:
                module.fail_json(msg=str("Unable to delete account error:{0}".format(e)))
        module.fail_json(msg="There is no account to revoke database privilege or delete. Please specify an account using 'account_name', and try again.")
    if account_password and current_account:
        try:
            changed = current_account.reset(db_instance_id, account_password)
        except Exception as e:
            module.fail_json(msg=str("Unable to reset account password error:{0}".format(e)))
    if not current_account:
        try:
            current_account = rds.create_account(db_instance_id, account_name, account_password, description, account_type)
        except Exception as e:
            module.fail_json(msg=str("Unable to create account error:{0}".format(e)))
    if description and description != current_account.account_description:
        try:
            changed = current_account.modify_description(db_instance_id, description)
            current_account.account_description = description
        except Exception as e:
            module.fail_json(msg=str("Unable to modify account description error:{0}".format(e)))
    if db_name:
        if account_privilege:
            try:
                changed = current_account.grant_privilege(db_instance_id, db_name, account_privilege)
                current_account = current_account_list[0]
            except Exception as e:
                module.fail_json(msg=str("Unable to grant privilege error:{0}".format(e)))
        else:
            module.fail_json(msg="grant privilege failed. Please check your account_privilege and try again.")
    module.exit_json(changed=changed, account_name=account_name, account=get_info(current_account))
示例#9
0
def main():
    argument_spec = ecs_argument_spec()
    argument_spec.update(dict(
        state=dict(default="present", choices=["present", "absent", "restart"]),
        zone_id=dict(type='str', aliases=['alicloud_zone']),
        engine=dict(type='str', choices=['MySQL', 'SQLServer', 'PostgreSQL', 'PPAS', 'MariaDB']),
        engine_version=dict(type='str'),
        db_instance_net_type=dict(type='str', choices=["Internet", "Intranet"], aliases=['instance_net_type']),
        db_instance_name=dict(type='str', aliases=['description', 'name']),
        db_instance_id=dict(type='str', aliases=['id']),
        security_ip_list=dict(type='str', aliases=['security_ips']),
        pay_type=dict(type='str', choices=["PostPaid", "PrePaid"]),
        period=dict(type='int', choices=[1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 24, 36], default=1),
        connection_mode=dict(type='str', choices=["Standard", "Safe"]),
        vswitch_id=dict(type='str'),
        private_ip_address=dict(type='str'),
        tags=dict(type='dict'),
        purge_tags=dict(type='bool', default=False),
        auto_pay=dict(type='bool', aliases=['auto_renew']),
        connection_string_prefix=dict(type='str'),
        port=dict(type='str'),
        current_connection_string=dict(type='str'),
        db_instance_class=dict(type='str', aliases=['instance_class']),
        db_instance_storage=dict(type='int', aliases=['instance_storage'])
    ))
    modules = AnsibleModule(argument_spec=argument_spec)

    if HAS_FOOTMARK is False:
        modules.fail_json(msg="Package 'footmark' required for the module ali_rds_instance.")

    rds = rds_connect(modules)
    vpc = vpc_connect(modules)

    state = modules.params['state']
    vswitch_id = modules.params['vswitch_id']
    connection_string_prefix = modules.params['connection_string_prefix']
    port = modules.params['port']
    tags = modules.params['tags']
    current_connection_string = modules.params['current_connection_string']
    db_instance_description = modules.params['db_instance_name']
    modules.params['db_instance_description'] = db_instance_description
    db_instance_class = modules.params['db_instance_class']
    db_instance_storage = modules.params['db_instance_storage']
    db_instance_id = modules.params['db_instance_id']
    pay_type = modules.params['pay_type']
    used_time = modules.params['period']
    modules.params['period'] = 'Month'
    modules.params['used_time'] = str(used_time)

    if used_time > 9:
        modules.params['period'] = 'Year'
        if used_time == 12:
            modules.params['used_time'] = '1'
        elif used_time == 24:
            modules.params['used_time'] = '2'
        else:
            modules.params['used_time'] = '3'
    if pay_type:
        modules.params['pay_type'] = pay_type.capitalize()

    current_instance = None
    changed = False

    if vswitch_id:
        modules.params['instance_network_type'] = 'VPC'
        try:
            vswitch_obj = vpc.describe_vswitch_attributes(vswitch_id=vswitch_id)
            if vswitch_obj:
                modules.params['vpc_id'] = vswitch_obj.vpc_id
        except Exception as e:
            modules.fail_json(msg=str("Unable to get vswitch, error:{0}".format(e)))

    try:
        current_instance = get_instance(db_instance_id, db_instance_description, modules, rds)
    except Exception as e:
        modules.fail_json(msg=str("Unable to describe instance, error:{0}".format(e)))

    if state == 'absent':
        if current_instance:
            if current_connection_string:
                try:
                    changed = rds.release_instance_public_connection(current_connection_string=current_connection_string, db_instance_id=current_instance.id)
                    modules.exit_json(changed=changed, instances=current_instance.get().read())
                except Exception as e:
                    modules.fail_json(msg=str("Unable to release public connection string error: {0}".format(e)))
            try:
                current_instance.delete()
                modules.exit_json(changed=True, instances={})
            except Exception as e:
                modules.fail_json(msg=str("Unable to release instance error: {0}".format(e)))
        modules.fail_json(msg=str("Unable to operate your instance, please check your instance_id and try again!"))

    if state == 'restart':
        if current_instance:
            try:
                changed = current_instance.restart()
                modules.exit_json(changed=changed, instances=current_instance.get().read())
            except Exception as e:
                modules.fail_json(msg=str("Unable to restart instance error: {0}".format(e)))
        modules.fail_json(msg=str("Unable to restart your instance, please check your instance_id and try again!"))

    if not current_instance:
        try:
            modules.params['client_token'] = "Ansible-Alicloud-%s-%s" % (hash(str(modules.params)), str(time.time()))
            current_instance = rds.create_db_instance(**modules.params)
            modules.exit_json(changed=True, instances=current_instance.get().read())
        except Exception as e:
            modules.fail_json(msg=str("Unable to create rds instance error: {0}".format(e)))

    if connection_string_prefix and port:
        if current_connection_string:
            try:
                changed = current_instance.modify_db_instance_connection_string(current_connection_string=current_connection_string, connection_string_prefix=connection_string_prefix, port=port)
                modules.exit_json(changed=changed, instances=current_instance.get().read())
            except Exception as e:
                modules.fail_json(msg=str("Unable to modify current string error: {0}".format(e)))
        else:
            try:
                changed = current_instance.allocate_public_connection_string(connection_string_prefix=connection_string_prefix, port=port)
                modules.exit_json(changed=changed, instances=current_instance.get().read())
            except Exception as e:
                modules.fail_json(msg=str("Unable to allocate public connection error: {0}".format(e)))

    if db_instance_class or db_instance_storage:
        try:
            changed = current_instance.modify_instance_spec(db_instance_class=db_instance_class, db_instance_storage=db_instance_storage)
        except Exception as e:
            modules.fail_json(msg=str("Unable to modify instance spec: {0}".format(e)))

    if modules.params['purge_tags']:
        if not tags:
            tags = current_instance.tags
        try:
            if current_instance.remove_tags(tags):
                changed = True
            modules.exit_json(changed=changed, instances=current_instance.get().read())
        except Exception as e:
            modules.fail_json(msg="{0}".format(e))

    if tags:
        try:
            if current_instance.add_tags(tags):
                changed = True
        except Exception as e:
            modules.fail_json(msg="{0}".format(e))

    modules.exit_json(changed=changed, instances=current_instance.get().read())
示例#10
0
def main():
    argument_spec = ecs_argument_spec()
    argument_spec.update(
        dict(state=dict(default="present",
                        choices=["present", "absent", "restart"],
                        alias=['status']),
             alicloud_zone=dict(type='str', aliases=['zone_id', 'zone']),
             engine=dict(type='str',
                         choices=["MySQL", "SQLServer", "PostgreSQL", "PPAS"]),
             engine_version=dict(type='str'),
             instance_net_type=dict(type='str',
                                    choices=["Internet", "Intranet"],
                                    aliases=['db_instance_net_type']),
             description=dict(type='str', aliases=['db_instance_description']),
             security_ips=dict(type='str'),
             instance_charge_type=dict(type='str',
                                       choices=["Postpaid", "Prepaid"]),
             period=dict(type='int', choices=range(1, 10).extend([12, 24,
                                                                  36])),
             connection_mode=dict(type='str', choices=["Performance",
                                                       "Safty"]),
             vpc_id=dict(type='str'),
             vswitch_id=dict(type='str'),
             private_ip_address=dict(type='str'),
             instance_id=dict(type='str', aliases=['db_instance_id']),
             tags=dict(type='str', aliases=['instance_tags']),
             page_size=dict(type='int', default=30, choices=[30, 50, 100]),
             page_number=dict(type='int', default=1),
             auto_renew_period=dict(type='int', choices=[1, 2, 3, 6, 12]),
             auto_renew=dict(type='bool'),
             public_connection_string_prefix=dict(type='str'),
             private_connection_string_prefix=dict(type='str'),
             dest_connection_string_prefix=dict(type='str'),
             dest_port=dict(type='str'),
             public_port=dict(type='str'),
             private_port=dict(type='int', choices=range(3001, 4000)),
             current_connection_string=dict(type='str'),
             instance_type=dict(type='str', aliases=['db_instance_class']),
             instance_storage=dict(type='int',
                                   aliases=['db_instance_storage'])))
    modules = AnsibleModule(argument_spec=argument_spec)

    if HAS_FOOTMARK is False:
        modules.fail_json(
            msg=
            "Package 'footmark' required for the module alicloud_rds_instance."
        )

    rds = rds_connect(modules)
    vpc = vpc_connect(modules)

    state = modules.params['state']
    alicloud_zone = modules.params['alicloud_zone']
    engine = modules.params['engine']
    engine_version = modules.params['engine_version']
    instance_net_type = modules.params['instance_net_type']
    description = modules.params['description']
    security_ips = modules.params['security_ips']
    instance_charge_type = modules.params['instance_charge_type']
    period = modules.params['period']
    connection_mode = modules.params['connection_mode']
    vswitch_id = modules.params['vswitch_id']
    private_ip_address = modules.params['private_ip_address']
    instance_id = modules.params['instance_id']
    tags = modules.params['tags']
    page_size = modules.params['page_size']
    page_number = modules.params['page_number']
    auto_renew_period = modules.params['auto_renew_period']
    auto_renew = modules.params['auto_renew']
    public_connection_string_prefix = modules.params[
        'public_connection_string_prefix']
    private_connection_string_prefix = modules.params[
        'private_connection_string_prefix']
    public_port = modules.params['public_port']
    private_port = modules.params['private_port']
    current_connection_string = modules.params['current_connection_string']
    dest_connection_string_prefix = modules.params[
        'dest_connection_string_prefix']
    dest_port = modules.params['dest_port']
    instance_type = modules.params['instance_type']
    instance_storage = modules.params['instance_storage']

    vpc_id = None
    instance_network_type = 'Classic'
    current_instance = None
    changed = False
    if vswitch_id:
        instance_network_type = 'VPC'
        try:
            vswitch_obj = vpc.get_vswitch_attribute(vswitch_id)
            if vswitch_obj:
                vpc_id = vswitch_obj.vpc_id
        except Exception as e:
            modules.fail_json(
                msg=str("Unable to get vswitch, error:{0}".format(e)))
    if instance_id:
        try:
            current_instance = rds.describe_db_instance_attribute(instance_id)
        except Exception as e:
            modules.fail_json(
                msg=str("Unable to describe instance, error:{0}".format(e)))

    if state == 'absent':
        if current_instance:
            if current_connection_string:
                try:
                    changed = current_instance.release_public_connection_string(
                        current_connection_string)
                    modules.exit_json(changed=changed,
                                      instance=get_info(current_instance))
                except Exception as e:
                    modules.fail_json(msg=str(
                        "Unable to release public connection string error: {0}"
                        .format(e)))
            try:
                changed = current_instance.terminate()
                modules.exit_json(changed=changed,
                                  instance=get_info(current_instance))
            except Exception as e:
                modules.fail_json(
                    msg=str("Unable to release instance error: {0}".format(e)))
        modules.fail_json(msg=str(
            "Unable to operate your instance, please check your instance_id and try again!"
        ))
    if state == 'restart':
        if current_instance:
            try:
                changed = current_instance.restart()
                modules.exit_json(changed=changed,
                                  instance=get_info(current_instance))
            except Exception as e:
                modules.fail_json(
                    msg=str("Unable to restart instance error: {0}".format(e)))
        modules.fail_json(msg=str(
            "Unable to restart your instance, please check your instance_id and try again!"
        ))
    if not current_instance:
        try:
            client_token = "Ansible-Alicloud-%s-%s" % (hash(str(
                module.params)), str(time.time()))
            current_instance = rds.create_rds_instance(
                engine=engine,
                engine_version=engine_version,
                db_instance_class=instance_type,
                db_instance_storage=instance_storage,
                db_instance_net_type=instance_net_type,
                security_ip_list=security_ips,
                pay_type=instance_charge_type,
                client_token=client_token,
                instance_network_type=instance_network_type,
                period='Month',
                used_time=period,
                alicloud_zone=alicloud_zone,
                db_instance_description=description,
                connection_mode=connection_mode,
                vpc_id=vpc_id,
                vswitch_id=vswitch_id,
                private_ip_address=private_ip_address)
            instance_id = current_instance.dbinstance_id
        except Exception as e:
            modules.fail_json(
                msg=str("Unable to create rds instance error: {0}".format(e)))
    if auto_renew:
        try:
            changed = current_instance.modify_auto_renewal_attribute(
                duration=auto_renew_period, auto_renew=auto_renew)
        except Exception as e:
            modules.fail_json(msg=str(
                "Unable to modify rds instance auto renewal attribute error: {0}"
                .format(e)))
    if public_connection_string_prefix and public_port:
        try:
            changed = current_instance.allocate_public_connection_string(
                public_connection_string_prefix, public_port)
        except Exception as e:
            modules.fail_json(msg=str(
                "Unable to allocate public connection error: {0}".format(e)))
    if private_connection_string_prefix:
        try:
            changed = current_instance.allocate_private_connection_string(
                private_connection_string_prefix, private_port)
        except Exception as e:
            modules.fail_json(msg=str(
                "Unable to allocate private connection string error: {0}".
                format(e)))
    if current_connection_string:
        try:
            changed = current_instance.modify_connection_string(
                current_connection_string, dest_connection_string_prefix,
                dest_port)
        except Exception as e:
            modules.fail_json(
                msg=str("Unable to modify current connection string error: {0}"
                        .format(e)))
    # get newest instance
    try:
        current_instance = rds.describe_db_instance_attribute(instance_id)
    except Exception as e:
        modules.fail_json(
            msg=str("Unable to describe instance error: {0}".format(e)))
    modules.exit_json(changed=changed, instance=get_info(current_instance))
def main():
    argument_spec = ecs_argument_spec()
    argument_spec.update(
        dict(state=dict(default="present", choices=["present", "absent"]),
             db_instance_id=dict(type='str',
                                 aliases=['instance_id'],
                                 required=True),
             db_name=dict(type='str', aliases=['name'], required=True),
             character_set_name=dict(type='str', aliases=['character']),
             db_description=dict(type='str', aliases=['description']),
             target_db_instance_id=dict(type='str',
                                        aliases=['target_instance_id']),
             target_db_name=dict(type='str'),
             backup_id=dict(type='str'),
             restore_time=dict(type='str'),
             sync_user_privilege=dict(type='bool', default=False)))
    modules = AnsibleModule(argument_spec=argument_spec)

    if HAS_FOOTMARK is False:
        modules.fail_json(msg="Package 'footmark' required for this module")

    rds = rds_connect(modules)
    state = modules.params['state']
    db_description = modules.params['db_description']
    target_db_instance_id = modules.params['target_db_instance_id']
    target_db_name = modules.params['target_db_name']
    db_name = modules.params['db_name']
    sync_user_privilege = modules.params['sync_user_privilege']
    modules.params['sync_user_privilege'] = 'NO'
    if sync_user_privilege:
        modules.params['sync_user_privilege'] = 'YES'
    db = ''
    try:
        db = database_exists(modules, rds)
    except Exception as e:
        modules.fail_json(
            msg=str("Unable to describe database, error:{0}".format(e)))

    if state == 'absent':
        if not db:
            modules.exit_json(changed=False, database={})
        try:
            db.delete()
            modules.exit_json(changed=True, database={})
        except Exception as e:
            modules.fail_json(
                msg=str("Unable to delete database error: {0}".format(e)))

    if not db:
        try:
            modules.params['client_token'] = "Ansible-Alicloud-%s-%s" % (hash(
                str(modules.params)), str(time.time()))
            db = rds.create_database(**modules.params)
            modules.exit_json(changed=True, database=db.read())
        except Exception as e:
            modules.fail_json(
                msg=str("Unable to create database error: {0}".format(e)))

    if db_description:
        try:
            res = db.modify_db_description(description=db_description)
            modules.exit_json(changed=res, database=db.read())
        except Exception as e:
            modules.fail_json(msg=str(
                "Unable to modify db description error: {0}".format(e)))

    if target_db_instance_id and target_db_name:
        try:
            modules.params['db_names'] = str({db_name: target_db_name})
            res = db.copy_database_between_instances(**modules.params)
            modules.exit_json(changed=res, database=db.read())
        except Exception as e:
            modules.fail_json(
                msg=str("Unable to copy db instance id error: {0}".format(e)))