use of com.cloud.db.model.Zone in project cosmic by MissionCriticalCloud.
the class CommandSetupHelper method createApplyStaticNatCommands.
public void createApplyStaticNatCommands(final List<? extends StaticNat> rules, final VirtualRouter router, final Commands cmds) {
final List<StaticNatRuleTO> rulesTO = new ArrayList<>();
if (rules != null) {
for (final StaticNat rule : rules) {
final IpAddress sourceIp = _networkModel.getIp(rule.getSourceIpAddressId());
final StaticNatRuleTO ruleTO = new StaticNatRuleTO(0, sourceIp.getAddress().addr(), null, null, rule.getDestIpAddress(), null, null, null, rule.isForRevoke(), false);
rulesTO.add(ruleTO);
}
}
final SetStaticNatRulesCommand cmd = new SetStaticNatRulesCommand(rulesTO, router.getVpcId());
cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));
cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
final Zone zone = zoneRepository.findById(router.getDataCenterId()).orElse(null);
cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, zone.getNetworkType().toString());
cmds.addCommand(cmd);
}
use of com.cloud.db.model.Zone in project cosmic by MissionCriticalCloud.
the class UserVmManagerImpl method restoreVM.
@Override
public UserVm restoreVM(final RestoreVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException {
// Input validation
final Account caller = CallContext.current().getCallingAccount();
final long vmId = cmd.getVmId();
final Long newTemplateId = cmd.getTemplateId();
final UserVmVO vm = _vmDao.findById(vmId);
if (vm == null) {
final InvalidParameterValueException ex = new InvalidParameterValueException("Cannot find VM with ID " + vmId);
ex.addProxyObject(String.valueOf(vmId), "vmId");
throw ex;
}
final Zone zone = zoneRepository.findById(vm.getDataCenterId()).orElse(null);
final VirtualMachineTemplate template = _entityMgr.findByIdIncludingRemoved(VirtualMachineTemplate.class, vm.getTemplateId());
checkHypervisorEnabled(zone, template);
_accountMgr.checkAccess(caller, null, true, vm);
return restoreVMInternal(caller, vm, newTemplateId);
}
use of com.cloud.db.model.Zone in project cosmic by MissionCriticalCloud.
the class UserVmManagerImpl method addNicToVirtualMachine.
@Override
@ActionEvent(eventType = EventTypes.EVENT_NIC_CREATE, eventDescription = "Creating Nic", async = true)
public UserVm addNicToVirtualMachine(final AddNicToVMCmd cmd) throws InvalidParameterValueException, PermissionDeniedException, CloudRuntimeException {
final Long vmId = cmd.getVmId();
final Long networkId = cmd.getNetworkId();
final String ipAddress = cmd.getIpAddress();
final String macAddress = cmd.getMacAddress();
final Account caller = CallContext.current().getCallingAccount();
final UserVmVO vmInstance = _vmDao.findById(vmId);
if (vmInstance == null) {
throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId);
}
// Check that Vm does not have VM Snapshots
if (_vmSnapshotDao.findByVm(vmId).size() > 0) {
throw new InvalidParameterValueException("NIC cannot be added to VM with VM Snapshots");
}
final NetworkVO network = _networkDao.findById(networkId);
if (network == null) {
throw new InvalidParameterValueException("unable to find a network with id " + networkId);
}
// Root admin may plug anything, Domain admin is allowed to plug into the public network
if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
if (!(network.getGuestType() == GuestType.Shared && network.getAclType() == ACLType.Domain) && !(network.getAclType() == ACLType.Account && network.getAccountId() == vmInstance.getAccountId()) && !(caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN && TrafficType.Public.equals(network.getTrafficType()))) {
throw new InvalidParameterValueException("only shared network or isolated network with the same account_id can be added to vmId: " + vmId);
}
}
final List<NicVO> allNics = _nicDao.listByVmId(vmInstance.getId());
for (final NicVO nic : allNics) {
if (nic.getNetworkId() == network.getId()) {
throw new CloudRuntimeException("A NIC already exists for VM:" + vmInstance.getInstanceName() + " in network: " + network.getUuid());
}
}
if (_nicDao.findByNetworkIdAndMacAddress(networkId, macAddress) != null) {
throw new CloudRuntimeException("A NIC with this MAC-Address already exists for network: " + network.getUuid());
}
NicProfile profile = new NicProfile(ipAddress, null, macAddress);
if (ipAddress != null) {
if (!(NetUtils.isValidIp4(ipAddress) || NetUtils.isValidIp6(ipAddress))) {
throw new InvalidParameterValueException("Invalid format for IP address parameter: " + ipAddress);
}
}
// Perform permission check on VM
_accountMgr.checkAccess(caller, null, true, vmInstance);
// Verify that zone is not Basic
final Zone zone = zoneRepository.findById(vmInstance.getDataCenterId()).orElse(null);
if (zone.getNetworkType() == NetworkType.Basic) {
throw new CloudRuntimeException("Zone " + vmInstance.getDataCenterId() + ", has a NetworkType of Basic. Can't add a new NIC to a VM on a Basic Network");
}
// Perform account permission check on network
_accountMgr.checkAccess(caller, AccessType.UseEntry, false, network);
// ensure network belongs in zone
if (network.getDataCenterId() != vmInstance.getDataCenterId()) {
throw new CloudRuntimeException(vmInstance + " is in zone:" + vmInstance.getDataCenterId() + " but " + network + " is in zone:" + network.getDataCenterId());
}
// Get all vms hostNames in the network
final List<String> hostNames = _vmInstanceDao.listDistinctHostNames(network.getId());
// This will also check if there are multiple nics of same vm in the network
if (hostNames.contains(vmInstance.getHostName())) {
for (final String hostName : hostNames) {
final VMInstanceVO vm = _vmInstanceDao.findVMByHostName(hostName);
if (_networkModel.getNicInNetwork(vm.getId(), network.getId()) != null && vm.getHostName().equals(vmInstance.getHostName())) {
throw new CloudRuntimeException(network + " already has a vm with host name: " + vmInstance.getHostName());
}
}
}
NicProfile guestNic = null;
boolean cleanUp = true;
try {
guestNic = _itMgr.addVmToNetwork(vmInstance, network, profile);
cleanUp = false;
} catch (final ResourceUnavailableException e) {
throw new CloudRuntimeException("Unable to add NIC to " + vmInstance + ": " + e);
} catch (final InsufficientCapacityException e) {
throw new CloudRuntimeException("Insufficient capacity when adding NIC to " + vmInstance + ": " + e);
} catch (final ConcurrentOperationException e) {
throw new CloudRuntimeException("Concurrent operations on adding NIC to " + vmInstance + ": " + e);
} finally {
if (cleanUp) {
try {
_itMgr.removeVmFromNetwork(vmInstance, network, null);
} catch (final ResourceUnavailableException e) {
throw new CloudRuntimeException("Error while cleaning up NIC " + e);
}
}
}
if (guestNic == null) {
throw new CloudRuntimeException("Unable to add NIC to " + vmInstance);
}
CallContext.current().putContextParameter(Nic.class, guestNic.getUuid());
s_logger.debug("Successful addition of " + network + " from " + vmInstance);
return _vmDao.findById(vmInstance.getId());
}
use of com.cloud.db.model.Zone in project cosmic by MissionCriticalCloud.
the class UserVmManagerImpl method rebootVirtualMachine.
private UserVm rebootVirtualMachine(final long userId, final long vmId) throws InsufficientCapacityException, ResourceUnavailableException {
final UserVmVO vm = _vmDao.findById(vmId);
if (vm == null || vm.getState() == State.Destroyed || vm.getState() == State.Expunging || vm.getRemoved() != null) {
s_logger.warn("Vm id=" + vmId + " doesn't exist");
return null;
}
if (vm.getState() == State.Running && vm.getHostId() != null) {
collectVmDiskStatistics(vm);
final Zone zone = zoneRepository.findById(vm.getDataCenterId()).orElse(null);
try {
if (zone.getNetworkType() == NetworkType.Advanced) {
// List all networks of vm
final List<Long> vmNetworks = _vmNetworkMapDao.getNetworks(vmId);
final List<DomainRouterVO> routers = new ArrayList<>();
// List the stopped routers
for (final long vmNetworkId : vmNetworks) {
final List<DomainRouterVO> router = _routerDao.listStopped(vmNetworkId);
routers.addAll(router);
}
// and routers are started serially ,may revisit to make this process parallel
for (final DomainRouterVO routerToStart : routers) {
s_logger.warn("Trying to start router " + routerToStart.getInstanceName() + " as part of vm: " + vm.getInstanceName() + " reboot");
_virtualNetAppliance.startRouter(routerToStart.getId(), true);
}
}
} catch (final ConcurrentOperationException e) {
throw new CloudRuntimeException("Concurrent operations on starting router. " + e);
} catch (final Exception ex) {
throw new CloudRuntimeException("Router start failed due to" + ex);
} finally {
s_logger.info("Rebooting vm " + vm.getInstanceName());
_itMgr.reboot(vm.getUuid(), null);
}
return _vmDao.findById(vmId);
} else {
s_logger.error("Vm id=" + vmId + " is not in Running state, failed to reboot");
return null;
}
}
use of com.cloud.db.model.Zone in project cosmic by MissionCriticalCloud.
the class UserVmManagerImpl method removeNicFromVirtualMachine.
@Override
@ActionEvent(eventType = EventTypes.EVENT_NIC_DELETE, eventDescription = "Removing Nic", async = true)
public UserVm removeNicFromVirtualMachine(final RemoveNicFromVMCmd cmd) throws InvalidParameterValueException, PermissionDeniedException, CloudRuntimeException {
final Long vmId = cmd.getVmId();
final Long nicId = cmd.getNicId();
final Account caller = CallContext.current().getCallingAccount();
final UserVmVO vmInstance = _vmDao.findById(vmId);
if (vmInstance == null) {
throw new InvalidParameterValueException("Unable to find a virtual machine with id " + vmId);
}
// Check that Vm does not have VM Snapshots
if (_vmSnapshotDao.findByVm(vmId).size() > 0) {
throw new InvalidParameterValueException("NIC cannot be removed from VM with VM Snapshots");
}
final NicVO nic = _nicDao.findById(nicId);
if (nic == null) {
throw new InvalidParameterValueException("Unable to find a nic with id " + nicId);
}
final NetworkVO network = _networkDao.findById(nic.getNetworkId());
if (network == null) {
throw new InvalidParameterValueException("Unable to find a network with id " + nic.getNetworkId());
}
// Perform permission check on VM
_accountMgr.checkAccess(caller, null, true, vmInstance);
// Verify that zone is not Basic
final Zone zone = zoneRepository.findById(vmInstance.getDataCenterId()).orElse(null);
if (zone.getNetworkType() == NetworkType.Basic) {
throw new InvalidParameterValueException("Zone " + vmInstance.getDataCenterId() + ", has a NetworkType of Basic. Can't remove a NIC from a VM on a Basic Network");
}
// check to see if nic is attached to VM
if (nic.getInstanceId() != vmId) {
throw new InvalidParameterValueException(nic + " is not a nic on " + vmInstance);
}
// Perform account permission check on network
_accountMgr.checkAccess(caller, AccessType.UseEntry, false, network);
// don't delete default NIC on a user VM
if (nic.isDefaultNic() && vmInstance.getType() == VirtualMachineType.User) {
throw new InvalidParameterValueException("Unable to remove nic from " + vmInstance + " in " + network + ", nic is default.");
}
// if specified nic is associated with PF/LB/Static NAT
if (_rulesMgr.listAssociatedRulesForGuestNic(nic).size() > 0) {
throw new InvalidParameterValueException("Unable to remove nic from " + vmInstance + " in " + network + ", nic has associated Port forwarding or Load balancer or " + "Static NAT rules.");
}
final boolean nicremoved;
try {
nicremoved = _itMgr.removeNicFromVm(vmInstance, nic);
} catch (final ResourceUnavailableException e) {
throw new CloudRuntimeException("Unable to remove " + network + " from " + vmInstance + ": " + e);
} catch (final ConcurrentOperationException e) {
throw new CloudRuntimeException("Concurrent operations on removing " + network + " from " + vmInstance + ": " + e);
}
if (!nicremoved) {
throw new CloudRuntimeException("Unable to remove " + network + " from " + vmInstance);
}
s_logger.debug("Successful removal of " + network + " from " + vmInstance);
return _vmDao.findById(vmInstance.getId());
}
Aggregations