Skip to content

Commit

Permalink
Add name_prefix and cidr_prefix for ali_vpc_facts
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhu36 committed Nov 2, 2018
1 parent aa416bc commit 592c87d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 49 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
45 changes: 14 additions & 31 deletions lib/ansible/modules/cloud/alicloud/ali_vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
# 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'}

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).
Expand All @@ -46,69 +49,51 @@
- 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:
- "He Guimin (@xiaozhu36)"
"""

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
# basic provisioning example to delete vpc
- 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
Expand Down Expand Up @@ -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)
Expand All @@ -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://')
Expand Down
67 changes: 49 additions & 18 deletions lib/ansible/modules/cloud/alicloud/ali_vpc_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
'''
Expand All @@ -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 = '''
Expand Down Expand Up @@ -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')
)
)
Expand All @@ -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:
Expand Down

0 comments on commit 592c87d

Please sign in to comment.