VMware: Add support for specify tag and category as dict

User can now specify tag and category using dict in vmware_tag_manager
module. This is useful when tag or category name contains colon.

Fixes: #65765

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2020-01-01 10:54:56 +05:30 committed by Gonéri Le Bouder
parent 33d5c68887
commit 7000c51c06
5 changed files with 161 additions and 11 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- Added support to vmware_tag_manager module for specifying tag and category as dict if any of the name contains colon (https://github.com/ansible/ansible/issues/65765).

View file

@ -35,8 +35,10 @@ options:
tag_names:
description:
- List of tag(s) to be managed.
- You can also specify category name by specifying colon separated value. For example, "category_name:tag_name".
- You can skip category name if you have unique tag names.
- User can also specify category name by specifying colon separated value. For example, "category_name:tag_name".
- User can also specify tag and category as dict, when tag or category contains colon.
See example for more information. Added in version 2.10.
- User can skip category name if you have unique tag names.
required: True
type: list
state:
@ -77,6 +79,22 @@ EXAMPLES = r'''
state: add
delegate_to: localhost
- name: Specify tag and category as dict
vmware_tag_manager:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: no
tag_names:
- tag: tag_0001
category: cat_0001
- tag: tag_0002
category: cat_0002
object_name: Fedora_VM
object_type: VirtualMachine
state: add
delegate_to: localhost
- name: Remove a tag from a virtual machine
vmware_tag_manager:
hostname: '{{ vcenter_hostname }}'
@ -218,15 +236,24 @@ class VmwareTagManager(VmwareRestClient):
removed_tags_for_set = False
for tag in self.tag_names:
category_obj, category_name, tag_name = None, None, None
if ":" in tag:
# User specified category
category_name, tag_name = tag.split(":", 1)
category_obj = self.search_svc_object_by_name(self.category_service, category_name)
if not category_obj:
self.module.fail_json(msg="Unable to find the category %s" % category_name)
else:
# User specified only tag
tag_name = tag
if isinstance(tag, dict):
tag_name = tag.get('tag')
category_name = tag.get('category')
if category_name:
# User specified category
category_obj = self.search_svc_object_by_name(self.category_service, category_name)
if not category_obj:
self.module.fail_json(msg="Unable to find the category %s" % category_name)
elif isinstance(tag, str):
if ":" in tag:
# User specified category
category_name, tag_name = tag.split(":", 1)
category_obj = self.search_svc_object_by_name(self.category_service, category_name)
if not category_obj:
self.module.fail_json(msg="Unable to find the category %s" % category_name)
else:
# User specified only tag
tag_name = tag
if category_name:
tag_obj = self.get_tag_by_category(tag_name=tag_name, category_name=category_name)

View file

@ -0,0 +1,3 @@
cloud/vcenter
unsupported
zuul/vmware/vcenter_only

View file

@ -0,0 +1,5 @@
# Test code for the vmware_tag_manger Operations.
# Copyright: (c) 2020, Abhijeet Kasurde <akasurde@redhat.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
- include: tag_manager_dict.yml

View file

@ -0,0 +1,113 @@
# Test code for the vmware_tag_manager Operations.
# Copyright: (c) 2020, Abhijeet Kasurde <akasurde@redhat.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Testcase for https://github.com/ansible/ansible/issues/65765
- when: vcsim is not defined
block:
- name: Create first category
vmware_category:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: no
category_name: "{{ cat_one }}"
category_cardinality: 'multiple'
state: present
register: category_one_create
- name: Set category one id
set_fact: cat_one_id={{ category_one_create['category_results']['category_id'] }}
- name: Create tag with colon in name
vmware_tag:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: no
tag_name: "{{ tag_one }}"
category_id: "{{ cat_one_id }}"
state: present
register: tag_one_create
- name: Check tag is created
assert:
that:
- tag_one_create.changed
- name: Get VM Facts
vmware_vm_info:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: False
register: vm_info
- set_fact: vm_name="{{ vm_info['virtual_machines'][0]['guest_name'] }}"
- name: Assign tag to given virtual machine
vmware_tag_manager:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: no
tag_names:
- category: "{{ cat_one }}"
tag: "{{ tag_one }}"
object_name: "{{ vm_name }}"
object_type: VirtualMachine
state: add
delegate_to: localhost
register: vm_tag_info
- name: Check if we assigned correct tags
assert:
that:
- vm_tag_info.changed
- name: Remove tag to given virtual machine
vmware_tag_manager:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: no
tag_names:
- category: "{{ cat_one }}"
tag: "{{ tag_one }}"
object_name: "{{ vm_name }}"
object_type: VirtualMachine
state: remove
delegate_to: localhost
register: vm_tag_info
- name: Check if we removed correct tag
assert:
that:
- vm_tag_info.changed
- name: Delete Tags
vmware_tag:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: no
tag_name: "{{ item }}"
state: absent
register: delete_tag
with_items:
- "{{ tag_one }}"
- name: Delete Categories
vmware_category:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: no
category_name: "{{ item }}"
state: absent
register: delete_categories
with_items:
- "{{ cat_one }}"
vars:
cat_one: category_1003
tag_one: tag:1003