From 70b8085a895971dc786beecd1ec0d25a75e74326 Mon Sep 17 00:00:00 2001 From: lx <1120722567@qq.com> Date: Wed, 20 Nov 2019 17:48:38 +0800 Subject: [PATCH] improve (dns): support domain group manage and modify ali_dns_domain --- .../modules/cloud/alicloud/ali_dns_domain.py | 22 ++- .../modules/cloud/alicloud/ali_dns_group.py | 171 ++++++++++++++++++ .../cloud/alicloud/ali_dns_group_info.py | 156 ++++++++++++++++ tests/ali_dns_domain_test.yml | 20 ++ tests/ali_dns_group_info_test.yml | 26 +++ tests/ali_dns_group_test.yml | 23 +++ tests/group_vars/all | 4 + tests/roles/dns_group/tasks/main.yml | 10 + 8 files changed, 428 insertions(+), 4 deletions(-) create mode 100644 lib/ansible/modules/cloud/alicloud/ali_dns_group.py create mode 100644 lib/ansible/modules/cloud/alicloud/ali_dns_group_info.py create mode 100644 tests/ali_dns_group_info_test.yml create mode 100644 tests/ali_dns_group_test.yml create mode 100644 tests/roles/dns_group/tasks/main.yml diff --git a/lib/ansible/modules/cloud/alicloud/ali_dns_domain.py b/lib/ansible/modules/cloud/alicloud/ali_dns_domain.py index ce17d9a7..d0ca5f28 100644 --- a/lib/ansible/modules/cloud/alicloud/ali_dns_domain.py +++ b/lib/ansible/modules/cloud/alicloud/ali_dns_domain.py @@ -31,16 +31,16 @@ short_description: Configure Alibaba Cloud DNS (DNS) description: - Create, Delete Alicloud cloud DNS(DNS). - It supports updating DNS remark. + It supports updating DNS remark and change domain group. options: domain_name: description: - The name to give your DNS. required: True aliases: ['name'] - group_id: + group_name: description: - - Domain name group, which is, by default, the GroupId of the “Default Group”. + - Specify name of group, when change domain group. lang: description: - The language which you choose @@ -80,6 +80,11 @@ domain_name: '{{ domain_name }}' remark: 'new--{{ remark }}' +- name: Changed. change domain group. + ali_dns_domain: + domain_name: '{{ domain_name }}' + group_name: '{{ group_name }}' + - name: Changed. Deleting dns ali_dns_domain: domain_name: '{{ domain_name }}' @@ -191,7 +196,7 @@ def main(): argument_spec = ecs_argument_spec() argument_spec.update(dict( domain_name=dict(type='str', aliases=['name'], required=True), - group_id=dict(type='str'), + group_name=dict(type='str'), lang=dict(type='str'), resource_group_id=dict(type='str'), remark=dict(type='str'), @@ -210,6 +215,7 @@ def main(): state = module.params['state'] domain_name = module.params['domain_name'] remark = module.params['remark'] + group_name = module.params['group_name'] changed = False # Check if VPC exists @@ -232,6 +238,14 @@ def main(): except DNSResponseError as e: module.fail_json(msg='Unable to create dns, error: {0}'.format(e)) + if domain_name and group_name: + try: + res = dns.change_domain_group(group_name=group_name, domain_name=domain_name) + if res: + changed = True + except DNSResponseError as e: + module.fail_json(msg='Unable to change domain group, error: {0}'.format(e)) + if remark: try: res = dns.modify_remark(remark=remark) diff --git a/lib/ansible/modules/cloud/alicloud/ali_dns_group.py b/lib/ansible/modules/cloud/alicloud/ali_dns_group.py new file mode 100644 index 00000000..dbb55998 --- /dev/null +++ b/lib/ansible/modules/cloud/alicloud/ali_dns_group.py @@ -0,0 +1,171 @@ +#!/usr/bin/python +# Copyright (c) 2017 Alibaba Group Holding Limited. He Guimin +# 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_dns_group +version_added: "2.8" +short_description: Configure Alibaba Cloud DNS (DNS) +description: + - Create, Delete Alicloud cloud DNS group(DNS group). + It supports updating group name. +options: + lang: + description: + - The language which you choose + group_name: + description: + - Give the name of group when create DNS group and Use this parameter to guarantee idempotence. + aliases: ['name'] + state: + description: + - Whether or not to create, delete DNS group. + choices: ['present', 'absent'] + default: 'present' +requirements: + - "python >= 3.6" + - "footmark >= 1.15.0" +extends_documentation_fragment: + - alicloud +author: + - "He Guimin (@xiaozhu36)" +""" + +EXAMPLES = """ +# Note: These examples do not set authentication details, see the Alibaba Cloud Guide for details. +- name: Changed. Create dns group. + ali_dns_group: + group_name: '{{ group_name }}' + +- name: Changed. Deleting dns group + ali_dns_group: + group_name: '{{ group_name }}' + state: absent +""" + +RETURN = ''' +groups: + description: info about the DNS group that was created or deleted + returned: always + type: complex + contains: + count: + description: alias of 'domain_count'. + returned: always + type: int + sample: 0 + domain_count: + description: Number of domain names in the group . + returned: always + type: dict + returned: always + type: int + sample: 0 + group_id: + description: The id of group. + returned: always + type: string + sample: xxxxxxxxxx + id: + description: alias of 'group_id'. + returned: always + type: string + sample: xxxxxxxxxx + group_name: + description: Name of group. + returned: always + type: string + sample: ansible_test +''' + + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.alicloud_ecs import ecs_argument_spec, dns_connect + +HAS_FOOTMARK = False + +try: + from footmark.exception import DNSResponseError + HAS_FOOTMARK = True +except ImportError: + HAS_FOOTMARK = False + + +def dns_group_exists(module, dns_conn, group_name): + try: + for v in dns_conn.describe_domain_groups(): + if v.name == group_name: + return v + return None + except Exception as e: + module.fail_json(msg="Failed to describe DNS group: {0}".format(e)) + + +def main(): + argument_spec = ecs_argument_spec() + argument_spec.update(dict( + group_name=dict(type='str', aliases=['name']), + lang=dict(type='str'), + state=dict(default='present', choices=['present', 'absent']), + )) + + module = AnsibleModule(argument_spec=argument_spec) + + if HAS_FOOTMARK is False: + module.fail_json(msg='footmark required for the module ali_dns_group.') + + dns_conn = dns_connect(module) + + # Get values of variable + state = module.params['state'] + group_name = module.params['group_name'] + changed = False + + dns_group = dns_group_exists(module, dns_conn,group_name) + + if state == 'absent': + if not dns_group: + module.exit_json(changed=changed, groups={}) + try: + module.exit_json(changed=dns_group.delete(), groups={}) + except DNSResponseError as ex: + module.fail_json(msg='Unable to delete dns group{0}, error: {1}'.format(dns_group.id, ex)) + + if not dns_group: + params = module.params + try: + dns_group = dns_conn.add_domain_group(**params) + if dns_group: + changed = True + module.exit_json(changed=changed, groups=dns_group.get().read()) + except DNSResponseError as e: + module.fail_json(msg='Unable to create dns group, error: {0}'.format(e)) + + module.exit_json(changed=changed, groups=dns_group.read()) + + +if __name__ == '__main__': + main() diff --git a/lib/ansible/modules/cloud/alicloud/ali_dns_group_info.py b/lib/ansible/modules/cloud/alicloud/ali_dns_group_info.py new file mode 100644 index 00000000..c382217f --- /dev/null +++ b/lib/ansible/modules/cloud/alicloud/ali_dns_group_info.py @@ -0,0 +1,156 @@ +#!/usr/bin/python +# Copyright (c) 2017 Alibaba Group Holding Limited. He Guimin +# 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_domain_group_info +version_added: "2.8" +short_description: Gather facts on domain group of Alibaba Cloud. +description: + - This module fetches data from the Open API in Alicloud. + The module must be called from within the group itself. +options: + group_id: + description: + - Id of group, specify it to filter group. + name_prefix: + description: + - Use a Group name prefix to filter groups. +author: + - "He Guimin (@xiaozhu36)" +requirements: + - "python >= 3.6" + - "footmark >= 1.15.0" +extends_documentation_fragment: + - alicloud +''' + +EXAMPLES = ''' +# Note: These examples do not set authentication details, see the Alibaba Cloud Guide for details. +- name: Get the existing group with name_prefix + ali_dns_group_info: + name_prefix: '{{ name }}' + register: groups + +- name: Get the existing group with group_id + ali_dns_group_info: + group_id: '{{ group_id }}' + register: groups + +- name: Get the existing group with domain_count + ali_dns_group_info: + domain_count: '{{ domain_count }}' + register: groups + +- name: Retrieving all dns group + ali_dns_group_info: +''' + +RETURN = ''' +ids: + description: List all group's id after operating group. + returned: when success + type: list + sample: [ "group-2zegusms7jwd94lq7ix8o", "group-2ze5hrb3y5ksx5oa3a0xa" ] +groups: + description: Returns an array of complex objects as described below. + returned: always + type: complex + contains: + count: + description: alias of 'domain_count'. + returned: always + type: int + sample: 0 + domain_count: + description: Number of domain names in the group . + returned: always + type: dict + returned: always + type: int + sample: 0 + group_id: + description: The id of group. + returned: always + type: string + sample: xxxxxxxxxx + id: + description: alias of 'group_id'. + returned: always + type: string + sample: xxxxxxxxxx + group_name: + description: Name of group. + returned: always + type: string + sample: ansible_test +''' + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.alicloud_ecs import ecs_argument_spec, dns_connect + +HAS_FOOTMARK = False + +try: + from footmark.exception import DNSResponseError + HAS_FOOTMARK = True +except ImportError: + HAS_FOOTMARK = False + + +def main(): + argument_spec = ecs_argument_spec() + argument_spec.update(dict( + name_prefix=dict(type='str'), + group_id=dict(type='str', aliases=['id']), + ) + ) + module = AnsibleModule(argument_spec=argument_spec) + + if HAS_FOOTMARK is False: + module.fail_json(msg="Package 'footmark' required for this module.") + + name_prefix = module.params['name_prefix'] + group_id = module.params['group_id'] + + try: + groups = [] + ids = [] + for _dns in dns_connect(module).describe_domain_groups(): + if name_prefix and not _dns.name.startswith(name_prefix): + continue + if group_id and _dns.id != group_id: + continue + groups.append(_dns.read()) + ids.append(_dns.id) + + module.exit_json(changed=False, groups=groups, ids=ids) + except Exception as e: + module.fail_json(msg=str("Unable to get dns group, error:{0}".format(e))) + + +if __name__ == '__main__': + main() diff --git a/tests/ali_dns_domain_test.yml b/tests/ali_dns_domain_test.yml index 7f07b965..2ef88caa 100644 --- a/tests/ali_dns_domain_test.yml +++ b/tests/ali_dns_domain_test.yml @@ -4,6 +4,7 @@ remote_user: root roles: + - dns_group - dns tasks: @@ -21,7 +22,26 @@ ali_dns_domain_info: domain_name: '{{ domain_name }}' + - name: Changed. change domain group. + ali_dns_domain: + domain_name: '{{ domain_name }}' + group_name: '{{ group_name }}' + + - name: No Changed. no domain group changed. + ali_dns_domain: + domain_name: '{{ domain_name }}' + group_name: '{{ group_name }}' + + - name: Get the existing dns + ali_dns_domain_info: + domain_name: '{{ domain_name }}' + - name: Changed. Deleting dns ali_dns_domain: domain_name: '{{ domain_name }}' state: absent + + - name: Changed. Deleting dns group + ali_dns_group: + group_name: '{{ group_name }}' + state: absent \ No newline at end of file diff --git a/tests/ali_dns_group_info_test.yml b/tests/ali_dns_group_info_test.yml new file mode 100644 index 00000000..59a1d3e3 --- /dev/null +++ b/tests/ali_dns_group_info_test.yml @@ -0,0 +1,26 @@ +--- +- name: Validate module ali_dns_group_info + hosts: localhost + remote_user: root + + roles: + - dns_group + + tasks: + - name: Get the existing group with group_name + ali_dns_group_info: + name_prefix: '{{ group_name }}' + register: groups + + - name: Get the existing group with group_id + ali_dns_group_info: + group_id: '{{ group.groups.id }}' + register: groups + + - name: Retrieving all dns group + ali_dns_group_info: + + - name: Changed. Deleting dns group + ali_dns_group: + group_name: '{{ group_name }}' + state: absent \ No newline at end of file diff --git a/tests/ali_dns_group_test.yml b/tests/ali_dns_group_test.yml new file mode 100644 index 00000000..f4de1ba4 --- /dev/null +++ b/tests/ali_dns_group_test.yml @@ -0,0 +1,23 @@ +--- +- name: Validate module ali_dns_group + hosts: localhost + remote_user: root + + roles: + - dns + - dns_group + + tasks: + - name: Get the existing group + ali_dns_group_info: + name_prefix: '{{ group_name }}_new' + + - name: Changed. Deleting dns + ali_dns_domain: + domain_name: '{{ domain_name }}' + state: absent + + - name: Changed. Deleting dns group + ali_dns_group: + group_name: '{{ group_name }}' + state: absent \ No newline at end of file diff --git a/tests/group_vars/all b/tests/group_vars/all index 419f202b..a3c1e96e 100644 --- a/tests/group_vars/all +++ b/tests/group_vars/all @@ -82,3 +82,7 @@ delete: False # create DNS domain parameters domain_name: ansibletest.abc remark: ansible_test_dns_domain + +# create DNS group parameters +group_name: ansible_test +domain_count: 10 \ No newline at end of file diff --git a/tests/roles/dns_group/tasks/main.yml b/tests/roles/dns_group/tasks/main.yml new file mode 100644 index 00000000..04ecdc60 --- /dev/null +++ b/tests/roles/dns_group/tasks/main.yml @@ -0,0 +1,10 @@ +--- +- name: Changed. Create dns group. + ali_dns_group: + group_name: '{{ group_name }}' + register: group + +- name: Get the existing group + ali_dns_group_info: + name_prefix: '{{ group_name }}' + register: groups