Skip to content

Commit

Permalink
test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemorency committed Dec 17, 2024
1 parent 4ad23f0 commit c348446
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
10 changes: 5 additions & 5 deletions plugins/inventory_utils/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def initialize_rest_client(self, config_data):
def get_credentials_from_options(self):
"""
The username and password options can be plain text, jinja templates, or encrypted strings.
This method handles these different options and returns a plain text version of username and password
This method handles these different options and returns a plain text version of the username and password
Returns:
A tuple of plain text username and password
A tuple of the plain text username and password
"""
username = self.get_option("username")
password = self.get_option("password")
Expand Down Expand Up @@ -137,11 +137,11 @@ def update_cached_result(self, cache, cache_key, result):
def get_objects_by_type(self, vim_type):
"""
Searches the requested search paths for objects of type vim_type. If the search path
doesn't actually exist, continue. If no search path is give, check everywhere
doesn't actually exist, continue. If no search path is given, check everywhere
Args:
vim_type: The vim object type. It should be given as a list, like [vim.HostSystem]
Returns:
List of objects that exist in the search path(s) and match vim type
List of objects that exist in the search path(s) and match the vim type
"""
if not self.get_option('search_paths'):
return self.pyvmomi_client.get_all_objs_by_type(vimtype=vim_type)
Expand All @@ -159,7 +159,7 @@ def gather_tags(self, object_moid):
"""
Given an object moid, gather any tags attached to the object.
Args:
object_moid: str, The objects MOID
object_moid: str, The object's MOID
Returns:
tuple
First item is a dict with the object's tags. Keys are tag IDs and values are tag names
Expand Down
16 changes: 8 additions & 8 deletions plugins/module_utils/clients/_pyvmomi.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, connection_params):
def connect_to_api(self, connection_params, disconnect_atexit=True, return_si=False):
"""
Connect to the vCenter/ESXi client using the pyvmomi SDK. This creates a service instance
which can then be used programmatically to make calls to vCenter or ESXi
which can then be used programmatically to make calls to the vCenter or ESXi
Args:
connection_params: dict, A dictionary with different authentication or connection parameters like
username, password, hostname, etc. The full list is found in the method below.
Expand Down Expand Up @@ -157,18 +157,18 @@ def __create_service_instance(self, connection_args, username, password, http_pr
service_instance = connect.SmartConnect(**connection_args)
except vim.fault.InvalidLogin as e:
raise ApiAccessError((
"Unable to log on to vCenter or ESXi API at %s:%s as %s: %s" %
"Unable to log on to the vCenter or ESXi API at %s:%s as %s: %s" %
(connection_args['host'], connection_args['port'], username, e.msg) +
error_msg_suffix
))
except vim.fault.NoPermission as e:
raise ApiAccessError((
"User %s does not have required permission to log on to vCenter or ESXi API at %s:%s : %s" %
"User %s does not have the required permissions to log on to the vCenter or ESXi API at %s:%s : %s" %
(username, connection_args['host'], connection_args['port'], e.msg)
))
except (requests.ConnectionError, ssl.SSLError) as e:
raise ApiAccessError((
"Unable to connect to vCenter or ESXi API at %s on TCP/%s: %s" %
"Unable to connect to the vCenter or ESXi API at %s on TCP/%s: %s" %
(connection_args['host'], connection_args['port'], e)
))
except vmodl.fault.InvalidRequest as e:
Expand All @@ -179,14 +179,14 @@ def __create_service_instance(self, connection_args, username, password, http_pr
))
except Exception as e:
raise ApiAccessError((
"Unknown error while connecting to vCenter or ESXi API at %s:%s : %s" %
"Unknown error while connecting to the vCenter or ESXi API at %s:%s : %s" %
(connection_args['host'], connection_args['port'], e.msg) +
error_msg_suffix
))

if service_instance is None:
raise ApiAccessError((
"Unknown error while connecting to vCenter or ESXi API at %s:%s" %
"Unknown error while connecting to the vCenter or ESXi API at %s:%s" %
(connection_args['host'], connection_args['port']) +
error_msg_suffix
))
Expand All @@ -204,15 +204,15 @@ def check_requirements(self):

def get_all_objs_by_type(self, vimtype, folder=None, recurse=True):
"""
Returns a list of all objects matching a given VMWare type.
Returns a list of all objects matching a given VMware type.
You can also limit the search by folder and recurse if desired
Args:
vimtype: The type of object to search for
folder: vim.Folder, the folder object to use as a base for the search. If
none is provided, the datacenter root will be used
recurse: If true, the search will recurse through the folder structure
Returns:
list of objs
A list of matching objects.
"""
if not folder:
folder = self.content.rootFolder
Expand Down
6 changes: 3 additions & 3 deletions plugins/module_utils/clients/_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def get_tags_by_vm_moid(self, vm_moid):
vm_moid: the VM MOID to use to gather tags
Returns:
List of tag object associated with the given virtual machine
List of tag objects associated with the given virtual machine
"""
dobj = DynamicID(type='VirtualMachine', id=vm_moid)
return self.get_tags_for_dynamic_id_obj(dobj=dobj)
Expand All @@ -176,14 +176,14 @@ def get_tags_by_host_moid(self, host_moid):
host_moid: the Host MOID to use to gather tags
Returns:
List of tag object associated with the given host
List of tag objects associated with the given host
"""
dobj = DynamicID(type='HostSystem', id=host_moid)
return self.get_tags_for_dynamic_id_obj(dobj=dobj)

def get_tags_for_dynamic_id_obj(self, dobj):
"""
Return tag objects associated with an DynamicID object.
Return tag objects associated with a DynamicID object.
Args:
dobj: Dynamic object
Returns:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
---
plugin: vmware.vmware.esxi_hosts

hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
port: "{{ vcenter_port }}"

cache: false
group_by_paths: true
group_by_paths_prefix: test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@
ansible.builtin.command: ansible-inventory -i "{{ role_path }}/files/test.esxi_hosts.yml" --list
register: _inventory_out

- name: Debug Inventory Output
ansible.builtin.debug:
var: _inventory_out

- name: Parse Inventory Results as JSON
ansible.builtin.set_fact:
inventory_results: "{{ _inventory_out.stdout | from_json }}"

- name: Debug Inventory Output Because It Failed
ansible.builtin.debug:
var: _inventory_out
when: not inventory_results._meta.hostvars

# you can't reference the 'all' property here for some reason. It reverts back to the test playbook inventory
# instead of the inventory_results
- name: Check Output
ansible.builtin.assert:
that:
- first_host.ansible_host
- first_host.tags is defined
- first_host.tags_by_category is defined
- first_host.ansible_host is regex('^[\d+\.]+$')
- first_host.tags is defined and first_host.tags is not None
- first_host.tags_by_category is defined and first_host.tags_by_category is not None
- >-
(inventory_results.poweredOn.hosts | length) ==
(inventory_results._meta.hostvars.values() | selectattr('summary.runtime.powerState', 'equalto', 'poweredOn') | length)
Expand Down

0 comments on commit c348446

Please sign in to comment.