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 = uamqp.AMQPClient(self._mgmt_target) try: conn = self._conn_manager.get_connection(self._host, mgmt_auth) #pylint:disable=assignment-from-none mgmt_client.open(connection=conn) response = mgmt_client.mgmt_request( 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 = _handle_exception(exception, self) self._try_delay(retried_times=retried_times, last_exception=last_exception) retried_times += 1 finally: mgmt_client.close() log.info("%r returns an exception %r", self._container_id, last_exception) # pylint:disable=specify-parameter-names-in-call raise last_exception
def _management_request(self, mgmt_msg, op_type): alt_creds = { "username": self._auth_config.get("iot_username"), "password": self._auth_config.get("iot_password") } retried_times = 0 while retried_times <= self._config.max_retries: mgmt_auth = self._create_auth(**alt_creds) mgmt_client = uamqp.AMQPClient(self._mgmt_target) try: conn = self._conn_manager.get_connection(self._host, mgmt_auth) #pylint:disable=assignment-from-none mgmt_client.open(connection=conn) response = mgmt_client.mgmt_request( 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 = _handle_exception(exception, self) self._try_delay(retried_times=retried_times, last_exception=last_exception) retried_times += 1 finally: mgmt_client.close()
def _management_request(self, mgmt_msg, op_type): alt_creds = { "username": self._auth_config.get("iot_username"), "password": self._auth_config.get("iot_password") } connect_count = 0 while True: connect_count += 1 mgmt_auth = self._create_auth(**alt_creds) mgmt_client = uamqp.AMQPClient(self.mgmt_target, auth=mgmt_auth, debug=self.config.network_tracing) try: mgmt_client.open() response = mgmt_client.mgmt_request( mgmt_msg, constants.READ_OPERATION, op_type=op_type, status_code_field=b'status-code', description_fields=b'status-description') return response except (errors.AMQPConnectionError, errors.TokenAuthFailure, compat.TimeoutException) as failure: if connect_count >= self.config.max_retries: err = ConnectError( "Can not connect to EventHubs or get management info from the service. " "Please make sure the connection string or token is correct and retry. " "Besides, this method doesn't work if you use an IoT connection string.", failure) raise err finally: mgmt_client.close()
def test_event_hubs_mgmt_op(live_eventhub_config): uri = "sb://{}/{}".format(live_eventhub_config['hostname'], live_eventhub_config['event_hub']) sas_auth = authentication.SASTokenAuth.from_shared_access_key( uri, live_eventhub_config['key_name'], live_eventhub_config['access_key']) target = "amqps://{}/{}".format(live_eventhub_config['hostname'], live_eventhub_config['event_hub']) with uamqp.AMQPClient(target, auth=sas_auth, debug=False) as send_client: mgmt_msg = uamqp.Message(application_properties={'name': live_eventhub_config['event_hub']}) response = send_client.mgmt_request( mgmt_msg, b'READ', op_type=b'com.microsoft:eventhub', status_code_field=b'status-code', description_fields=b'status-description') output = response.get_data() assert output[b'partition_ids'] == [b"0", b"1"]
def get_eventhub_info(self): """ Get details on the specified EventHub. Keys in the details dictionary include: -'name' -'type' -'created_at' -'partition_count' -'partition_ids' :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 = uamqp.AMQPClient(self.mgmt_target, auth=mgmt_auth, debug=self.debug) mgmt_client.open() mgmt_msg = Message(application_properties={'name': self.eh_name}) response = mgmt_client.mgmt_request( 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 except: raise finally: mgmt_client.close()
def get_eventhub_info(self): """ Get details on the specified EventHub. Keys in the details dictionary include: -'name' -'type' -'created_at' -'partition_count' -'partition_ids' :rtype: dict """ self._create_connection() eh_name = self.address.path.lstrip('/') target = "amqps://{}/{}".format(self.address.hostname, eh_name) mgmt_client = uamqp.AMQPClient(target, auth=self.auth, debug=self.debug) mgmt_client.open(self.connection) try: mgmt_msg = Message(application_properties={'name': eh_name}) response = mgmt_client.mgmt_request( 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 except: raise finally: mgmt_client.close()
def _management_request(self, mgmt_msg, op_type): max_retries = self.config.max_retries retry_count = 0 while retry_count <= self.config.max_retries: mgmt_auth = self._create_auth() mgmt_client = uamqp.AMQPClient(self.mgmt_target) try: conn = self._conn_manager.get_connection(self.host, mgmt_auth) #pylint:disable=assignment-from-none mgmt_client.open(connection=conn) response = mgmt_client.mgmt_request( 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 self._handle_exception(exception, retry_count, max_retries) retry_count += 1 finally: mgmt_client.close()