|
14 | 14 |
|
15 | 15 | """Helpers for the 'hello' and legacy hello commands.""" |
16 | 16 |
|
| 17 | +import itertools |
17 | 18 |
|
18 | | -class HelloCompat: |
19 | | - CMD = 'hello' |
20 | | - LEGACY_CMD = 'ismaster' |
21 | | - PRIMARY = 'isWritablePrimary' |
22 | | - LEGACY_PRIMARY = 'ismaster' |
| 19 | +from bson.py3compat import imap |
| 20 | +from pymongo import common |
| 21 | +from pymongo.hello_compat import HelloCompat |
| 22 | +from pymongo.server_type import SERVER_TYPE |
| 23 | + |
| 24 | + |
| 25 | +def _get_server_type(doc): |
| 26 | + """Determine the server type from a hello response.""" |
| 27 | + if not doc.get('ok'): |
| 28 | + return SERVER_TYPE.Unknown |
| 29 | + |
| 30 | + if doc.get('serviceId'): |
| 31 | + return SERVER_TYPE.LoadBalancer |
| 32 | + elif doc.get('isreplicaset'): |
| 33 | + return SERVER_TYPE.RSGhost |
| 34 | + elif doc.get('setName'): |
| 35 | + if doc.get('hidden'): |
| 36 | + return SERVER_TYPE.RSOther |
| 37 | + elif doc.get(HelloCompat.PRIMARY): |
| 38 | + return SERVER_TYPE.RSPrimary |
| 39 | + elif doc.get(HelloCompat.LEGACY_PRIMARY): |
| 40 | + return SERVER_TYPE.RSPrimary |
| 41 | + elif doc.get('secondary'): |
| 42 | + return SERVER_TYPE.RSSecondary |
| 43 | + elif doc.get('arbiterOnly'): |
| 44 | + return SERVER_TYPE.RSArbiter |
| 45 | + else: |
| 46 | + return SERVER_TYPE.RSOther |
| 47 | + elif doc.get('msg') == 'isdbgrid': |
| 48 | + return SERVER_TYPE.Mongos |
| 49 | + else: |
| 50 | + return SERVER_TYPE.Standalone |
| 51 | + |
| 52 | + |
| 53 | +class Hello(object): |
| 54 | + """Parse a hello response from the server.""" |
| 55 | + __slots__ = ('_doc', '_server_type', '_is_writable', '_is_readable', |
| 56 | + '_awaitable') |
| 57 | + |
| 58 | + def __init__(self, doc, awaitable=False): |
| 59 | + self._server_type = _get_server_type(doc) |
| 60 | + self._doc = doc |
| 61 | + self._is_writable = self._server_type in ( |
| 62 | + SERVER_TYPE.RSPrimary, |
| 63 | + SERVER_TYPE.Standalone, |
| 64 | + SERVER_TYPE.Mongos, |
| 65 | + SERVER_TYPE.LoadBalancer) |
| 66 | + |
| 67 | + self._is_readable = ( |
| 68 | + self.server_type == SERVER_TYPE.RSSecondary |
| 69 | + or self._is_writable) |
| 70 | + self._awaitable = awaitable |
| 71 | + |
| 72 | + @property |
| 73 | + def document(self): |
| 74 | + """The complete hello command response document. |
| 75 | +
|
| 76 | + .. versionadded:: 3.4 |
| 77 | + """ |
| 78 | + return self._doc.copy() |
| 79 | + |
| 80 | + @property |
| 81 | + def server_type(self): |
| 82 | + return self._server_type |
| 83 | + |
| 84 | + @property |
| 85 | + def all_hosts(self): |
| 86 | + """List of hosts, passives, and arbiters known to this server.""" |
| 87 | + return set(imap(common.clean_node, itertools.chain( |
| 88 | + self._doc.get('hosts', []), |
| 89 | + self._doc.get('passives', []), |
| 90 | + self._doc.get('arbiters', [])))) |
| 91 | + |
| 92 | + @property |
| 93 | + def tags(self): |
| 94 | + """Replica set member tags or empty dict.""" |
| 95 | + return self._doc.get('tags', {}) |
| 96 | + |
| 97 | + @property |
| 98 | + def primary(self): |
| 99 | + """This server's opinion about who the primary is, or None.""" |
| 100 | + if self._doc.get('primary'): |
| 101 | + return common.partition_node(self._doc['primary']) |
| 102 | + else: |
| 103 | + return None |
| 104 | + |
| 105 | + @property |
| 106 | + def replica_set_name(self): |
| 107 | + """Replica set name or None.""" |
| 108 | + return self._doc.get('setName') |
| 109 | + |
| 110 | + @property |
| 111 | + def max_bson_size(self): |
| 112 | + return self._doc.get('maxBsonObjectSize', common.MAX_BSON_SIZE) |
| 113 | + |
| 114 | + @property |
| 115 | + def max_message_size(self): |
| 116 | + return self._doc.get('maxMessageSizeBytes', 2 * self.max_bson_size) |
| 117 | + |
| 118 | + @property |
| 119 | + def max_write_batch_size(self): |
| 120 | + return self._doc.get('maxWriteBatchSize', common.MAX_WRITE_BATCH_SIZE) |
| 121 | + |
| 122 | + @property |
| 123 | + def min_wire_version(self): |
| 124 | + return self._doc.get('minWireVersion', common.MIN_WIRE_VERSION) |
| 125 | + |
| 126 | + @property |
| 127 | + def max_wire_version(self): |
| 128 | + return self._doc.get('maxWireVersion', common.MAX_WIRE_VERSION) |
| 129 | + |
| 130 | + @property |
| 131 | + def set_version(self): |
| 132 | + return self._doc.get('setVersion') |
| 133 | + |
| 134 | + @property |
| 135 | + def election_id(self): |
| 136 | + return self._doc.get('electionId') |
| 137 | + |
| 138 | + @property |
| 139 | + def cluster_time(self): |
| 140 | + return self._doc.get('$clusterTime') |
| 141 | + |
| 142 | + @property |
| 143 | + def logical_session_timeout_minutes(self): |
| 144 | + return self._doc.get('logicalSessionTimeoutMinutes') |
| 145 | + |
| 146 | + @property |
| 147 | + def is_writable(self): |
| 148 | + return self._is_writable |
| 149 | + |
| 150 | + @property |
| 151 | + def is_readable(self): |
| 152 | + return self._is_readable |
| 153 | + |
| 154 | + @property |
| 155 | + def me(self): |
| 156 | + me = self._doc.get('me') |
| 157 | + if me: |
| 158 | + return common.clean_node(me) |
| 159 | + |
| 160 | + @property |
| 161 | + def last_write_date(self): |
| 162 | + return self._doc.get('lastWrite', {}).get('lastWriteDate') |
| 163 | + |
| 164 | + @property |
| 165 | + def compressors(self): |
| 166 | + return self._doc.get('compression') |
| 167 | + |
| 168 | + @property |
| 169 | + def sasl_supported_mechs(self): |
| 170 | + """Supported authentication mechanisms for the current user. |
| 171 | +
|
| 172 | + For example:: |
| 173 | +
|
| 174 | + >>> hello.sasl_supported_mechs |
| 175 | + ["SCRAM-SHA-1", "SCRAM-SHA-256"] |
| 176 | +
|
| 177 | + """ |
| 178 | + return self._doc.get('saslSupportedMechs', []) |
| 179 | + |
| 180 | + @property |
| 181 | + def speculative_authenticate(self): |
| 182 | + """The speculativeAuthenticate field.""" |
| 183 | + return self._doc.get('speculativeAuthenticate') |
| 184 | + |
| 185 | + @property |
| 186 | + def topology_version(self): |
| 187 | + return self._doc.get('topologyVersion') |
| 188 | + |
| 189 | + @property |
| 190 | + def awaitable(self): |
| 191 | + return self._awaitable |
| 192 | + |
| 193 | + @property |
| 194 | + def service_id(self): |
| 195 | + return self._doc.get('serviceId') |
| 196 | + |
| 197 | + @property |
| 198 | + def hello_ok(self): |
| 199 | + return self._doc.get('helloOk', False) |
0 commit comments