Skip to content

Commit

Permalink
Add runtime configuration validation (#8983)
Browse files Browse the repository at this point in the history
* Sync config models

* re-sync

* sync config models

* see #9750

Co-authored-by: Fanny Jiang <[email protected]>
  • Loading branch information
ofek and fanny-jiang authored Sep 29, 2021
1 parent 314f11b commit 5d949a6
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 0 deletions.
18 changes: 18 additions & 0 deletions snowflake/datadog_checks/snowflake/config_models/__init__.py
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 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
96 changes: 96 additions & 0 deletions snowflake/datadog_checks/snowflake/config_models/defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# (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_global_custom_queries(field, value):
return get_default_field_value(field, value)


def shared_proxy_host(field, value):
return get_default_field_value(field, value)


def shared_proxy_password(field, value):
return get_default_field_value(field, value)


def shared_proxy_port(field, value):
return get_default_field_value(field, value)


def shared_proxy_user(field, value):
return get_default_field_value(field, value)


def shared_service(field, value):
return get_default_field_value(field, value)


def instance_authenticator(field, value):
return 'snowflake'


def instance_client_prefetch_threads(field, value):
return 4


def instance_client_session_keep_alive(field, value):
return False


def instance_custom_queries(field, value):
return get_default_field_value(field, value)


def instance_database(field, value):
return 'SNOWFLAKE'


def instance_disable_generic_tags(field, value):
return False


def instance_empty_default_hostname(field, value):
return False


def instance_login_timeout(field, value):
return 60


def instance_metric_groups(field, value):
return get_default_field_value(field, value)


def instance_min_collection_interval(field, value):
return 3600


def instance_ocsp_response_cache_filename(field, value):
return get_default_field_value(field, value)


def instance_schema(field, value):
return 'ACCOUNT_USAGE'


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_token(field, value):
return get_default_field_value(field, value)


def instance_use_global_custom_queries(field, value):
return 'true'


def instance_warehouse(field, value):
return get_default_field_value(field, value)
71 changes: 71 additions & 0 deletions snowflake/datadog_checks/snowflake/config_models/instance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# (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, 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

account: str
authenticator: Optional[str]
client_prefetch_threads: Optional[int]
client_session_keep_alive: Optional[bool]
custom_queries: Optional[Sequence[CustomQuery]]
database: Optional[str]
disable_generic_tags: Optional[bool]
empty_default_hostname: Optional[bool]
login_timeout: Optional[int]
metric_groups: Optional[Sequence[str]]
min_collection_interval: Optional[float]
ocsp_response_cache_filename: Optional[str]
password: str
role: str
schema_: Optional[str] = Field(None, alias='schema')
service: Optional[str]
tags: Optional[Sequence[str]]
token: Optional[str]
use_global_custom_queries: Optional[str]
username: str
warehouse: 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))
47 changes: 47 additions & 0 deletions snowflake/datadog_checks/snowflake/config_models/shared.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# (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

global_custom_queries: Optional[Sequence[Mapping[str, Any]]]
proxy_host: Optional[str]
proxy_password: Optional[str]
proxy_port: Optional[int]
proxy_user: Optional[str]
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))
11 changes: 11 additions & 0 deletions snowflake/datadog_checks/snowflake/config_models/validators.py
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 initialize_instance(values, **kwargs):
# TODO: remove when deprecation is finalized https://github.com/DataDog/integrations-core/pull/9340
if 'username' not in values and 'user' in values:
values['username'] = values['user']

return values

0 comments on commit 5d949a6

Please sign in to comment.