diff --git a/sqlserver/datadog_checks/sqlserver/config_models/__init__.py b/sqlserver/datadog_checks/sqlserver/config_models/__init__.py new file mode 100644 index 0000000000000..ba42dbdc7ffb0 --- /dev/null +++ b/sqlserver/datadog_checks/sqlserver/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/sqlserver/datadog_checks/sqlserver/config_models/defaults.py b/sqlserver/datadog_checks/sqlserver/config_models/defaults.py new file mode 100644 index 0000000000000..2387d994dc2d2 --- /dev/null +++ b/sqlserver/datadog_checks/sqlserver/config_models/defaults.py @@ -0,0 +1,144 @@ +# (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_custom_metrics(field, value): + return get_default_field_value(field, value) + + +def shared_global_custom_queries(field, value): + return get_default_field_value(field, value) + + +def shared_service(field, value): + return get_default_field_value(field, value) + + +def instance_adoprovider(field, value): + return 'SQLOLEDB' + + +def instance_ao_database(field, value): + return get_default_field_value(field, value) + + +def instance_autodiscovery_exclude(field, value): + return get_default_field_value(field, value) + + +def instance_autodiscovery_include(field, value): + return get_default_field_value(field, value) + + +def instance_availability_group(field, value): + return get_default_field_value(field, value) + + +def instance_command_timeout(field, value): + return 5 + + +def instance_connection_string(field, value): + return get_default_field_value(field, value) + + +def instance_connector(field, value): + return 'adodbapi' + + +def instance_custom_queries(field, value): + return get_default_field_value(field, value) + + +def instance_database(field, value): + return 'master' + + +def instance_database_autodiscovery(field, value): + return False + + +def instance_database_autodiscovery_interval(field, value): + return 3600 + + +def instance_db_fragmentation_object_names(field, value): + return get_default_field_value(field, value) + + +def instance_driver(field, value): + return 'SQL Server' + + +def instance_dsn(field, value): + return get_default_field_value(field, value) + + +def instance_empty_default_hostname(field, value): + return False + + +def instance_ignore_missing_database(field, value): + return False + + +def instance_include_ao_metrics(field, value): + return False + + +def instance_include_db_fragmentation_metrics(field, value): + return False + + +def instance_include_fci_metrics(field, value): + return False + + +def instance_include_instance_metrics(field, value): + return True + + +def instance_include_task_scheduler_metrics(field, value): + return False + + +def instance_min_collection_interval(field, value): + return 15 + + +def instance_only_emit_local(field, value): + return False + + +def instance_password(field, value): + return get_default_field_value(field, value) + + +def instance_proc_only_if(field, value): + return get_default_field_value(field, value) + + +def instance_proc_only_if_database(field, value): + return 'master' + + +def instance_service(field, value): + return get_default_field_value(field, value) + + +def instance_stored_procedure(field, value): + return get_default_field_value(field, value) + + +def instance_tags(field, value): + return get_default_field_value(field, value) + + +def instance_use_global_custom_queries(field, value): + return 'true' + + +def instance_username(field, value): + return get_default_field_value(field, value) diff --git a/sqlserver/datadog_checks/sqlserver/config_models/instance.py b/sqlserver/datadog_checks/sqlserver/config_models/instance.py new file mode 100644 index 0000000000000..cb3215873d541 --- /dev/null +++ b/sqlserver/datadog_checks/sqlserver/config_models/instance.py @@ -0,0 +1,83 @@ +# (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 Any, Mapping, 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 CustomQuery(BaseModel): + class Config: + allow_mutation = False + + columns: Optional[Sequence[Mapping[str, Any]]] + query: Optional[str] + tags: Optional[Sequence[str]] + + +class InstanceConfig(BaseModel): + class Config: + allow_mutation = False + + adoprovider: Optional[str] + ao_database: Optional[str] + autodiscovery_exclude: Optional[Sequence[str]] + autodiscovery_include: Optional[Sequence[str]] + availability_group: Optional[str] + command_timeout: Optional[int] + connection_string: Optional[str] + connector: Optional[str] + custom_queries: Optional[Sequence[CustomQuery]] + database: Optional[str] + database_autodiscovery: Optional[bool] + database_autodiscovery_interval: Optional[int] + db_fragmentation_object_names: Optional[Sequence[str]] + driver: Optional[str] + dsn: Optional[str] + empty_default_hostname: Optional[bool] + host: str + ignore_missing_database: Optional[bool] + include_ao_metrics: Optional[bool] + include_db_fragmentation_metrics: Optional[bool] + include_fci_metrics: Optional[bool] + include_instance_metrics: Optional[bool] + include_task_scheduler_metrics: Optional[bool] + min_collection_interval: Optional[float] + only_emit_local: Optional[bool] + password: Optional[str] + proc_only_if: Optional[str] + proc_only_if_database: Optional[str] + service: Optional[str] + stored_procedure: Optional[str] + tags: Optional[Sequence[str]] + use_global_custom_queries: Optional[str] + username: Optional[str] + + @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/sqlserver/datadog_checks/sqlserver/config_models/shared.py b/sqlserver/datadog_checks/sqlserver/config_models/shared.py new file mode 100644 index 0000000000000..1033560ba7398 --- /dev/null +++ b/sqlserver/datadog_checks/sqlserver/config_models/shared.py @@ -0,0 +1,44 @@ +# (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 Any, Mapping, 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 SharedConfig(BaseModel): + class Config: + allow_mutation = False + + custom_metrics: Optional[Sequence[Mapping[str, Any]]] + global_custom_queries: Optional[Sequence[Mapping[str, Any]]] + 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/sqlserver/datadog_checks/sqlserver/config_models/validators.py b/sqlserver/datadog_checks/sqlserver/config_models/validators.py new file mode 100644 index 0000000000000..9d0b0155542cb --- /dev/null +++ b/sqlserver/datadog_checks/sqlserver/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)