async def _management_request(self, mgmt_msg, op_type): max_retries = self.config.max_retries retry_count = 0 while True: mgmt_auth = self._create_auth() mgmt_client = AMQPClientAsync(self.mgmt_target, auth=mgmt_auth, debug=self.config.network_tracing) try: conn = await self._conn_manager.get_connection( self.host, mgmt_auth) await mgmt_client.open_async(connection=conn) response = await mgmt_client.mgmt_request_async( mgmt_msg, constants.READ_OPERATION, op_type=op_type, status_code_field=b'status-code', description_fields=b'status-description') return response except Exception as exception: # pylint:disable=broad-except await self._handle_exception(exception, retry_count, max_retries) retry_count += 1 finally: await mgmt_client.close_async()
async def get_eventhub_info_async(self): """ Get details on the specified EventHub async. :rtype: dict """ eh_name = self.address.path.lstrip('/') target = "amqps://{}/{}".format(self.address.hostname, eh_name) async with AMQPClientAsync(target, auth=self.auth, debug=self.debug) as mgmt_client: mgmt_msg = Message(application_properties={'name': eh_name}) response = await mgmt_client.mgmt_request_async( mgmt_msg, constants.READ_OPERATION, op_type=b'com.microsoft:eventhub', status_code_field=b'status-code', description_fields=b'status-description') eh_info = response.get_data() output = {} if eh_info: output['name'] = eh_info[b'name'].decode('utf-8') output['type'] = eh_info[b'type'].decode('utf-8') output['created_at'] = datetime.datetime.fromtimestamp(float(eh_info[b'created_at'])/1000) output['partition_count'] = eh_info[b'partition_count'] output['partition_ids'] = [p.decode('utf-8') for p in eh_info[b'partition_ids']] return output
async def _management_request(self, mgmt_msg, op_type): retried_times = 0 last_exception = None while retried_times <= self._config.max_retries: mgmt_auth = self._create_auth() mgmt_client = AMQPClientAsync(self._mgmt_target, auth=mgmt_auth, debug=self._config.network_tracing) try: conn = await self._conn_manager.get_connection( self._address.hostname, mgmt_auth) await mgmt_client.open_async(connection=conn) response = await mgmt_client.mgmt_request_async( mgmt_msg, constants.READ_OPERATION, op_type=op_type, status_code_field=b'status-code', description_fields=b'status-description') return response except Exception as exception: # pylint:disable=broad-except last_exception = await _handle_exception(exception, self) await self._backoff(retried_times=retried_times, last_exception=last_exception) retried_times += 1 if retried_times > self._config.max_retries: _LOGGER.info("%r returns an exception %r", self._container_id, last_exception) raise last_exception finally: await mgmt_client.close_async()
def _build_handler(self): auth = None if self.connection else authentication.SASTokenAsync.from_shared_access_key( **self.auth_config) self._handler = AMQPClientAsync(self.endpoint, loop=self.loop, auth=auth, debug=self.debug, properties=self.properties, error_policy=self.error_policy, encoding=self.encoding, **self.handler_kwargs)
async def _management_request_async(self, mgmt_msg: Message, op_type: bytes) -> Any: retried_times = 0 last_exception = None while retried_times <= self._config.max_retries: mgmt_auth = await self._create_auth_async() mgmt_client = AMQPClientAsync(self._mgmt_target, auth=mgmt_auth, debug=self._config.network_tracing) try: conn = await self._conn_manager_async.get_connection( self._address.hostname, mgmt_auth) mgmt_msg.application_properties[ "security_token"] = mgmt_auth.token await mgmt_client.open_async(connection=conn) response = await mgmt_client.mgmt_request_async( mgmt_msg, constants.READ_OPERATION, op_type=op_type, status_code_field=MGMT_STATUS_CODE, description_fields=MGMT_STATUS_DESC, ) status_code = int( response.application_properties[MGMT_STATUS_CODE]) description = response.application_properties.get( MGMT_STATUS_DESC) # type: Optional[Union[str, bytes]] if description and isinstance(description, six.binary_type): description = description.decode('utf-8') if status_code < 400: return response if status_code in [401]: raise errors.AuthenticationException( "Management authentication failed. Status code: {}, Description: {!r}" .format(status_code, description)) if status_code in [404]: raise ConnectError( "Management connection failed. Status code: {}, Description: {!r}" .format(status_code, description)) raise errors.AMQPConnectionError( "Management request error. Status code: {}, Description: {!r}" .format(status_code, description)) except asyncio.CancelledError: # pylint: disable=try-except-raise raise except Exception as exception: # pylint:disable=broad-except last_exception = await _handle_exception(exception, self) await self._backoff_async(retried_times=retried_times, last_exception=last_exception) retried_times += 1 if retried_times > self._config.max_retries: _LOGGER.info("%r returns an exception %r", self._container_id, last_exception) raise last_exception finally: await mgmt_client.close_async()
async def get_eventhub_info_async(self): """ Get details on the specified EventHub async. :rtype: dict """ alt_creds = { "username": self._auth_config.get("iot_username"), "password": self._auth_config.get("iot_password") } try: mgmt_auth = self._create_auth(**alt_creds) mgmt_client = AMQPClientAsync(self.mgmt_target, auth=mgmt_auth, debug=self.debug) await mgmt_client.open_async() mgmt_msg = Message(application_properties={'name': self.eh_name}) response = await mgmt_client.mgmt_request_async( mgmt_msg, constants.READ_OPERATION, op_type=b'com.microsoft:eventhub', status_code_field=b'status-code', description_fields=b'status-description') eh_info = response.get_data() output = {} if eh_info: output['name'] = eh_info[b'name'].decode('utf-8') output['type'] = eh_info[b'type'].decode('utf-8') output['created_at'] = datetime.datetime.fromtimestamp( float(eh_info[b'created_at']) / 1000) output['partition_count'] = eh_info[b'partition_count'] output['partition_ids'] = [ p.decode('utf-8') for p in eh_info[b'partition_ids'] ] return output finally: await mgmt_client.close_async()
async def _management_request_async(self, mgmt_msg: Message, op_type: bytes) -> Any: retried_times = 0 last_exception = None while retried_times <= self._config.max_retries: mgmt_auth = await self._create_auth_async() mgmt_client = AMQPClientAsync(self._mgmt_target, auth=mgmt_auth, debug=self._config.network_tracing) try: conn = await self._conn_manager_async.get_connection( self._address.hostname, mgmt_auth) await mgmt_client.open_async(connection=conn) response = await mgmt_client.mgmt_request_async( mgmt_msg, constants.READ_OPERATION, op_type=op_type, status_code_field=b"status-code", description_fields=b"status-description", ) status_code = response.application_properties[b"status-code"] if status_code < 400: return response raise errors.AuthenticationException( "Management request error. Status code: {}".format( status_code)) except Exception as exception: # pylint:disable=broad-except last_exception = await _handle_exception(exception, self) await self._backoff_async(retried_times=retried_times, last_exception=last_exception) retried_times += 1 if retried_times > self._config.max_retries: _LOGGER.info("%r returns an exception %r", self._container_id, last_exception) raise last_exception finally: await mgmt_client.close_async()