Repository: ansible-collections/community.vmware

Issue:
vmware_vm_info module throws AttributeError: 'NoneType' object has no attribute 'datastoreUrl'
<!--- Verify first that your issue is not already reported on GitHub -->
<!--- Also test if the latest release and devel branch are affected too -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
<!--- Explain the problem briefly below -->
Module `vmware_vm_info` chokes on objects without datastoreUrl attribute. Most likely this is not an issue under normal circumstances, and the playbook worked for me in the past. It took some time to figure out our vCenter had a couple of inaccessible VMs, which obviously don't have a lot of attributes.

I barely know anything about Python but was able to fix the issue based on examples of similar past issues.
```
diff -u vmware_vm_info.py.bak vmware_vm_info.py
--- vmware_vm_info.py.bak       2022-07-21 09:09:41.482721250 -0500
+++ vmware_vm_info.py   2022-07-21 10:12:08.529573282 -0500
@@ -350,7 +350,8 @@
             datacenter = get_parent_datacenter(vm)
             datastore_url = list()
             datastore_attributes = ('name', 'url')
-            if vm.config.datastoreUrl:
+            has_datastoreurl = _get_vm_prop(vm, ('config', 'datastoreUrl'))
+            if has_datastoreurl:
                 for entry in vm.config.datastoreUrl:
                     datastore_url.append({key: getattr(entry, key) for key in dir(entry) if key in datastore_attributes})
             virtual_machine = {
```

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below, use your best guess if unsure -->
vmware_vm_info

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```paste below
ansible --version
ansible [core 2.12.4]
  config file = /etc/ansible/ansible.cfg
  ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.9.7 (default, Sep 13 2021, 08:18:39) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
  jinja version = 3.1.1
  libyaml = True
```

##### COLLECTION VERSION
<!--- Paste verbatim output from "ansible-galaxy collection list <namespace>.<collection>"  between the quotes
for example: ansible-galaxy collection list community.general
-->
```paste below
Collection       Version
---------------- -------
community.vmware 2.7.0

```

##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
  - name: Get all VMs
    community.vmware.vmware_vm_info:
      hostname: "{{ vcc_hostname }}"
      username: "{{ vcc_user }}"
      password: "{{ vcc_pass }}"
      validate_certs: false
      vm_type: vm
    register: all_vms
```

<!--- HINT: You can paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->
Get a list of all VMs.

##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes -->
```paste below
MODULE_STDERR:

Traceback (most recent call last):
  File "/home/user/.ansible/tmp/ansible-tmp-1658346852.8637314-2616787-129994423621433/AnsiballZ_vmware_vm_info.py", line 107, in <module>
    _ansiballz_main()
  File "/home/user/.ansible/tmp/ansible-tmp-1658346852.8637314-2616787-129994423621433/AnsiballZ_vmware_vm_info.py", line 99, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/home/user/.ansible/tmp/ansible-tmp-1658346852.8637314-2616787-129994423621433/AnsiballZ_vmware_vm_info.py", line 47, in invoke_module
    runpy.run_module(mod_name='ansible_collections.community.vmware.plugins.modules.vmware_vm_info', init_globals=dict(_module_fqn='ansible_collections.community.vmware.plugins.modules.vmware_vm_info', _modlib_path=modlib_path),
  File "/usr/lib64/python3.9/runpy.py", line 210, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib64/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/lib64/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/tmp/ansible_community.vmware.vmware_vm_info_payload_dk_rqyae/ansible_community.vmware.vmware_vm_info_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_vm_info.py", line 409, in <module>
  File "/tmp/ansible_community.vmware.vmware_vm_info_payload_dk_rqyae/ansible_community.vmware.vmware_vm_info_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_vm_info.py", line 403, in main
  File "/tmp/ansible_community.vmware.vmware_vm_info_payload_dk_rqyae/ansible_community.vmware.vmware_vm_info_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_vm_info.py", line 353, in get_virtual_machines
AttributeError: 'NoneType' object has no attribute 'datastoreUrl'

```



Discussion:
Files identified in the description:
None

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->

Resolving patch:
diff --git a/plugins/modules/vmware_vm_info.py b/plugins/modules/vmware_vm_info.py
--- a/plugins/modules/vmware_vm_info.py
+++ b/plugins/modules/vmware_vm_info.py
@@ -351,8 +351,9 @@ def get_virtual_machines(self):
             datacenter = get_parent_datacenter(vm)
             datastore_url = list()
             datastore_attributes = ('name', 'url')
-            if vm.config.datastoreUrl:
-                for entry in vm.config.datastoreUrl:
+            vm_datastore_urls = _get_vm_prop(vm, ('config', 'datastoreUrl'))
+            if vm_datastore_urls:
+                for entry in vm_datastore_urls:
                     datastore_url.append({key: getattr(entry, key) for key in dir(entry) if key in datastore_attributes})
             virtual_machine = {
                 "guest_name": summary.config.name,


Test patch: