use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.
the class IpAddressManagerImpl method allocateIp.
@DB
@Override
public IpAddress allocateIp(final Account ipOwner, final boolean isSystem, final Account caller, final long callerUserId, final DataCenter zone, final Boolean displayIp) throws ConcurrentOperationException, ResourceAllocationException, InsufficientAddressCapacityException {
final VlanType vlanType = VlanType.VirtualNetwork;
final boolean assign = false;
if (AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(caller.getId())) {
// zone is of type DataCenter. See DataCenterVO.java.
final PermissionDeniedException ex = new PermissionDeniedException("Cannot perform this operation, " + "Zone is currently disabled");
ex.addProxyObject(zone.getUuid(), "zoneId");
throw ex;
}
PublicIp ip = null;
Account accountToLock = null;
try {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Associate IP address called by the user " + callerUserId + " account " + ipOwner.getId());
}
accountToLock = _accountDao.acquireInLockTable(ipOwner.getId());
if (accountToLock == null) {
s_logger.warn("Unable to lock account: " + ipOwner.getId());
throw new ConcurrentOperationException("Unable to acquire account lock");
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Associate IP address lock acquired");
}
ip = Transaction.execute(new TransactionCallbackWithException<PublicIp, InsufficientAddressCapacityException>() {
@Override
public PublicIp doInTransaction(final TransactionStatus status) throws InsufficientAddressCapacityException {
final PublicIp ip = fetchNewPublicIp(zone.getId(), null, null, ipOwner, vlanType, null, false, assign, null, isSystem, null, displayIp);
if (ip == null) {
final InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Unable to find available public IP addresses", DataCenter.class, zone.getId());
ex.addProxyObject(ApiDBUtils.findZoneById(zone.getId()).getUuid());
throw ex;
}
CallContext.current().setEventDetails("Ip Id: " + ip.getId());
final Ip ipAddress = ip.getAddress();
s_logger.debug("Got " + ipAddress + " to assign for account " + ipOwner.getId() + " in zone " + zone.getId());
return ip;
}
});
} finally {
if (accountToLock != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Releasing lock account " + ipOwner);
}
_accountDao.releaseFromLockTable(ipOwner.getId());
s_logger.debug("Associate IP address lock released");
}
}
return ip;
}
use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.
the class LdapAuthenticator method createCloudStackUserAccount.
private void createCloudStackUserAccount(final LdapUser user, final long domainId, final short accountType) {
final String username = user.getUsername();
final Account account = _accountManager.getActiveAccountByName(ldapGroupName, domainId);
if (account == null) {
s_logger.info("Account (" + ldapGroupName + ") for LDAP group does not exist. Creating account and user (" + username + ").");
_accountManager.createUserAccount(username, "", user.getFirstname(), user.getLastname(), user.getEmail(), null, ldapGroupName, accountType, domainId, null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), User.Source.LDAP);
} else {
s_logger.debug("Account (" + ldapGroupName + ") for LDAP group already exists. Creating user (" + username + ").");
_accountManager.createUser(username, "", user.getFirstname(), user.getLastname(), user.getEmail(), null, ldapGroupName, domainId, UUID.randomUUID().toString(), User.Source.LDAP);
}
}
use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.
the class UserVmManagerImpl method updateDefaultNicForVirtualMachine.
@Override
@ActionEvent(eventType = EventTypes.EVENT_NIC_UPDATE, eventDescription = "Creating Nic", async = true)
public UserVm updateDefaultNicForVirtualMachine(final UpdateDefaultNicForVMCmd cmd) throws InvalidParameterValueException, 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 updated for VM with VM Snapshots");
}
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 CloudRuntimeException("Zone " + vmInstance.getDataCenterId() + ", has a NetworkType of Basic. Can't change default NIC on a Basic Network");
}
// no need to check permissions for network, we'll enumerate the ones they already have access to
final Network existingdefaultnet = _networkModel.getDefaultNetworkForVm(vmId);
// check to see if nic is attached to VM
if (nic.getInstanceId() != vmId) {
throw new InvalidParameterValueException(nic + " is not a nic on " + vmInstance);
}
// if current default equals chosen new default, Throw an exception
if (nic.isDefaultNic()) {
throw new CloudRuntimeException("refusing to set default nic because chosen nic is already the default");
}
// make sure the VM is Running or Stopped
if (vmInstance.getState() != State.Running && vmInstance.getState() != State.Stopped) {
throw new CloudRuntimeException("refusing to set default " + vmInstance + " is not Running or Stopped");
}
NicProfile existing = null;
final List<NicProfile> nicProfiles = _networkMgr.getNicProfiles(vmInstance);
for (final NicProfile nicProfile : nicProfiles) {
if (nicProfile.isDefaultNic() && existingdefaultnet != null && nicProfile.getNetworkId() == existingdefaultnet.getId()) {
existing = nicProfile;
}
}
if (existing == null) {
s_logger.warn("Failed to update default nic, no nic profile found for existing default network");
throw new CloudRuntimeException("Failed to find a nic profile for the existing default network. This is bad and probably means some sort of configuration corruption");
}
NicVO existingVO = _nicDao.findById(existing.id);
nic.setDefaultNic(true);
existingVO.setDefaultNic(false);
nic = _nicDao.persist(nic);
existingVO = _nicDao.persist(existingVO);
Network newdefault = _networkModel.getDefaultNetworkForVm(vmId);
if (newdefault == null) {
nic.setDefaultNic(false);
existingVO.setDefaultNic(true);
nic = _nicDao.persist(nic);
_nicDao.persist(existingVO);
newdefault = _networkModel.getDefaultNetworkForVm(vmId);
if (newdefault.getId() == existingdefaultnet.getId()) {
throw new CloudRuntimeException("Setting a default nic failed, and we had no default nic, but we were able to set it back to the original");
}
throw new CloudRuntimeException("Failed to change default nic to " + nic + " and now we have no default");
} else if (newdefault.getId() == nic.getNetworkId()) {
s_logger.debug("successfully set default network to " + network + " for " + vmInstance);
return _vmDao.findById(vmInstance.getId());
}
throw new CloudRuntimeException("something strange happened, new default network(" + newdefault.getId() + ") is not null, and is not equal to the network(" + nic.getNetworkId() + ") of the chosen nic");
}
use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.
the class UserVmManagerImpl method updateNicIpForVirtualMachine.
@Override
public UserVm updateNicIpForVirtualMachine(final UpdateVmNicIpCmd cmd) {
final Long nicId = cmd.getNicId();
String ipaddr = cmd.getIpaddress();
final Account caller = CallContext.current().getCallingAccount();
// check whether the nic belongs to user vm.
final NicVO nicVO = _nicDao.findById(nicId);
if (nicVO == null) {
throw new InvalidParameterValueException("There is no nic for the " + nicId);
}
if (nicVO.getVmType() != VirtualMachineType.User) {
throw new InvalidParameterValueException("The nic is not belongs to user vm");
}
final UserVm vm = _vmDao.findById(nicVO.getInstanceId());
if (vm == null) {
throw new InvalidParameterValueException("There is no vm with the nic");
}
final Network network = _networkDao.findById(nicVO.getNetworkId());
if (network == null) {
throw new InvalidParameterValueException("There is no network with the nic");
}
// Don't allow to update vm nic ip if network is not in Implemented/Setup/Allocated state
if (!(network.getState() == Network.State.Allocated || network.getState() == Network.State.Implemented || network.getState() == Network.State.Setup)) {
throw new InvalidParameterValueException("Network is not in the right state to update vm nic ip. Correct states are: " + Network.State.Allocated + ", " + Network.State.Implemented + ", " + Network.State.Setup);
}
final NetworkOfferingVO offering = _networkOfferingDao.findByIdIncludingRemoved(network.getNetworkOfferingId());
if (offering == null) {
throw new InvalidParameterValueException("There is no network offering with the network");
}
if (!_networkModel.listNetworkOfferingServices(offering.getId()).isEmpty() && vm.getState() != State.Stopped) {
final InvalidParameterValueException ex = new InvalidParameterValueException("VM is not Stopped, unable to update the vm nic having the specified id");
ex.addProxyObject(vm.getUuid(), "vmId");
throw ex;
}
// verify permissions
_accountMgr.checkAccess(caller, null, true, vm);
final Account ipOwner = _accountDao.findByIdIncludingRemoved(vm.getAccountId());
// verify ip address
s_logger.debug("Calling the ip allocation ...");
final Zone zone = zoneRepository.findById(network.getDataCenterId()).orElse(null);
if (zone == null) {
throw new InvalidParameterValueException("There is no dc with the nic");
}
if (zone.getNetworkType() == NetworkType.Advanced && (network.getGuestType() == GuestType.Isolated || network.getGuestType() == GuestType.Private)) {
try {
ipaddr = _ipAddrMgr.allocateGuestIP(network, ipaddr);
} catch (final InsufficientAddressCapacityException e) {
throw new InvalidParameterValueException("Allocating ip to guest nic " + nicVO.getUuid() + " failed, for insufficient address capacity");
}
if (ipaddr == null) {
throw new InvalidParameterValueException("Allocating ip to guest nic " + nicVO.getUuid() + " failed, please choose another ip");
}
if (_networkModel.areServicesSupportedInNetwork(network.getId(), Service.StaticNat)) {
final IPAddressVO oldIP = _ipAddressDao.findByAssociatedVmId(vm.getId());
if (oldIP != null) {
oldIP.setVmIp(ipaddr);
_ipAddressDao.persist(oldIP);
}
}
// implementing the network elements and resources as a part of vm nic ip update if network has services and it is in Implemented state
if (!_networkModel.listNetworkOfferingServices(offering.getId()).isEmpty() && network.getState() == Network.State.Implemented) {
final User callerUser = _accountMgr.getActiveUser(CallContext.current().getCallingUserId());
final ReservationContext context = new ReservationContextImpl(null, null, callerUser, caller);
final DeployDestination dest = new DeployDestination(zoneRepository.findById(network.getDataCenterId()).orElse(null), null, null, null);
s_logger.debug("Implementing the network " + network + " elements and resources as a part of vm nic ip update");
try {
// implement the network elements and rules again
_networkMgr.implementNetworkElementsAndResources(dest, context, network, offering);
} catch (final Exception ex) {
s_logger.warn("Failed to implement network " + network + " elements and resources as a part of vm nic ip update due to ", ex);
final CloudRuntimeException e = new CloudRuntimeException("Failed to implement network (with specified id) elements and resources as a part of vm nic ip " + "update");
e.addProxyObject(network.getUuid(), "networkId");
// restore to old ip address
if (_networkModel.areServicesSupportedInNetwork(network.getId(), Service.StaticNat)) {
final IPAddressVO oldIP = _ipAddressDao.findByAssociatedVmId(vm.getId());
if (oldIP != null) {
oldIP.setVmIp(nicVO.getIPv4Address());
_ipAddressDao.persist(oldIP);
}
}
throw e;
}
}
} else if (zone.getNetworkType() == NetworkType.Basic || network.getGuestType() == GuestType.Shared) {
// handle the basic networks here
// for basic zone, need to provide the podId to ensure proper ip alloation
Long podId = null;
if (zone.getNetworkType() == NetworkType.Basic) {
podId = vm.getPodIdToDeployIn();
if (podId == null) {
throw new InvalidParameterValueException("vm pod id is null in Basic zone; can't decide the range for ip allocation");
}
}
try {
ipaddr = _ipAddrMgr.allocatePublicIpForGuestNic(network, podId, ipOwner, ipaddr);
if (ipaddr == null) {
throw new InvalidParameterValueException("Allocating ip to guest nic " + nicVO.getUuid() + " failed, please choose another ip");
}
final IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nicVO.getNetworkId(), nicVO.getIPv4Address());
if (ip != null) {
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(final TransactionStatus status) {
_ipAddrMgr.markIpAsUnavailable(ip.getId());
_ipAddressDao.unassignIpAddress(ip.getId());
}
});
}
} catch (final InsufficientAddressCapacityException e) {
s_logger.error("Allocating ip to guest nic " + nicVO.getUuid() + " failed, for insufficient address capacity");
return null;
}
} else {
s_logger.error("UpdateVmNicIpCmd is not supported in this network...");
return null;
}
// update nic ipaddress
nicVO.setIPv4Address(ipaddr);
_nicDao.persist(nicVO);
return vm;
}
use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.
the class UserVmManagerImpl method rebootVirtualMachine.
@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_REBOOT, eventDescription = "rebooting Vm", async = true)
public UserVm rebootVirtualMachine(final RebootVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException {
final Account caller = CallContext.current().getCallingAccount();
final Long vmId = cmd.getId();
// Verify input parameters
final UserVmVO vmInstance = _vmDao.findById(vmId);
if (vmInstance == null) {
throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId);
}
_accountMgr.checkAccess(caller, null, true, vmInstance);
// If the VM is Volatile in nature, on reboot discard the VM's root disk and create a new root disk for it: by calling restoreVM
final long serviceOfferingId = vmInstance.getServiceOfferingId();
final ServiceOfferingVO offering = _serviceOfferingDao.findById(vmInstance.getId(), serviceOfferingId);
if (offering != null && offering.getRemoved() == null) {
if (offering.getVolatileVm()) {
return restoreVMInternal(caller, vmInstance, null);
}
} else {
throw new InvalidParameterValueException("Unable to find service offering: " + serviceOfferingId + " corresponding to the vm");
}
return rebootVirtualMachine(CallContext.current().getCallingUserId(), vmId);
}
Aggregations