Add explicit coverage of suboptions=list without elements (#72108)

* Add explicit coverage of suboptions=list without elements

* ci_complete ci_coverage

* Remove incidental_vmware_guest_custom_attributes

* ci_complete ci_coverage
pull/72138/head
Matt Martz 4 years ago committed by GitHub
parent aff78f4cbc
commit 50c8c87fe2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -28,7 +28,7 @@ def main():
'thing': {}, 'thing': {},
'other': {}, 'other': {},
'another': {}, 'another': {},
} },
}, },
'required_if': { 'required_if': {
'required_if': ( 'required_if': (
@ -40,7 +40,7 @@ def main():
'thing': {}, 'thing': {},
'other': {}, 'other': {},
'another': {}, 'another': {},
} },
}, },
'json': { 'json': {
'type': 'json', 'type': 'json',
@ -52,6 +52,12 @@ def main():
'needed_param': {}, 'needed_param': {},
'required_together_one': {}, 'required_together_one': {},
'required_together_two': {}, 'required_together_two': {},
'suboptions_list_no_elements': {
'type': 'list',
'options': {
'thing': {},
},
},
}, },
required_if=( required_if=(
('state', 'present', ('path', 'content'), True), ('state', 'present', ('path', 'content'), True),

@ -121,6 +121,11 @@
register: argspec_fail_required_together register: argspec_fail_required_together
ignore_errors: true ignore_errors: true
- argspec:
suboptions_list_no_elements:
- thing: foo
register: argspec_suboptions_list_no_elements
- assert: - assert:
that: that:
- argspec_required_if_fail is failed - argspec_required_if_fail is failed
@ -145,13 +150,16 @@
- argpsec_required_if_fail is failed - argpsec_required_if_fail is failed
- argspec_good_json_string is successful - argspec_good_json_string is successful
- > - >-
argspec_good_json_string.json == '{"foo": "bar"}' argspec_good_json_string.json == '{"foo": "bar"}'
- argspec_good_json_dict is successful - argspec_good_json_dict is successful
- > - >-
argspec_good_json_dict.json == '{"foo": "bar"}' argspec_good_json_dict.json == '{"foo": "bar"}'
- argspec_bad_json is failed - argspec_bad_json is failed
- argspec_fail_on_missing_params_bad is failed - argspec_fail_on_missing_params_bad is failed
- argspec_fail_required_together is failed - argspec_fail_required_together is failed
- >-
argspec_suboptions_list_no_elements.suboptions_list_no_elements.0 == {'thing': 'foo'}

@ -1,3 +0,0 @@
cloud/vcenter
shippable/vcenter/incidental
needs/target/incidental_vmware_prepare_tests

@ -1,110 +0,0 @@
# Test code for the vmware_guest_custom_attributes module.
# Copyright: (c) 2018, Abhijeet Kasurde <akasurde@redhat.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# TODO: Current pinned version of vcsim does not support custom fields
# commenting testcase below
- import_role:
name: incidental_vmware_prepare_tests
vars:
setup_attach_host: true
setup_datastore: true
setup_virtualmachines: true
- name: Add custom attribute to the given virtual machine
vmware_guest_custom_attributes:
validate_certs: False
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter: "{{ dc1 }}"
name: "{{ virtual_machines[0].name }}"
folder: "{{ virtual_machines[0].folder }}"
state: present
attributes:
- name: 'sample_1'
value: 'sample_1_value'
- name: 'sample_2'
value: 'sample_2_value'
- name: 'sample_3'
value: 'sample_3_value'
register: guest_info_0001
- debug: var=guest_info_0001
- assert:
that:
- guest_info_0001 is changed
- name: Add custom attribute to the given virtual machine again
vmware_guest_custom_attributes:
validate_certs: False
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter: "{{ dc1 }}"
name: "{{ virtual_machines[0].name }}"
folder: "{{ virtual_machines[0].folder }}"
state: present
attributes:
- name: 'sample_1'
value: 'sample_1_value'
- name: 'sample_2'
value: 'sample_2_value'
- name: 'sample_3'
value: 'sample_3_value'
register: guest_info_0002
- debug: var=guest_info_0002
- assert:
that:
- not (guest_info_0002 is changed)
- name: Remove custom attribute to the given virtual machine
vmware_guest_custom_attributes:
validate_certs: False
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter: "{{ dc1 }}"
name: "{{ virtual_machines[0].name }}"
folder: "{{ virtual_machines[0].folder }}"
state: absent
attributes:
- name: 'sample_1'
- name: 'sample_2'
- name: 'sample_3'
register: guest_info_0004
- debug: msg="{{ guest_info_0004 }}"
- assert:
that:
- "guest_info_0004.changed"
# TODO: vcsim returns duplicate values so removing custom attributes
# results in change. vCenter show correct behavior. Commenting this
# till this is supported by vcsim.
- when: vcsim is not defined
block:
- name: Remove custom attribute to the given virtual machine again
vmware_guest_custom_attributes:
validate_certs: False
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter: "{{ dc1 }}"
name: "{{ virtual_machines[0].name }}"
folder: "{{ virtual_machines[0].folder }}"
state: absent
attributes:
- name: 'sample_1'
- name: 'sample_2'
- name: 'sample_3'
register: guest_info_0005
- debug: var=guest_info_0005
- assert:
that:
- not (guest_info_0005 is changed)

@ -1,259 +0,0 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright, (c) 2018, Ansible Project
# Copyright, (c) 2018, Abhijeet Kasurde <akasurde@redhat.com>
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'
}
DOCUMENTATION = '''
---
module: vmware_guest_custom_attributes
short_description: Manage custom attributes from VMware for the given virtual machine
description:
- This module can be used to add, remove and update custom attributes for the given virtual machine.
version_added: 2.7
author:
- Jimmy Conner (@cigamit)
- Abhijeet Kasurde (@Akasurde)
notes:
- Tested on vSphere 6.5
requirements:
- "python >= 2.6"
- PyVmomi
options:
name:
description:
- Name of the virtual machine to work with.
- This is required parameter, if C(uuid) or C(moid) is not supplied.
type: str
state:
description:
- The action to take.
- If set to C(present), then custom attribute is added or updated.
- If set to C(absent), then custom attribute is removed.
default: 'present'
choices: ['present', 'absent']
type: str
uuid:
description:
- UUID of the virtual machine to manage if known. This is VMware's unique identifier.
- This is required parameter, if C(name) or C(moid) is not supplied.
type: str
moid:
description:
- Managed Object ID of the instance to manage if known, this is a unique identifier only within a single vCenter instance.
- This is required if C(name) or C(uuid) is not supplied.
version_added: '2.9'
type: str
use_instance_uuid:
description:
- Whether to use the VMware instance UUID rather than the BIOS UUID.
default: no
type: bool
version_added: '2.8'
folder:
description:
- Absolute path to find an existing guest.
- This is required parameter, if C(name) is supplied and multiple virtual machines with same name are found.
type: str
datacenter:
description:
- Datacenter name where the virtual machine is located in.
required: True
type: str
attributes:
description:
- A list of name and value of custom attributes that needs to be manage.
- Value of custom attribute is not required and will be ignored, if C(state) is set to C(absent).
default: []
type: list
extends_documentation_fragment: vmware.documentation
'''
EXAMPLES = '''
- name: Add virtual machine custom attributes
vmware_guest_custom_attributes:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
uuid: 421e4592-c069-924d-ce20-7e7533fab926
state: present
attributes:
- name: MyAttribute
value: MyValue
delegate_to: localhost
register: attributes
- name: Add multiple virtual machine custom attributes
vmware_guest_custom_attributes:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
uuid: 421e4592-c069-924d-ce20-7e7533fab926
state: present
attributes:
- name: MyAttribute
value: MyValue
- name: MyAttribute2
value: MyValue2
delegate_to: localhost
register: attributes
- name: Remove virtual machine Attribute
vmware_guest_custom_attributes:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
uuid: 421e4592-c069-924d-ce20-7e7533fab926
state: absent
attributes:
- name: MyAttribute
delegate_to: localhost
register: attributes
- name: Remove virtual machine Attribute using Virtual Machine MoID
vmware_guest_custom_attributes:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
moid: vm-42
state: absent
attributes:
- name: MyAttribute
delegate_to: localhost
register: attributes
'''
RETURN = """
custom_attributes:
description: metadata about the virtual machine attributes
returned: always
type: dict
sample: {
"mycustom": "my_custom_value",
"mycustom_2": "my_custom_value_2",
"sample_1": "sample_1_value",
"sample_2": "sample_2_value",
"sample_3": "sample_3_value"
}
"""
try:
from pyVmomi import vim
except ImportError:
pass
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec
class VmAttributeManager(PyVmomi):
def __init__(self, module):
super(VmAttributeManager, self).__init__(module)
def set_custom_field(self, vm, user_fields):
result_fields = dict()
change_list = list()
changed = False
for field in user_fields:
field_key = self.check_exists(field['name'])
found = False
field_value = field.get('value', '')
for k, v in [(x.name, v.value) for x in self.custom_field_mgr for v in vm.customValue if x.key == v.key]:
if k == field['name']:
found = True
if v != field_value:
if not self.module.check_mode:
self.content.customFieldsManager.SetField(entity=vm, key=field_key.key, value=field_value)
result_fields[k] = field_value
change_list.append(True)
if not found and field_value != "":
if not field_key and not self.module.check_mode:
field_key = self.content.customFieldsManager.AddFieldDefinition(name=field['name'], moType=vim.VirtualMachine)
change_list.append(True)
if not self.module.check_mode:
self.content.customFieldsManager.SetField(entity=vm, key=field_key.key, value=field_value)
result_fields[field['name']] = field_value
if any(change_list):
changed = True
return {'changed': changed, 'failed': False, 'custom_attributes': result_fields}
def check_exists(self, field):
for x in self.custom_field_mgr:
if x.name == field:
return x
return False
def main():
argument_spec = vmware_argument_spec()
argument_spec.update(
datacenter=dict(type='str'),
name=dict(type='str'),
folder=dict(type='str'),
uuid=dict(type='str'),
moid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
state=dict(type='str', default='present',
choices=['absent', 'present']),
attributes=dict(
type='list',
default=[],
options=dict(
name=dict(type='str', required=True),
value=dict(type='str'),
)
),
)
module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True,
required_one_of=[
['name', 'uuid', 'moid']
],
)
if module.params.get('folder'):
# FindByInventoryPath() does not require an absolute path
# so we should leave the input folder path unmodified
module.params['folder'] = module.params['folder'].rstrip('/')
pyv = VmAttributeManager(module)
results = {'changed': False, 'failed': False, 'instance': dict()}
# Check if the virtual machine exists before continuing
vm = pyv.get_vm()
if vm:
# virtual machine already exists
if module.params['state'] == "present":
results = pyv.set_custom_field(vm, module.params['attributes'])
elif module.params['state'] == "absent":
results = pyv.set_custom_field(vm, module.params['attributes'])
module.exit_json(**results)
else:
# virtual machine does not exists
vm_id = (module.params.get('name') or module.params.get('uuid') or module.params.get('moid'))
module.fail_json(msg="Unable to manage custom attributes for non-existing"
" virtual machine %s" % vm_id)
if __name__ == '__main__':
main()
Loading…
Cancel
Save