trigger.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. import datetime
  18. from typing import Any, Dict, Optional
  19. from airflow.utils import timezone
  20. from airflow.utils.pydantic import BaseModel as BaseModelPydantic, ConfigDict
  21. class TriggerPydantic(BaseModelPydantic):
  22. """Serializable representation of the Trigger ORM SqlAlchemyModel used by internal API."""
  23. # This is technically non-optional, however when we serialize it in from_object we do not have the ID
  24. id: Optional[int]
  25. classpath: str
  26. encrypted_kwargs: str
  27. created_date: datetime.datetime
  28. triggerer_id: Optional[int]
  29. model_config = ConfigDict(from_attributes=True)
  30. def __init__(self, **kwargs) -> None:
  31. from airflow.models.trigger import Trigger
  32. # Here we have to handle two ways the object can be created:
  33. # * when Pydantic recreates it from Trigger model, we need a default __init__ behavior
  34. # * when we create it in from_object - the kwargs will contain classpath, kwargs to create it and
  35. # created_date
  36. if "kwargs" in kwargs:
  37. self.classpath = kwargs.pop("classpath")
  38. self.encrypted_kwargs = Trigger._encrypt_kwargs(kwargs.pop("kwargs"))
  39. self.created_date = kwargs.pop("created_date", timezone.utcnow())
  40. super().__init__(**kwargs)
  41. @property
  42. def kwargs(self) -> Dict[str, Any]:
  43. """Return the decrypted kwargs of the trigger."""
  44. from airflow.models import Trigger
  45. return Trigger._decrypt_kwargs(self.encrypted_kwargs)
  46. @kwargs.setter
  47. def kwargs(self, kwargs: Dict[str, Any]) -> None:
  48. """Set the encrypted kwargs of the trigger."""
  49. from airflow.models import Trigger
  50. self.encrypted_kwargs = Trigger._encrypt_kwargs(kwargs)