def update_cmdb(cmdb, var, pref, forced_find): '''Handle CMDB settings if present. CMDB is updated with var. var is also augmented with the cmdb entry found. ''' def update_entry(entry, cmdb, idx): 'Update var using a cmdb entry and save the full cmdb on disk.' entry.update(var) var.update(entry) var['used'] = 1 cmdb[idx] = var # First pass to lookup if the var is already in the database # and if this is the case, reuse the entry. idx = 0 for entry in cmdb: if is_included(pref, entry): update_entry(entry, cmdb, idx) break idx += 1 else: # not looking for $$ type matches if not forced_find: # Second pass, find a not used entry. idx = 0 for entry in cmdb: if 'used' not in entry: update_entry(entry, cmdb, idx) break idx += 1 else: raise CmdbError("No more entry in the CMDB, aborting.") else: raise CmdbError("No entry matched in the CMDB, aborting.") return True
def test_is_included_more(self): a = {'a': 1, 'b': 2} b = {'a': 1, 'b': 2, 'c': 3} self.assertTrue(generate.is_included(a, b))
def test_is_included_different(self): a = {'a': 1} b = {'a': 2} self.assertTrue(not generate.is_included(a, b))
def test_is_included_same(self): a = {'a': 1} self.assertTrue(generate.is_included(a, a))