Extract shared adapter validation helpers
This commit is contained in:
@@ -8,6 +8,11 @@ from dns_sync.adapters import (
|
||||
PlanAction,
|
||||
PlanOperation,
|
||||
)
|
||||
from dns_sync.adapter_support import (
|
||||
require_action_endpoint,
|
||||
require_desired,
|
||||
require_endpoint_plugin,
|
||||
)
|
||||
from dns_sync.models import DesiredRecord
|
||||
from .provider import Plugin as VerizonProvider
|
||||
|
||||
@@ -32,10 +37,7 @@ class Plugin:
|
||||
)
|
||||
|
||||
def __init__(self, config, endpoint):
|
||||
if endpoint.plugin != self.name:
|
||||
raise RuntimeError(
|
||||
"Verizon adapter requires a verizon endpoint"
|
||||
)
|
||||
require_endpoint_plugin(endpoint, self.name)
|
||||
|
||||
self.config = config
|
||||
self.endpoint = endpoint
|
||||
@@ -121,10 +123,10 @@ class Plugin:
|
||||
)
|
||||
|
||||
def apply(self, action: PlanAction) -> ApplyResult | None:
|
||||
if action.endpoint_id != self.endpoint.identity:
|
||||
raise RuntimeError(
|
||||
"Plan action belongs to a different endpoint"
|
||||
)
|
||||
require_action_endpoint(
|
||||
action,
|
||||
self.endpoint,
|
||||
)
|
||||
|
||||
if action.operation in {
|
||||
PlanOperation.NOOP,
|
||||
@@ -132,12 +134,12 @@ class Plugin:
|
||||
}:
|
||||
return None
|
||||
|
||||
if action.desired is None:
|
||||
raise RuntimeError(
|
||||
"Verizon apply action requires desired state"
|
||||
)
|
||||
desired = require_desired(
|
||||
action,
|
||||
context="Verizon apply action",
|
||||
)
|
||||
|
||||
record = self._desired_record(action.desired)
|
||||
record = self._desired_record(desired)
|
||||
|
||||
if action.operation == PlanOperation.CREATE:
|
||||
detail = self.provider.add_record(record)
|
||||
@@ -168,11 +170,11 @@ class Plugin:
|
||||
)
|
||||
|
||||
applied = NormalizedObject(
|
||||
object_type=action.desired.object_type,
|
||||
logical_key=action.desired.logical_key,
|
||||
object_type=desired.object_type,
|
||||
logical_key=desired.logical_key,
|
||||
local_key=str(slot),
|
||||
payload=dict(action.desired.payload),
|
||||
references=action.desired.references,
|
||||
payload=dict(desired.payload),
|
||||
references=desired.references,
|
||||
)
|
||||
|
||||
return ApplyResult(
|
||||
|
||||
Reference in New Issue
Block a user