From d9a3db383e14748ea7ad3790244954fd14dd95e4 Mon Sep 17 00:00:00 2001 From: Ofek Lev Date: Mon, 17 May 2021 12:17:21 -0400 Subject: [PATCH] Sync config models (#8994) --- .../tcp_check/config_models/__init__.py | 18 +++++++ .../tcp_check/config_models/defaults.py | 32 ++++++++++++ .../tcp_check/config_models/instance.py | 50 +++++++++++++++++++ .../tcp_check/config_models/shared.py | 42 ++++++++++++++++ .../tcp_check/config_models/validators.py | 3 ++ 5 files changed, 145 insertions(+) create mode 100644 tcp_check/datadog_checks/tcp_check/config_models/__init__.py create mode 100644 tcp_check/datadog_checks/tcp_check/config_models/defaults.py create mode 100644 tcp_check/datadog_checks/tcp_check/config_models/instance.py create mode 100644 tcp_check/datadog_checks/tcp_check/config_models/shared.py create mode 100644 tcp_check/datadog_checks/tcp_check/config_models/validators.py diff --git a/tcp_check/datadog_checks/tcp_check/config_models/__init__.py b/tcp_check/datadog_checks/tcp_check/config_models/__init__.py new file mode 100644 index 0000000000000..ba42dbdc7ffb0 --- /dev/null +++ b/tcp_check/datadog_checks/tcp_check/config_models/__init__.py @@ -0,0 +1,18 @@ +# (C) Datadog, Inc. 2021-present +# All rights reserved +# Licensed under a 3-clause BSD style license (see LICENSE) +from .instance import InstanceConfig +from .shared import SharedConfig + + +class ConfigMixin: + _config_model_instance: InstanceConfig + _config_model_shared: SharedConfig + + @property + def config(self) -> InstanceConfig: + return self._config_model_instance + + @property + def shared_config(self) -> SharedConfig: + return self._config_model_shared diff --git a/tcp_check/datadog_checks/tcp_check/config_models/defaults.py b/tcp_check/datadog_checks/tcp_check/config_models/defaults.py new file mode 100644 index 0000000000000..456eb4809b3a2 --- /dev/null +++ b/tcp_check/datadog_checks/tcp_check/config_models/defaults.py @@ -0,0 +1,32 @@ +# (C) Datadog, Inc. 2021-present +# All rights reserved +# Licensed under a 3-clause BSD style license (see LICENSE) +from datadog_checks.base.utils.models.fields import get_default_field_value + + +def shared_service(field, value): + return get_default_field_value(field, value) + + +def instance_collect_response_time(field, value): + return False + + +def instance_empty_default_hostname(field, value): + return False + + +def instance_min_collection_interval(field, value): + return 15 + + +def instance_service(field, value): + return get_default_field_value(field, value) + + +def instance_tags(field, value): + return get_default_field_value(field, value) + + +def instance_timeout(field, value): + return 10 diff --git a/tcp_check/datadog_checks/tcp_check/config_models/instance.py b/tcp_check/datadog_checks/tcp_check/config_models/instance.py new file mode 100644 index 0000000000000..d6f84c0cbac70 --- /dev/null +++ b/tcp_check/datadog_checks/tcp_check/config_models/instance.py @@ -0,0 +1,50 @@ +# (C) Datadog, Inc. 2021-present +# All rights reserved +# Licensed under a 3-clause BSD style license (see LICENSE) +from __future__ import annotations + +from typing import Optional, Sequence + +from pydantic import BaseModel, root_validator, validator + +from datadog_checks.base.utils.functions import identity +from datadog_checks.base.utils.models import validation + +from . import defaults, validators + + +class InstanceConfig(BaseModel): + class Config: + allow_mutation = False + + collect_response_time: Optional[bool] + empty_default_hostname: Optional[bool] + host: str + min_collection_interval: Optional[float] + name: str + port: int + service: Optional[str] + tags: Optional[Sequence[str]] + timeout: Optional[int] + + @root_validator(pre=True) + def _initial_validation(cls, values): + return validation.core.initialize_config(getattr(validators, 'initialize_instance', identity)(values)) + + @validator('*', pre=True, always=True) + def _ensure_defaults(cls, v, field): + if v is not None or field.required: + return v + + return getattr(defaults, f'instance_{field.name}')(field, v) + + @validator('*') + def _run_validations(cls, v, field): + if not v: + return v + + return getattr(validators, f'instance_{field.name}', identity)(v, field=field) + + @root_validator(pre=False) + def _final_validation(cls, values): + return validation.core.finalize_config(getattr(validators, 'finalize_instance', identity)(values)) diff --git a/tcp_check/datadog_checks/tcp_check/config_models/shared.py b/tcp_check/datadog_checks/tcp_check/config_models/shared.py new file mode 100644 index 0000000000000..d1c10eced36ca --- /dev/null +++ b/tcp_check/datadog_checks/tcp_check/config_models/shared.py @@ -0,0 +1,42 @@ +# (C) Datadog, Inc. 2021-present +# All rights reserved +# Licensed under a 3-clause BSD style license (see LICENSE) +from __future__ import annotations + +from typing import Optional + +from pydantic import BaseModel, root_validator, validator + +from datadog_checks.base.utils.functions import identity +from datadog_checks.base.utils.models import validation + +from . import defaults, validators + + +class SharedConfig(BaseModel): + class Config: + allow_mutation = False + + service: Optional[str] + + @root_validator(pre=True) + def _initial_validation(cls, values): + return validation.core.initialize_config(getattr(validators, 'initialize_shared', identity)(values)) + + @validator('*', pre=True, always=True) + def _ensure_defaults(cls, v, field): + if v is not None or field.required: + return v + + return getattr(defaults, f'shared_{field.name}')(field, v) + + @validator('*') + def _run_validations(cls, v, field): + if not v: + return v + + return getattr(validators, f'shared_{field.name}', identity)(v, field=field) + + @root_validator(pre=False) + def _final_validation(cls, values): + return validation.core.finalize_config(getattr(validators, 'finalize_shared', identity)(values)) diff --git a/tcp_check/datadog_checks/tcp_check/config_models/validators.py b/tcp_check/datadog_checks/tcp_check/config_models/validators.py new file mode 100644 index 0000000000000..9d0b0155542cb --- /dev/null +++ b/tcp_check/datadog_checks/tcp_check/config_models/validators.py @@ -0,0 +1,3 @@ +# (C) Datadog, Inc. 2021-present +# All rights reserved +# Licensed under a 3-clause BSD style license (see LICENSE)