in lib/ansible/modules/cloud/alicloud/_alicloud_rds_account.py [0: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))