|
9 | 9 | import logging |
10 | 10 | import re |
11 | 11 | from pathlib import Path |
12 | | -from typing import Literal, cast |
| 12 | +from typing import Final, Literal, cast |
13 | 13 | from urllib.parse import urlsplit |
14 | 14 |
|
15 | 15 | import yaml |
16 | 16 | from charms.data_platform_libs.v0.data_interfaces import DatabaseRequires |
17 | 17 | from github_runner_manager.configuration import ProxyConfig, SSHDebugConnection |
18 | 18 | from github_runner_manager.configuration.github import GitHubPath, parse_github_path |
19 | 19 | from ops import CharmBase |
20 | | -from pydantic import BaseModel, MongoDsn, ValidationError, create_model_from_typeddict, validator |
| 20 | +from pydantic import ( |
| 21 | + BaseModel, |
| 22 | + MongoDsn, |
| 23 | + ValidationError, |
| 24 | + create_model_from_typeddict, |
| 25 | + validator, |
| 26 | +) |
21 | 27 |
|
22 | 28 | from errors import MissingMongoDBError |
23 | 29 | from models import AnyHttpsUrl, FlavorLabel, OpenStackCloudsYAML |
|
61 | 67 | DEBUG_SSH_INTEGRATION_NAME = "debug-ssh" |
62 | 68 | IMAGE_INTEGRATION_NAME = "image" |
63 | 69 | MONGO_DB_INTEGRATION_NAME = "mongodb" |
| 70 | +PLANNER_INTEGRATION_NAME = "planner" |
| 71 | + |
| 72 | +# Keys and defaults for planner relation app data bag |
| 73 | +PLANNER_FLAVOR_RELATION_KEY: Final[str] = "flavor" |
| 74 | +PLANNER_LABELS_RELATION_KEY: Final[str] = "labels" |
| 75 | +PLANNER_PLATFORM_RELATION_KEY: Final[str] = "platform" |
| 76 | +PLANNER_PRIORITY_RELATION_KEY: Final[str] = "priority" |
| 77 | +PLANNER_MINIMUM_PRESSURE_RELATION_KEY: Final[str] = "minimum-pressure" |
| 78 | +PLANNER_DEFAULT_PLATFORM: Final[str] = "github" |
| 79 | +PLANNER_DEFAULT_PRIORITY: Final[int] = 50 |
64 | 80 |
|
65 | 81 | LogLevel = Literal["CRITICAL", "FATAL", "ERROR", "WARNING", "INFO", "DEBUG"] |
66 | 82 |
|
67 | 83 |
|
| 84 | +@dataclasses.dataclass(frozen=True) |
| 85 | +class PlannerRelationData: |
| 86 | + """Data written to the planner relation app databag. |
| 87 | +
|
| 88 | + Attributes: |
| 89 | + flavor: The flavor name (app name). |
| 90 | + labels: Runner labels for this flavor. |
| 91 | + platform: The platform identifier. |
| 92 | + priority: Scheduling priority. |
| 93 | + minimum_pressure: Minimum number of runners to maintain. |
| 94 | + """ |
| 95 | + |
| 96 | + flavor: str |
| 97 | + labels: tuple[str, ...] |
| 98 | + platform: str = PLANNER_DEFAULT_PLATFORM |
| 99 | + priority: int = PLANNER_DEFAULT_PRIORITY |
| 100 | + minimum_pressure: int = 0 |
| 101 | + |
| 102 | + def to_relation_data(self) -> dict[str, str]: |
| 103 | + """Serialize to relation databag format. |
| 104 | +
|
| 105 | + Returns: |
| 106 | + Dictionary of string key-value pairs for the Juju relation databag. |
| 107 | + """ |
| 108 | + return { |
| 109 | + PLANNER_FLAVOR_RELATION_KEY: self.flavor, |
| 110 | + PLANNER_LABELS_RELATION_KEY: json.dumps(list(self.labels)), |
| 111 | + PLANNER_PLATFORM_RELATION_KEY: self.platform, |
| 112 | + PLANNER_PRIORITY_RELATION_KEY: str(self.priority), |
| 113 | + PLANNER_MINIMUM_PRESSURE_RELATION_KEY: str(self.minimum_pressure), |
| 114 | + } |
| 115 | + |
| 116 | + |
68 | 117 | @dataclasses.dataclass |
69 | 118 | class GithubConfig: |
70 | 119 | """Charm configuration related to GitHub. |
@@ -455,7 +504,6 @@ def from_charm(cls, charm: CharmBase) -> "CharmConfig": |
455 | 504 | runner_manager_log_level = cast( |
456 | 505 | LogLevel, charm.config.get(RUNNER_MANAGER_LOG_LEVEL_CONFIG_NAME, "INFO") |
457 | 506 | ) |
458 | | - # pydantic allows to pass str as AnyHttpUrl, mypy complains about it |
459 | 507 | return cls( |
460 | 508 | allow_external_contributor=cast( |
461 | 509 | bool, charm.config.get(ALLOW_EXTERNAL_CONTRIBUTOR_CONFIG_NAME, False) |
|
0 commit comments