|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import json |
4 | | -from typing import Optional, List, TYPE_CHECKING |
| 4 | +from typing import Optional, List, TYPE_CHECKING, Dict |
5 | 5 |
|
6 | 6 | import requests |
7 | 7 |
|
8 | 8 | from ravendb import ServerNode |
| 9 | +from ravendb.documents.operations.definitions import MaintenanceOperation, VoidMaintenanceOperation |
9 | 10 | from ravendb.documents.operations.ongoing_tasks import OngoingTaskType |
10 | 11 | from ravendb.http.raven_command import RavenCommand, VoidRavenCommand |
11 | 12 | from ravendb.http.topology import RaftCommand |
12 | 13 | from ravendb.documents.operations.backups.settings import PeriodicBackupConfiguration |
13 | | -from ravendb.serverwide.operations.common import ServerOperation, T, VoidServerOperation |
| 14 | +from ravendb.serverwide.operations.common import ServerOperation, T, VoidServerOperation, DatabaseSettings |
14 | 15 | from ravendb.serverwide.operations.ongoing_tasks import IServerWideTask, ServerWideTaskResponse |
15 | 16 | from ravendb.tools.utils import Utils |
16 | 17 | from ravendb.util.util import RaftIdGenerator |
@@ -163,3 +164,72 @@ def get_raft_unique_request_id(self) -> str: |
163 | 164 | def create_request(self, node: ServerNode) -> requests.Request: |
164 | 165 | url = f"{node.url}/admin/configuration/server-wide/task?type={self._type.value}&name={Utils.quote_key(self._name)}" |
165 | 166 | return requests.Request(method="DELETE", url=url) |
| 167 | + |
| 168 | + |
| 169 | +class GetDatabaseSettingsOperation(MaintenanceOperation[DatabaseSettings]): |
| 170 | + def __init__(self, database_name: str = None): |
| 171 | + if database_name is None: |
| 172 | + raise ValueError("Database name cannot be None") |
| 173 | + self._database_name = database_name |
| 174 | + |
| 175 | + def get_command(self, conventions: "DocumentConventions") -> "RavenCommand[DatabaseSettings]": |
| 176 | + return self.GetDatabaseSettingsCommand(self._database_name) |
| 177 | + |
| 178 | + class GetDatabaseSettingsCommand(RavenCommand[DatabaseSettings]): |
| 179 | + def __init__(self, database_name: str = None): |
| 180 | + super().__init__(DatabaseSettings) |
| 181 | + |
| 182 | + if database_name is None: |
| 183 | + raise ValueError("Database name cannot be None") |
| 184 | + |
| 185 | + self._database_name = database_name |
| 186 | + |
| 187 | + def is_read_request(self) -> bool: |
| 188 | + return False |
| 189 | + |
| 190 | + def create_request(self, node: ServerNode) -> requests.Request: |
| 191 | + url = f"{node.url}/databases/{self._database_name}/admin/record" |
| 192 | + return requests.Request(method="GET", url=url) |
| 193 | + |
| 194 | + def set_response(self, response: Optional[str], from_cache: bool) -> None: |
| 195 | + if response is None: |
| 196 | + self.result = None |
| 197 | + return |
| 198 | + |
| 199 | + self.result = DatabaseSettings.from_json(json.loads(response)) |
| 200 | + |
| 201 | + |
| 202 | +class PutDatabaseSettingsOperation(VoidMaintenanceOperation): |
| 203 | + def __init__(self, database_name: str, configuration_settings: Dict[str, str]): |
| 204 | + if database_name is None: |
| 205 | + raise ValueError("Database name cannot be None") |
| 206 | + self._database_name = database_name |
| 207 | + |
| 208 | + if configuration_settings is None: |
| 209 | + raise ValueError("Configuration settings cannot be None") |
| 210 | + self._configuration_settings = configuration_settings |
| 211 | + |
| 212 | + def get_command(self, conventions: "DocumentConventions") -> "VoidRavenCommand": |
| 213 | + return self.PutDatabaseSettingsCommand(self._configuration_settings, self._database_name) |
| 214 | + |
| 215 | + class PutDatabaseSettingsCommand(VoidRavenCommand, RaftCommand): |
| 216 | + def __init__(self, configuration_settings: Dict[str, str], database_name: str): |
| 217 | + super().__init__() |
| 218 | + if database_name is None: |
| 219 | + raise ValueError("Database name cannot be None.") |
| 220 | + self._database_name = database_name |
| 221 | + |
| 222 | + if configuration_settings is None: |
| 223 | + raise ValueError("Configuration settings cannot be None.") |
| 224 | + self._configuration_settings = configuration_settings |
| 225 | + |
| 226 | + def is_read_request(self) -> bool: |
| 227 | + return False |
| 228 | + |
| 229 | + def get_raft_unique_request_id(self) -> str: |
| 230 | + return RaftIdGenerator.new_id() |
| 231 | + |
| 232 | + def create_request(self, node: ServerNode) -> requests.Request: |
| 233 | + url = f"{node.url}/databases/{self._database_name}/admin/configuration/settings" |
| 234 | + |
| 235 | + return requests.Request(method="PUT", url=url, data=self._configuration_settings) |
0 commit comments