Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lookups return empty string when looking up resources inside folders #445

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ThSchrad
Copy link

SUMMARY

When lookups search for resources inside folders, they return an empty string even if the resource is available in this path. For example, a vm_moid lookup with the term /mydc/vm/myfolder1/myvm2 returns an empty string while a lookup for /mydc/vm/myvm1 returns the expected moid.

One could've fixed this issue with no major overhaul of the file. But by looking at the code, I expect that more issues would arise such as finding the wrong folder if multiple folders have the same name, e. g. /mydc/vm/myfolder1/foo and /mydc/vm/myfolder2/foo.

So I rewrote large parts of the file to strictly follow the path top-down. On some occasions the lookups now make multiple requests against the api to ensure we find the correct resource (e. g. finding the root resource pool on a standalone host). This will certainly decrease performance at the expense of correct results.

Also I think the code is no easier to read and reason about as there is no complex recursive function anymore.

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

cluster_moid
datacenter_moid
datastore_moid
folder_moid
host_moid
network_moid
resource_pool_moid
vm_moid

ADDITIONAL INFORMATION

I used an ansible playbook to reproduce the issue. Please note that parts of this playbook (path, moid) must be adjusted to the environment in which the tests are executed. The credentials were set via environment variables.

- name: Test
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Test
      ansible.builtin.debug:
        msg: "{{ lookup('vmware.vmware_rest.' + item['type'] + '_moid', item['path']) == item['moid'] }}"
      loop:
        - type: datacenter
          path: /mydc
          moid: datacenter-xxxxxx
        - type: network
          path: /mydc/network/mynet1
          moid: network-xxxxxx
        - type: datastore
          path: /mydc/datastore/myds1
          moid: datastore-xxxxxx
        - type: folder
          path: /mydc/vm
          moid: group-xxxxxx
        - type: folder
          path: /mydc/vm/myfolder1
          moid: group-xxxxxx
        - type: vm
          path: /mydc/vm/myvm1
          moid: vm-xxxxxx
        - type: vm
          path: /mydc/vm/myfolder1/myvm2
          moid: vm-xxxxxx
        - type: cluster
          path: /mydc/host/mycluster1
          moid: domain-xxxxxx
        - type: host
          path: /mydc/host/mycluster1/myhost1
          moid: host-xxxxxx
        - type: resource_pool
          path: /mydc/host/myhost1/Resources
          moid: resgroup-xxxxxx
        - type: resource_pool
          path: /mydc/host/myhost1/Resources/myrp1
          moid: resgroup-xxxxxx

The output before the changes was:

ok: [localhost] => (item={'type': 'datacenter', 'path': '/mydc', 'moid': 'datacenter-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'network', 'path': '/mydc/network/mynet1', 'moid': 'network-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'datastore', 'path': '/mydc/datastore/myds1', 'moid': 'datastore-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'folder', 'path': '/mydc/vm', 'moid': 'group-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'folder', 'path': '/mydc/vm/myfolder1', 'moid': 'group-xxxxxx'}) => {
    "msg": false
}
ok: [localhost] => (item={'type': 'vm', 'path': '/mydc/vm/myvm1', 'moid': 'vm-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'vm', 'path': '/mydc/vm/myfolder1/myvm2', 'moid': 'vm-xxxxxx'}) => {
    "msg": false
}
ok: [localhost] => (item={'type': 'cluster', 'path': '/mydc/host/mycluster1', 'moid': 'domain-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'host', 'path': '/mydc/host/mycluster1/myhost1', 'moid': 'host-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'resource_pool', 'path': '/mydc/host/myhost1/Resources', 'moid': 'resgroup-xxxxxx'}) => {
    "msg": false
}
ok: [localhost] => (item={'type': 'resource_pool', 'path': '/mydc/host/myhost1/Resources/myrp1', 'moid': 'resgroup-xxxxxx'}) => {
    "msg": false
}

The output after the changes was:

ok: [localhost] => (item={'type': 'datacenter', 'path': '/mydc', 'moid': 'datacenter-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'network', 'path': '/mydc/network/mynet1', 'moid': 'network-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'datastore', 'path': '/mydc/datastore/myds1', 'moid': 'datastore-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'folder', 'path': '/mydc/vm', 'moid': 'group-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'folder', 'path': '/mydc/vm/myfolder1', 'moid': 'group-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'vm', 'path': '/mydc/vm/myvm1', 'moid': 'vm-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'vm', 'path': '/mydc/vm/myfolder1/myvm2', 'moid': 'vm-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'cluster', 'path': '/mydc/host/mycluster1', 'moid': 'domain-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'host', 'path': '/mydc/host/mycluster1/myhost1', 'moid': 'host-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'resource_pool', 'path': '/mydc/host/myhost1/Resources', 'moid': 'resgroup-xxxxxx'}) => {
    "msg": true
}
ok: [localhost] => (item={'type': 'resource_pool', 'path': '/mydc/host/myhost1/Resources/myrp1', 'moid': 'resgroup-xxxxxx'}) => {
    "msg": true
}

@ThSchrad
Copy link
Author

Fixes #359 and #324.

Copy link

@ThSchrad
Copy link
Author

Build failed. https://ansible.softwarefactory-project.io/zuul/buildset/63e29f1c41584dd39f3805078a3d26c2

ansible-test-cloud-integration-vmware-rest FAILURE in 23m 02s ✔️ build-ansible-collection SUCCESS in 11m 20s ✔️ tox-cloud-refresh-examples-vmware SUCCESS in 11m 26s ✔️ ansible-galaxy-importer SUCCESS in 4m 42s

Is this something I can fix? To me it seems that the CI pipeline can't connect to the vcenter.

@alinabuzachis
Copy link
Collaborator

recheck

Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants