From 41f6585754ed9d7e878f3b53ad4294e225cdb759 Mon Sep 17 00:00:00 2001 From: Likitha Shetty Date: Mon, 11 Feb 2013 12:04:55 -0800 Subject: [PATCH] CLOUDSTACK-1136: [EC2 Query API] AssociateAdress, DisassociateAddress and ReleaseAddress fail with NPE When invalid parameter is provided as input for any of these API's we get an NPE --- .../bridge/service/core/ec2/EC2Engine.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java b/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java index 9573d5ba092..b729f778ed3 100644 --- a/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java +++ b/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java @@ -767,7 +767,10 @@ public class EC2Engine extends ManagerBase { */ public boolean releaseAddress(EC2ReleaseAddress request) { try { - CloudStackIpAddress cloudIp = getApi().listPublicIpAddresses(null, null, null, null, null, request.getPublicIp(), null, null, null).get(0); + List cloudIps = getApi().listPublicIpAddresses(null, null, null, null, null, request.getPublicIp(), null, null, null); + if (cloudIps == null) + throw new EC2ServiceException(ServerError.InternalError, "Specified ipAddress doesn't exist"); + CloudStackIpAddress cloudIp = cloudIps.get(0); CloudStackInfoResponse resp = getApi().disassociateIpAddress(cloudIp.getId()); if (resp != null) { return resp.getSuccess(); @@ -787,8 +790,17 @@ public class EC2Engine extends ManagerBase { */ public boolean associateAddress( EC2AssociateAddress request ) { try { - CloudStackIpAddress cloudIp = getApi().listPublicIpAddresses(null, null, null, null, null, request.getPublicIp(), null, null, null).get(0); - CloudStackUserVm cloudVm = getApi().listVirtualMachines(null, null, true, null, null, null, null, request.getInstanceId(), null, null, null, null, null, null, null, null, null).get(0); + List cloudIps = getApi().listPublicIpAddresses(null, null, null, null, null, request.getPublicIp(), null, null, null); + if (cloudIps == null) + throw new EC2ServiceException(ServerError.InternalError, "Specified ipAddress doesn't exist"); + CloudStackIpAddress cloudIp = cloudIps.get(0); + + List vmList = getApi().listVirtualMachines(null, null, true, null, null, null, null, + request.getInstanceId(), null, null, null, null, null, null, null, null, null); + if (vmList == null || vmList.size() == 0) { + throw new EC2ServiceException(ServerError.InternalError, "Specified instance-id doesn't exist"); + } + CloudStackUserVm cloudVm = vmList.get(0); CloudStackInfoResponse resp = getApi().enableStaticNat(cloudIp.getId(), cloudVm.getId()); if (resp != null) { @@ -809,7 +821,11 @@ public class EC2Engine extends ManagerBase { */ public boolean disassociateAddress( EC2DisassociateAddress request ) { try { - CloudStackIpAddress cloudIp = getApi().listPublicIpAddresses(null, null, null, null, null, request.getPublicIp(), null, null, null).get(0); + List cloudIps = getApi().listPublicIpAddresses(null, null, null, null, null, request.getPublicIp(), null, null, null); + if (cloudIps == null) + throw new EC2ServiceException(ServerError.InternalError, "Specified ipAddress doesn't exist"); + CloudStackIpAddress cloudIp = cloudIps.get(0); + CloudStackInfoResponse resp = getApi().disableStaticNat(cloudIp.getId()); if (resp != null) { return resp.getSuccess();