def test_merge_list_by_key_when_original_list_is_null(self): original_list = None list_with_changes = [dict(id=1, value="123")] merged_list = merge_list_by_key(original_list, list_with_changes, key="id") expected_list = [dict(id=1, value="123")] self.assertEqual(merged_list, expected_list)
def _merge_connections(self, merged_data, resource, data): if self._should_merge(data, resource, key=Keys.CONNECTIONS): existing_connections = resource[Keys.CONNECTIONS] params_connections = data[Keys.CONNECTIONS] merged_data[Keys.CONNECTIONS] = merge_list_by_key(existing_connections, params_connections, key=Keys.ID) # merge Boot from Connections merged_data = self._merge_connections_boot(merged_data, resource) return merged_data
def _merge_controllers(self, merged_data, resource, data): if self._should_merge(data[Keys.LOCAL_STORAGE], resource[Keys.LOCAL_STORAGE], key=Keys.CONTROLLERS): existing_items = resource[Keys.LOCAL_STORAGE][Keys.CONTROLLERS] provided_items = merged_data[Keys.LOCAL_STORAGE][Keys.CONTROLLERS] merged_controllers = merge_list_by_key(existing_items, provided_items, key=Keys.DEVICE_SLOT) merged_data[Keys.LOCAL_STORAGE][Keys.CONTROLLERS] = merged_controllers # Merge Drives from Mezzanine and Embedded controllers merged_data = self._merge_controller_drives(merged_data, resource) return merged_data
def _merge_sas_logical_jbods(self, merged_data, resource, data): if self._should_merge(data[Keys.LOCAL_STORAGE], resource[Keys.LOCAL_STORAGE], key=Keys.SAS_LOGICAL_JBODS): existing_items = resource[Keys.LOCAL_STORAGE][Keys.SAS_LOGICAL_JBODS] provided_items = merged_data[Keys.LOCAL_STORAGE][Keys.SAS_LOGICAL_JBODS] merged_jbods = merge_list_by_key(existing_items, provided_items, key=Keys.ID, ignore_when_null=[Keys.SAS_LOGICAL_JBOD_URI]) merged_data[Keys.LOCAL_STORAGE][Keys.SAS_LOGICAL_JBODS] = merged_jbods return merged_data
def _merge_san_volumes(self, merged_data, resource, data): if self._should_merge(data[Keys.SAN], resource[Keys.SAN], key=Keys.VOLUMES): existing_volumes = resource[Keys.SAN][Keys.VOLUMES] params_volumes = data[Keys.SAN][Keys.VOLUMES] merged_volumes = merge_list_by_key(existing_volumes, params_volumes, key=Keys.ID) merged_data[Keys.SAN][Keys.VOLUMES] = merged_volumes # Merge Paths from SAN Storage Volumes merged_data = self._merge_san_storage_paths(merged_data, resource) return merged_data
def _merge_connections(self, merged_data, resource, data): if self._should_merge(data, resource, key=Keys.CONNECTIONS): existing_connections = resource[Keys.CONNECTIONS] params_connections = data[Keys.CONNECTIONS] merged_data[Keys.CONNECTIONS] = merge_list_by_key( existing_connections, params_connections, key=Keys.ID) # merge Boot from Connections merged_data = self._merge_connections_boot(merged_data, resource) return merged_data
def test_merge_list_by_key_should_ignore_key_when_null(self): original_list = [dict(id=1, value1="123", value2="345")] list_with_changes = [dict(id=1, value1=None, value2="345-changed")] merged_list = merge_list_by_key(original_list, list_with_changes, key="id", ignore_when_null=['value1', 'value2']) expected_list = [dict(id=1, value1="123", value2="345-changed")] self.assertEqual(merged_list, expected_list)
def test_merge_list_by_key_should_not_fail_when_ignored_key_absent(self): original_list = [dict(id=1, value1="123", value2="345")] list_with_changes = [dict(id=1, value3="678")] merged_list = merge_list_by_key(original_list, list_with_changes, key="id", ignore_when_null=['value1']) expected_list = [dict(id=1, value1="123", value2="345", value3="678")] self.assertEqual(merged_list, expected_list)
def test_merge_list_by_key_with_removed_items(self): original_list = [dict(id=2, allocatedMbps=1000, mac="E2:4B:0D:30:00:0B", requestedMbps=1000), dict(id=1, allocatedMbps=2500, mac="E2:4B:0D:30:00:09", requestedMbps=3500)] list_with_changes = [dict(id=1, requestedMbps=2700, allocatedVFs=3500)] merged_list = merge_list_by_key(original_list, list_with_changes, key="id") expected_list = [dict(id=1, allocatedMbps=2500, mac="E2:4B:0D:30:00:09", requestedMbps=2700, allocatedVFs=3500)] self.assertEqual(merged_list, expected_list)
def _merge_controller_drives(self, merged_data, resource): for current_controller in merged_data[Keys.LOCAL_STORAGE][Keys.CONTROLLERS][:]: for existing_controller in resource[Keys.LOCAL_STORAGE][Keys.CONTROLLERS][:]: same_slot = current_controller.get(Keys.DEVICE_SLOT) == existing_controller.get(Keys.DEVICE_SLOT) same_mode = existing_controller.get(Keys.MODE) == existing_controller.get(Keys.MODE) if same_slot and same_mode and current_controller[Keys.LOGICAL_DRIVES]: key_merge = self._define_key_to_merge_drives(current_controller) if key_merge: merged_drives = merge_list_by_key(existing_controller[Keys.LOGICAL_DRIVES], current_controller[Keys.LOGICAL_DRIVES], key=key_merge) current_controller[Keys.LOGICAL_DRIVES] = merged_drives return merged_data
def _merge_controllers(self, merged_data, resource, data): if self._should_merge(data[Keys.LOCAL_STORAGE], resource[Keys.LOCAL_STORAGE], key=Keys.CONTROLLERS): existing_items = resource[Keys.LOCAL_STORAGE][Keys.CONTROLLERS] provided_items = merged_data[Keys.LOCAL_STORAGE][Keys.CONTROLLERS] merged_controllers = merge_list_by_key(existing_items, provided_items, key=Keys.DEVICE_SLOT) merged_data[Keys.LOCAL_STORAGE][ Keys.CONTROLLERS] = merged_controllers # Merge Drives from Mezzanine and Embedded controllers merged_data = self._merge_controller_drives(merged_data, resource) return merged_data
def _merge_san_storage_paths(self, merged_data, resource): existing_volumes_map = OrderedDict([(i[Keys.ID], i) for i in resource[Keys.SAN][Keys.VOLUMES]]) merged_volumes = merged_data[Keys.SAN][Keys.VOLUMES] for merged_volume in merged_volumes: volume_id = merged_volume[Keys.ID] if volume_id in existing_volumes_map: if Keys.PATHS in merged_volume and Keys.PATHS in existing_volumes_map[volume_id]: existent_paths = existing_volumes_map[volume_id][Keys.PATHS] paths_from_merged_volume = merged_volume[Keys.PATHS] merged_paths = merge_list_by_key(existent_paths, paths_from_merged_volume, key=Keys.CONN_ID) merged_volume[Keys.PATHS] = merged_paths return merged_data
def _merge_sas_logical_jbods(self, merged_data, resource, data): if self._should_merge(data[Keys.LOCAL_STORAGE], resource[Keys.LOCAL_STORAGE], key=Keys.SAS_LOGICAL_JBODS): existing_items = resource[Keys.LOCAL_STORAGE][ Keys.SAS_LOGICAL_JBODS] provided_items = merged_data[Keys.LOCAL_STORAGE][ Keys.SAS_LOGICAL_JBODS] merged_jbods = merge_list_by_key( existing_items, provided_items, key=Keys.ID, ignore_when_null=[Keys.SAS_LOGICAL_JBOD_URI]) merged_data[Keys.LOCAL_STORAGE][ Keys.SAS_LOGICAL_JBODS] = merged_jbods return merged_data
def _merge_san_storage_paths(self, merged_data, resource): existing_volumes_map = OrderedDict([ (i[Keys.ID], i) for i in resource[Keys.SAN][Keys.VOLUMES] ]) merged_volumes = merged_data[Keys.SAN][Keys.VOLUMES] for merged_volume in merged_volumes: volume_id = merged_volume[Keys.ID] if volume_id in existing_volumes_map: if Keys.PATHS in merged_volume and Keys.PATHS in existing_volumes_map[ volume_id]: existent_paths = existing_volumes_map[volume_id][ Keys.PATHS] paths_from_merged_volume = merged_volume[Keys.PATHS] merged_paths = merge_list_by_key(existent_paths, paths_from_merged_volume, key=Keys.CONN_ID) merged_volume[Keys.PATHS] = merged_paths return merged_data
def _merge_controller_drives(self, merged_data, resource): for current_controller in merged_data[Keys.LOCAL_STORAGE][ Keys.CONTROLLERS][:]: for existing_controller in resource[Keys.LOCAL_STORAGE][ Keys.CONTROLLERS][:]: same_slot = current_controller.get( Keys.DEVICE_SLOT) == existing_controller.get( Keys.DEVICE_SLOT) same_mode = existing_controller.get( Keys.MODE) == existing_controller.get(Keys.MODE) if same_slot and same_mode and current_controller[ Keys.LOGICAL_DRIVES]: key_merge = self._define_key_to_merge_drives( current_controller) if key_merge: merged_drives = merge_list_by_key( existing_controller[Keys.LOGICAL_DRIVES], current_controller[Keys.LOGICAL_DRIVES], key=key_merge) current_controller[Keys.LOGICAL_DRIVES] = merged_drives return merged_data