Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve module ali_vpc and ali_security_group multi_ok #149

Merged
merged 1 commit into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ FEATURES:

IMPROVEMENTS:

- Improve module ali_vpc and ali_security_group multi_ok ([#149](https://github.com/alibaba/ansible-provider/pull/149))
- Improve module ali_slb_vsg and ali_slb_vsg_facts ([#148](https://github.com/alibaba/ansible-provider/pull/148))
- Improve module ali_slb_lb and ali_slb_lb_facts ([#147](https://github.com/alibaba/ansible-provider/pull/147))
- Improve ecs instance and eni docs ([#146](https://github.com/alibaba/ansible-provider/pull/146))
Expand Down
6 changes: 3 additions & 3 deletions lib/ansible/modules/cloud/alicloud/ali_security_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ def group_exists(conn, module, vpc_id, name, multi, recent):
When supplied with a vpc_id and Name, it will check them to determine if it is a match
otherwise it will assume the Security Group does not exist and thus return None.
"""
if multi:
return None
matching_groups = []
filters = {}
if vpc_id:
Expand All @@ -317,9 +319,7 @@ def group_exists(conn, module, vpc_id, name, multi, recent):
if len(matching_groups) == 1:
return matching_groups[0]
elif len(matching_groups) > 1:
if multi:
return None
elif recent:
if recent:
return matching_groups[-1]
module.fail_json(msg='Currently there are {0} Security Groups that have the same name and '
'vpc id you specified. If you would like to create anyway '
Expand Down
8 changes: 4 additions & 4 deletions lib/ansible/modules/cloud/alicloud/ali_vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
short_description: Configure Alibaba Cloud virtual private cloud(VPC)
description:
- Create, Delete Alicloud virtual private cloud(VPC).
It support updating VPC description.
It supports updating VPC description.
options:
state:
description:
Expand Down Expand Up @@ -192,6 +192,8 @@ def vpc_exists(module, vpc, name, cidr_block, multi, recent):
with a CIDR and Name, it will check them to determine if it is a match
otherwise it will assume the VPC does not exist and thus return None.
"""
if multi:
return None
matching_vpcs = []
try:
for v in vpc.describe_vpcs():
Expand All @@ -203,9 +205,7 @@ def vpc_exists(module, vpc, name, cidr_block, multi, recent):
if len(matching_vpcs) == 1:
return matching_vpcs[0]
elif len(matching_vpcs) > 1:
if multi:
return None
elif recent:
if recent:
return matching_vpcs[-1]
module.fail_json(msg='Currently there are {0} VPCs that have the same name and '
'CIDR block you specified. If you would like to create '
Expand Down
28 changes: 17 additions & 11 deletions tests/ali_eni_facts_test.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
---
- name: Validate module ali_instance
- name: Validate module ali_eni_facts
hosts: localhost
remote_user: root

vars:
name: ansible-testacc-ali_eni_facts

roles:
- vpc
- vswitch
- security_group
- network_interface

tasks:
- name: Retrieving instances using eni_ids
- name: Retrieving enis using eni_ids
ali_eni_facts:
eni_ids: '{{ enis.ids}}'

- name: Add two new enis and used to test name_regex and tags
ali_eni:
vswitch_id: '{{ vswitches.vswitches.0.id }}'
security_groups: ['{{ sgs.groups.0.id }}']
name: '{{ name }}regex--ali_eni'
name: '{{ name }}regex'
description: '{{description}}-name-regex-ali_eni'
tags:
Test: tags
Expand All @@ -32,10 +35,10 @@
tags:
Test: tags

- name: Retrieving all instances
- name: Retrieving all enis
ali_eni_facts:

- name: Retrieving all instances based on name_regex
- name: Retrieving all enis based on name_regex
ali_eni_facts:
name_prefix: '{{name}}'
register: enis
Expand All @@ -49,19 +52,22 @@

- name: Changed. Deleting security groups
ali_security_group:
group_id: '{{item}}'
name: '{{ item.group_name }}'
vpc_id: '{{item.vpc_id}}'
state: absent
with_items: '{{sgs.ids}}'
with_items: '{{sgs.groups}}'

- name: Changed. Deleting vswitches
ali_vswitch:
id: '{{item}}'
vpc_id: '{{ item.vpc_id}}'
cidr_block: '{{ item.cidr_block}}'
state: absent
with_items: '{{vswitches.ids}}'
with_items: '{{vswitches.vswitches}}'

- name: Changed. Deleting vpcs
ali_vpc:
vpc_id: '{{item}}'
name: '{{item.vpc_name}}'
cidr_block: '{{item.cidr_block}}'
state: absent
with_items: '{{vpcs.ids}}'
with_items: '{{vpcs.vpcs}}'

26 changes: 16 additions & 10 deletions tests/ali_eni_test.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
---
- name: Validate module ali_instance
- name: Validate module ali_eni
hosts: localhost
remote_user: root

vars:
name: ansible-testacc-ali_eni

roles:
- vpc
- vswitch
Expand All @@ -14,27 +17,27 @@
- name: Changed. Modifying name.
ali_eni:
id: '{{ item}}'
name: '{{ name }}-ali_eni-modified'
name: '{{ name }}-modified'
with_items: '{{enis.ids}}'

- name: Changed. Modifying description.
ali_eni:
id: '{{ item}}'
description: '{{description}}ali_eni-modified'
description: '{{description}}-modified'
with_items: '{{enis.ids}}'

- name: No Changed. There is no any attribute changed
ali_eni:
id: '{{ item}}'
name: '{{ name }}-ali_eni-modified'
description: '{{description}}ali_eni-modified'
name: '{{ name }}-modified'
description: '{{description}}-modified'
with_items: '{{enis.ids}}'

- name: Changed. Go back init value.
ali_eni:
id: '{{item}}'
name: '{{ name }}-ali_eni'
description: '{{description}}ali_eni'
name: '{{ name }}'
description: '{{description}}'
with_items: '{{enis.ids}}'

- name: Creating a new security group
Expand Down Expand Up @@ -186,19 +189,22 @@
#
- name: Changed. Deleting security groups
ali_security_group:
group_id: '{{item.id}}'
name: '{{ item.group_name }}'
vpc_id: '{{item.vpc_id}}'
state: absent
with_items: '{{sgs.groups}}'

- name: Changed. Deleting vswitches
ali_vswitch:
id: '{{item.id}}'
vpc_id: '{{ item.vpc_id}}'
cidr_block: '{{ item.cidr_block}}'
state: absent
with_items: '{{vswitches.vswitches}}'

- name: Changed. Deleting vpcs
ali_vpc:
vpc_id: '{{item.id}}'
name: '{{item.vpc_name}}'
cidr_block: '{{item.cidr_block}}'
state: absent
with_items: '{{vpcs.vpcs}}'

15 changes: 9 additions & 6 deletions tests/ali_security_group_facts_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
- name: Validate module ali_instance
hosts: localhost
remote_user: root

vars:
name: ansible-testacc-ali_security_group_facts
roles:
- vpc
- security_group
Expand All @@ -14,7 +15,7 @@

- name: Add a group and used to test name_regex
ali_security_group:
name: '{{name}}regex-vswitch'
name: '{{name}}regex'
description: '{{group_description}} name regex.'
vpc_id: '{{vpcs.vpcs.0.id}}'

Expand All @@ -32,13 +33,15 @@

- name: Changed. Deleting security groups
ali_security_group:
id: '{{ item}}'
name: '{{ item.group_name }}'
vpc_id: '{{item.vpc_id}}'
state: absent
with_items: '{{sgs.ids}}'
with_items: '{{sgs.groups}}'

- name: Changed. Deleting vpcs
ali_vpc:
vpc_id: '{{item}}'
name: '{{item.vpc_name}}'
cidr_block: '{{item.cidr_block}}'
state: absent
with_items: '{{vpcs.ids}}'
with_items: '{{vpcs.vpcs}}'

Loading