Skip to content

Commit

Permalink
Add new modules ali_ecs_tag ali_slb_tag ali_vpc_tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
lixue323 authored and xiaozhu36 committed Feb 24, 2020
1 parent cb450f6 commit c1b35d0
Show file tree
Hide file tree
Showing 12 changed files with 1,171 additions and 27 deletions.
211 changes: 211 additions & 0 deletions lib/ansible/modules/cloud/alicloud/ali_ecs_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
#!/usr/bin/python
# Copyright (c) 2017-present Alibaba Group Holding Limited. He Guimin <[email protected]>
# 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_ecs_tag
version_added: "2.9"
short_description: Add tags for Alibaba Cloud resource.
description:
- Add tags to Alibaba Cloud resources
options:
state:
description:
- Whether or not to add, remove tags.
choices: ['present', 'absent']
default: 'present'
resource_ids:
description:
- A list of resource ids.
resource_type:
description:
- The type of resource.
choices: ['instance', 'disk', 'image', 'eni', 'securitygroup']
default: 'instance'
tags:
description:
- A hash/dictionaries of resource tags. C({"key":"value"})
requirements:
- "python >= 3.6"
- "footmark >= 1.17.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. Add Tags.
ali_ecs_tag:
resource_type: 'instance'
resource_ids: [i_id, i_id1]
tags: {
"Ansible": "Add tags"
}
- name: Changed. Remove Tags.
ali_ecs_tag:
state: absent
resource_type: 'instance'
resource_ids: [i_id, i_id1]
tags: {
"Ansible": "Add tags"
}
"""

RETURN = '''
tags:
description:
- info about the resource that was added tags.
returned: always
type: complex
contains:
tags:
description: The resource tags.
returned: always
type: complex
sample: {"tag_key": "tag_value"}
'''

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.alicloud_ecs import ecs_argument_spec, ecs_connect

HAS_FOOTMARK = False

try:
from footmark.exception import ECSResponseError
HAS_FOOTMARK = True
except ImportError:
HAS_FOOTMARK = False


def ecs_exists(module, conn):
try:
return conn.describe_instances(instance_ids=module.params["resource_ids"])
except Exception as e:
module.fail_json(msg="Failed to describe ecs instances: {0}".format(e))
return None


def disk_exists(module, conn):
try:
disk_l = []
result = conn.describe_disks()
for res in result:
if res.id in module.params['resource_ids']:
disk_l.append(res)
return disk_l
except Exception as e:
module.fail_json(msg="Failed to describe disks: {0}".format(e))
return None


def image_exists(module, conn):
try:
image_l = []
result = conn.describe_images()
for res in result:
if res.id in module.params['resource_ids']:
image_l.append(res)
return image_l
except Exception as e:
module.fail_json(msg="Failed to describe images: {0}".format(e))
return None


def sgroup_exists(module, conn):
try:
sgroup_l = []
result = conn.describe_security_groups()
for res in result:
if res.id in module.params['resource_ids']:
sgroup_l.append(res)
return sgroup_l
except Exception as e:
module.fail_json(msg="Failed to describe security group: {0}".format(e))
return None


def eni_exists(module, conn):
try:
eni_l = []
result = conn.describe_network_interfaces()
for res in result:
if res.id in module.params['resource_ids']:
eni_l.append(res)
return eni_l
except Exception as e:
module.fail_json(msg="Failed to describe network interfaces: {0}".format(e))
return None


def main():
argument_spec = ecs_argument_spec()
argument_spec.update(dict(
state=dict(default='present', choices=['present', 'absent']),
resource_ids=dict(type='list', required=True),
resource_type=dict(type='str', default='instance', choices=['instance', 'disk', 'image', 'eni', 'securitygroup']),
tags=dict(type='dict')
))

module = AnsibleModule(argument_spec=argument_spec)

if HAS_FOOTMARK is False:
module.fail_json(msg='footmark required for the module ali_ecs_tag.')

ecs_conn = ecs_connect(module)
if module.params['resource_type'] == 'instance':
resources = ecs_exists(module, ecs_conn)
elif module.params['resource_type'] == 'disk':
resources = disk_exists(module, ecs_conn)
elif module.params['resource_type'] == 'image':
resources = image_exists(module, ecs_conn)
elif module.params['resource_type'] == 'eni':
resources = eni_exists(module, ecs_conn)
else:
resources = sgroup_exists(module, ecs_conn)

if not resources:
module.fail_json(msg='No matching resource was found based on the IDS provided.')

#Get values of variable
tags = module.params['tags']

if module.params['state'] == "present":
ecs_changed = ecs_conn.tag_resources(resource_ids=module.params['resource_ids'], tags=tags, resource_type=module.params['resource_type'])
else:
ecs_changed = ecs_conn.untag_resources(resource_ids=module.params['resource_ids'], tags=tags, resource_type=module.params['resource_type'])

result = []
for resource in resources:
result.append(resource.read()['tags'])

module.exit_json(changed=ecs_changed, tags=result)


if __name__ == '__main__':
main()
30 changes: 13 additions & 17 deletions lib/ansible/modules/cloud/alicloud/ali_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ def main():
instances.pop(len(instances) - 1)
else:
try:
if re.search("-\[\d+,\d+\]-", host_name).group():
if re.search("-\[\d+,\d+\]-", host_name):
module.fail_json(msg='Ordered hostname is not supported, If you want to add an ordered suffix to the hostname, you can set unique_suffix to True')
new_instances = run_instance(module, ecs, count - len(instances))
if new_instances:
Expand Down Expand Up @@ -881,29 +881,25 @@ def main():
module.fail_json(msg='Reboot instances got an error: {0}'.format(e))

tags = module.params['tags']
if tags or module.params['purge_tags']:
if module.params['purge_tags']:
for inst in instances:
removed = {}
if not tags:
removed = inst.tags
else:
for key, value in list(inst.tags.items()):
if key not in list(tags.keys()):
removed[key] = value
tags = inst.tags
try:
if inst.remove_tags(removed):
if inst.remove_tags(tags):
changed = True
except Exception as e:
module.fail_json(msg="{0}".format(e))
module.exit_json(changed=changed, instances=get_instances_info(ecs, ids))

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

module.exit_json(changed=changed, ids=ids, instances=get_instances_info(ecs, ids))
if tags:
for inst in instances:
try:
if inst.add_tags(tags):
changed = True
except Exception as e:
module.fail_json(msg="{0}".format(e))
module.exit_json(changed=changed, instances=get_instances_info(ecs, ids))


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/modules/cloud/alicloud/ali_security_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def group_exists(conn, module, vpc_id, name, multi, recent):
try:
for g in conn.describe_security_groups(**filters):
if g.security_group_name == name:
matching_groups.append(g)
matching_groups.append(g.get())
except Exception as e:
module.fail_json(msg="Failed to describe Security Groups: {0}".format(e))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def main():
continue
if name_prefix and not str(sg.security_group_name).startswith(name_prefix):
continue
groups.append(sg.read())
groups.append(sg.get().read())
ids.append(sg.id)
except ECSResponseError as e:
module.fail_json(msg='Error in describe_security_groups: {0}'.format(e))
Expand Down
Loading

0 comments on commit c1b35d0

Please sign in to comment.