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

Add entrypoints for moved modules #17

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ dependencies = [ # runtime deps # https://packaging.python.org/en/latest/guide
# GUIDANCE: only add things that this project imports directly
# GUIDANCE: only set lower version bounds
# "awx_plugins.base_interface.api", # keep `__init__.py` empty
"Django", # <- credentials.injectors (and inventory.plugins indirectly)
"PyYAML", # credentials.injectors, inventory.plugins
"azure-identity", # credentials.azure_kv
"azure-keyvault", # credentials.azure_kv
"boto3", # credentials.awx_secretsmanager
"msrestazure", # credentials.azure_kv
"python-dsv-sdk >= 1.0.4", # credentials.thycotic_dsv
"python-tss-sdk >= 1.2.1", # credentials.thycotic_tss
"requests", # credentials.aim, credentials.centrify_vault, credentials.conjur, credentials.hashivault
Comment on lines +23 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few things to say here:

  1. Q: Are all of these mandatory in all installs? Can individual plugins be invoked w/o depending on others? If yes, maybe the deps should be put into a series of extras through optional-dependencies rather than unconditional ones.
  2. ACTION REQUIRED: https://github.com/ansible/awx-plugins/actions/runs/10599668926/job/29375336003?pr=17#step:17:61 revealed that the awx dependency is missing. It should be listed somewhere. Perhaps, in an extra, depending on what's decided above.

]
classifiers = [ # Allowlist: https://pypi.org/classifiers/
"Development Status :: 1 - Planning",
Expand Down Expand Up @@ -65,12 +74,29 @@ name = "Ansible maintainers and contributors"
# PLUGIN ACTIVATION GUIDANCE (UX):
# `pip install awx_plugins.credentials.x` would auto-activate any plugins the packaged project ships
[project.entry-points."awx_plugins.credentials"] # new entry points group name
x = "awx_plugins.credentials.x.api:XPlugin"

# awx calls `importlib.metadata.entry_points(group='awx.credential_plugins')` to discover and later enable any plugins present in the same env
[project.entry-points."awx.credential_plugins"] # pre-existing entry points group name
x = "awx_plugins.credentials.x.api:XPlugin"
# conjur = awx.main.credential_plugins.conjur:conjur_plugin
conjur = "awx_plugins.credentials.conjur:conjur_plugin"
hashivault_kv = "awx_plugins.credentials.hashivault:hashivault_kv_plugin"
webknjaz marked this conversation as resolved.
Show resolved Hide resolved
hashivault_ssh = "awx_plugins.credentials.hashivault:hashivault_ssh_plugin"
azure_kv = "awx_plugins.credentials.azure_kv:azure_keyvault_plugin"
aim = "awx_plugins.credentials.aim:aim_plugin"
centrify_vault_kv = "awx_plugins.credentials.centrify_vault:centrify_plugin"
thycotic_dsv = "awx_plugins.credentials.dsv:dsv_plugin"
thycotic_tss = "awx_plugins.credentials.tss:tss_plugin"
aws_secretsmanager_credential = "awx_plugins.credentials.aws_secretsmanager:aws_secretmanager_plugin"
Comment on lines +77 to +85
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the inventory plugin missing from here?


[project.entry-points."awx_plugins.inventory"] # new entry points group name
azure-rm = "awx_plugins.inventory.plugins:azure_rm"
ec2 = "awx_plugins.inventory.plugins:ec2"
gce = "awx_plugins.inventory.plugins:gce"
vmware = "awx_plugins.inventory.plugins:vmware"
openstack = "awx_plugins.inventory.plugins:openstack"
rhv = "awx_plugins.inventory.plugins:rhv"
satellite6 = "awx_plugins.inventory.plugins:satellite6"
terraform = "awx_plugins.inventory.plugins:terraform"
controller = "awx_plugins.inventory.plugins:controller"
insights = "awx_plugins.inventory.plugins:insights"
openshift_virtualization = "awx_plugins.inventory.plugins:openshift_virtualization"
constructed = "awx_plugins.inventory.plugins:constructed"

[project.license]
file = "LICENSE"
Expand Down
8 changes: 6 additions & 2 deletions src/awx_plugins/credentials/injectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
import os
import stat
import tempfile
from contextlib import suppress as _suppress_exception

from awx.main.utils.execution_environments import to_container_path
from django.conf import settings

with _suppress_exception(ImportError):
# FIXME: stop suppressing once the circular dependency is untangled
from awx.main.utils.execution_environments import to_container_path
from django.conf import settings

import yaml

Expand Down
31 changes: 27 additions & 4 deletions src/awx_plugins/credentials/plugins.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
# Django
# AWX
from awx.main.models.credential import ManagedCredentialType
from django.utils.translation import gettext_noop
from __future__ import annotations


try:
# Django
# AWX
from awx.main.models.credential import ManagedCredentialType
from django.utils.translation import gettext_noop
except ImportError:
# FIXME: stop suppressing once the circular dependency is untangled
# FIXME: these stubs are temporary
from dataclasses import dataclass

@dataclass(frozen=True)
class ManagedCredentialType:
"""Managed credential type stub."""

namespace: str
name: str
kind: str
inputs: dict[str, list[dict[str, bool | str] | str]]
injectors: dict[str, dict[str, str]] = None
managed: bool = False

def gettext_noop(_text: str) -> str:
"""Emulate a Django-imported no-op."""
return _text


ManagedCredentialType(
Expand Down
5 changes: 0 additions & 5 deletions src/awx_plugins/credentials/x/api.py

This file was deleted.

8 changes: 6 additions & 2 deletions src/awx_plugins/inventory/plugins.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import os.path
import stat
import tempfile
from contextlib import suppress as _suppress_exception

from awx.main.utils.execution_environments import to_container_path
from awx.main.utils.licensing import server_product_name

with _suppress_exception(ImportError):
# FIXME: stop suppressing once the circular dependency is untangled
from awx.main.utils.execution_environments import to_container_path
from awx.main.utils.licensing import server_product_name

import yaml

Expand Down
184 changes: 174 additions & 10 deletions tests/importable_test.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,190 @@
"""Smoke tests related to loading entry points."""

from dataclasses import dataclass
from importlib.metadata import entry_points as _discover_entry_points

import pytest

from awx_plugins.inventory.plugins import PluginFileInjector

@pytest.mark.parametrize(
'entry_points_group',
(
'awx.credential_plugins',

@dataclass(frozen=True)
class EntryPointParam:
"""Data structure representing a single exposed plugin."""

group: str
name: str
spec: str

def __str__(self):
"""Render an entry-point parameter as a string.

To be used as a part of parametrized test ID.
"""
return f'{self.name}={self.spec}@{self.group}'


credential_plugins = (
EntryPointParam(
'awx_plugins.credentials',
'aim',
'awx_plugins.credentials.aim:aim_plugin',
),
EntryPointParam(
'awx_plugins.credentials',
'conjur',
'awx_plugins.credentials.conjur:conjur_plugin',
),
EntryPointParam(
'awx_plugins.credentials',
'hashivault_kv',
'awx_plugins.credentials.hashivault:hashivault_kv_plugin',
),
EntryPointParam(
'awx_plugins.credentials',
'hashivault_ssh',
'awx_plugins.credentials.hashivault:hashivault_ssh_plugin',
),
EntryPointParam(
'awx_plugins.credentials',
'azure_kv',
'awx_plugins.credentials.azure_kv:azure_keyvault_plugin',
),
EntryPointParam(
'awx_plugins.credentials',
'centrify_vault_kv',
'awx_plugins.credentials.centrify_vault:centrify_plugin',
),
EntryPointParam(
'awx_plugins.credentials',
'thycotic_dsv',
'awx_plugins.credentials.dsv:dsv_plugin',
),
EntryPointParam(
'awx_plugins.credentials',
'thycotic_tss',
'awx_plugins.credentials.tss:tss_plugin',
),
EntryPointParam(
'awx_plugins.credentials',
'aws_secretsmanager_credential',
'awx_plugins.credentials.aws_secretsmanager:aws_secretmanager_plugin',
),
)
def test_entry_points_exposed(entry_points_group: str) -> None:
"""Verify the plugin entry point is discoverable.


inventory_plugins = (
EntryPointParam(
'awx_plugins.inventory',
'azure-rm',
'awx_plugins.inventory.plugins:azure_rm',
),
EntryPointParam(
'awx_plugins.inventory',
'ec2',
'awx_plugins.inventory.plugins:ec2',
),
EntryPointParam(
'awx_plugins.inventory',
'gce',
'awx_plugins.inventory.plugins:gce',
),
EntryPointParam(
'awx_plugins.inventory',
'vmware',
'awx_plugins.inventory.plugins:vmware',
),
EntryPointParam(
'awx_plugins.inventory',
'openstack',
'awx_plugins.inventory.plugins:openstack',
),
EntryPointParam(
'awx_plugins.inventory',
'rhv',
'awx_plugins.inventory.plugins:rhv',
),
EntryPointParam(
'awx_plugins.inventory',
'satellite6',
'awx_plugins.inventory.plugins:satellite6',
),
EntryPointParam(
'awx_plugins.inventory',
'terraform',
'awx_plugins.inventory.plugins:terraform',
),
EntryPointParam(
'awx_plugins.inventory',
'controller',
'awx_plugins.inventory.plugins:controller',
),
EntryPointParam(
'awx_plugins.inventory',
'insights',
'awx_plugins.inventory.plugins:insights',
),
EntryPointParam(
'awx_plugins.inventory',
'openshift_virtualization',
'awx_plugins.inventory.plugins:openshift_virtualization',
),
EntryPointParam(
'awx_plugins.inventory',
'constructed',
'awx_plugins.inventory.plugins:constructed',
),
)


with_credential_plugins = pytest.mark.parametrize(
'entry_point',
credential_plugins,
ids=str,
)


with_inventory_plugins = pytest.mark.parametrize(
'entry_point',
inventory_plugins,
ids=str,
)


with_all_plugins = pytest.mark.parametrize(
'entry_point',
credential_plugins + inventory_plugins,
ids=str,
)


@with_all_plugins
def test_entry_points_exposed(entry_point: str) -> None:
"""Verify the plugin entry points are discoverable.

This check relies on the plugin-declaring distribution package to be
pre-installed.
"""
entry_points = _discover_entry_points(group=entry_points_group)
assert 'x' in entry_points.names
entry_points = _discover_entry_points(group=entry_point.group)

assert entry_point.name in entry_points.names
assert entry_points[entry_point.name].value == entry_point.spec


@with_credential_plugins
def test_entry_points_are_credential_plugin(entry_point: str) -> None:
"""Ensure all exposed credential plugins are of the same class."""
entry_points = _discover_entry_points(group=entry_point.group)
loaded_plugin_class = entry_points[entry_point.name].load()

loaded_plugin_class_name = type(loaded_plugin_class).__name__
assert loaded_plugin_class_name == 'CredentialPlugin'


assert entry_points['x'].value == 'awx_plugins.credentials.x.api:XPlugin'
@with_inventory_plugins
def test_entry_points_are_inventory_plugin(entry_point: str) -> None:
"""Ensure all exposed inventory plugins are of the same class."""
entry_points = _discover_entry_points(group=entry_point.group)
loaded_plugin_class = entry_points[entry_point.name].load()

assert callable(entry_points['x'].load())
chrismeyersfsu marked this conversation as resolved.
Show resolved Hide resolved
assert issubclass(loaded_plugin_class, PluginFileInjector)
Loading