-
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
49 changed files
with
2,593 additions
and
15 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 |
15 changes: 15 additions & 0 deletions
15
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,15 @@ | ||
# (C) Datadog, Inc. 2021-present | ||
# All rights reserved | ||
# Licensed under a 3-clause BSD style license (see LICENSE) | ||
from immutables import Map | ||
|
||
|
||
def make_immutable_check_config(obj): | ||
# Only consider container types that can be de-serialized from YAML/JSON | ||
if isinstance(obj, list): | ||
return tuple(make_immutable_check_config(item) for item in obj) | ||
elif isinstance(obj, dict): | ||
# 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 |
17 changes: 17 additions & 0 deletions
17
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,17 @@ | ||
# (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): | ||
# This is what is returned by the initial root validator of each config model. | ||
return values | ||
|
||
|
||
def finalize_config(values): | ||
# 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()} |
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) |
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,44 @@ | ||
# (C) Datadog, Inc. 2021-present | ||
# All rights reserved | ||
# Licensed under a 3-clause BSD style license (see LICENSE) | ||
from ..utils import requires_py3 | ||
|
||
pytestmark = [requires_py3] | ||
|
||
|
||
def test_make_immutable_check_config(): | ||
# TODO: move imports up top when we drop Python 2 | ||
from immutables import Map | ||
|
||
from datadog_checks.base.utils.models.types import make_immutable_check_config | ||
|
||
obj = make_immutable_check_config( | ||
{ | ||
'string': 'foo', | ||
'integer': 9000, | ||
'float': 3.14, | ||
'boolean': True, | ||
'array': [{'key': 'foo'}, {'key': 'bar'}], | ||
'mapping': {'foo': 'bar'}, | ||
} | ||
) | ||
|
||
assert isinstance(obj, Map) | ||
assert len(obj) == 6 | ||
assert isinstance(obj['string'], str) | ||
assert obj['string'] == 'foo' | ||
assert isinstance(obj['integer'], int) | ||
assert obj['integer'] == 9000 | ||
assert isinstance(obj['float'], float) | ||
assert obj['float'] == 3.14 | ||
assert isinstance(obj['boolean'], bool) | ||
assert obj['boolean'] is True | ||
assert isinstance(obj['array'], tuple) | ||
assert len(obj['array']) == 2 | ||
assert isinstance(obj['array'][0], Map) | ||
assert obj['array'][0]['key'] == 'foo' | ||
assert isinstance(obj['array'][1], Map) | ||
assert obj['array'][1]['key'] == 'bar' | ||
assert isinstance(obj['mapping'], Map) | ||
assert len(obj['mapping']) == 1 | ||
assert obj['mapping']['foo'] == 'bar' |
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
Oops, something went wrong.