From 592c87d7d6f92f0bd50a9789d3493d0553edc7a1 Mon Sep 17 00:00:00 2001 From: He Guimin Date: Fri, 2 Nov 2018 17:41:25 +0800 Subject: [PATCH] Add name_prefix and cidr_prefix for ali_vpc_facts --- CHANGELOG.md | 1 + lib/ansible/modules/cloud/alicloud/ali_vpc.py | 45 ++++--------- .../modules/cloud/alicloud/ali_vpc_facts.py | 67 ++++++++++++++----- 3 files changed, 64 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c72f7b0..1ebc938b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ IMPROVEMENTS: +- Add name_prefix and cidr_prefix for ali_vpc_facts ([#126](https://github.com/alibaba/ansible-provider/pull/126)) - Add name_prefix and filters for ali_instance_facts ([#125](https://github.com/alibaba/ansible-provider/pull/125)) - Improve alicloud inventory based on footmark 1.6.3+ ([#124](https://github.com/alibaba/ansible-provider/pull/124)) - Improve ali_security_group and support purge_rules and purge_rules_egress ([#123](https://github.com/alibaba/ansible-provider/pull/123)) diff --git a/lib/ansible/modules/cloud/alicloud/ali_vpc.py b/lib/ansible/modules/cloud/alicloud/ali_vpc.py index 13035613..1ced3c27 100644 --- a/lib/ansible/modules/cloud/alicloud/ali_vpc.py +++ b/lib/ansible/modules/cloud/alicloud/ali_vpc.py @@ -17,6 +17,9 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see http://www.gnu.org/licenses/. +from __future__ import absolute_import, division, print_function +__metaclass__ = type + ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'community'} @@ -24,7 +27,7 @@ DOCUMENTATION = """ --- module: ali_vpc -version_added: "1.5.0" +version_added: "2.8" short_description: Configure Alibaba Cloud virtual private cloud(VPC) description: - Create, modify, and delete Alicloud virtual private cloud(VPC). @@ -46,27 +49,22 @@ - The description of VPC, which is a string of 2 to 256 characters. It cannot begin with http:// or https://. cidr_block: description: - - The CIDR block representing the vpc. The value can be subnet block of its choices. It is required when creating a vpc. - default: '172.16.0.0/12' - choices: ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'] + - The CIDR block representing the vpc. The value can be subnet block of its choices. + Required when C(state=present) and creating new a vpc. aliases: [ 'cidr' ] user_cidrs: description: - List of user custom cidr in the VPC. It no more than three. vpc_id: description: - - The ID of a VPC. It required when managing an existing VPC. Such as deleting vpc and querying vpc attribute. + - The ID of a VPC. Required when C(state=absent). aliases: ['id'] - is_default: - description: - - When retrieving vpc, it can mark the VPC is created by system. - type: bool notes: - There will be launch a virtual router along with creating a vpc successfully. - There is only one virtual router in one vpc and one route table in one virtual router. requirements: - "python >= 2.6" - - "footmark >= 1.1.16" + - "footmark >= 1.7.0" extends_documentation_fragment: - alicloud author: @@ -74,28 +72,17 @@ """ EXAMPLES = """ -# -# provisioning to create vpc in VPC -# - # basic provisioning example to create vpc in VPC - name: create vpc hosts: localhost connection: local - vars: - alicloud_region: cn-hongkong - state: present - cidr_block: 192.168.0.0/16 - vpc_name: Demo_VPC - description: Demo VPC + tasks: - name: create vpc ali_vpc: - alicloud_region: '{{ alicloud_region }}' - state: '{{ state }}' - cidr_block: '{{ cidr_block }}' - vpc_name: '{{ vpc_name }}' - description: '{{ description }}' + cidr_block: '192.168.0.0/16' + vpc_name: 'Demo_VPC' + description: 'Demo VPC' register: result - debug: var=result @@ -103,12 +90,10 @@ - name: delete vpc hosts: localhost connection: local - vars: - alicloud_region: cn-hongkong + tasks: - name: delete vpc ali_vpc: - alicloud_region: '{{ alicloud_region }}' state: absent vpc_id: xxxxxxxxxx register: result @@ -200,12 +185,11 @@ def main(): argument_spec = ecs_argument_spec() argument_spec.update(dict( state=dict(default='present', choices=['present', 'absent']), - cidr_block=dict(default='172.16.0.0/16', aliases=['cidr']), + cidr_block=dict(aliases=['cidr']), user_cidrs=dict(type='list'), vpc_name=dict(aliases=['name']), description=dict(), vpc_id=dict(aliases=['id']), - is_default=dict(type='bool'), )) module = AnsibleModule(argument_spec=argument_spec) @@ -220,7 +204,6 @@ def main(): vpc_id = module.params['vpc_id'] vpc_name = module.params['vpc_name'] description = module.params['description'] - user_cidrs = module.params['user_cidrs'] if str(description).startswith('http://') or str(description).startswith('https://'): module.fail_json(msg='description can not start with http:// or https://') diff --git a/lib/ansible/modules/cloud/alicloud/ali_vpc_facts.py b/lib/ansible/modules/cloud/alicloud/ali_vpc_facts.py index d83182e9..4bb262e3 100644 --- a/lib/ansible/modules/cloud/alicloud/ali_vpc_facts.py +++ b/lib/ansible/modules/cloud/alicloud/ali_vpc_facts.py @@ -27,7 +27,7 @@ DOCUMENTATION = ''' --- module: ali_vpc_facts -version_added: "1.5.0" +version_added: "2.8" short_description: Gather facts on vpcs of Alibaba Cloud. description: - This module fetches data from the Open API in Alicloud. @@ -36,23 +36,29 @@ options: vpc_name: description: - - Name of one or more VPC that exist in your account. + - (Deprecated) Name of one or more VPC that exist in your account. New option `name_prefix` instead. aliases: ["name"] vpc_ids: description: - A list of VPC IDs that exist in your account. aliases: ["ids"] + name_prefix: + description: + - Use a VPC name prefix to filter VPCs. + cidr_prefix: + description: + - Use a VPC cidr block prefix to filter VPCs. filters: description: - - A dict of filters to apply. Each dict item consists of a filter key and a filter value. - The filter keys can be all of request parameters. See U(https://www.alibabacloud.com/help/doc-detail/35739.htm) for parameter details. - Filter keys can be same as request parameter name or be lower case and use underscores (_) or dashes (-) to - connect different words in one parameter. 'VpcId' should be a list and use I(vpc_ids) instead. + - A dict of filters to apply. Each dict item consists of a filter key and a filter value. The filter keys can be + all of request parameters. See U(https://www.alibabacloud.com/help/doc-detail/35739.htm) for parameter details. + Filter keys can be same as request parameter name or be lower case and use underscore ("_") or dash ("-") to + connect different words in one parameter. author: - "He Guimin (@xiaozhu36)" requirements: - "python >= 2.6" - - "footmark" + - "footmark >= 1.7.0" extends_documentation_fragment: - alicloud ''' @@ -65,13 +71,19 @@ # Gather facts about a particular VPC using VPC ID - ali_vpc_facts: - vpc_ids: vpc-aaabbb + vpc_ids: + - vpc-aaabbb + - vpc-123fwec -# Gather facts about any VPC with 'is_default' and name +# Gather facts about any VPC with 'is_default' and name_prefix - ali_vpc_facts: - vpc_name: my-vpc + name_prefix: "my-vpc" filters: is_default: False + +# Gather facts about any VPC with cidr_prefix +- ali_vpc_facts: + cidr_prefix: "172.16" ''' RETURN = ''' @@ -163,7 +175,9 @@ def main(): argument_spec = ecs_argument_spec() argument_spec.update(dict( vpc_ids=dict(type='list', aliases=['ids']), - vpc_name=dict(type='str', aliases=['name']), + vpc_name=dict(aliases=['name']), + name_prefix=dict(), + cidr_prefix=dict(), filters=dict(type='dict') ) ) @@ -175,18 +189,35 @@ def main(): filters = module.params['filters'] if not filters: filters = {} - if module.params['vpc_ids']: - filters['vpc_ids'] = module.params['vpc_ids'] + + vpc_ids = module.params['vpc_ids'] + if vpc_ids: + filter_vpc_id = filters['vpc_id'] + if filter_vpc_id and filter_vpc_id not in vpc_ids: + vpc_ids.append(filter_vpc_id) + name = module.params['vpc_name'] + name_prefix = module.params['name_prefix'] + cidr_prefix = module.params['cidr_prefix'] try: vpcs = [] ids = [] - for vpc in vpc_connect(module).describe_vpcs(**filters): - if name and vpc.vpc_name != name: - continue - vpcs.append(vpc.read()) - ids.append(vpc.id) + while True: + if vpc_ids: + filters['vpc_id'] = vpc_ids[0] + vpc_ids.pop(0) + for vpc in vpc_connect(module).describe_vpcs(**filters): + if name and vpc.vpc_name != name: + continue + if name_prefix and not str(vpc.vpc_name).startswith(name_prefix): + continue + if cidr_prefix and not str(vpc.cidr_block).startswith(cidr_prefix): + continue + vpcs.append(vpc.read()) + ids.append(vpc.id) + if not vpc_ids: + break module.exit_json(changed=False, ids=ids, vpcs=vpcs) except Exception as e: