-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
61 changed files
with
3,228 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
datadog_checks_base/datadog_checks/base/utils/models/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# (C) Datadog, Inc. 2021-present | ||
# All rights reserved | ||
# Licensed under a 3-clause BSD style license (see LICENSE) |
17 changes: 17 additions & 0 deletions
17
datadog_checks_base/datadog_checks/base/utils/models/fields.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# (C) Datadog, Inc. 2021-present | ||
# All rights reserved | ||
# Licensed under a 3-clause BSD style license (see LICENSE) | ||
from pydantic.fields import SHAPE_MAPPING, SHAPE_SEQUENCE, SHAPE_SINGLETON | ||
|
||
|
||
def get_default_field_value(field, value): | ||
if field.shape == SHAPE_MAPPING: | ||
return {} | ||
elif field.shape == SHAPE_SEQUENCE: | ||
return [] | ||
elif field.shape == SHAPE_SINGLETON: | ||
field_type = field.type_ | ||
if field_type in (float, int, str): | ||
return field_type() | ||
|
||
return value |
16 changes: 16 additions & 0 deletions
16
datadog_checks_base/datadog_checks/base/utils/models/types.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# (C) Datadog, Inc. 2021-present | ||
# All rights reserved | ||
# Licensed under a 3-clause BSD style license (see LICENSE) | ||
from collections.abc import Mapping, Sequence | ||
|
||
from immutables import Map | ||
|
||
|
||
def make_immutable_check_config(obj): | ||
if isinstance(obj, Sequence) and not isinstance(obj, str): | ||
return tuple(make_immutable_check_config(item) for item in obj) | ||
elif isinstance(obj, Mapping): | ||
# There are no ordering guarantees, see https://github.com/MagicStack/immutables/issues/57 | ||
return Map((k, make_immutable_check_config(v)) for k, v in obj.items()) | ||
|
||
return obj |
4 changes: 4 additions & 0 deletions
4
datadog_checks_base/datadog_checks/base/utils/models/validation/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# (C) Datadog, Inc. 2021-present | ||
# All rights reserved | ||
# Licensed under a 3-clause BSD style license (see LICENSE) | ||
from . import core |
21 changes: 21 additions & 0 deletions
21
datadog_checks_base/datadog_checks/base/utils/models/validation/core.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# (C) Datadog, Inc. 2021-present | ||
# All rights reserved | ||
# Licensed under a 3-clause BSD style license (see LICENSE) | ||
from ..types import make_immutable_check_config | ||
|
||
|
||
def initialize_config(values, **kwargs): | ||
# This is what is returned by the initial root validator of each config model. | ||
return values | ||
|
||
|
||
def finalize_config(values, **kwargs): | ||
# This is what is returned by the final root validator of each config model. Note: | ||
# | ||
# 1. the final object must be a dict | ||
# 2. we maintain the original order of keys | ||
return {field: make_immutable_check_config(value) for field, value in values.items()} | ||
|
||
|
||
def handle_deprecations(values, model_id, deprecations): | ||
return values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# (C) Datadog, Inc. 2021-present | ||
# All rights reserved | ||
# Licensed under a 3-clause BSD style license (see LICENSE) |
18 changes: 18 additions & 0 deletions
18
datadog_checks_base/tests/models/config_models/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ConfigProvider: | ||
_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 |
52 changes: 52 additions & 0 deletions
52
datadog_checks_base/tests/models/config_models/defaults.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# (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_deprecated(field, value): | ||
return get_default_field_value(field, value) | ||
|
||
|
||
def shared_timeout(field, value): | ||
return get_default_field_value(field, value) | ||
|
||
|
||
def instance_array(field, value): | ||
return get_default_field_value(field, value) | ||
|
||
|
||
def instance_deprecated(field, value): | ||
return get_default_field_value(field, value) | ||
|
||
|
||
def instance_flag(field, value): | ||
return False | ||
|
||
|
||
def instance_hyphenated_name(field, value): | ||
return get_default_field_value(field, value) | ||
|
||
|
||
def instance_mapping(field, value): | ||
return get_default_field_value(field, value) | ||
|
||
|
||
def instance_obj(field, value): | ||
return get_default_field_value(field, value) | ||
|
||
|
||
def instance_pass_(field, value): | ||
return get_default_field_value(field, value) | ||
|
||
|
||
def instance_pid(field, value): | ||
return get_default_field_value(field, value) | ||
|
||
|
||
def instance_text(field, value): | ||
return get_default_field_value(field, value) | ||
|
||
|
||
def instance_timeout(field, value): | ||
return get_default_field_value(field, value) |
11 changes: 11 additions & 0 deletions
11
datadog_checks_base/tests/models/config_models/deprecations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# (C) Datadog, Inc. 2021-present | ||
# All rights reserved | ||
# Licensed under a 3-clause BSD style license (see LICENSE) | ||
|
||
|
||
def shared(): | ||
return {'deprecated': {'Release': '8.0.0', 'Migration': 'do this\nand that\n'}} | ||
|
||
|
||
def instance(): | ||
return {'deprecated': {'Release': '9.0.0', 'Migration': 'do this\nand that\n'}} |
63 changes: 63 additions & 0 deletions
63
datadog_checks_base/tests/models/config_models/instance.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# (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, Field, root_validator, validator | ||
|
||
from datadog_checks.base.utils.functions import identity | ||
from datadog_checks.base.utils.models import validation | ||
|
||
from . import defaults, deprecations, validators | ||
|
||
|
||
class Obj(BaseModel): | ||
class Config: | ||
allow_mutation = False | ||
|
||
bar: Optional[Sequence[str]] | ||
foo: bool | ||
|
||
|
||
class InstanceConfig(BaseModel): | ||
class Config: | ||
allow_mutation = False | ||
|
||
array: Optional[Sequence[str]] | ||
deprecated: Optional[str] | ||
flag: Optional[bool] | ||
hyphenated_name: Optional[str] = Field(None, alias='hyphenated-name') | ||
mapping: Optional[Mapping[str, Any]] | ||
obj: Optional[Obj] | ||
pass_: Optional[str] = Field(None, alias='pass') | ||
pid: Optional[int] | ||
text: Optional[str] | ||
timeout: Optional[float] | ||
|
||
@root_validator(pre=True) | ||
def _handle_deprecations(cls, values): | ||
return validation.core.handle_deprecations(values, 'instance', deprecations.instance()) | ||
|
||
@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)) |
Oops, something went wrong.