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 Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/41f65857 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/41f65857 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/41f65857 Branch: refs/heads/vim51_win8 Commit: 41f6585754ed9d7e878f3b53ad4294e225cdb759 Parents: 1e8648c Author: Likitha Shetty <[email protected]> Authored: Mon Feb 11 12:04:55 2013 -0800 Committer: Prachi Damle <[email protected]> Committed: Mon Feb 11 12:05:03 2013 -0800 ---------------------------------------------------------------------- .../cloud/bridge/service/core/ec2/EC2Engine.java | 24 ++++++++++++-- 1 files changed, 20 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/41f65857/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java ---------------------------------------------------------------------- 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 9573d5b..b729f77 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<CloudStackIpAddress> 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<CloudStackIpAddress> 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<CloudStackUserVm> 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<CloudStackIpAddress> 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();
