def stop(self): LOGGER.info("Stopping") self._closing = True self._ctpe.shutdown(wait=True) self.stop_consuming() if self._connection: self._connection.ioloop.stop() LOGGER.info("Stopped")
def stop_consuming(self): if self._channel: if self._channel.is_closed or self._channel.is_closing: LOGGER.info("Channel is already closed or is closing.") return LOGGER.info( f"Sending a Basic.Cancel RPC command to RabbitMQ for consumer " f"({self._consumer_tag})") self._channel.basic_cancel(self.on_cancelok, self._consumer_tag)
def connect(self) -> pika.connection.Connection: LOGGER.info(f"Connecting to {self.host}:{self.port}") credentials = pika.PlainCredentials(self.username, self.password) parameters = pika.ConnectionParameters(self.host, self.port, self.vhost, credentials, connection_attempts=3, retry_delay=2, socket_timeout=5) return pika.SelectConnection(parameters, self.on_connection_open)
def delete_dnat_rule(self, rule_name): nat_rule_id = self._get_dnat_rule_id(rule_name) if not nat_rule_id: raise Exception(f'No dnat rule found with name: {rule_name}') try: self._cloudapi_client.do_request( method=shared_constants.RequestMethod.DELETE, cloudapi_version=cloudapi_constants.CloudApiVersion. VERSION_1_0_0, # noqa: E501 resource_url_relative_path= f"{self._nat_rules_relative_path}/{nat_rule_id}") # noqa: E501 self._wait_for_last_cloudapi_task() except Exception as err: SERVER_LOGGER.info(f'Failed to delete dnat rule: {str(err)}')
def add_dnat_rule(self, name, internal_address, external_address, dnat_external_port=None, description='', logging_enabled=False, enabled=True, application_port_profile=None): """Add a DNAT rule for an NSX-T backed gateway. :param str name: name of the rule :param str internal address: internal ip address :param str external address: external ip address :param int dnat_external_port: external port :param str description: rule description :param bool logging_enabled: indicate if logging is enabled :param bool enabled: indicate state :param dict application_port_profile: dict with keys "id" and "name" for port profile """ post_body = { NsxtNATRuleKey.NAME: name, NsxtNATRuleKey.DESCRIPTION: description, NsxtNATRuleKey.ENABLED: enabled, NsxtNATRuleKey.RULE_TYPE: nsxt_constants.DNAT_RULE_TYPE, NsxtNATRuleKey.EXTERNAL_ADDRESSES: external_address, NsxtNATRuleKey.INTERNAL_ADDRESSES: internal_address, NsxtNATRuleKey.LOGGING: logging_enabled, NsxtNATRuleKey.APPLICATION_PORT_PROFILE: application_port_profile, NsxtNATRuleKey.DNAT_EXTERNAL_PORT: dnat_external_port } try: self._cloudapi_client.do_request( method=shared_constants.RequestMethod.POST, cloudapi_version=cloudapi_constants.CloudApiVersion. VERSION_1_0_0, # noqa: E501 resource_url_relative_path=self._nat_rules_relative_path, payload=post_body, content_type='application/json') self._wait_for_last_cloudapi_task() except Exception as err: SERVER_LOGGER.info(f'Error when creating dnat rule: {err}') raise
def delete_dnat_rule(self, rule_name): nat_rule_id = self._get_dnat_rule_id(rule_name) if not nat_rule_id: raise Exception(f'No dnat rule found with name: {rule_name}') try: self._cloudapi_client.do_request( method=shared_constants.RequestMethod.DELETE, cloudapi_version=cloudapi_constants.CloudApiVersion. VERSION_1_0_0, # noqa: E501 resource_url_relative_path= f"{self._nat_rules_relative_path}/{nat_rule_id}") # noqa: E501 delete_response = self._cloudapi_client.get_last_response() task_href = delete_response.headers._store['location'][1] task_monitor = self._client.get_task_monitor() task = task_monitor._get_task_status(task_href) task_monitor.wait_for_status(task) except Exception as err: SERVER_LOGGER.info(f'Failed to delete dnat rule: {str(err)}') return
def close_connection(self): LOGGER.info("Closing connection") self._connection.close()
def on_connect(mqtt_client, userdata, flags, rc): LOGGER.info(f'MQTT client connected with result code {rc} and ' f'flags {flags}') mqtt_client.subscribe(self.listen_topic, qos=constants.QOS_LEVEL)
def stop(self): LOGGER.info("MQTT consumer stopping") self._is_closing = True self._ctpe.shutdown(wait=True) # Let jobs finish before disconnecting if self._mqtt_client: self._mqtt_client.disconnect()
def on_disconnect(mqtt_client, userdata, rc): LOGGER.info(f'MQTT disconnect with reason: {rc}')
def on_subscribe(mqtt_client, userdata, msg_id, given_qos): LOGGER.info(f'MQTT client subscribed with given_qos: {given_qos}')