Moti Asayag has uploaded a new change for review.

Change subject: engine: Prevent removing a labeled network
......................................................................

engine: Prevent removing a labeled network

Removing a labeled network from an interface should
be block if the nic is labeled with the same label.
In that case, in order to remove the network, the
nic should be unlabeled.

Change-Id: I300c0327fcf838b7581ad5a7ec12ee6bd9b9f0b2
Signed-off-by: Moti Asayag <masa...@redhat.com>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/SetupNetworksHelper.java
M 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/network/host/SetupNetworksHelperTest.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/errors/VdcBllMessages.java
M backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties
M 
frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/AppErrors.java
M 
frontend/webadmin/modules/userportal-gwtp/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
M 
frontend/webadmin/modules/webadmin/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
7 files changed, 114 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/41/22941/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/SetupNetworksHelper.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/SetupNetworksHelper.java
index 8664318..bd8478e 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/SetupNetworksHelper.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/SetupNetworksHelper.java
@@ -48,6 +48,7 @@
     private Set<String> attachedNetworksNames = new HashSet<String>();
 
     private Map<String, List<NetworkType>> ifacesWithExclusiveNetwork = new 
HashMap<String, List<NetworkType>>();
+    private Map<String, VdsNetworkInterface> ifacesByNetworkName;
 
     public SetupNetworksHelper(SetupNetworksParameters parameters, VDS vds) {
         params = parameters;
@@ -93,10 +94,31 @@
         extractModifiedLabeledInterfaces();
         detectSlaveChanges();
         validateMTU();
+        validateNotRemovingLabeledNetworks();
 
         return translateViolations();
     }
 
+    private void validateNotRemovingLabeledNetworks() {
+        Map<String, VdsNetworkInterface> nicsByName = 
Entities.entitiesByName(params.getInterfaces());
+        Map<String, VdsNetworkInterface> hostInterfacesByNetworkName =
+                
Entities.hostInterfacesByNetworkName(getExistingIfaces().values());
+
+        for (String network : removedNetworks) {
+            VdsNetworkInterface nic = hostInterfacesByNetworkName.get(network);
+            if (nic != null) {
+                if (NetworkUtils.isVlan(nic)) {
+                    nic = 
nicsByName.get(NetworkUtils.stripVlan(nic.getName()));
+                }
+
+                if (nic.getLabels() != null
+                        && 
nic.getLabels().contains(getExistingClusterNetworks().get(network).getLabel())) 
{
+                    
addViolation(VdcBllMessages.ACTION_TYPE_FAILED_CANNOT_REMOVE_LABELED_NETWORK_FROM_NIC,
 network);
+                }
+            }
+        }
+    }
+
     private void extractModifiedLabeledInterfaces() {
         for (VdsNetworkInterface nic : params.getInterfaces()) {
             VdsNetworkInterface existingNic = 
getExistingIfaces().get(nic.getName());
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/network/host/SetupNetworksHelperTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/network/host/SetupNetworksHelperTest.java
index c7ca256..ac96766 100644
--- 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/network/host/SetupNetworksHelperTest.java
+++ 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/network/host/SetupNetworksHelperTest.java
@@ -13,7 +13,9 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import org.apache.commons.lang.StringUtils;
 import org.junit.Rule;
@@ -1241,6 +1243,50 @@
         validateAndExpectNoViolations(helper);
     }
 
+    @Test
+    public void unlabeledNetworkRemovedFromLabeledNic() {
+        String networkName = "net";
+        VdsNetworkInterface nic = createLabeledNic("nic0", networkName, 
"lbl1");
+        mockExistingIfaces(nic);
+        nic.setNetworkName(null);
+        mockExistingNetworks(createNetwork(networkName));
+        SetupNetworksHelper helper = 
createHelper(createParametersForNics(nic));
+
+        validateAndExpectNoViolations(helper);
+        assertNoNetworksModified(helper);
+        assertNetworkRemoved(helper, networkName);
+    }
+
+    @Test
+    public void labeledNetworkRemovedFromUnlabeledNic() {
+        String networkName = "net";
+        VdsNetworkInterface nic = createNic("nic0", networkName);
+        mockExistingIfaces(nic);
+        nic.setNetworkName(null);
+        mockExistingNetworks(createLabeledNetwork(networkName, "lbl1"));
+        SetupNetworksHelper helper = 
createHelper(createParametersForNics(nic));
+
+        validateAndExpectNoViolations(helper);
+        assertNoNetworksModified(helper);
+        assertNetworkRemoved(helper, networkName);
+    }
+
+    @Test
+    public void labeledNetworkRemovedFromNic() {
+        String networkName = "net";
+        String label = "lbl1";
+        VdsNetworkInterface nic = createLabeledNic("nic0", networkName, label);
+        mockExistingIfaces(nic);
+        nic.setNetworkName(null);
+        mockExistingNetworks(createLabeledNetwork(networkName, label));
+
+        SetupNetworksHelper helper = 
createHelper(createParametersForNics(nic));
+
+        validateAndExpectViolation(helper,
+                
VdcBllMessages.ACTION_TYPE_FAILED_CANNOT_REMOVE_LABELED_NETWORK_FROM_NIC,
+                networkName);
+    }
+
     /* --- Helper methods for tests --- */
 
     private void validateAndExpectNoViolations(SetupNetworksHelper helper) {
@@ -1350,6 +1396,19 @@
     }
 
     /**
+     * @param networkName
+     *            The network's name.
+     * @param label
+     *            The label to be set on the network
+     * @return A network with some defaults, the given name and the given label
+     */
+    private Network createLabeledNetwork(String networkName, String label) {
+        Network network = createNetwork(networkName);
+        network.setLabel(label);
+        return network;
+    }
+
+    /**
      * Base method to create any sort of network interface with the given 
parameters.
      *
      * @param id
@@ -1369,7 +1428,8 @@
             Integer vlanId,
             String networkName,
             boolean bridged,
-            String address) {
+            String address,
+            Set<String> labels) {
         VdsNetworkInterface iface = new VdsNetworkInterface();
         iface.setId(id);
         iface.setName(name);
@@ -1379,6 +1439,7 @@
         iface.setNetworkName(networkName);
         iface.setBridged(bridged);
         iface.setAddress(address);
+        iface.setLabels(labels);
         return iface;
     }
 
@@ -1390,7 +1451,22 @@
      * @return {@link VdsNetworkInterface} representing a regular NIC with the 
given parameters.
      */
     private VdsNetworkInterface createNic(String nicName, String networkName) {
-        return createVdsInterface(Guid.newGuid(), nicName, false, null, null, 
networkName, true, null);
+        return createVdsInterface(Guid.newGuid(), nicName, false, null, null, 
networkName, true, null, null);
+    }
+
+    /**
+     * @param nicName
+     *            The name of the NIC.
+     * @param networkName
+     *            The network that is on the NIC. Can be <code>null</code>.
+     * @param labels
+     *            The labels to be set for the nic
+     * @return {@link VdsNetworkInterface} representing a regular labeled NIC 
with the given parameters.
+     */
+    private VdsNetworkInterface createLabeledNic(String string, String 
networkName, String... labels) {
+        VdsNetworkInterface nic = createNic("nic0", networkName);
+        nic.setLabels(new HashSet<>(Arrays.asList(labels)));
+        return nic;
     }
 
     /**
@@ -1408,7 +1484,8 @@
                 network.getVlanId(),
                 network.getName(),
                 network.isVmNetwork(),
-                network.getAddr());
+                network.getAddr(),
+                null);
         return nic;
     }
 
@@ -1434,7 +1511,7 @@
      * @return Bond with the given parameters.
      */
     private VdsNetworkInterface createBond(String name, String networkName) {
-        return createVdsInterface(Guid.newGuid(), name, true, null, null, 
networkName, true, null);
+        return createVdsInterface(Guid.newGuid(), name, true, null, null, 
networkName, true, null, null);
     }
 
     /**
@@ -1454,6 +1531,7 @@
                 vlanId,
                 networkName,
                 true,
+                null,
                 null);
     }
 
@@ -1467,7 +1545,7 @@
      * @return NIC from given NIC which is either enslaved or freed.
      */
     private VdsNetworkInterface enslaveOrReleaseNIC(VdsNetworkInterface iface, 
String bondName) {
-        return createVdsInterface(iface.getId(), iface.getName(), false, 
bondName, null, null, true, null);
+        return createVdsInterface(iface.getId(), iface.getName(), false, 
bondName, null, null, true, null, null);
     }
 
     /**
@@ -1584,7 +1662,8 @@
                     nics[i].getVlanId(),
                     nics[i].getNetworkName(),
                     nics[i].isBridged(),
-                    nics[i].getAddress()));
+                    nics[i].getAddress(),
+                    nics[i].getLabels()));
         }
         
when(interfaceDAO.getAllInterfacesForVds(any(Guid.class))).thenReturn(existingIfaces);
     }
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/errors/VdcBllMessages.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/errors/VdcBllMessages.java
index f9b427b..c14a894 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/errors/VdcBllMessages.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/errors/VdcBllMessages.java
@@ -479,6 +479,7 @@
     NETWORK_MTU_OVERRIDE_NOT_SUPPORTED(ErrorType.CONFLICT),
     EXTERNAL_NETWORK_CANNOT_BE_PROVISIONED(ErrorType.NOT_SUPPORTED),
     NETWORK_LABEL_FORMAT_INVALID(ErrorType.BAD_PARAMETERS),
+    
ACTION_TYPE_FAILED_CANNOT_REMOVE_LABELED_NETWORK_FROM_NIC(ErrorType.BAD_PARAMETERS),
     IMPROPER_INTERFACE_IS_LABELED(ErrorType.BAD_PARAMETERS),
     INTERFACE_ALREADY_LABELED(ErrorType.BAD_PARAMETERS),
     INTERFACE_NOT_LABELED(ErrorType.BAD_PARAMETERS),
diff --git 
a/backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties 
b/backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties
index 38b44db..434bc77 100644
--- 
a/backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties
+++ 
b/backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties
@@ -474,6 +474,7 @@
 EXTERNAL_NETWORK_CANNOT_BE_PROVISIONED=The specified external network cannot 
be configured on the host's interface.
 NETWORK_LABEL_FORMAT_INVALID=Network label must be formed only from: English 
letters, numbers, hyphen or underscore.
 ACTION_TYPE_FAILED_NETWORK_ALREADY_LABELED=Cannot ${action} ${type}. The 
specified network is already labeled.
+ACTION_TYPE_FAILED_CANNOT_REMOVE_LABELED_NETWORK_FROM_NIC=Cannot ${action} 
${type}. The following networks cannot be removed from the network interface 
since they are managed by the label: 
${ACTION_TYPE_FAILED_CANNOT_REMOVE_LABELED_NETWORK_FROM_NIC_LIST}. Please 
remove the label from the network interface in order to remove the network.
 ERROR_CANNOT_RECOVERY_STORAGE_POOL_THERE_IS_ACTIVE_DATA_DOMAINS=Cannot recover 
Data Center with active Data Storage Domain in Data Center.
 ERROR_CANNOT_RECOVERY_STORAGE_POOL_STORAGE_TYPE_MISSMATCH=Cannot recover Data 
Center. Mismatch between Storage Domain type and Data Center type.
 ERROR_CANNOT_DETACH_LAST_STORAGE_DOMAIN=Cannot remove the master Storage 
Domain from the Data Center without another active Storage Domain to take its 
place.\n\
diff --git 
a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/AppErrors.java
 
b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/AppErrors.java
index a460804..64abb2c 100644
--- 
a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/AppErrors.java
+++ 
b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/AppErrors.java
@@ -1313,6 +1313,9 @@
     @DefaultStringValue("Cannot ${action} ${type}. The specified network is 
already labeled.")
     String ACTION_TYPE_FAILED_NETWORK_ALREADY_LABELED();
 
+    @DefaultStringValue("Cannot ${action} ${type}. The following networks 
cannot be removed from the network interface since they are managed by the 
label: ${ACTION_TYPE_FAILED_CANNOT_REMOVE_LABELED_NETWORK_FROM_NIC_LIST}. 
Please remove the label from the network interface in order to remove the 
network.")
+    String ACTION_TYPE_FAILED_CANNOT_REMOVE_LABELED_NETWORK_FROM_NIC();
+
     @DefaultStringValue("Cannot recover Data Center with active Data Storage 
Domain in Data Center.")
     String ERROR_CANNOT_RECOVERY_STORAGE_POOL_THERE_IS_ACTIVE_DATA_DOMAINS();
 
diff --git 
a/frontend/webadmin/modules/userportal-gwtp/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
 
b/frontend/webadmin/modules/userportal-gwtp/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
index a412a17..fb6921f 100644
--- 
a/frontend/webadmin/modules/userportal-gwtp/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
+++ 
b/frontend/webadmin/modules/userportal-gwtp/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
@@ -455,6 +455,7 @@
 EXTERNAL_NETWORK_CANNOT_BE_PROVISIONED=The specified external network cannot 
be configured on the host's interface.
 NETWORK_LABEL_FORMAT_INVALID=Network label must be formed only from: English 
letters, numbers, hyphen or underscore.
 ACTION_TYPE_FAILED_NETWORK_ALREADY_LABELED=Cannot ${action} ${type}. The 
specified network is already labeled.
+ACTION_TYPE_FAILED_CANNOT_REMOVE_LABELED_NETWORK_FROM_NIC=Cannot ${action} 
${type}. The following networks cannot be removed from the network interface 
since they are managed by the label: 
${ACTION_TYPE_FAILED_CANNOT_REMOVE_LABELED_NETWORK_FROM_NIC_LIST}. Please 
remove the label from the network interface in order to remove the network.
 ERROR_CANNOT_RECOVERY_STORAGE_POOL_THERE_IS_ACTIVE_DATA_DOMAINS=Cannot recover 
Data Center with active Data Storage Domain in Data Center.
 ERROR_CANNOT_RECOVERY_STORAGE_POOL_STORAGE_TYPE_MISSMATCH=Cannot recover Data 
Center. Mismatch between Storage Domain type and Data Center type.
 ERROR_CANNOT_DETACH_LAST_STORAGE_DOMAIN=Cannot remove the master Storage 
Domain from the Data Center without another active Storage Domain to take its 
place.\n\
diff --git 
a/frontend/webadmin/modules/webadmin/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
 
b/frontend/webadmin/modules/webadmin/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
index 691861a..f7fa226 100644
--- 
a/frontend/webadmin/modules/webadmin/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
+++ 
b/frontend/webadmin/modules/webadmin/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
@@ -478,6 +478,7 @@
 EXTERNAL_NETWORK_CANNOT_BE_PROVISIONED=The specified external network cannot 
be configured on the host's interface.
 NETWORK_LABEL_FORMAT_INVALID=Network label must be formed only from: English 
letters, numbers, hyphen or underscore.
 ACTION_TYPE_FAILED_NETWORK_ALREADY_LABELED=Cannot ${action} ${type}. The 
specified network is already labeled.
+ACTION_TYPE_FAILED_CANNOT_REMOVE_LABELED_NETWORK_FROM_NIC=Cannot ${action} 
${type}. The following networks cannot be removed from the network interface 
since they are managed by the label: 
${ACTION_TYPE_FAILED_CANNOT_REMOVE_LABELED_NETWORK_FROM_NIC_LIST}. Please 
remove the label from the network interface in order to remove the network.
 ERROR_CANNOT_RECOVERY_STORAGE_POOL_THERE_IS_ACTIVE_DATA_DOMAINS=Cannot recover 
Data Center with active Data Storage Domain in Data Center.
 ERROR_CANNOT_RECOVERY_STORAGE_POOL_STORAGE_TYPE_MISSMATCH=Cannot recover Data 
Center. Mismatch between Storage Domain type and Data Center type.
 ERROR_CANNOT_DETACH_LAST_STORAGE_DOMAIN=Cannot remove the master Storage 
Domain from the Data Center without another active Storage Domain to take its 
place.\n\


-- 
To view, visit http://gerrit.ovirt.org/22941
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I300c0327fcf838b7581ad5a7ec12ee6bd9b9f0b2
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Moti Asayag <masa...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to