Skip to content

Commit

Permalink
linnode inventory, remove redundant (ansible-collections#5438)
Browse files Browse the repository at this point in the history
* remove redundant

templar is already in base class
env var is already consulted in via config resolution

* more whites

* no need to import templar again

* Add changelog fragment.

* Try to update tests.

Co-authored-by: Felix Fontein <[email protected]>
  • Loading branch information
2 people authored and Dušan Markovič committed Nov 7, 2022
1 parent c503c92 commit d12cfd7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/5438-linode.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "linode inventory plugin - simplify option handling (https://github.com/ansible-collections/community.general/pull/5438)."
15 changes: 3 additions & 12 deletions plugins/inventory/linode.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@
from ansible.errors import AnsibleError, AnsibleParserError
from ansible.module_utils.six import string_types
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
from ansible.template import Templar


try:
Expand All @@ -145,22 +144,14 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def _build_client(self, loader):
"""Build the Linode client."""

t = Templar(loader=loader)

access_token = self.get_option('access_token')
if t.is_template(access_token):
access_token = t.template(variable=access_token, disable_lookups=False)

if access_token is None:
try:
access_token = os.environ['LINODE_ACCESS_TOKEN']
except KeyError:
pass
if self.templar.is_template(access_token):
access_token = self.templar.template(variable=access_token, disable_lookups=False)

if access_token is None:
raise AnsibleError((
'Could not retrieve Linode access token '
'from plugin configuration or environment'
'from plugin configuration sources'
))

self.client = LinodeClient(access_token)
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/plugins/inventory/test_linode.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@

from ansible.errors import AnsibleError, AnsibleParserError
from ansible.parsing.dataloader import DataLoader
from ansible.template import Templar
from ansible_collections.community.general.plugins.inventory.linode import InventoryModule


@pytest.fixture(scope="module")
def inventory():
return InventoryModule()
plugin = InventoryModule()
plugin.templar = Templar(loader=DataLoader())
return plugin


def test_missing_access_token_lookup(inventory):
Expand Down

0 comments on commit d12cfd7

Please sign in to comment.