|
| 1 | +import json |
| 2 | +import urllib |
| 3 | +from typing import Dict |
| 4 | + |
| 5 | +import requests |
| 6 | + |
| 7 | +from ravendb import RavenCommand, RaftCommand, ServerNode |
| 8 | +from ravendb.documents.operations.definitions import MaintenanceOperation |
| 9 | +from ravendb.util.util import RaftIdGenerator |
| 10 | +from ravendb.serverwide.server_operation_executor import ConnectionStringType |
| 11 | + |
| 12 | + |
| 13 | +class RemoveConnectionStringByNameResult: |
| 14 | + def __init__(self, raft_command_index: int = None): |
| 15 | + self.raft_command_index = raft_command_index |
| 16 | + |
| 17 | + def to_json(self) -> Dict: |
| 18 | + return {"RaftCommandIndex": self.raft_command_index} |
| 19 | + |
| 20 | + @classmethod |
| 21 | + def from_json(cls, json_dict: Dict) -> "RemoveConnectionStringResult": |
| 22 | + return cls(json_dict["RaftCommandIndex"]) |
| 23 | + |
| 24 | + |
| 25 | +class RemoveConnectionStringByNameOperation(MaintenanceOperation[RemoveConnectionStringByNameResult]): |
| 26 | + def __init__(self, connection_string_name: str, connection_string_type: ConnectionStringType): |
| 27 | + self._connection_string_name = connection_string_name |
| 28 | + self._connection_string_type = connection_string_type |
| 29 | + |
| 30 | + def get_command(self, conventions: "DocumentConventions") -> "RavenCommand[RemoveConnectionStringResult]": |
| 31 | + return self.RemoveConnectionStringCommand(self._connection_string_name, self._connection_string_type) |
| 32 | + |
| 33 | + class RemoveConnectionStringCommand(RavenCommand[RemoveConnectionStringByNameResult], RaftCommand): |
| 34 | + def __init__(self, connection_string_name: str, connection_string_type: ConnectionStringType): |
| 35 | + super().__init__(RemoveConnectionStringByNameResult) |
| 36 | + self._connection_string_name = connection_string_name |
| 37 | + self._connection_string_type = connection_string_type |
| 38 | + |
| 39 | + def is_read_request(self) -> bool: |
| 40 | + return False |
| 41 | + |
| 42 | + def create_request(self, node: ServerNode) -> requests.Request: |
| 43 | + url = f"{node.url}/databases/{node.database}/admin/connection-strings?connectionString={urllib.parse.quote(self._connection_string_name)}&type={self._connection_string_type.value}" |
| 44 | + |
| 45 | + request = requests.Request("DELETE") |
| 46 | + request.url = url |
| 47 | + |
| 48 | + return request |
| 49 | + |
| 50 | + def set_response(self, response: str, from_cache: bool) -> None: |
| 51 | + if response is None: |
| 52 | + self._throw_invalid_response() |
| 53 | + |
| 54 | + self.result = RemoveConnectionStringByNameResult.from_json(json.loads(response)) |
| 55 | + |
| 56 | + def get_raft_unique_request_id(self) -> str: |
| 57 | + return RaftIdGenerator.new_id() |
0 commit comments