Repository: cloudstack Updated Branches: refs/heads/master e85d1f911 -> 73b622c82
CLOUDSTACK-7068: addIpToNicCmd changed to BaseAsyncCreate Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/73b622c8 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/73b622c8 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/73b622c8 Branch: refs/heads/master Commit: 73b622c82368f6c58b1a5c8e32dcb1c808d534c5 Parents: e85d1f9 Author: Jayapal <jaya...@apache.org> Authored: Wed Jul 9 12:43:32 2014 +0530 Committer: Jayapal <jaya...@apache.org> Committed: Wed Jul 9 14:57:25 2014 +0530 ---------------------------------------------------------------------- api/src/com/cloud/event/EventTypes.java | 10 +++ api/src/com/cloud/network/NetworkService.java | 2 + .../api/command/user/vm/AddIpToVmNicCmd.java | 68 +++++++++++--------- .../command/user/vm/RemoveIpFromVmNicCmd.java | 2 +- .../com/cloud/network/NetworkServiceImpl.java | 22 +++++++ .../com/cloud/vpc/MockNetworkManagerImpl.java | 5 ++ 6 files changed, 76 insertions(+), 33 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/73b622c8/api/src/com/cloud/event/EventTypes.java ---------------------------------------------------------------------- diff --git a/api/src/com/cloud/event/EventTypes.java b/api/src/com/cloud/event/EventTypes.java index bc8a8c2..71bfdb6 100755 --- a/api/src/com/cloud/event/EventTypes.java +++ b/api/src/com/cloud/event/EventTypes.java @@ -31,6 +31,7 @@ import com.cloud.network.Site2SiteVpnConnection; import com.cloud.server.ResourceTag; import com.cloud.storage.snapshot.SnapshotPolicy; import com.cloud.vm.ConsoleProxy; +import com.cloud.vm.NicSecondaryIp; import com.cloud.vm.SecondaryStorageVm; import org.apache.cloudstack.config.Configuration; @@ -508,6 +509,11 @@ public class EventTypes { public static final String EVENT_GUEST_OS_MAPPING_REMOVE = "GUEST.OS.MAPPING.REMOVE"; public static final String EVENT_GUEST_OS_MAPPING_UPDATE = "GUEST.OS.MAPPING.UPDATE"; + public static final String EVENT_NIC_SECONDARY_IP_ASSIGN = "NIC.SECONDARY.IP.ASSIGN"; + public static final String EVENT_NIC_SECONDARY_IP_UNASSIGN = "NIC.SECONDARY.IP.UNASSIGN"; + public static final String EVENT_NIC_SECONDARY_IP_CONFIGURE = "NIC.SECONDARY.IP.CONFIGURE"; + + static { // TODO: need a way to force author adding event types to declare the entity details as well, with out braking @@ -849,6 +855,10 @@ public class EventTypes { entityEventDetails.put(EVENT_GUEST_OS_MAPPING_ADD, GuestOSHypervisor.class); entityEventDetails.put(EVENT_GUEST_OS_MAPPING_REMOVE, GuestOSHypervisor.class); entityEventDetails.put(EVENT_GUEST_OS_MAPPING_UPDATE, GuestOSHypervisor.class); + entityEventDetails.put(EVENT_NIC_SECONDARY_IP_ASSIGN, NicSecondaryIp.class); + entityEventDetails.put(EVENT_NIC_SECONDARY_IP_UNASSIGN, NicSecondaryIp.class); + entityEventDetails.put(EVENT_NIC_SECONDARY_IP_CONFIGURE, NicSecondaryIp.class); + } public static String getEntityForEvent(String eventName) { http://git-wip-us.apache.org/repos/asf/cloudstack/blob/73b622c8/api/src/com/cloud/network/NetworkService.java ---------------------------------------------------------------------- diff --git a/api/src/com/cloud/network/NetworkService.java b/api/src/com/cloud/network/NetworkService.java index 1e126f5..18b59e9 100755 --- a/api/src/com/cloud/network/NetworkService.java +++ b/api/src/com/cloud/network/NetworkService.java @@ -178,4 +178,6 @@ public interface NetworkService { Map<Network.Capability, String> getNetworkOfferingServiceCapabilities(NetworkOffering offering, Service service); IpAddress updateIP(Long id, String customId, Boolean displayIp); + + boolean configureNicSecondaryIp(NicSecondaryIp secIp, boolean isZoneSgEnabled); } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/73b622c8/api/src/org/apache/cloudstack/api/command/user/vm/AddIpToVmNicCmd.java ---------------------------------------------------------------------- diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/AddIpToVmNicCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/AddIpToVmNicCmd.java index 870bbbf..4b805ac 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/AddIpToVmNicCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/AddIpToVmNicCmd.java @@ -20,7 +20,7 @@ import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiCommandJobType; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; -import org.apache.cloudstack.api.BaseAsyncCmd; +import org.apache.cloudstack.api.BaseAsyncCreateCmd; import org.apache.cloudstack.api.Parameter; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.NicResponse; @@ -45,7 +45,7 @@ import com.cloud.vm.VirtualMachine; @APICommand(name = "addIpToNic", description = "Assigns secondary IP to NIC", responseObject = NicSecondaryIpResponse.class, requestHasSensitiveInfo = false, responseHasSensitiveInfo = false) -public class AddIpToVmNicCmd extends BaseAsyncCmd { +public class AddIpToVmNicCmd extends BaseAsyncCreateCmd { public static final Logger s_logger = Logger.getLogger(AddIpToVmNicCmd.class.getName()); private static final String s_name = "addiptovmnicresponse"; @@ -100,7 +100,7 @@ public class AddIpToVmNicCmd extends BaseAsyncCmd { @Override public String getEventType() { - return EventTypes.EVENT_NET_IP_ASSIGN; + return EventTypes.EVENT_NIC_SECONDARY_IP_ASSIGN; } @Override @@ -125,35 +125,18 @@ public class AddIpToVmNicCmd extends BaseAsyncCmd { public void execute() throws ResourceUnavailableException, ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException { CallContext.current().setEventDetails("Nic Id: " + getNicId()); - String ip; - NicSecondaryIp result; - String secondaryIp = null; - if ((ip = getIpaddress()) != null) { - if (!NetUtils.isValidIp(ip)) { - throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Invalid ip address " + ip); - } - } - - try { - result = _networkService.allocateSecondaryGuestIP(getNicId(), getIpaddress()); - } catch (InsufficientAddressCapacityException e) { - throw new InvalidParameterValueException("Allocating guest ip for nic failed : " + e.getMessage()); - } + NicSecondaryIp result = _entityMgr.findById(NicSecondaryIp.class, getEntityId()); if (result != null) { - secondaryIp = result.getIp4Address(); - if (isZoneSGEnabled()) { - // add security group rules for the secondary ip addresses - boolean success = false; - success = _securityGroupService.securityGroupRulesForVmSecIp(getNicId(), secondaryIp, true); - if (success == false) { - throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to set security group rules for the secondary ip"); - } + CallContext.current().setEventDetails("secondary Ip Id: " + getEntityId()); + boolean success = false; + success = _networkService.configureNicSecondaryIp(result, isZoneSGEnabled()); + + if (success == false) { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to set security group rules for the secondary ip"); } - s_logger.info("Associated ip address to NIC : " + secondaryIp); - NicSecondaryIpResponse response = new NicSecondaryIpResponse(); - response = _responseGenerator.createSecondaryIPToNicResponse(result); + NicSecondaryIpResponse response = _responseGenerator.createSecondaryIPToNicResponse(result); response.setResponseName(getCommandName()); this.setResponseObject(response); } else { @@ -161,10 +144,6 @@ public class AddIpToVmNicCmd extends BaseAsyncCmd { } } - @Override - public String getSyncObjType() { - return BaseAsyncCmd.networkSyncObject; - } @Override public Long getSyncObjId() { @@ -188,4 +167,29 @@ public class AddIpToVmNicCmd extends BaseAsyncCmd { return vm.getAccountId(); } + @Override + public void create() throws ResourceAllocationException { + String ip; + NicSecondaryIp result; + String secondaryIp = null; + if ((ip = getIpaddress()) != null) { + if (!NetUtils.isValidIp(ip)) { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Invalid ip address " + ip); + } + } + + try { + result = _networkService.allocateSecondaryGuestIP(getNicId(), getIpaddress()); + if (result != null) { + setEntityId(result.getId()); + setEntityUuid(result.getUuid()); + } + } catch (InsufficientAddressCapacityException e) { + throw new InvalidParameterValueException("Allocating guest ip for nic failed : " + e.getMessage()); + } + + if (result == null) { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to assign secondary ip to nic"); + } + } } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/73b622c8/api/src/org/apache/cloudstack/api/command/user/vm/RemoveIpFromVmNicCmd.java ---------------------------------------------------------------------- diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/RemoveIpFromVmNicCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/RemoveIpFromVmNicCmd.java index 70d5b48..19da39a 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/RemoveIpFromVmNicCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/RemoveIpFromVmNicCmd.java @@ -86,7 +86,7 @@ public class RemoveIpFromVmNicCmd extends BaseAsyncCmd { @Override public String getEventType() { - return EventTypes.EVENT_NET_IP_ASSIGN; + return EventTypes.EVENT_NIC_SECONDARY_IP_UNASSIGN; } public NicSecondaryIp getIpEntry() { http://git-wip-us.apache.org/repos/asf/cloudstack/blob/73b622c8/server/src/com/cloud/network/NetworkServiceImpl.java ---------------------------------------------------------------------- diff --git a/server/src/com/cloud/network/NetworkServiceImpl.java b/server/src/com/cloud/network/NetworkServiceImpl.java index b3de9e3..c8105e8 100755 --- a/server/src/com/cloud/network/NetworkServiceImpl.java +++ b/server/src/com/cloud/network/NetworkServiceImpl.java @@ -40,6 +40,7 @@ import javax.ejb.Local; import javax.inject.Inject; import javax.naming.ConfigurationException; +import com.cloud.network.security.SecurityGroupService; import org.apache.log4j.Logger; import org.apache.cloudstack.acl.ControlledEntity.ACLType; @@ -315,6 +316,9 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { @Inject LoadBalancingRulesService _lbService; + @Inject + public SecurityGroupService _securityGroupService; + int _cidrLimit; boolean _allowSubdomainNetworkAccess; @@ -648,6 +652,22 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { } @Override + @ActionEvent(eventType = EventTypes.EVENT_NIC_SECONDARY_IP_CONFIGURE, eventDescription = "Configuring secondary ip " + + "rules", async = true) + public boolean configureNicSecondaryIp(NicSecondaryIp secIp, boolean isZoneSgEnabled) { + boolean success = false; + + if (isZoneSgEnabled) { + success = _securityGroupService.securityGroupRulesForVmSecIp(secIp.getNicId(), secIp.getIp4Address(), true); + s_logger.info("Associated ip address to NIC : " + secIp.getIp4Address()); + } else { + success = true; + } + return success; + } + + @Override + @ActionEvent(eventType = EventTypes.EVENT_NIC_SECONDARY_IP_ASSIGN, eventDescription = "assigning secondary ip to nic", create = true) public NicSecondaryIp allocateSecondaryGuestIP(final long nicId, String requestedIp) throws InsufficientAddressCapacityException { Account caller = CallContext.current().getCallingAccount(); @@ -752,6 +772,8 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { @Override @DB + @ActionEvent(eventType = EventTypes.EVENT_NIC_SECONDARY_IP_UNASSIGN, eventDescription = "Removing secondary ip " + + "from nic", async = true) public boolean releaseSecondaryIpFromNic(long ipAddressId) { Account caller = CallContext.current().getCallingAccount(); boolean success = false; http://git-wip-us.apache.org/repos/asf/cloudstack/blob/73b622c8/server/test/com/cloud/vpc/MockNetworkManagerImpl.java ---------------------------------------------------------------------- diff --git a/server/test/com/cloud/vpc/MockNetworkManagerImpl.java b/server/test/com/cloud/vpc/MockNetworkManagerImpl.java index 1ed8b6e..097019d 100644 --- a/server/test/com/cloud/vpc/MockNetworkManagerImpl.java +++ b/server/test/com/cloud/vpc/MockNetworkManagerImpl.java @@ -862,4 +862,9 @@ public class MockNetworkManagerImpl extends ManagerBase implements NetworkOrches return null; } + @Override + public boolean configureNicSecondaryIp(NicSecondaryIp secIp, boolean isZoneSgEnabled) { + return false; //To change body of implemented methods use File | Settings | File Templates. + } + }