def test_set(self): obj = {} DictUtils.set(obj, 'a.b.c', 1) self.assertEqual(1, DictUtils.get(obj, 'a.b.c')) obj = {} DictUtils.set(obj, 'a', 2) self.assertEqual(2, DictUtils.get(obj, 'a'))
def merge(self, existing, to_add) -> bool: """ Merges the given openshift object into one. :param existing: Existing where the data should be added to :param to_add: New data :return: True if the data has been merged, false otherwise """ expected_type = existing['kind'].lower() type_str = to_add['kind'].lower() if expected_type != type_str: return False expected_name = DictUtils.get(existing, self.NAME_PATH) name = DictUtils.get(to_add, self.NAME_PATH) if name is not None and expected_name is not None and expected_name != name: return False if expected_type == 'DeploymentConfig'.lower(): self._merge_dc(DeploymentConfig(existing), DeploymentConfig(to_add)) return True self.log.warning('Don\'t know how to merge ' + expected_type)
def get_for_each(self) -> List[AppConfig]: """ Returns all instances of this app which should be created. :return: Instances, 1 by default :raise MissingVar: Gets raised if the data inside forEach is not complete """ instances = [] for instance_vars in self.data.get('forEach', []): assert isinstance(instance_vars, dict) dc_name = instance_vars.get('DC_NAME') if dc_name is None: raise MissingVar('DC_NAME not defined in forEach for app ' + self.get_dc_name()) config = AppConfig(self._config_root, None, instance_vars) # Inherit all parameters config.data.update(self.data) # Update the DC_NAME DictUtils.set(config.data, 'dc.name', dc_name) instances.append(config) if len(instances) == 0: # No forEach defined, just create one instance instances.append(self) return instances
def get_dc_name(self) -> Optional[str]: """ Returns the configured name of the deployment config :return: Name """ return DictUtils.get(self.data, 'dc.name')
def process(self, yml): kind = yml.get('kind') if kind == 'DeploymentConfig': yml['kind'] = 'Deployment' version = yml.get('apiVersion') if not version.startswith('apps/'): yml['apiVersion'] = 'apps/' + version name_selector = DictUtils.get(yml, 'spec.selector.name') if name_selector is not None: DictUtils.delete(yml, 'spec.selector.name') DictUtils.set(yml, 'spec.selector.matchLabels.app', name_selector) strategy_type = DictUtils.get(yml, 'spec.strategy.type') if strategy_type == 'Rolling': DictUtils.set(yml, 'spec.strategy.type', 'RollingUpdate') name = DictUtils.get(yml, 'spec.template.metadata.labels.name') if name is not None: DictUtils.delete(yml, 'spec.template.metadata.labels.name') DictUtils.set(yml, 'spec.template.metadata.labels.app', name) return
def test_get(self): out = DictUtils.get({'a': {'b': {'c': 1}}}, 'a.b.c') self.assertEqual(1, out)
def test_get_none(self): out = DictUtils.get(None, 'a.f') self.assertIsNone(out)
def test_get_missing(self): out = DictUtils.get({'a': {'b': {'c': 1}}}, 'a.f') self.assertIsNone(out)
def get_template_name(self) -> Optional[str]: """ Returns the name of the template :return: Name or None if no name is defined """ return DictUtils.get(self.get_template(), 'metadata.labels.name')
def get_template(self) -> Optional[dict]: """ Returns the template object """ return DictUtils.get(self.data, 'spec.template')