import jsonschema
from kingpin.actors import exceptions
[docs]
class REQUIRED:
"""Meta class to identify required arguments for actors."""
[docs]
class StringCompareBase:
"""Meta class to identify the desired state for a resource.
This basic type of constant allows someone to easily define a set of valid
strings for their option and have the base actor class automatically
validate the inputs against those strings.
"""
valid = None
@classmethod
def validate(self, option: object) -> None:
if option not in self.valid:
raise exceptions.InvalidOptions(f"{option} not valid, use: {self.valid}")
[docs]
class STATE(StringCompareBase):
"""Meta class to identify the desired state for a resource.
Simple tester for 'present' or 'absent' on actors. Used for any actor thats
idempotent and used to ensure some state of a resource.
"""
valid = ("present", "absent")
[docs]
class SchemaCompareBase:
"""Meta class that compares the schema of a dict against rules."""
SCHEMA = None
@classmethod
def validate(self, option: object) -> None:
try:
jsonschema.Draft202012Validator(self.SCHEMA).validate(option)
except jsonschema.exceptions.ValidationError as e:
raise exceptions.InvalidOptions(
f"Supplied parameter does not match schema: {e}"
) from e