def _register_cse(client, routing_key, exchange, msg_update_callback=utils.NullPrinter()): """Register or update CSE on vCD. :param pyvcloud.vcd.client.Client client: :param pyvcloud.vcd.client.Client client: :param str routing_key: :param str exchange: :param utils.ConsoleMessagePrinter msg_update_callback: Callback object. """ ext = APIExtension(client) patterns = [ f'/api/{server_constants.CSE_SERVICE_NAME}', f'/api/{server_constants.CSE_SERVICE_NAME}/.*', f'/api/{server_constants.CSE_SERVICE_NAME}/.*/.*', f'/api/{server_constants.PKS_SERVICE_NAME}', f'/api/{server_constants.PKS_SERVICE_NAME}/.*', f'/api/{server_constants.PKS_SERVICE_NAME}/.*/.*' ] cse_info = None try: cse_info = ext.get_extension_info( server_constants.CSE_SERVICE_NAME, namespace=server_constants.CSE_SERVICE_NAMESPACE) # noqa: E501 except MissingRecordException: pass if cse_info is None: ext.add_extension( server_constants.CSE_SERVICE_NAME, server_constants.CSE_SERVICE_NAMESPACE, routing_key, # noqa: E501 exchange, patterns) msg = f"Registered {server_constants.CSE_SERVICE_NAME} as an API extension in vCD" # noqa: E501 else: ext.update_extension( server_constants.CSE_SERVICE_NAME, namespace=server_constants.CSE_SERVICE_NAMESPACE, # noqa: E501 routing_key=routing_key, exchange=exchange) msg = f"Updated {server_constants.CSE_SERVICE_NAME} API Extension in vCD" # noqa: E501 msg_update_callback.general(msg) INSTALL_LOGGER.info(msg)
def test_0080_update_service(self): """Test the method APIExtension.update_extension(). This test passes if the routing key and exchange after execution of the method matches the respective test strings. """ logger = Environment.get_default_logger() api_extension = APIExtension(TestApiExtension._client) ext_name = TestApiExtension._service_name ext_namespace = TestApiExtension._service1_namespace logger.debug('Updating service (name:' + ext_name + ', namespace:' + ext_namespace + ').') test_routing_key = 'testroutingkey' test_exchange = 'testexchange' href = api_extension.update_extension( name=ext_name, namespace=ext_namespace, routing_key=test_routing_key, exchange=test_exchange) self.assertEqual(href, TestApiExtension._service1_href) ext_info = api_extension.get_extension_info(ext_name, namespace=ext_namespace) self.assertEqual(ext_info['routingKey'], test_routing_key) self.assertEqual(ext_info['exchange'], test_exchange)
def test_0080_update_service(self): """Test the method APIExtension.update_extension(). This test passes if the routing key and exchange after execution of the method matches the respective test strings. """ logger = Environment.get_default_logger() api_extension = APIExtension(TestApiExtension._client) ext_name = TestApiExtension._service_name ext_namespace = TestApiExtension._service1_namespace logger.debug('Updating service (name:' + ext_name + ', namespace:' + ext_namespace + ').') test_routing_key = 'testroutingkey' test_exchange = 'testexchange' href = api_extension.update_extension(name=ext_name, namespace=ext_namespace, routing_key=test_routing_key, exchange=test_exchange) self.assertEqual(href, TestApiExtension._service1_href) ext_info = api_extension.get_extension_info(ext_name, namespace=ext_namespace) self.assertEqual(ext_info['routingKey'], test_routing_key) self.assertEqual(ext_info['exchange'], test_exchange)
def initialize_on_vcloud(self): """Check and/register the extension on vCloud. """ self.log('info', 'Checking the initialization status of extension in vCloud.') if not ( self.conf('vcloud.api_extension.namespace') and self.conf('vcloud.api_extension.exchange') and self.conf('vcloud.api_extension.routing_key') ): self.log('warning', 'Missing items in configuration to make the initialization check-up. Ignoring.') return client = login_as_system_admin() ext_manager = APIExtension(client) try: current_ext_on_vcd = ext_manager.get_extension_info( self.name, namespace=self.conf('vcloud.api_extension.namespace')) self.log('info', 'Extension is already registered on vCloud') except MissingRecordException: self.log('warning', "This extension is not (yet) declared on vCloud.") current_ext_on_vcd = None except MultipleRecordsException: self.log('error', "Multiple extensions found with same name and namespace") sys.exit(-1) # Force a fresh redeploy of the full extension (Warning: be carrefull, ID will change !) if current_ext_on_vcd and self.conf('vcloud.api_extension.force_redeploy', False): ext_manager.delete_extension( self.name, namespace=self.conf('vcloud.api_extension.namespace')) self.log('info', 'Extension is unregistered on vCloud') current_ext_on_vcd = None # Only update an existing extension (Warning: does not update the API filters/patterns!) if current_ext_on_vcd and self.conf('vcloud.api_extension.auto_update', False): current_ext_on_vcd = ext_manager.update_extension( self.name, namespace=self.conf('vcloud.api_extension.namespace'), routing_key=self.conf('vcloud.api_extension.routing_key'), exchange=self.conf('vcloud.api_extension.exchange')) self.log('info', 'Extension is updated on vCloud') # Register a new extension if not current_ext_on_vcd: ext_manager.add_extension( self.name, namespace=self.conf('vcloud.api_extension.namespace'), routing_key=self.conf('vcloud.api_extension.routing_key'), exchange=self.conf('vcloud.api_extension.exchange'), patterns=self.conf('vcloud.api_extension.api_filters')) self.log('info', 'Extension is registered on vCloud') # Ensure to enable it ext_manager.enable_extension(self.name, namespace=self.conf('vcloud.api_extension.namespace'), enabled=True) self.log('info', 'Extension is enabled on vCloud')
def _register_cse(client, routing_key, exchange, msg_update_callback=None): """Register or update CSE on vCD. :param pyvcloud.vcd.client.Client client: :param pyvcloud.vcd.client.Client client: :param str routing_key: :param str exchange: :param utils.ConsoleMessagePrinter msg_update_callback: Callback object that writes messages onto console. """ ext = APIExtension(client) patterns = [ f'/api/{CSE_SERVICE_NAME}', f'/api/{CSE_SERVICE_NAME}/.*', f'/api/{CSE_SERVICE_NAME}/.*/.*' ] cse_info = None try: cse_info = ext.get_extension_info(CSE_SERVICE_NAME, namespace=CSE_SERVICE_NAMESPACE) except MissingRecordException: pass if cse_info is None: ext.add_extension(CSE_SERVICE_NAME, CSE_SERVICE_NAMESPACE, routing_key, exchange, patterns) msg = f"Registered {CSE_SERVICE_NAME} as an API extension in vCD" else: ext.update_extension(CSE_SERVICE_NAME, namespace=CSE_SERVICE_NAMESPACE, routing_key=routing_key, exchange=exchange) msg = f"Updated {CSE_SERVICE_NAME} API Extension in vCD" if msg_update_callback: msg_update_callback.general(msg) LOGGER.info(msg)