def test_merge_nested_dict_no_depth(self): dest = { 'key': 'value', 'nested': { 'key2': 'value2', 'key3': 'value3', 'nested2': { 'key': 'value', 'some': 'thing' } } } source = { 'key': 'modified', 'nested': { 'key3': 'modified3', 'nested2': { 'key5': 'value5' } } } utils.merge_nested_dict(dest, source) self.assertEqual( dest, { 'key': 'modified', 'nested': { 'key3': 'modified3', 'nested2': { 'key5': 'value5' } } })
def update(self, name, **kwargs): archive_policy = self.get(name) if archive_policy is None: raise exc.CommandError('ArchivePolicy not found: %s' % name) updated = archive_policy.to_dict() kwargs = dict((k, v) for k, v in kwargs.items() if k in updated and (k in UPDATE_ATTR)) utils.merge_nested_dict(updated, kwargs, depth=1) return self._update("/v1/archive_policy/%s" % name, updated)
def update(self, alarm_id, **kwargs): self._compat_legacy_alarm_kwargs(kwargs) updated = self.get(alarm_id).to_dict() updated['time_constraints'] = self._merge_time_constraints( updated.get('time_constraints', []), kwargs) kwargs = dict((k, v) for k, v in kwargs.items() if k in updated and k in UPDATABLE_ATTRIBUTES) utils.merge_nested_dict(updated, kwargs, depth=1) return self._update(self._path(alarm_id), updated)
def update(self, id, **kwargs): metric = self.get(id) if metric is None: raise exc.CommandError('Metric not found: %s' % id) updated = metric.to_dict() kwargs = dict((k, v) for k, v in kwargs.items() if k in updated and (k in UPDATE_ATTR)) utils.merge_nested_dict(updated, kwargs, depth=1) return self._update("/v1/metric/%s" % id, updated)
def test_merge_nested_dict_no_depth(self): dest = {'key': 'value', 'nested': {'key2': 'value2', 'key3': 'value3', 'nested2': {'key': 'value', 'some': 'thing'}}} source = {'key': 'modified', 'nested': {'key3': 'modified3', 'nested2': {'key5': 'value5'}}} utils.merge_nested_dict(dest, source) self.assertEqual(dest, {'key': 'modified', 'nested': {'key3': 'modified3', 'nested2': {'key5': 'value5'}}})
def _merge_time_constraints(existing_tcs, kwargs): new_tcs = kwargs.get('time_constraints', []) if not existing_tcs: updated_tcs = new_tcs else: updated_tcs = [dict(tc) for tc in existing_tcs] for tc in new_tcs: for i, old_tc in enumerate(updated_tcs): if old_tc['name'] == tc['name']: # if names match, merge utils.merge_nested_dict(updated_tcs[i], tc) break else: updated_tcs.append(tc) tcs_to_remove = kwargs.get('remove_time_constraints', []) for tc in updated_tcs: if tc['name'] in tcs_to_remove: updated_tcs.remove(tc) return updated_tcs