Search in sources :

Example 6 with VirtualMachineTemplate

use of com.cloud.legacymodel.storage.VirtualMachineTemplate in project cosmic by MissionCriticalCloud.

the class DomainChecker method checkAccess.

@Override
public boolean checkAccess(final Account caller, final ControlledEntity entity, final AccessType accessType) throws PermissionDeniedException {
    if (entity instanceof VirtualMachineTemplate) {
        final VirtualMachineTemplate template = (VirtualMachineTemplate) entity;
        final Account owner = _accountDao.findById(template.getAccountId());
        // validate that the template is usable by the account
        if (!template.isPublicTemplate()) {
            if (_accountService.isRootAdmin(caller.getId()) || (owner.getId() == caller.getId())) {
                return true;
            }
            // special handling for the project case
            if (owner.getType() == Account.ACCOUNT_TYPE_PROJECT && _projectMgr.canAccessProjectAccount(caller, owner.getId())) {
                return true;
            }
            // since the current account is not the owner of the template, check the launch permissions table to see if the
            // account can launch a VM from this template
            final LaunchPermissionVO permission = _launchPermissionDao.findByTemplateAndAccount(template.getId(), caller.getId());
            if (permission == null) {
                throw new PermissionDeniedException(caller + " does not have permission to launch instances from " + template);
            }
        } else {
            // Domain admin and regular user can delete/modify only templates created by them
            if (accessType != null && accessType == AccessType.OperateEntry) {
                if (!_accountService.isRootAdmin(caller.getId()) && owner.getId() != caller.getId()) {
                    // For projects check if the caller account can access the project account
                    if (owner.getType() != Account.ACCOUNT_TYPE_PROJECT || !(_projectMgr.canAccessProjectAccount(caller, owner.getId()))) {
                        throw new PermissionDeniedException("Domain Admin and regular users can modify only their own Public templates");
                    }
                }
            }
        }
        return true;
    } else if (entity instanceof Network && accessType != null && accessType == AccessType.UseEntry) {
        _networkMgr.checkNetworkPermissions(caller, (Network) entity);
    } else if (entity instanceof AffinityGroup) {
        return false;
    } else {
        if (_accountService.isNormalUser(caller.getId())) {
            final Account account = _accountDao.findById(entity.getAccountId());
            if (account != null && account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
                // only project owner can delete/modify the project
                if (accessType != null && accessType == AccessType.ModifyProject) {
                    if (!_projectMgr.canModifyProjectAccount(caller, account.getId())) {
                        throw new PermissionDeniedException(caller + " does not have permission to operate with resource " + entity);
                    }
                } else if (!_projectMgr.canAccessProjectAccount(caller, account.getId())) {
                    throw new PermissionDeniedException(caller + " does not have permission to operate with resource " + entity);
                }
            } else {
                if (caller.getId() != entity.getAccountId()) {
                    throw new PermissionDeniedException(caller + " does not have permission to operate with resource " + entity);
                }
            }
        }
    }
    return true;
}
Also used : Account(com.cloud.legacymodel.user.Account) VirtualMachineTemplate(com.cloud.legacymodel.storage.VirtualMachineTemplate) Network(com.cloud.legacymodel.network.Network) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) AffinityGroup(com.cloud.affinity.AffinityGroup) LaunchPermissionVO(com.cloud.storage.LaunchPermissionVO)

Example 7 with VirtualMachineTemplate

use of com.cloud.legacymodel.storage.VirtualMachineTemplate in project cosmic by MissionCriticalCloud.

the class UpdateIsoCmdByAdmin method execute.

@Override
public void execute() {
    final VirtualMachineTemplate result = _templateService.updateTemplate(this);
    if (result != null) {
        final TemplateResponse response = _responseGenerator.createTemplateUpdateResponse(ResponseView.Full, result);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update iso");
    }
}
Also used : VirtualMachineTemplate(com.cloud.legacymodel.storage.VirtualMachineTemplate) ServerApiException(com.cloud.api.ServerApiException) TemplateResponse(com.cloud.api.response.TemplateResponse)

Example 8 with VirtualMachineTemplate

use of com.cloud.legacymodel.storage.VirtualMachineTemplate in project cosmic by MissionCriticalCloud.

the class DeployVMCmd method create.

@Override
public void create() {
    try {
        // Verify that all objects exist before passing them to the service
        final Account owner = _accountService.getActiveAccountById(getEntityOwnerId());
        verifyDetails();
        final Zone zone = zoneRepository.findById(zoneId).orElse(null);
        if (zone == null) {
            throw new InvalidParameterValueException("Unable to find zone by id=" + zoneId);
        }
        final ServiceOffering serviceOffering = _entityMgr.findById(ServiceOffering.class, serviceOfferingId);
        if (serviceOffering == null) {
            throw new InvalidParameterValueException("Unable to find service offering: " + serviceOfferingId);
        }
        final VirtualMachineTemplate template = _entityMgr.findById(VirtualMachineTemplate.class, templateId);
        // Make sure a valid template ID was specified
        if (template == null) {
            throw new InvalidParameterValueException("Unable to find the template " + templateId);
        }
        DiskOffering diskOffering = null;
        if (diskOfferingId != null) {
            diskOffering = _entityMgr.findById(DiskOffering.class, diskOfferingId);
            if (diskOffering == null) {
                throw new InvalidParameterValueException("Unable to find disk offering " + diskOfferingId);
            }
        }
        final IpAddresses addrs = new IpAddresses(ipAddress, ip6Address, getMacAddress());
        final UserVm vm = _userVmService.createAdvancedVirtualMachine(zone, serviceOffering, template, getNetworkIds(), owner, name, displayName, diskOfferingId, size, group, getHypervisor(), getHttpMethod(), userData, sshKeyPairName, getIpToNetworkMap(), addrs, displayVm, keyboard, getAffinityGroupIdList(), getDetails(), getCustomId(), getDiskController(), getBootMenuTimeout(), getMaintenancePolicy(), getOptimiseFor(), getManufacturerString(), getBootOrder());
        if (vm != null) {
            setEntityId(vm.getId());
            setEntityUuid(vm.getUuid());
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to deploy vm");
        }
    } catch (final InsufficientCapacityException ex) {
        s_logger.info(ex.toString());
        s_logger.trace(ex.getMessage(), ex);
        throw new ServerApiException(ApiErrorCode.INSUFFICIENT_CAPACITY_ERROR, ex.getMessage());
    } catch (final ResourceUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    } catch (final ConcurrentOperationException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    } catch (final ResourceAllocationException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.RESOURCE_ALLOCATION_ERROR, ex.getMessage());
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) DiskOffering(com.cloud.legacymodel.storage.DiskOffering) VirtualMachineTemplate(com.cloud.legacymodel.storage.VirtualMachineTemplate) ServiceOffering(com.cloud.offering.ServiceOffering) Zone(com.cloud.db.model.Zone) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) IpAddresses(com.cloud.legacymodel.network.Network.IpAddresses) UserVm(com.cloud.uservm.UserVm) ServerApiException(com.cloud.api.ServerApiException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) InsufficientCapacityException(com.cloud.legacymodel.exceptions.InsufficientCapacityException) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException)

Example 9 with VirtualMachineTemplate

use of com.cloud.legacymodel.storage.VirtualMachineTemplate in project cosmic by MissionCriticalCloud.

the class CreateTemplateCmd method create.

@Override
public void create() throws ResourceAllocationException {
    VirtualMachineTemplate template = null;
    template = _templateService.createPrivateTemplateRecord(this, _accountService.getAccount(getEntityOwnerId()));
    if (template != null) {
        setEntityId(template.getId());
        setEntityUuid(template.getUuid());
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create a template");
    }
}
Also used : VirtualMachineTemplate(com.cloud.legacymodel.storage.VirtualMachineTemplate) ServerApiException(com.cloud.api.ServerApiException)

Example 10 with VirtualMachineTemplate

use of com.cloud.legacymodel.storage.VirtualMachineTemplate in project cosmic by MissionCriticalCloud.

the class CopyTemplateCmd method execute.

@Override
public void execute() throws ResourceAllocationException {
    try {
        CallContext.current().setEventDetails(getEventDescription());
        final VirtualMachineTemplate template = _templateService.copyTemplate(this);
        if (template != null) {
            final List<TemplateResponse> listResponse = _responseGenerator.createTemplateResponses(ResponseView.Restricted, template, getDestinationZoneId(), false);
            TemplateResponse response = new TemplateResponse();
            if (listResponse != null && !listResponse.isEmpty()) {
                response = listResponse.get(0);
            }
            response.setResponseName(getCommandName());
            setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to copy template");
        }
    } catch (final StorageUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    }
}
Also used : VirtualMachineTemplate(com.cloud.legacymodel.storage.VirtualMachineTemplate) ServerApiException(com.cloud.api.ServerApiException) StorageUnavailableException(com.cloud.legacymodel.exceptions.StorageUnavailableException) TemplateResponse(com.cloud.api.response.TemplateResponse)

Aggregations

VirtualMachineTemplate (com.cloud.legacymodel.storage.VirtualMachineTemplate)26 ServerApiException (com.cloud.api.ServerApiException)14 TemplateResponse (com.cloud.api.response.TemplateResponse)13 Account (com.cloud.legacymodel.user.Account)9 ListResponse (com.cloud.api.response.ListResponse)5 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)5 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)4 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)4 Zone (com.cloud.db.model.Zone)3 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)3 InsufficientCapacityException (com.cloud.legacymodel.exceptions.InsufficientCapacityException)3 NoTransitionException (com.cloud.legacymodel.exceptions.NoTransitionException)3 ResourceAllocationException (com.cloud.legacymodel.exceptions.ResourceAllocationException)3 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)3 StorageUnavailableException (com.cloud.legacymodel.exceptions.StorageUnavailableException)3 Network (com.cloud.legacymodel.network.Network)3 ArrayList (java.util.ArrayList)3 AffinityGroup (com.cloud.affinity.AffinityGroup)2 DeployDestination (com.cloud.deploy.DeployDestination)2 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)2