def main()

in src/ansible_collections/alibaba/apsarastack/plugins/modules/ali_rds_database.py [0:0]


def main():
    argument_spec = common_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-Apsarastack-%s-%s" % (hash(str(modules.params)), str(time.time()))
            db = rds.create_database(**modules.params)
            modules.exit_json(changed=False, 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)))