Пример #1
0
 def load_mconfig(self) -> GatewayConfigs:
     cfg_file_name = self._get_mconfig_file_path()
     try:
         with open(cfg_file_name, 'r') as cfg_file:
             mconfig_str = cfg_file.read()
         return self.deserialize_mconfig(mconfig_str)
     except (OSError, json.JSONDecodeError, json_format.ParseError) as e:
         raise LoadConfigError('Error loading mconfig') from e
Пример #2
0
    def load_service_mconfig(self, service_name: str) -> Any:
        mconfig = self.load_mconfig()
        if service_name not in mconfig.configs_by_key:
            raise LoadConfigError(
                "Service ({}) missing in mconfig".format(service_name), )

        service_mconfig = mconfig.configs_by_key[service_name]
        return unpack_mconfig_any(service_mconfig)
Пример #3
0
    def load_service_mconfig(self, service_name: str) -> Any:
        offset_mconfig = self.load_mconfig()
        if service_name not in offset_mconfig.configs.configs_by_key:
            raise LoadConfigError(
                'Service {} missing in mconfig'.format(service_name), )

        service_mconfig = offset_mconfig.configs.configs_by_key[service_name]
        return unpack_mconfig_any(service_mconfig)
Пример #4
0
    def load_service_mconfig_as_json(self, service_name) -> Any:
        cfg_file_name = self._get_mconfig_file_path()
        with open(cfg_file_name, 'r') as f:
            json_mconfig = json.load(f)
            service_configs = json_mconfig["configsByKey"]
        if service_name not in service_configs:
            raise LoadConfigError(
                "Service ({}) missing in mconfig".format(service_name), )

        return service_configs[service_name]
Пример #5
0
    def load_service_mconfig_as_json(self, service_name) -> Any:
        cfg_file_name = self._get_mconfig_file_path()
        with open(cfg_file_name, 'r', encoding='utf-8') as f:
            json_mconfig = json.load(f)
            service_configs = json_mconfig.get('configsByKey', {})
            service_configs.update(json_mconfig.get('configs_by_key', {}))
        if service_name not in service_configs:
            raise LoadConfigError(
                "Service ({}) missing in mconfig".format(service_name), )

        return service_configs[service_name]
Пример #6
0
    def test_both_mconfigs_load_error(self, new_manager_mock,
                                      old_manager_mock):
        """
        Test both mconfig managers erroring out on load
        """
        new_manager_instance = Mock()
        new_manager_instance.load_service_mconfig = Mock(
            side_effect=LoadConfigError('mock'), )
        new_manager_mock.return_value = new_manager_instance

        old_manager_instance = Mock()
        old_manager_instance.load_service_mconfig = Mock(
            side_effect=LoadConfigError('mock2'), )
        old_manager_mock.return_value = old_manager_instance

        actual = get_mconfig_manager()
        self.assertIs(actual, old_manager_instance)
        new_manager_instance.load_service_mconfig \
            .assert_called_once_with('magmad')
        old_manager_instance.load_service_mconfig \
            .assert_called_once_with('magmad')
Пример #7
0
def unpack_mconfig_any(mconfig_any: Any, mconfig_struct: TAny) -> TAny:
    """
    Unpack a protobuf Any type into a given an empty protobuf message struct
    for a service.

    Args:
        mconfig_any: protobuf Any type to unpack
        mconfig_struct: protobuf message struct

    Returns: Concrete protobuf object that the provided Any wraps
    """
    unpacked = mconfig_any.Unpack(mconfig_struct)
    if not unpacked:
        raise LoadConfigError(
            'Cannot unpack Any type into message: %s' % mconfig_struct, )
    return mconfig_struct
Пример #8
0
def unpack_mconfig_any(mconfig_any: Any) -> TAny:
    """
    Unpack a protobuf Any type into its concrete protobuf type.

    Args:
        mconfig_any: protobuf Any type to unpack

    Returns: Concrete protobuf object that the provided Any wraps
    """
    type_name = mconfig_any.TypeName()
    try:
        msg = symbol_database.Default().GetSymbol(type_name)()
    except KeyError as e:
        raise LoadConfigError(
            'Mconfig proto type %s not found' % type_name,
        ) from e
    mconfig_any.Unpack(msg)
    return msg
Пример #9
0
def _load_yaml_file(file_name: str) -> Any:
    """
    Load the yaml file and returns the python object.

    Args:
        file_name: name of the .yml file

    Returns:
        Contents of the yml file deserialized into a Python object

    Raises:
        LoadConfigError: on error
    """

    try:
        with open(file_name, 'r') as stream:
            data = yaml.safe_load(stream)
            return data
    except (OSError, yaml.YAMLError) as e:
        raise LoadConfigError('Error loading yml config') from e
Пример #10
0
    def test_new_mconfig_load_error(self, new_manager_mock, old_manager_mock):
        """
        Test feature flag on, but new mconfig manager errors out on load
        """
        new_manager_instance = Mock()
        new_manager_instance.load_service_mconfig = Mock(
            side_effect=LoadConfigError('mock'), )
        new_manager_mock.return_value = new_manager_instance

        old_manager_instance = Mock()
        old_manager_instance.load_service_mconfig.return_value = MagmaD(
            feature_flags={'kafka_config_streamer': True}, )
        old_manager_mock.return_value = old_manager_instance

        actual = get_mconfig_manager()
        self.assertIs(actual, new_manager_instance)
        new_manager_instance.load_service_mconfig\
            .assert_called_once_with('magmad')
        old_manager_instance.load_service_mconfig\
            .assert_called_once_with('magmad')