def main()

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


def main():
    argument_spec = common_argument_spec()
    argument_spec.update(dict(
        state=dict(default='present', choices=['present', 'absent']),
        db_names=dict(type='list', elements='str'),
        db_instance_id=dict(type='str', aliases=['instance_id'], 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', 'DDLOnly', 'DMLOnly', 'DBOwner']),
        account_description=dict(type='str', aliases=['description']),
        account_type=dict(default='Normal', type='str', choices=['Normal', 'Super'], aliases=['type'])
    ))

    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']
    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']
    account_description = module.params['account_description']
    db_names = module.params['db_names']

    current_account = None
    changed = False

    try:
        current_account_list = rds.describe_accounts(db_instance_id=db_instance_id, account_name=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_names:
                try:
                    changed = current_account.revoke_privilege(db_names)
                    module.exit_json(changed=True, account=current_account.get().read())
                except Exception as e:
                    module.fail_json(msg=str("Unable to revoke privilege error:{0}".format(e)))
            try:
                changed = current_account.delete()
                module.exit_json(changed=True, 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(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(**module.params)
            changed = False
        except Exception as e:
            module.fail_json(msg=str("Unable to create account error:{0}".format(e)))

    if account_description and account_description != current_account.description:
        try:
            changed = current_account.modify_description(description=account_description)
        except Exception as e:
            module.fail_json(msg=str("Unable to modify account description error:{0}".format(e)))

    if db_names and account_privilege:
        try:
            changed = current_account.grant_privilege(db_names, account_privilege)
        except Exception as e:
            module.fail_json(msg=str("Unable to grant privilege error:{0}".format(e)))
    module.exit_json(changed=changed, account=current_account.read())