Skip to content

Commit

Permalink
Add tags param
Browse files Browse the repository at this point in the history
  • Loading branch information
lixue323 committed Nov 7, 2019
1 parent 557b9e7 commit 290dbf5
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 4 deletions.
30 changes: 29 additions & 1 deletion lib/ansible/modules/cloud/alicloud/ali_eip.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@
ECS or SLB Instance to be re-associated with the specified instance.
type: bool
default: False
tags:
description:
- A hash/dictionaries of eip tags. C({"key":"value"})
purge_tags:
description:
- Delete existing tags on the eip that are not specified in the task.
If True, it means you have to specify all the desired tags on each task affecting a eip.
default: False
type: bool
notes:
- A ip address or a instance id which has been associated with EIP can ensure idempotence.
requirements:
Expand Down Expand Up @@ -254,7 +263,9 @@ def main():
release_on_disassociation=dict(type='bool', default=False),
allow_reassociation=dict(type='bool', default=False),
name=dict(type='str'),
description=dict(type='str')
description=dict(type='str'),
tags=dict(type='dict'),
purge_tags=dict(type='bool', default=False)
)
)

Expand Down Expand Up @@ -332,6 +343,23 @@ def main():
except VPCResponseError as e:
module.fail_json(msg='Modify EIP attribute with an error {0}.'.format(e))

tags = module.params['tags']
if module.params['purge_tags']:
if not tags:
tags = eip.tags
try:
if eip.remove_tags(tags):
changed = True
except Exception as e:
module.fail_json(msg="{0}".format(e))

if tags:
try:
if eip.add_tags(tags):
changed = True
except Exception as e:
module.fail_json(msg="{0}".format(e))

# Associate instance
if instance_id:
if eip.instance_id and eip.instance_id != instance_id:
Expand Down
12 changes: 12 additions & 0 deletions lib/ansible/modules/cloud/alicloud/ali_eip_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
all of request parameters. See U(https://www.alibabacloud.com/help/doc-detail/36018.htm) for parameter details.
Filter keys can be same as request parameter name or be lower case and use underscore ("_") or dashes ("-") to
connect different words in one parameter. 'AllocationId' will be appended to I(eip_ids) automatically.
tags:
description:
- A hash/dictionaries of eip tags. C({"key":"value"})
author:
- "He Guimin (@xiaozhu36)"
requirements:
Expand Down Expand Up @@ -185,6 +188,7 @@ def main():
name_prefix=dict(),
ip_address_prefix=dict(type='str', aliases=['ip_prefix']),
filters=dict(type='dict'),
tags=dict(type='dict')
)
)

Expand All @@ -210,6 +214,7 @@ def main():

name_prefix = module.params["name_prefix"]
address_prefix = module.params["ip_address_prefix"]
tags = module.params["tags"]

try:
for eip in vpc_connect(module).describe_eip_addresses(**new_filters):
Expand All @@ -219,6 +224,13 @@ def main():
continue
if eip_ids and eip.allocation_id not in eip_ids:
continue
if tags:
flag = False
for key, value in list(tags.items()):
if key in list(eip.tags.keys()) and value == eip.tags[key]:
flag = True
if not flag:
continue
eips.append(eip.read())
ids.append(eip.id)

Expand Down
30 changes: 29 additions & 1 deletion lib/ansible/modules/cloud/alicloud/ali_vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@
There will be conflict when I(multi_ok=True) and I(recent=True).
default: False
type: bool
tags:
description:
- A hash/dictionaries of vpc tags. C({"key":"value"})
purge_tags:
description:
- Delete existing tags on the vpc that are not specified in the task.
If True, it means you have to specify all the desired tags on each task affecting a vpc.
default: False
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.
Expand Down Expand Up @@ -224,6 +233,8 @@ def main():
multi_ok=dict(type='bool', default=False),
description=dict(),
recent=dict(type='bool', default=False),
tags=dict(type='dict'),
purge_tags=dict(type='bool', default=False)
))

module = AnsibleModule(argument_spec=argument_spec)
Expand Down Expand Up @@ -273,10 +284,27 @@ def main():
try:
if vpc.modify(vpc_name, description):
changed = True
module.exit_json(changed=changed, vpc=vpc.get().read())
except VPCResponseError as e:
module.fail_json(msg='Unable to modify vpc {0}, error: {1}'.format(vpc.id, e))

tags = module.params['tags']
if module.params['purge_tags']:
if not tags:
tags = vpc.tags
try:
if vpc.remove_tags(tags):
changed = True
except Exception as e:
module.fail_json(msg="{0}".format(e))

if tags:
try:
if vpc.add_tags(tags):
changed = True
except Exception as e:
module.fail_json(msg="{0}".format(e))
module.exit_json(changed=changed, vpc=vpc.get().read())


if __name__ == '__main__':
main()
14 changes: 13 additions & 1 deletion lib/ansible/modules/cloud/alicloud/ali_vpc_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
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. 'VpcId' will be appended to I(vpc_ids) automatically.
tags:
description:
- A hash/dictionaries of vpc tags. C({"key":"value"})
author:
- "He Guimin (@xiaozhu36)"
requirements:
Expand Down Expand Up @@ -178,7 +181,8 @@ def main():
vpc_name=dict(aliases=['name']),
name_prefix=dict(),
cidr_prefix=dict(),
filters=dict(type='dict')
filters=dict(type='dict'),
tags=dict(type='dict')
)
)
module = AnsibleModule(argument_spec=argument_spec)
Expand All @@ -200,6 +204,7 @@ def main():
name = module.params['vpc_name']
name_prefix = module.params['name_prefix']
cidr_prefix = module.params['cidr_prefix']
tags = module.params['tags']

try:
vpcs = []
Expand All @@ -215,6 +220,13 @@ def main():
continue
if cidr_prefix and not str(vpc.cidr_block).startswith(cidr_prefix):
continue
if tags:
flag = False
for key, value in list(tags.items()):
if key in list(vpc.tags.keys()) and value == vpc.tags[key]:
flag = True
if not flag:
continue
vpcs.append(vpc.read())
ids.append(vpc.id)
if not vpc_ids:
Expand Down
27 changes: 27 additions & 0 deletions lib/ansible/modules/cloud/alicloud/ali_vswitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@
description:
- (Deprecated) VSwitch ID.
aliases: ['subnet_id', 'id']
tags:
description:
- A hash/dictionaries of vswitch tags. C({"key":"value"})
purge_tags:
description:
- Delete existing tags on the vswitch that are not specified in the task.
If True, it means you have to specify all the desired tags on each task affecting a vswitch.
default: False
type: bool
requirements:
- "python >= 2.6"
- "footmark >= 1.7.0"
Expand Down Expand Up @@ -199,6 +208,8 @@ def main():
vpc_id=dict(required=True),
name=dict(aliases=['vswitch_name', 'subnet_name']),
vswitch_id=dict(aliases=['subnet_id', 'id']),
tags=dict(type='dict'),
purge_tags=dict(type='bool', default=False)
))

module = AnsibleModule(argument_spec=argument_spec)
Expand Down Expand Up @@ -251,6 +262,22 @@ def main():
except VPCResponseError as e:
module.fail_json(msg='Unable to modify vswitch attribute, error: {0}'.format(e))

tags = module.params['tags']
if module.params['purge_tags']:
if not tags:
tags = vswitch.tags
try:
if vswitch.remove_tags(tags):
changed = True
except Exception as e:
module.fail_json(msg="{0}".format(e))

if tags:
try:
if vswitch.add_tags(tags):
changed = True
except Exception as e:
module.fail_json(msg="{0}".format(e))
module.exit_json(changed=changed, vswitch=vswitch.get().read())


Expand Down
14 changes: 13 additions & 1 deletion lib/ansible/modules/cloud/alicloud/ali_vswitch_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
all of request parameters. See U(https://www.alibabacloud.com/help/doc-detail/35748.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. 'VSwitchId' will be appended to I(vswitch_ids) automatically.
tags:
description:
- A hash/dictionaries of vswitch tags. C({"key":"value"})
author:
- "He Guimin (@xiaozhu36)"
requirements:
Expand Down Expand Up @@ -182,7 +185,8 @@ def main():
name_prefix=dict(),
cidr_prefix=dict(),
vswitch_ids=dict(type='list', aliases=['ids', 'subnet_ids']),
filters=dict(type='dict')
filters=dict(type='dict'),
tags=dict(type='dict')
)
)

Expand All @@ -206,6 +210,7 @@ def main():
cidr_block = module.params['cidr_block']
name_prefix = module.params['name_prefix']
cidr_prefix = module.params['cidr_prefix']
tags = module.params['tags']

try:
vswitches = []
Expand All @@ -223,6 +228,13 @@ def main():
continue
if cidr_prefix and not str(vsw.cidr_block).startswith(cidr_prefix):
continue
if tags:
flag = False
for key, value in list(tags.items()):
if key in list(vsw.tags.keys()) and value == vsw.tags[key]:
flag = True
if not flag:
continue
vswitches.append(vsw.read())
ids.append(vsw.id)
if not vswitch_ids:
Expand Down
33 changes: 33 additions & 0 deletions tests/ali_eip_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,39 @@
description: "{{ description }}-ali-eip"
bandwidth: 2

- name: Changed. Add Tags.
ali_eip:
ip_address: "{{ eip.eip.ip_address }}"
tags:
Test1: "add1"
Test2: "add2"

- name: Filter eip using tags
ali_eip_info:
tags:
Test1: "add1"

- name: Changed. Modify Tags.
ali_eip:
ip_address: "{{ eip.eip.ip_address }}"
tags:
Test1: "add1"
Test2: "add3"

- name: No Changed. No tags need to be added
ali_eip:
ip_address: "{{ eip.eip.ip_address }}"
tags:
Test1: "add1"
Test2: "add3"

- name: Changed. Removing tags.
ali_eip:
ip_address: "{{ eip.eip.ip_address }}"
purge_tags: True
tags:
Test1: "add1"

- name: Changed. Release ips.
ali_eip:
ip_address: "{{ eip.eip.ip_address }}"
Expand Down
41 changes: 41 additions & 0 deletions tests/ali_vpc_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,47 @@
# new_name: '{{ item.vpc_name }}-modified'
# with_items: '{{vpcs.vpcs}}'

- name: Changed. Add Tags.
ali_vpc:
cidr_block: '{{ vpc_cidr }}'
name: '{{ item.vpc_name }}'
tags:
Test1: "add1"
Test2: "add2"
with_items: '{{vpcs.vpcs}}'

- name: Filter vpc using tags
ali_vpc_info:
tags:
Test1: "add1"

- name: Changed. Modify Tags.
ali_vpc:
cidr_block: '{{ vpc_cidr }}'
name: '{{ item.vpc_name }}'
tags:
Test1: "add1"
Test2: "add3"
with_items: '{{vpcs.vpcs}}'

- name: No Changed. No tags need to be added
ali_vpc:
cidr_block: '{{ vpc_cidr }}'
name: '{{ item.vpc_name }}'
tags:
Test1: "add1"
Test2: "add3"
with_items: '{{vpcs.vpcs}}'

- name: Changed. Removing tags.
ali_vpc:
cidr_block: '{{ vpc_cidr }}'
name: '{{ item.vpc_name }}'
purge_tags: True
tags:
Test1: "add1"
with_items: '{{vpcs.vpcs}}'

- name: Changed. Modify description.
ali_vpc:
cidr_block: '{{ vpc_cidr }}'
Expand Down
Loading

0 comments on commit 290dbf5

Please sign in to comment.