Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #470 from priley86/network-identification
Browse files Browse the repository at this point in the history
[#455] use lan id and uid_ems to uniquely identify network
(cherry picked from commit 2102048)

https://bugzilla.redhat.com/show_bug.cgi?id=1599864
  • Loading branch information
AparnaKarve authored and simaishi committed Jul 19, 2018
1 parent ffa7fa8 commit 17e475e
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 79 deletions.
2 changes: 1 addition & 1 deletion app/javascript/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const coreComponents = [
'&sort_order=desc',
fetchClustersUrl:
'api/clusters/' +
'?attributes=ext_management_system.emstype,v_parent_datacenter,ext_management_system.name' +
'?attributes=ext_management_system.emstype,v_parent_datacenter,ext_management_system.name,lans,storages' +
'&expand=resources',
fetchNetworksUrl: 'api/lans/?expand=resources',
fetchDatastoresUrl: 'api/data_stores?expand=resources'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Immutable from 'seamless-immutable';
import { networkKey } from '../../../common/networkKey';

export const mapInfrastructureMappings = (transformation_mapping_items, clusters, datastores, networks) => {
/**
* map the target source -> destination clusters/networks/datastores for
Expand Down Expand Up @@ -54,12 +57,6 @@ export const mapInfrastructureMappings = (transformation_mapping_items, clusters
item => item.destination_type.toLowerCase() === 'lan'
);

// transform cluster mappings to key/value
const clustersByMappingId = {};
clusterMappingItems.forEach(cluster => {
clustersByMappingId[cluster.transformation_mapping_id] = cluster;
});

// create unique cluster mappings by unique target cluster
const targetClusters = {};
clusterMappingItems.forEach(clusterMapping => {
Expand All @@ -76,16 +73,31 @@ export const mapInfrastructureMappings = (transformation_mapping_items, clusters
}
});

// transform cluster lans and datastores to key/value lookups for use in datastore/lan mappings
const clusterDatastores = {};
const clusterLans = {};
clusters.forEach(cluster => {
if (cluster.storages && cluster.storages.length) {
cluster.storages.forEach(datastore => {
clusterDatastores[datastore.id] = cluster.id;
});
}
if (cluster.lans && cluster.lans.length) {
cluster.lans.forEach(lan => {
clusterLans[lan.id] = cluster.id;
});
}
});

// create unique datastore mappings by unique target datastore
const targetDatastores = {};
datastoreMappingItems.forEach(datastoreMapping => {
const clusterMapping = clustersByMappingId[datastoreMapping.transformation_mapping_id];
const sourceCluster = clusters.find(c => c.id === clusterMapping.source_id);
const targetCluster = clusters.find(c => c.id === clusterMapping.destination_id);
const sourceCluster = clusters.find(c => c.id === clusterDatastores[datastoreMapping.source_id]);
const targetCluster = clusters.find(c => c.id === clusterDatastores[datastoreMapping.destination_id]);
const sourceDatastore = datastores.find(d => d.id === datastoreMapping.source_id);
const targetDatastore = datastores.find(d => d.id === datastoreMapping.destination_id);

if (clusterMapping && sourceCluster && targetCluster && sourceDatastore && targetDatastore) {
if (sourceCluster && targetCluster && sourceDatastore && targetDatastore) {
const source = {
sourceDatastore,
sourceCluster
Expand All @@ -105,16 +117,19 @@ export const mapInfrastructureMappings = (transformation_mapping_items, clusters
}
});

// create unique networks mappings by unique target network (using ems_uid)
// create unique networks mappings by unique target network
const targetNetworks = {};
networkMappingItems.forEach(networkMapping => {
const clusterMapping = clustersByMappingId[networkMapping.transformation_mapping_id];
const sourceCluster = clusters.find(c => c.id === clusterMapping.source_id);
const targetCluster = clusters.find(c => c.id === clusterMapping.destination_id);
const sourceNetwork = networks.find(d => d.id === networkMapping.source_id);
const targetNetwork = networks.find(d => d.id === networkMapping.destination_id);
const sourceCluster = clusters.find(c => c.id === clusterLans[networkMapping.source_id]);
const targetCluster = clusters.find(c => c.id === clusterLans[networkMapping.destination_id]);

if (clusterMapping && sourceCluster && targetCluster && sourceNetwork && targetNetwork) {
const sn = networks.find(d => d.id === networkMapping.source_id);
const sourceNetwork = Immutable.set(sn, 'clusterId', sourceCluster.id);

const tn = networks.find(d => d.id === networkMapping.destination_id);
const targetNetwork = Immutable.set(tn, 'clusterId', targetCluster.id);

if (sourceCluster && targetCluster && sourceNetwork && targetNetwork) {
const source = {
sourceNetwork,
sourceCluster
Expand All @@ -124,19 +139,19 @@ export const mapInfrastructureMappings = (transformation_mapping_items, clusters
targetNetwork,
targetCluster
};
// LANs are currently duplicated in the backend database model, so
// we dedupe them using uid_ems attribute for now.
if (targetNetworks[targetNetwork.uid_ems]) {
const duplicatedLanIndex = targetNetworks[targetNetwork.uid_ems].sources.findIndex(
s => s.sourceNetwork.uid_ems === sourceNetwork.uid_ems

const targetNetworkKey = networkKey(targetNetwork);
if (targetNetworks[targetNetworkKey]) {
const duplicatedLanIndex = targetNetworks[targetNetworkKey].sources.findIndex(
s => networkKey(s.sourceNetwork) === networkKey(sourceNetwork)
);
if (duplicatedLanIndex === -1) {
targetNetworks[targetNetwork.uid_ems].sources.push(source);
targetNetworks[targetNetworkKey].sources.push(source);
}
} else {
targetNetworks[targetNetwork.uid_ems] = {};
targetNetworks[targetNetwork.uid_ems].target = target;
targetNetworks[targetNetwork.uid_ems].sources = [source];
targetNetworks[targetNetworkKey] = {};
targetNetworks[targetNetworkKey].target = target;
targetNetworks[targetNetworkKey].sources = [source];
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const _filterSourceNetworks = response => {
if (data.lans) {
const sourceNetworks = data.lans.map(lan => ({
...lan,
providerName: data.ext_management_system.name
providerName: data.ext_management_system.name,
clusterId: data.id
}));
return {
sourceNetworks
Expand Down Expand Up @@ -45,7 +46,8 @@ const _filterTargetNetworks = response => {
if (data.lans) {
const targetNetworks = data.lans.map(lan => ({
...lan,
providerName: data.ext_management_system.name
providerName: data.ext_management_system.name,
clusterId: data.id
}));
return {
targetNetworks
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export const groupByUidEms = (networks = []) =>
import { networkKey } from '../../../../../common/networkKey';

export const uniqueNetworks = (networks = []) =>
networks.reduce(
(networksMap, network) => ({
...networksMap,
[network.uid_ems]: networksMap[network.uid_ems] ? [...networksMap[network.uid_ems], network] : [network]
[networkKey(network)]: networksMap[networkKey(network)]
? [...networksMap[networkKey(network)], network]
: [network]
}),
{}
);
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { sourceClusterNetworks } from '../mappingWizardNetworksStep.fixtures';
import { groupByUidEms } from '../MappingWizardNetworksStepSelectors';
import { uniqueNetworks } from '../MappingWizardNetworksStepSelectors';

test('groupByUidEms creates a mapping of networks organized by uid_ems', () => {
test('uniqueNetworks creates a mapping of grouped networks', () => {
const sourceNetworks = sourceClusterNetworks[0].lans;

expect(groupByUidEms(sourceNetworks)).toMatchSnapshot();
expect(uniqueNetworks(sourceNetworks)).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Object {
"sourceNetworks": Array [
Object {
"allow_promiscuous": null,
"clusterId": "1",
"computed_allow_promiscuous": null,
"computed_forged_transmits": null,
"computed_mac_changes": null,
Expand Down Expand Up @@ -69,6 +70,7 @@ Object {
"targetNetworks": Array [
Object {
"allow_promiscuous": null,
"clusterId": "1",
"computed_allow_promiscuous": null,
"computed_forged_transmits": null,
"computed_mac_changes": null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`groupByUidEms creates a mapping of networks organized by uid_ems 1`] = `
exports[`uniqueNetworks creates a mapping of grouped networks 1`] = `
Object {
"VMWareCluster1-vm_network1": Array [
"1\\\\VMWareCluster1-vm_network1": Array [
Object {
"allow_promiscuous": null,
"clusterId": "1",
"computed_allow_promiscuous": null,
"computed_forged_transmits": null,
"computed_mac_changes": null,
Expand All @@ -22,6 +23,7 @@ Object {
},
Object {
"allow_promiscuous": null,
"clusterId": "1",
"computed_allow_promiscuous": null,
"computed_forged_transmits": null,
"computed_mac_changes": null,
Expand All @@ -38,9 +40,10 @@ Object {
"updated_on": "2017-06-01T04:24:18Z",
},
],
"VMWareCluster1-vm_network2": Array [
"1\\\\VMWareCluster1-vm_network2": Array [
Object {
"allow_promiscuous": null,
"clusterId": "1",
"computed_allow_promiscuous": null,
"computed_forged_transmits": null,
"computed_mac_changes": null,
Expand All @@ -57,9 +60,10 @@ Object {
"updated_on": "2017-06-01T04:24:18Z",
},
],
"VMWareCluster1-vm_network3": Array [
"1\\\\VMWareCluster1-vm_network3": Array [
Object {
"allow_promiscuous": null,
"clusterId": "1",
"computed_allow_promiscuous": null,
"computed_forged_transmits": null,
"computed_mac_changes": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import DualPaneMapperList from '../../../DualPaneMapper/DualPaneMapperList';
import DualPaneMapperCount from '../../../DualPaneMapper/DualPaneMapperCount';
import DualPaneMapperListItem from '../../../DualPaneMapper/DualPaneMapperListItem';
import MappingWizardTreeView from '../../../MappingWizardTreeView/MappingWizardTreeView';
import { networkKey } from '../../../../../../../common/networkKey';

import {
sourceNetworksFilter,
Expand Down Expand Up @@ -38,12 +39,12 @@ class NetworksStepForm extends React.Component {
selectSourceNetwork = sourceNetwork => {
this.setState(prevState => {
const isAlreadySelected = prevState.selectedSourceNetworks.some(
selectedSourceNetwork => selectedSourceNetwork.uid_ems === sourceNetwork.uid_ems
selectedSourceNetwork => networkKey(selectedSourceNetwork) === networkKey(sourceNetwork)
);
if (isAlreadySelected) {
return {
selectedSourceNetworks: prevState.selectedSourceNetworks.filter(
selectedSourceNetwork => selectedSourceNetwork.uid_ems !== sourceNetwork.uid_ems
selectedSourceNetwork => networkKey(selectedSourceNetwork) !== networkKey(sourceNetwork)
)
};
}
Expand Down Expand Up @@ -201,7 +202,7 @@ class NetworksStepForm extends React.Component {
...targetNetwork,
selected: false,
nodes: sourceNetworks.map(sourceNetwork => {
if (sourceNetwork.uid_ems === selectedNode.uid_ems) {
if (networkKey(sourceNetwork) === networkKey(selectedNode)) {
return {
...sourceNetwork,
selected: !sourceNetwork.selected
Expand Down Expand Up @@ -309,7 +310,7 @@ class NetworksStepForm extends React.Component {
selected={
selectedSourceNetworks &&
selectedSourceNetworks.some(
selectedSourceNetwork => selectedSourceNetwork.uid_ems === sourceNetwork.uid_ems
selectedSourceNetwork => networkKey(selectedSourceNetwork) === networkKey(sourceNetwork)
)
}
handleClick={this.selectSourceNetwork}
Expand All @@ -324,7 +325,7 @@ class NetworksStepForm extends React.Component {
item={targetNetwork}
text={`${targetNetwork.providerName} \\ ${targetNetwork.name}`}
key={targetNetwork.id}
selected={selectedTargetNetwork && selectedTargetNetwork.uid_ems === targetNetwork.uid_ems}
selected={selectedTargetNetwork && networkKey(selectedTargetNetwork) === networkKey(targetNetwork)}
handleClick={this.selectTargetNetwork}
handleKeyPress={this.selectTargetNetwork}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { shallow } from 'enzyme';

import NetworksStepForm from '../NetworksStepForm';
import { groupByUidEms } from '../../../MappingWizardNetworksStepSelectors';
import { uniqueNetworks } from '../../../MappingWizardNetworksStepSelectors';

import { clustersMappingWithTreeViewAttrs, targetNetworkWithTreeViewAttrs, networkGroupingForRep } from '../helpers';

Expand Down Expand Up @@ -36,8 +36,8 @@ describe('#addNetworkMapping', () => {
<NetworksStepForm
{...props}
input={input}
groupedSourceNetworks={groupByUidEms(networkGrouping)}
groupedTargetNetworks={groupByUidEms([targetNetworkToMap])}
groupedSourceNetworks={uniqueNetworks(networkGrouping)}
groupedTargetNetworks={uniqueNetworks([targetNetworkToMap])}
/>
);
const [networkGroupRep] = networkGrouping;
Expand All @@ -58,7 +58,7 @@ describe('#addNetworkMapping', () => {
nodes: [
{
...targetNetworkWithTreeViewAttrs(targetNetworkToMap),
nodes: networkGroupingForRep(networkGroupRep, groupByUidEms(networkGrouping), props.selectedCluster)
nodes: networkGroupingForRep(networkGroupRep, uniqueNetworks(networkGrouping), props.selectedCluster)
}
]
}
Expand All @@ -69,7 +69,7 @@ describe('#addNetworkMapping', () => {
const [mappedTargetNetwork, , targetNetworkToMap] = targetNetworks;
const [, , mappedSourceNetwork] = sourceNetworks;
const srcNetworks = [...networkGrouping, mappedSourceNetwork];
const groupedNetworks = groupByUidEms(srcNetworks);
const groupedNetworks = uniqueNetworks(srcNetworks);
const networksStepMapping = {
...clustersMappingWithTreeViewAttrs(clustersMapping),
nodes: [
Expand All @@ -87,8 +87,8 @@ describe('#addNetworkMapping', () => {
<NetworksStepForm
{...props}
input={input}
groupedSourceNetworks={groupByUidEms(srcNetworks)}
groupedTargetNetworks={groupByUidEms(targetNetworks)}
groupedSourceNetworks={uniqueNetworks(srcNetworks)}
groupedTargetNetworks={uniqueNetworks(targetNetworks)}
/>
);
const [networkGroupRep] = networkGrouping;
Expand Down Expand Up @@ -122,7 +122,7 @@ describe('#addNetworkMapping', () => {
const [mappedTargetNetwork] = targetNetworks;
const [, , mappedSourceNetwork] = sourceNetworks;
const srcNetworks = [...networkGrouping, mappedSourceNetwork];
const groupedNetworks = groupByUidEms(srcNetworks);
const groupedNetworks = uniqueNetworks(srcNetworks);
const networksStepMapping = {
...clustersMappingWithTreeViewAttrs(clustersMapping),
nodes: [
Expand All @@ -141,8 +141,8 @@ describe('#addNetworkMapping', () => {
<NetworksStepForm
{...props}
input={input}
groupedSourceNetworks={groupByUidEms(srcNetworks)}
groupedTargetNetworks={groupByUidEms([mappedTargetNetwork])}
groupedSourceNetworks={uniqueNetworks(srcNetworks)}
groupedTargetNetworks={uniqueNetworks([mappedTargetNetwork])}
/>
);
const [networkGroupRep] = networkGrouping;
Expand Down Expand Up @@ -179,8 +179,8 @@ describe('#removeNode', () => {
const [targetNetworkOne, targetNetworkTwo] = targetNetworks;
const sourceNetworkGroupOne = sourceNetworks.slice(0, 2);
const sourceNetworkGroupTwo = sourceNetworks.slice(2, 3);
const groupedSourceNetworks = groupByUidEms([...sourceNetworkGroupOne, ...sourceNetworkGroupTwo]);
const groupedTargetNetworks = groupByUidEms([targetNetworkOne, targetNetworkTwo]);
const groupedSourceNetworks = uniqueNetworks([...sourceNetworkGroupOne, ...sourceNetworkGroupTwo]);
const groupedTargetNetworks = uniqueNetworks([targetNetworkOne, targetNetworkTwo]);
props = {
...props,
groupedSourceNetworks,
Expand Down
Loading

0 comments on commit 17e475e

Please sign in to comment.