def _register_right(client, right_name, description, category, bundle_key, msg_update_callback=utils.NullPrinter()): """Register a right for CSE. :param pyvcloud.vcd.client.Client client: :param str right_name: the name of the new right to be registered. :param str description: brief description about the new right. :param str category: add the right in existing categories in vCD Roles and Rights or specify a new category name. :param str bundle_key: is used to identify the right name and change its value to different languages using localization bundle. :param utils.ConsoleMessagePrinter msg_update_callback: Callback object. :raises BadRequestException: if a right with given name already exists in vCD. """ ext = APIExtension(client) # Since the client is a sys admin, org will hold a reference to System org system_org = Org(client, resource=client.get_org()) try: right_name_in_vcd = f"{{{server_constants.CSE_SERVICE_NAME}}}:{right_name}" # noqa: E501 # TODO(): When org.get_right_record() is moved outside the org scope in # pyvcloud, update the code below to adhere to the new method names. system_org.get_right_record(right_name_in_vcd) msg = f"Right: {right_name} already exists in vCD" msg_update_callback.general(msg) INSTALL_LOGGER.info(msg) # Presence of the right in vCD is not a guarantee that the right will # be assigned to system org too. rights_in_system = system_org.list_rights_of_org() for dikt in rights_in_system: # TODO(): When localization support comes in, this check should be # ditched for a better one. if dikt['name'] == right_name_in_vcd: msg = f"Right: {right_name} already assigned to System " \ f"organization." msg_update_callback.general(msg) INSTALL_LOGGER.info(msg) return # Since the right is not assigned to system org, we need to add it. msg = f"Assigning Right: {right_name} to System organization." msg_update_callback.general(msg) INSTALL_LOGGER.info(msg) system_org.add_rights([right_name_in_vcd]) except EntityNotFoundException: # Registering a right via api extension end point, auto assigns it to # System org. msg = f"Registering Right: {right_name} in vCD" msg_update_callback.general(msg) INSTALL_LOGGER.info(msg) ext.add_service_right(right_name, server_constants.CSE_SERVICE_NAME, server_constants.CSE_SERVICE_NAMESPACE, description, category, bundle_key)
def test_007_register_service_right(self): """Test the method APIExtension.add_service_right(). This test passes if the right-name returned after execution of the method matches the expected right-name. """ logger = Environment.get_default_logger() api_extension = APIExtension(TestApiExtension._client) # Create a new right for CSE RBAC logger.debug('Registering service right(name:' + TestApiExtension._right_name + ', description:' + TestApiExtension._description + ', category:' + TestApiExtension._category + ').') register_right = api_extension.add_service_right( right_name=TestApiExtension._right_name, service_name=TestApiExtension._service_name, namespace=TestApiExtension._service1_namespace, description=TestApiExtension._description, category=TestApiExtension._category, bundle_key=TestApiExtension._bundle_key) expected_right_name = '{' + TestApiExtension._service1_namespace +\ '}:' + TestApiExtension._right_name registered_right_name = register_right.get('name') self.assertEqual(expected_right_name, registered_right_name)