Skip to content

Commit

Permalink
pydantic 2: remove deprecated function calls
Browse files Browse the repository at this point in the history
 * dict -> model_dump
 * json -> model_dump_json
 * parse_obj_as -> TypeAdapter
 * parse_file_as -> open().read() + model.model_validate_json

Change-Id: I9d56acee3f5f9cdebc5d3447197b6e462e0c8b50
  • Loading branch information
Christoph Rauch committed Nov 16, 2023
1 parent fb8e6a1 commit 291e3c9
Show file tree
Hide file tree
Showing 47 changed files with 125 additions and 96 deletions.
2 changes: 1 addition & 1 deletion cmk/base/plugins/agent_based/aws_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Section:


def parse_string_table(string_table: type_defs.StringTable) -> Section:
agent_output = AgentOutput.parse_raw(string_table[0][0])
agent_output = AgentOutput.model_validate_json(string_table[0][0])
return Section(
discovery_param=agent_output.discovery_param,
aws_rss_feed=AWSRSSFeed.parse_rss(agent_output.rss_str),
Expand Down
2 changes: 1 addition & 1 deletion cmk/base/plugins/agent_based/azure_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AzureStatusesPerRegion(BaseModel, frozen=True):


def parse_azure_status(string_table: StringTable) -> AzureStatusesPerRegion:
azure_status = AzureStatus.parse_raw(string_table[0][0])
azure_status = AzureStatus.model_validate_json(string_table[0][0])

regions: dict[str, list[AzureIssue]] = {r: [] for r in azure_status.regions}
for issue in azure_status.issues:
Expand Down
2 changes: 1 addition & 1 deletion cmk/base/plugins/agent_based/cmk_agent_ctl_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def parse_cmk_agent_ctl_status(string_table: StringTable) -> ControllerSection |
except IndexError:
return None

return ControllerSection.parse_raw(json_str)
return ControllerSection.model_validate_json(json_str)


register.agent_section(
Expand Down
5 changes: 2 additions & 3 deletions cmk/base/plugins/agent_based/cmk_update_agent_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

from cmk.plugins.lib.checkmk import CMKAgentUpdateSection

from .agent_based_api.v1 import register
Expand All @@ -13,8 +12,8 @@ def _parse_cmk_update_agent_status(string_table: StringTable) -> CMKAgentUpdateS
"""parse cmk_update_agent_status"""

try:
return CMKAgentUpdateSection.parse_raw(string_table[0][0])
except IndexError:
return CMKAgentUpdateSection.model_validate_json(string_table[0][0])
except (IndexError, ValueError):
return None


Expand Down
2 changes: 1 addition & 1 deletion cmk/base/plugins/agent_based/gcp_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Section:


def parse(string_table: StringTable) -> Section:
output = AgentOutput.parse_raw(string_table[0][0])
output = AgentOutput.model_validate_json(string_table[0][0])
data: dict[str, list[Incident]] = {}
for incident in output.health_info:
for location in incident.currently_affected_locations:
Expand Down
2 changes: 1 addition & 1 deletion cmk/base/plugins/agent_based/graylog_failures.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class FailureMessage(BaseModel):
def to_human_readable(self) -> Iterable[str]:
yield from (
f"{field_name.title()}: {field_value}"
for field_name, field_value in self.dict(exclude_none=True).items()
for field_name, field_value in self.model_dump(exclude_none=True).items()
)


Expand Down
2 changes: 1 addition & 1 deletion cmk/base/plugins/agent_based/kube_deployment_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def condition_levels(params: Mapping[str, VSResultAge], condition: str) -> tuple
def _check(
now: float, params: Mapping[str, VSResultAge], section: DeploymentConditions
) -> CheckResult:
conditions = section.dict()
conditions = section.model_dump()
if all(
condition["status"] is CONDITIONS_OK_MAPPINGS[name]
for name, condition in conditions.items()
Expand Down
2 changes: 1 addition & 1 deletion cmk/base/plugins/agent_based/kube_node_container_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def discovery(section: ContainerCount) -> DiscoveryResult:
def check(params: KubeContainersLevelsUpperLower, section: ContainerCount) -> CheckResult:
"""Computes `total` and uses `check_levels` for each section element,
setting levels from `params` individually"""
section_dict = section.dict()
section_dict = section.model_dump()
section_dict["total"] = sum(section_dict.values())
for name, value in section_dict.items():
level_count_name = cast(CountName, name)
Expand Down
2 changes: 1 addition & 1 deletion cmk/base/plugins/agent_based/kube_node_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class KubeNodeCountVSResult(TypedDict):


def parse(string_table: StringTable) -> NodeCount:
return NodeCount.parse_raw(string_table[0][0])
return NodeCount.model_validate_json(string_table[0][0])


def discovery(section: NodeCount) -> DiscoveryResult:
Expand Down
2 changes: 1 addition & 1 deletion cmk/base/plugins/agent_based/kube_replicas.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def parse_kube_strategy(string_table: StringTable) -> UpdateStrategy:


def parse_kube_controller_spec(string_table: StringTable) -> ControllerSpec:
return ControllerSpec.parse_raw(string_table[0][0])
return ControllerSpec.model_validate_json(string_table[0][0])


register.agent_section(
Expand Down
2 changes: 1 addition & 1 deletion cmk/base/plugins/agent_based/openshift_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


def parse(string_table: StringTable) -> OpenShiftEndpoint:
return OpenShiftEndpoint.parse_raw(string_table[0][0])
return OpenShiftEndpoint.model_validate_json(string_table[0][0])


register.agent_section(
Expand Down
2 changes: 1 addition & 1 deletion cmk/base/plugins/agent_based/prometheus_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def parse(string_table: type_defs.StringTable) -> cpu.Section:
>>> parse([['{"load1": 1.06, "load5": 1.68, "load15": 1.41, "num_cpus": 8}']])
Section(load=Load(load1=1.06, load5=1.68, load15=1.41), num_cpus=8, threads=None, type=<ProcessorType.unspecified: 0>)
"""
parsed_section = CPULoad.parse_raw(string_table[0][0])
parsed_section = CPULoad.model_validate_json(string_table[0][0])
return cpu.Section(
cpu.Load(
load1=parsed_section.load1,
Expand Down
2 changes: 1 addition & 1 deletion cmk/base/plugins/agent_based/prometheus_uptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def parse(string_table: type_defs.StringTable) -> uptime.Section:
>>> parse([['{"seconds": 2117}']])
Section(uptime_sec=2117, message=None)
"""
seconds = Uptime.parse_raw(string_table[0][0]).seconds
seconds = Uptime.model_validate_json(string_table[0][0]).seconds
return uptime.Section(uptime_sec=seconds, message=None)


Expand Down
2 changes: 1 addition & 1 deletion cmk/gui/background_job/_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def exists(self) -> bool:
return self._jobstatus_path.exists()

def write(self, status: JobStatusSpec) -> None:
store.save_object_to_file(self._jobstatus_path, status.dict())
store.save_object_to_file(self._jobstatus_path, status.model_dump())

def update(self, params: JobStatusSpecUpdate) -> None:
if not self._jobstatus_path.parent.exists():
Expand Down
2 changes: 1 addition & 1 deletion cmk/gui/dashboard/dashlet/dashlets/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def _reload_js(self) -> str:

return "dashboard_render_graph(%d, %s, %s, %s)" % (
self._dashlet_id,
self._graph_specification.json(),
self._graph_specification.model_dump_json(),
json.dumps(
default_dashlet_graph_render_options()
# Something is wrong with the typing here. self._dashlet_spec is a subclass of
Expand Down
3 changes: 2 additions & 1 deletion cmk/gui/global_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class GlobalConfig(BaseModel):
def load_global_config() -> GlobalConfig:
path = cse_config_dir / "global-config.json"
try:
return GlobalConfig.parse_file(path)
with open(path, encoding="utf-8") as file:
return GlobalConfig.model_validate_json(file.read())
except Exception as e:
LOGGER.debug("Failed to load config from %s: %s", path, e)
return GlobalConfig(
Expand Down
2 changes: 1 addition & 1 deletion cmk/gui/graphing/_graph_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def graph_recipes_for_api_request(

if consolidation_function := api_request.get("consolidation_function"):
graph_recipes = [
graph_recipe.copy(update={"consolidation_function": consolidation_function})
graph_recipe.model_copy(update={"consolidation_function": consolidation_function})
for graph_recipe in graph_recipes
]

Expand Down
5 changes: 2 additions & 3 deletions cmk/gui/graphing/_graph_specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from dataclasses import dataclass
from typing import Annotated, Callable, Literal, Self, Union

from pydantic import BaseModel, Field, SerializeAsAny, TypeAdapter, validator
from pydantic import BaseModel, Field, field_validator, SerializeAsAny, TypeAdapter
from typing_extensions import TypedDict

from livestatus import SiteId
Expand Down Expand Up @@ -460,7 +460,6 @@ class GraphRecipe(GraphRecipeBase, frozen=True):
# https://docs.pydantic.dev/2.4/concepts/serialization/#serializing-with-duck-typing
specification: SerializeAsAny[GraphSpecification]

@validator("specification", pre=True)
@classmethod
@field_validator("specification", mode="before")
def parse_specification(cls, value: Mapping[str, object]) -> GraphSpecification:
return parse_raw_graph_specification(value)
12 changes: 6 additions & 6 deletions cmk/gui/graphing/_html_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def _render_graph_html(
"cmk.graphs.create_graph(%s, %s, %s, %s);"
% (
json.dumps(html_code),
graph_artwork.json(),
graph_artwork.model_dump_json(),
graph_render_config.model_dump_json(),
json.dumps(_graph_ajax_context(graph_artwork, graph_data_range, graph_render_config)),
)
Expand All @@ -194,7 +194,7 @@ def _graph_ajax_context(
graph_render_config: GraphRenderConfig,
) -> dict[str, Any]:
return {
"definition": graph_artwork.definition.dict(),
"definition": graph_artwork.definition.model_dump(),
"data_range": graph_data_range.model_dump(),
"render_config": graph_render_config.model_dump(),
"display_id": graph_artwork.display_id,
Expand Down Expand Up @@ -637,7 +637,7 @@ def _render_ajax_graph(
save_graph_pin()

if request.has_var("consolidation_function"):
graph_recipe = graph_recipe.copy(
graph_recipe = graph_recipe.model_copy(
update={"consolidation_function": request.var("consolidation_function")}
)

Expand All @@ -664,10 +664,10 @@ def _render_ajax_graph(

return {
"html": html_code,
"graph": graph_artwork.dict(),
"graph": graph_artwork.model_dump(),
"context": {
"graph_id": context["graph_id"],
"definition": graph_recipe.dict(),
"definition": graph_recipe.model_dump(),
"data_range": graph_data_range.model_dump(),
"render_config": graph_render_config.model_dump(),
},
Expand Down Expand Up @@ -812,7 +812,7 @@ def _render_graph_container_html(
output += HTMLWriter.render_javascript(
"cmk.graphs.load_graph_content(%s, %s, %s, %s)"
% (
graph_recipe.json(),
graph_recipe.model_dump_json(),
graph_data_range.model_dump_json(),
graph_render_config.model_dump_json(),
json.dumps(graph_display_id),
Expand Down
2 changes: 1 addition & 1 deletion cmk/gui/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def get_model_mandatory(
) -> Model_T:
"""Try to convert the value of an HTTP request variable to a given pydantic model"""
try:
return model.parse_raw(mandatory_parameter(varname, self.var(varname)))
return model.model_validate_json(mandatory_parameter(varname, self.var(varname)))
except ValueError as exception:
raise MKUserError(varname, _("The value is not valid: '%s'") % exception)

Expand Down
2 changes: 1 addition & 1 deletion cmk/gui/watolib/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ def _cleaned_up_status(job_status: JobStatusSpec) -> JobStatusSpec:
"JobException": [],
"JobResult": job_status.loginfo["JobResult"],
}
return job_status.copy(
return job_status.model_copy(
update={"state": JobStatusStates.FINISHED, "loginfo": new_loginfo},
deep=True, # not sure, better play it safe.
)
Expand Down
8 changes: 3 additions & 5 deletions cmk/plugins/lib/checkmk.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from pyasn1.error import PyAsn1Error
from pyasn1.type.useful import GeneralizedTime
from pydantic import BaseModel, Field, validator
from pydantic import BaseModel, Field, field_validator

CheckmkSection = Mapping[str, str | None]
CmkUpdateAgentStatus = Mapping[str, str]
Expand Down Expand Up @@ -53,8 +53,7 @@ class CertInfoController(BaseModel):
to: datetime
issuer: str

@validator("to", pre=True)
@classmethod
@field_validator("to", mode="before")
def _parse_cert_validity(cls, value: str | datetime) -> datetime:
return (
value
Expand Down Expand Up @@ -104,8 +103,7 @@ class CertInfo(BaseModel):
signature_algorithm: str | None = Field(None)
common_name: str | None = Field(None)

@validator("not_after", pre=True)
@classmethod
@field_validator("not_after", mode="before")
def _validate_not_after(cls, value: str | datetime | None) -> datetime | None:
"""convert not_after from str to datetime
Expand Down
5 changes: 4 additions & 1 deletion cmk/plugins/lib/kube_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,10 @@ def performance_cpu(
no new data is available.
"""
if section_kube_performance_cpu is not None:
host_value_store[value_store_key] = (current_timestamp, section_kube_performance_cpu.json())
host_value_store[value_store_key] = (
current_timestamp,
section_kube_performance_cpu.model_dump_json(),
)
return section_kube_performance_cpu

if (timestamped_usage := host_value_store.get(value_store_key)) is not None:
Expand Down
4 changes: 2 additions & 2 deletions cmk/special_agents/agent_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -5830,7 +5830,7 @@ def _compute_content(
return AWSComputedContent(clusters, raw_content.cache_timestamp)

def _create_results(self, computed_content: AWSComputedContent) -> list[AWSSectionResult]:
return [AWSSectionResult("", [c.dict() for c in computed_content.content])]
return [AWSSectionResult("", [c.model_dump() for c in computed_content.content])]


class ECS(AWSSectionCloudwatch):
Expand Down Expand Up @@ -6179,7 +6179,7 @@ def _compute_content(
return AWSComputedContent((clusters, nodes), raw_content.cache_timestamp)

def _create_results(self, computed_content: AWSComputedContent) -> list[AWSSectionResult]:
return [AWSSectionResult("", [c.dict() for c in computed_content.content[0]])]
return [AWSSectionResult("", [c.model_dump() for c in computed_content.content[0]])]


class ElastiCache(AWSSectionCloudwatch):
Expand Down
2 changes: 1 addition & 1 deletion cmk/special_agents/agent_aws_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def write_section(args: Args, get_rss: typing.Callable[[], requests.Response] =
rss_str=response.text,
)
with agent_common.SectionWriter("aws_status") as writer:
writer.append(section.json())
writer.append(section.model_dump_json())
return 0


Expand Down
2 changes: 1 addition & 1 deletion cmk/special_agents/agent_azure_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def write_section(args: Args) -> int:
azure_status = AzureStatus(link=feed.feed.link, regions=selected_regions, issues=azure_issues)

with SectionWriter("azure_status") as writer:
writer.append_json(azure_status.dict())
writer.append_json(azure_status.model_dump())
return 0


Expand Down
2 changes: 1 addition & 1 deletion cmk/special_agents/agent_gcp_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AgentOutput(pydantic.BaseModel):

def _health_serializer(section: AgentOutput) -> None:
with agent_common.SectionWriter("gcp_status") as w:
w.append(section.json())
w.append(section.model_dump_json())


def parse_arguments(argv: Sequence[str] | None) -> Args:
Expand Down
10 changes: 5 additions & 5 deletions cmk/special_agents/agent_kube.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def _write_sections(sections: Mapping[str, Callable[[], section.Section | None]]
for section_name, section_call in sections.items():
if section_output := section_call():
with SectionWriter(section_name) as writer:
writer.append(section_output.json())
writer.append(section_output.model_dump_json())


def namespaced_name_from_metadata(metadata: api.MetaData) -> str:
Expand Down Expand Up @@ -704,7 +704,7 @@ def _group_metadata_by_node(
for node_collector in node_collectors_metadata:
components = nodes_components.setdefault(node_collector.node, {})

for component, version in node_collector.components.dict().items():
for component, version in node_collector.components.model_dump().items():
if version is not None:
components[component] = section.NodeComponent(
collector_type=node_collector.collector_type,
Expand All @@ -730,7 +730,7 @@ def write_cluster_collector_info_section(
processing_log=processing_log,
cluster_collector=cluster_collector,
nodes=node_collectors_metadata,
).json()
).model_dump_json()
)


Expand Down Expand Up @@ -1137,7 +1137,7 @@ def main(args: list[str] | None = None) -> int: # pylint: disable=too-many-bran
metadata = request_cluster_collector(
query.CollectorPath.metadata,
usage_config,
section.Metadata.parse_raw,
section.Metadata.model_validate_json,
)

supported_collector_version = 1
Expand Down Expand Up @@ -1281,7 +1281,7 @@ def main(args: list[str] | None = None) -> int: # pylint: disable=too-many-bran
section.CollectorProcessingLogs(
container=collector_container_logs[-1],
machine=collector_machine_logs[-1],
).json()
).model_dump_json()
)
except Exception as e:
if arguments.debug:
Expand Down
6 changes: 4 additions & 2 deletions cmk/special_agents/utils/node_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def uptime_summary(self) -> dict[str, SectionStr]:
return {
sample["labels"]["instance"]: _create_section(
"prometheus_uptime_v1:sep(0)",
[Uptime.model_validate({"seconds": sample["value"]}).json()],
[Uptime.model_validate({"seconds": sample["value"]}).model_dump_json()],
)
for sample in uptime_samples
}
Expand All @@ -405,7 +405,9 @@ def cpu_summary(self) -> dict[str, SectionStr]:
raw = node_to_raw.setdefault(instance, {})
raw[key] = sample["value"]
return {
node: _create_section("prometheus_cpu_v1:sep(0)", [CPULoad.model_validate(raw).json()])
node: _create_section(
"prometheus_cpu_v1:sep(0)", [CPULoad.model_validate(raw).model_dump_json()]
)
for node, raw in node_to_raw.items()
}

Expand Down
Loading

0 comments on commit 291e3c9

Please sign in to comment.