Skip to content

Commit

Permalink
linnode inventory, remove redundant (#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 <felix@fontein.de>
  • Loading branch information
bcoca and felixfontein committed Nov 1, 2022
1 parent a1f75ef commit 20b84fc
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 20b84fc

Please sign in to comment.