|
| 1 | +import json |
| 2 | +from typing import Dict |
| 3 | + |
| 4 | +import requests |
| 5 | + |
| 6 | +from ravendb import ConnectionString, RavenCommand, ServerNode, RaftCommand |
| 7 | +from ravendb.documents.conventions import DocumentConventions |
| 8 | +from ravendb.documents.operations.definitions import MaintenanceOperation |
| 9 | +from ravendb.documents.operations.etl.configuration import RavenConnectionString |
| 10 | +from ravendb.documents.operations.etl.olap import OlapConnectionString |
| 11 | +from ravendb.documents.operations.etl.sql import SqlConnectionString |
| 12 | +from ravendb.serverwide.server_operation_executor import ConnectionStringType |
| 13 | +from ravendb.util.util import RaftIdGenerator |
| 14 | + |
| 15 | + |
| 16 | +class PutConnectionStringResult: |
| 17 | + def __init__(self, raft_command_index: int = None): |
| 18 | + self.raft_command_index = raft_command_index |
| 19 | + |
| 20 | + def to_json(self) -> Dict: |
| 21 | + return {"RaftCommandIndex": self.raft_command_index} |
| 22 | + |
| 23 | + @classmethod |
| 24 | + def from_json(cls, json_dict: Dict) -> "PutConnectionStringResult": |
| 25 | + return cls(json_dict["RaftCommandIndex"]) |
| 26 | + |
| 27 | + |
| 28 | +class PutConnectionStringOperation(MaintenanceOperation[PutConnectionStringResult]): |
| 29 | + def __init__(self, connection_string: ConnectionString = None): |
| 30 | + self._connection_string = connection_string |
| 31 | + |
| 32 | + def get_command(self, conventions: "DocumentConventions") -> "RavenCommand[PutConnectionStringResult]": |
| 33 | + return self.PutConnectionStringCommand(conventions, self._connection_string) |
| 34 | + |
| 35 | + class PutConnectionStringCommand(RavenCommand[PutConnectionStringResult], RaftCommand): |
| 36 | + def __init__( |
| 37 | + self, document_conventions: DocumentConventions = None, connection_string: ConnectionString = None |
| 38 | + ): |
| 39 | + super().__init__(PutConnectionStringResult) |
| 40 | + |
| 41 | + if connection_string is None: |
| 42 | + raise ValueError("Connection string cannot be None") |
| 43 | + |
| 44 | + self._document_conventions = document_conventions |
| 45 | + self._connection_string = connection_string |
| 46 | + |
| 47 | + def _to_data(self) -> Dict: |
| 48 | + if isinstance(self._connection_string, RavenConnectionString): |
| 49 | + return { |
| 50 | + "Name": self._connection_string.name, |
| 51 | + "Database": self._connection_string.database, |
| 52 | + "TopologyDiscoveryUrls": self._connection_string.topology_discovery_urls, |
| 53 | + "Type": ConnectionStringType.RAVEN, |
| 54 | + } |
| 55 | + |
| 56 | + if isinstance(self._connection_string, SqlConnectionString): |
| 57 | + return { |
| 58 | + "Name": self._connection_string.name, |
| 59 | + "ConnectionString": self._connection_string.connection_string, |
| 60 | + "FactoryName": self._connection_string.factory_name, |
| 61 | + "Type": ConnectionStringType.SQL, |
| 62 | + } |
| 63 | + |
| 64 | + if isinstance(self._connection_string, OlapConnectionString): |
| 65 | + return { |
| 66 | + "Name": self._connection_string.name, |
| 67 | + "LocalSettings": self._connection_string.local_settings, |
| 68 | + "S3Settings": self._connection_string.s3_settings, |
| 69 | + "AzureSettings": self._connection_string.azure_settings, |
| 70 | + "GlacierSettings": self._connection_string.glacier_settings, |
| 71 | + "GoogleCloudSettings": self._connection_string.google_cloud_settings, |
| 72 | + "FtpSettings": self._connection_string.ftp_settings, |
| 73 | + "Type": ConnectionStringType.OLAP, |
| 74 | + } |
| 75 | + |
| 76 | + return None |
| 77 | + |
| 78 | + def is_read_request(self) -> bool: |
| 79 | + return False |
| 80 | + |
| 81 | + def create_request(self, node: ServerNode) -> requests.Request: |
| 82 | + url = f"{node.url}/databases/{node.database}/admin/connection-strings" |
| 83 | + |
| 84 | + request = requests.Request("PUT") |
| 85 | + request.url = url |
| 86 | + request.data = self._to_data() |
| 87 | + |
| 88 | + return request |
| 89 | + |
| 90 | + def set_response(self, response: str, from_cache: bool) -> None: |
| 91 | + if response is None: |
| 92 | + self._throw_invalid_response() |
| 93 | + |
| 94 | + self.result = PutConnectionStringResult.from_json(json.loads(response)) |
| 95 | + |
| 96 | + def get_raft_unique_request_id(self) -> str: |
| 97 | + return RaftIdGenerator.new_id() |
0 commit comments