Moti Asayag has uploaded a new change for review.

Change subject: restapi: Attaching a network to cluster by network id
......................................................................

restapi: Attaching a network to cluster by network id

The server throws an internal server error when
add network command is executed with invalid network
name or when providing an id only.

The patch adds support to attaching a network to a cluster
either by network ID or network name or both, as long as
there is a match between the ID to the name of the network.

Bug-Url: https://bugzilla.redhat.com/961046
Change-Id: Ie5fac9af37fec642fd3c0c6fbdb43ee04b364f2c
Signed-off-by: Moti Asayag <[email protected]>
---
M 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendClusterNetworksResource.java
M 
backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendClusterNetworksResourceTest.java
2 files changed, 25 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/85/14685/1

diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendClusterNetworksResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendClusterNetworksResource.java
index 11666e1..04446a9 100644
--- 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendClusterNetworksResource.java
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendClusterNetworksResource.java
@@ -4,6 +4,7 @@
 
 import javax.ws.rs.core.Response;
 
+import org.apache.commons.lang.StringUtils;
 import org.ovirt.engine.api.model.Cluster;
 import org.ovirt.engine.api.model.Network;
 import org.ovirt.engine.api.resource.AssignedNetworkResource;
@@ -34,22 +35,35 @@
 
     @Override
     public Response add(Network network) {
-        validateParameters(network, "name"); //right now, name is mandatory 
(future - id alone will be enough)
-        String networkId = getNetworkId(network.getName(), clusterId);
-        if (networkId == null) {
-            notFound(Network.class);
+        validateParameters(network, "id|name");
+
+        String networkName = null;
+        if (network.isSetId()) {
+            org.ovirt.engine.core.common.businessentities.network.Network net 
= lookupNetwork(asGuid(network.getId()));
+            if (net == null) {
+                notFound(Network.class);
+            }
+            networkName = net.getName();
+        }
+
+        String networkId = null;
+        if (network.isSetName()) {
+            networkId = getNetworkId(network.getName(), clusterId);
+            if (networkId == null) {
+                notFound(Network.class);
+            }
         }
 
         if (!network.isSetId()) {
             network.setId(networkId);
-        } else if (!network.getId().equals(networkId)) {
+        } else if (network.isSetName() && !network.getId().equals(networkId)) {
             badRequest("Network ID provided does not match the ID for network 
with name: " + network.getName());
         }
 
         org.ovirt.engine.core.common.businessentities.network.Network entity = 
map(network);
         return performCreate(addAction,
                                getActionParameters(network, entity),
-                               new NetworkIdResolver(network.getName()));
+                               new 
NetworkIdResolver(StringUtils.defaultIfEmpty(network.getName(),networkName)));
     }
 
     @Override
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendClusterNetworksResourceTest.java
 
b/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendClusterNetworksResourceTest.java
index 7c4c426..f84c8b0 100644
--- 
a/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendClusterNetworksResourceTest.java
+++ 
b/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendClusterNetworksResourceTest.java
@@ -89,10 +89,10 @@
 
     @Test
     public void testAddNetwork() throws Exception {
-        VDSGroup vdsGroup = setUpVDSGroupExpectations(CLUSTER_ID);
+        setUpVDSGroupExpectations(CLUSTER_ID);
 
         setUriInfo(setUpBasicUriExpectations());
-        setUpEntityQueryExpectations(1, null);
+        setUpEntityQueryExpectations(2, null);
         setUpGetClusterExpectations();
         setUpGetNetworksByDataCenterExpectations(1, null);
         setUpActionExpectations(VdcActionType.AttachNetworkToVdsGroup,
@@ -119,10 +119,11 @@
     }
 
     private void doTestBadAddNetwork(boolean canDo, boolean success, String 
detail) throws Exception {
-        VDSGroup vdsGroup = setUpVDSGroupExpectations(CLUSTER_ID);
+        setUpVDSGroupExpectations(CLUSTER_ID);
 
         setUriInfo(setUpBasicUriExpectations());
         setUpGetClusterExpectations();
+        setUpEntityQueryExpectations(1, null);
         setUpGetNetworksByDataCenterExpectations(1, null);
         setUpActionExpectations(VdcActionType.AttachNetworkToVdsGroup,
                 AttachNetworkToVdsGroupParameter.class,
@@ -162,7 +163,6 @@
     @Test
     public void testAddIncompleteParameters_noName() throws Exception {
         Network model = new Network();
-        model.setId(GUIDS[0].toString());
         model.setDescription(DESCRIPTIONS[0]);
         setUriInfo(setUpBasicUriExpectations());
         control.replay();
@@ -170,7 +170,7 @@
             collection.add(model);
             fail("expected WebApplicationException on incomplete parameters");
         } catch (WebApplicationException wae) {
-             verifyIncompleteException(wae, "Network", "add", "name");
+            verifyIncompleteException(wae, "Network", "add", "id|name");
         }
     }
 


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie5fac9af37fec642fd3c0c6fbdb43ee04b364f2c
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Moti Asayag <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to