Extract shared adapter validation helpers
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
from .adapters import Endpoint, NormalizedObject, PlanAction
|
||||
|
||||
|
||||
def require_endpoint_plugin(
|
||||
endpoint: Endpoint,
|
||||
plugin_name: str,
|
||||
) -> None:
|
||||
expected = plugin_name.strip().lower()
|
||||
|
||||
if not expected:
|
||||
raise ValueError("Adapter plugin name cannot be empty")
|
||||
|
||||
if endpoint.plugin != expected:
|
||||
raise RuntimeError(
|
||||
f"Adapter {expected!r} cannot use endpoint "
|
||||
f"{endpoint.identity!r}"
|
||||
)
|
||||
|
||||
|
||||
def require_action_endpoint(
|
||||
action: PlanAction,
|
||||
endpoint: Endpoint,
|
||||
) -> None:
|
||||
if action.endpoint_id != endpoint.identity:
|
||||
raise RuntimeError(
|
||||
f"Plan action for {action.endpoint_id!r} does not belong "
|
||||
f"to adapter endpoint {endpoint.identity!r}"
|
||||
)
|
||||
|
||||
|
||||
def require_desired(
|
||||
action: PlanAction,
|
||||
*,
|
||||
context: str = "Adapter apply",
|
||||
) -> NormalizedObject:
|
||||
if action.desired is None:
|
||||
raise RuntimeError(
|
||||
f"{context} requires desired state"
|
||||
)
|
||||
|
||||
return action.desired
|
||||
@@ -13,6 +13,11 @@ from ...adapters import (
|
||||
PlanOperation,
|
||||
RetireRequest,
|
||||
)
|
||||
from ...adapter_support import (
|
||||
require_action_endpoint,
|
||||
require_desired,
|
||||
require_endpoint_plugin,
|
||||
)
|
||||
from ...certificates.material import CertificateMaterial
|
||||
from ...npm_client import NpmClient
|
||||
from ...object_normalizer import normalize_discovered_object
|
||||
@@ -67,6 +72,7 @@ class Plugin:
|
||||
)
|
||||
|
||||
def __init__(self, config, endpoint):
|
||||
require_endpoint_plugin(endpoint, self.name)
|
||||
self.config = config
|
||||
self.endpoint = endpoint
|
||||
self.client = NpmClient(config, endpoint)
|
||||
@@ -593,17 +599,15 @@ class Plugin:
|
||||
self,
|
||||
action: PlanAction,
|
||||
) -> ApplyResult | None:
|
||||
desired = action.desired
|
||||
desired = require_desired(
|
||||
action,
|
||||
context="NPM apply",
|
||||
)
|
||||
|
||||
if desired is None:
|
||||
raise RuntimeError(
|
||||
"NPM apply requires desired state"
|
||||
)
|
||||
|
||||
if action.endpoint_id != self.endpoint.identity:
|
||||
raise RuntimeError(
|
||||
"NPM apply action endpoint does not match adapter"
|
||||
)
|
||||
require_action_endpoint(
|
||||
action,
|
||||
self.endpoint,
|
||||
)
|
||||
|
||||
if (
|
||||
desired.object_type == CERTIFICATES
|
||||
|
||||
Reference in New Issue
Block a user