-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve (rds): modify rds instance and database
- Loading branch information
Showing
10 changed files
with
1,249 additions
and
572 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
#!/usr/bin/python | ||
# Copyright (c) 2017-present Alibaba Group Holding Limited. He Guimin <[email protected]> | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
# | ||
# This file is part of Ansible | ||
# | ||
# Ansible is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# Ansible is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with Ansible. If not, see http://www.gnu.org/licenses/. | ||
|
||
|
||
__metaclass__ = type | ||
|
||
ANSIBLE_METADATA = {'metadata_version': '1.1', | ||
'status': ['preview'], | ||
'supported_by': 'community'} | ||
|
||
DOCUMENTATION = ''' | ||
--- | ||
module: ali_rds_database | ||
version_added: "2.9.2" | ||
short_description: Create, delete or copy an database in Alibaba Cloud RDS. | ||
description: | ||
- Create, delete, copy or modify description for database in RDS. | ||
options: | ||
state: | ||
description: | ||
- The state of the database after operating. | ||
default: 'present' | ||
choices: ['present', 'absent'] | ||
db_instance_id: | ||
description: | ||
- rds instance id. Required when C(state=present). this and db_name will specify unique database. | ||
aliases: ['instance_id'] | ||
db_name: | ||
description: | ||
- database name. It must be 2 to 64 characters in length.It must start with a lowercase letter and end with a | ||
lowercase letter or digit. It can contain lowercase letters, digits, underscores (_), and hyphens (-). | ||
The database name must be unique whithin the instance. | ||
For more information about invalid characters, see Forbidden keywords table U(https://www.alibabacloud.com/help/doc-detail/26317.htm). | ||
Required when C(state=present). | ||
aliases: ['name'] | ||
character_set_name: | ||
description: | ||
- database character name. MySQL or MariaDB (utf8 | gbk | latin1 | utf8mb4). | ||
SQL Server (Chinese_PRC_CI_AS | Chinese_PRC_CS_AS | SQL_Latin1_General_CP1_CI_AS | SQL_Latin1_General_CP1_CS_AS | Chinese_PRC_BIN). | ||
see more U(https://www.alibabacloud.com/help/doc-detail/26258.htm) | ||
Required when C(state=present). | ||
aliases: ['character'] | ||
db_description: | ||
description: | ||
- The description of the database. It must be 2 to 256 characters in length. | ||
It can contain letters, digits, underscores (_), and hyphens (-), and must start with a letter. | ||
aliases: ['description'] | ||
target_db_instance_id: | ||
description: | ||
- The ID of the destination instance, which must differ from the ID of the source instance. | ||
aliases: ['target_instance_id'] | ||
db_names: | ||
description: | ||
- when copy databases between instances, format {"db_name": "target instance database name"}. | ||
aliases: ['names'] | ||
author: | ||
- "li Xue" | ||
requirements: | ||
- "python >= 3.6" | ||
- "footmark >= 1.16.0" | ||
extends_documentation_fragment: | ||
- alicloud | ||
''' | ||
|
||
EXAMPLES = ''' | ||
# Note: These examples do not set authentication details, see the Alibaba Cloud Guide for details. | ||
- name: Create Database | ||
ali_rds_database: | ||
db_instance_id: '{{ db_instance_id }}' | ||
db_name: ansible_test | ||
character_set_name: utf8 | ||
db_description: create for ansible test | ||
state: present | ||
- name: Changed. Modify db description. | ||
ali_rds_database: | ||
db_instance_id: '{{ db_instance_id }}' | ||
db_name: ansible_test | ||
db_description: modify db description test | ||
- name: Changed. Copy Database Between Instances | ||
ali_rds_database: | ||
db_instance_id: '{{ db_instance_id }}' | ||
target_db_instance_id: '{{ db_instance_id2 }}' | ||
db_names: '{ansible_test: ansible_test2}' | ||
- name: Changed. Deleting database | ||
ali_rds_database: | ||
state: absent | ||
db_instance_id: '{{ db_instance_id }}' | ||
db_name: ansible_test | ||
''' | ||
|
||
RETURN = ''' | ||
database: | ||
description: Describe the info after operating database. | ||
returned: always | ||
type: complex | ||
contains: | ||
character_set_name: | ||
description: The character of database. | ||
returned: always | ||
type: string | ||
sample: utf8 | ||
dbdescription: | ||
description: The description of database. | ||
returned: always | ||
type: string | ||
sample: test for ansible | ||
dbinstance_id: | ||
description: The id of rds instance. | ||
returned: always | ||
type: string | ||
sample: rm-uf6wjk5xxxxxxx | ||
dbname: | ||
description: The name of database. | ||
returned: always | ||
type: string | ||
sample: ansible_test | ||
dbstatus: | ||
description: The status of database. | ||
returned: always | ||
type: string | ||
sample: Creating | ||
description: | ||
description: alias of 'dbdescription'. | ||
returned: always | ||
type: string | ||
sample: test for ansible | ||
engine: | ||
description: The engine of database. | ||
returned: always | ||
type: string | ||
sample: MySQL | ||
id: | ||
description: alias of 'dbinstance_id'. | ||
returned: always | ||
type: string | ||
sample: rm-uf6wjk5xxxxxxx | ||
name: | ||
description: alias of 'dbname'. | ||
returned: always | ||
type: string | ||
sample: ansible_test | ||
status: | ||
description: alias of 'dbstatus'. | ||
returned: always | ||
type: string | ||
sample: Creating | ||
''' | ||
|
||
import time | ||
from ansible.module_utils.basic import AnsibleModule | ||
from ansible.module_utils.alicloud_ecs import ecs_argument_spec, rds_connect, vpc_connect | ||
|
||
HAS_FOOTMARK = False | ||
|
||
try: | ||
from footmark.exception import ECSResponseError | ||
HAS_FOOTMARK = True | ||
except ImportError: | ||
HAS_FOOTMARK = False | ||
|
||
|
||
def database_exists(modules, rds): | ||
name = modules.params['db_name'] | ||
if modules.params['db_names']: | ||
for key, value in eval(modules.params['db_names']).items(): | ||
name = key | ||
match = '' | ||
for db in rds.describe_databases(db_instance_id=modules.params['db_instance_id']): | ||
if db.name == name: | ||
match = db | ||
return match | ||
|
||
|
||
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']), | ||
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']), | ||
db_names=dict(type='str', aliases=['names']) | ||
)) | ||
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'] | ||
|
||
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: | ||
try: | ||
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 target db instance id error: {0}".format(e))) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() | ||
|
Oops, something went wrong.