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

[PR #4524/8e72e98a backport][stable-4] Implement contructable support for opennebula inventory plugin: keyed… #4549

Conversation

patchback[bot]
Copy link

@patchback patchback bot commented Apr 21, 2022

This is a backport of PR #4524 as merged into main (8e72e98).

…_groups, compose, groups

SUMMARY

#4497 noted that the opennebula inventory plugin inherits Constructable, and the doc string also includes the macro(?) for that, however the code does not implement the standard dynamic inventory features such as keyed_groups, etc. This PR aims to implement that functionality.

I have included a new unit test, as well as new test-fixture data and re-worked some of the helper logic in the tests. Hopefully @feldsam is ok with the changes. I would be happy to refactor the pre-existing test and move the pre-existing sample data to the external file to make it match what I did.

Note, the PR is incomplete as not all of the testing logic passes, see below.

Fixes #4497

ISSUE TYPE
  • Bugfix Pull Request
  • Feature Pull Request
COMPONENT NAME

opennebula inventory plugin

ADDITIONAL INFORMATION

The code currently produces the output I would expect, however the unit test I added fails due to a traceback. I looked briefly into this, but wasn't quite sure why it was failing. The two test_ functions I added are identical except that one of them has commented out portions that are currently failing here:

[gw1] linux -- Python 3.8.0 /usr/bin/python3.8
inventory = <ansible_collections.community.general.plugins.inventory.opennebula.InventoryModule object at 0x7f1ceb5ce5b0>
mocker = <pytest_mock.plugin.MockerFixture object at 0x7f1ceb5ce6d0>

    def test_populate_constructable_templating(inventory, mocker):
        # bypass API fetch call
        inventory._get_vm_pool = mocker.MagicMock(side_effect=get_vm_pool_json)
        inventory.get_option = mocker.MagicMock(side_effect=mk_get_options(options_constructable_test))
>       inventory._populate()

tests/unit/plugins/inventory/test_opennebula.py:288: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
plugins/inventory/opennebula.py:239: in _populate
    self._add_host_to_composed_groups(self.get_option('groups'), server, hostname, strict=strict)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <ansible_collections.community.general.plugins.inventory.opennebula.InventoryModule object at 0x7f1ceb5ce5b0>
groups = {'benchmark_clients': "TGROUP.endswith('clients')", 'lin': 'is_linux == True'}
variables = {'GUEST_OS': 'linux', 'INPUTS_ORDER': '', 'LABELS': ['foo', 'bench'], 'LOGO': 'images/logos/linux.png', ...}
host = 'terraform_demo_00', strict = False, fetch_hostvars = True

    def _add_host_to_composed_groups(self, groups, variables, host, strict=False, fetch_hostvars=True):
        ''' helper to create complex groups for plugins based on jinja2 conditionals, hosts that meet the conditional are added to group'''
        # process each 'group entry'
        if groups and isinstance(groups, dict):
            if fetch_hostvars:
                variables = combine_vars(variables, self.inventory.get_host(host).get_vars())
>           self.templar.available_variables = variables
E           AttributeError: 'InventoryModule' object has no attribute 'templar'

/root/ansible/lib/ansible/plugins/inventory/__init__.py:369: AttributeError

#4524)

* Implement contructable support for opennebula inventory plugin: keyed_groups, compose, groups

* Fixed templating mock issues in unit tests, corrected some linting errors

* trying to make the linter happy

* Now trying to make python2.7 happy

* Added changelog fragment

* changelog fragment needs pluralization

* Update changelogs/fragments/4524-update-opennebula-inventory-plugin-to-match-documentation.yaml

Co-authored-by: Felix Fontein <[email protected]>

Co-authored-by: Felix Fontein <[email protected]>
(cherry picked from commit 8e72e98)
@ansibullbot ansibullbot added backport bug This issue/PR relates to a bug cloud inventory inventory plugin new_contributor Help guide this first time contributor plugins plugin (any type) tests tests unit tests/unit labels Apr 21, 2022
@felixfontein felixfontein merged commit 5f59ec2 into stable-4 Apr 21, 2022
@felixfontein felixfontein deleted the patchback/backports/stable-4/8e72e98adbf9ad0b16523605ca6c1d0deff170c6/pr-4524 branch April 21, 2022 12:03
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 cloud inventory inventory plugin new_contributor Help guide this first time contributor plugins plugin (any type) tests tests unit tests/unit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants