Search in sources :

Example 11 with CallContext

use of com.cloud.context.CallContext in project cosmic by MissionCriticalCloud.

the class CreateEgressFirewallRuleCmd method execute.

@Override
public void execute() throws ResourceUnavailableException {
    final CallContext callerContext = CallContext.current();
    boolean success = false;
    FirewallRule rule = _entityMgr.findById(FirewallRule.class, getEntityId());
    try {
        CallContext.current().setEventDetails("Rule Id: " + getEntityId());
        success = _firewallService.applyEgressFirewallRules(rule, callerContext.getCallingAccount());
        // State is different after the rule is applied, so get new object here
        rule = _entityMgr.findById(FirewallRule.class, getEntityId());
        FirewallResponse fwResponse = new FirewallResponse();
        if (rule != null) {
            fwResponse = _responseGenerator.createFirewallResponse(rule);
            setResponseObject(fwResponse);
        }
        fwResponse.setResponseName(getCommandName());
    } finally {
        if (!success || rule == null) {
            _firewallService.revokeEgressFirewallRule(getEntityId(), true);
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create firewall rule");
        }
    }
}
Also used : ServerApiException(com.cloud.api.ServerApiException) CallContext(com.cloud.context.CallContext) FirewallRule(com.cloud.network.rules.FirewallRule) FirewallResponse(com.cloud.api.response.FirewallResponse)

Example 12 with CallContext

use of com.cloud.context.CallContext in project cosmic by MissionCriticalCloud.

the class CreateFirewallRuleCmd method execute.

// ///////////////////////////////////////////////////
// ///////////////// Accessors ///////////////////////
// ///////////////////////////////////////////////////
@Override
public void execute() throws ResourceUnavailableException {
    final CallContext callerContext = CallContext.current();
    boolean success = false;
    FirewallRule rule = _entityMgr.findById(FirewallRule.class, getEntityId());
    try {
        CallContext.current().setEventDetails("Rule ID: " + getEntityId());
        success = _firewallService.applyIngressFwRules(rule.getSourceIpAddressId(), callerContext.getCallingAccount());
        // State is different after the rule is applied, so get new object here
        rule = _entityMgr.findById(FirewallRule.class, getEntityId());
        FirewallResponse fwResponse = new FirewallResponse();
        if (rule != null) {
            fwResponse = _responseGenerator.createFirewallResponse(rule);
            setResponseObject(fwResponse);
        }
        fwResponse.setResponseName(getCommandName());
    } finally {
        if (!success || rule == null) {
            _firewallService.revokeIngressFwRule(getEntityId(), true);
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create firewall rule");
        }
    }
}
Also used : ServerApiException(com.cloud.api.ServerApiException) CallContext(com.cloud.context.CallContext) FirewallRule(com.cloud.network.rules.FirewallRule) FirewallResponse(com.cloud.api.response.FirewallResponse)

Example 13 with CallContext

use of com.cloud.context.CallContext in project cosmic by MissionCriticalCloud.

the class CreateLoadBalancerRuleCmd method execute.

@Override
public void execute() throws ResourceAllocationException, ResourceUnavailableException {
    final CallContext callerContext = CallContext.current();
    boolean success = true;
    LoadBalancer rule = null;
    try {
        CallContext.current().setEventDetails("Rule Id: " + getEntityId());
        if (getOpenFirewall()) {
            success = success && _firewallService.applyIngressFirewallRules(getSourceIpAddressId(), callerContext.getCallingAccount());
        }
        // State might be different after the rule is applied, so get new object here
        rule = _entityMgr.findById(LoadBalancer.class, getEntityId());
        LoadBalancerResponse lbResponse = new LoadBalancerResponse();
        if (rule != null) {
            lbResponse = _responseGenerator.createLoadBalancerResponse(rule);
            setResponseObject(lbResponse);
        }
        lbResponse.setResponseName(getCommandName());
    } catch (final Exception ex) {
        s_logger.warn("Failed to create LB rule due to exception ", ex);
    }
    if (!success || rule == null) {
        if (getOpenFirewall()) {
            _firewallService.revokeRelatedFirewallRule(getEntityId(), true);
        }
        // no need to apply the rule on the backend as it exists in the db only
        _lbService.deleteLoadBalancerRule(getEntityId(), false);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create load balancer rule");
    }
}
Also used : LoadBalancerResponse(com.cloud.api.response.LoadBalancerResponse) ServerApiException(com.cloud.api.ServerApiException) LoadBalancer(com.cloud.network.rules.LoadBalancer) CallContext(com.cloud.context.CallContext) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ServerApiException(com.cloud.api.ServerApiException) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException)

Example 14 with CallContext

use of com.cloud.context.CallContext in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImpl method migrateVmStorageThroughJobQueue.

public Outcome<VirtualMachine> migrateVmStorageThroughJobQueue(final String vmUuid, final StoragePool destPool) {
    final CallContext context = CallContext.current();
    final User user = context.getCallingUser();
    final Account account = context.getCallingAccount();
    final VMInstanceVO vm = _vmDao.findByUuid(vmUuid);
    final List<VmWorkJobVO> pendingWorkJobs = _workJobDao.listPendingWorkJobs(VirtualMachine.Type.Instance, vm.getId(), VmWorkStorageMigration.class.getName());
    VmWorkJobVO workJob = null;
    if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) {
        assert pendingWorkJobs.size() == 1;
        workJob = pendingWorkJobs.get(0);
    } else {
        workJob = new VmWorkJobVO(context.getContextId());
        workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER);
        workJob.setCmd(VmWorkStorageMigration.class.getName());
        workJob.setAccountId(account.getId());
        workJob.setUserId(user.getId());
        workJob.setVmType(VirtualMachine.Type.Instance);
        workJob.setVmInstanceId(vm.getId());
        workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
        // save work context info (there are some duplications)
        final VmWorkStorageMigration workInfo = new VmWorkStorageMigration(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, destPool.getId());
        workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo));
        _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId());
    }
    AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId());
    return new VmJobVirtualMachineOutcome(workJob, vm.getId());
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) CallContext(com.cloud.context.CallContext) VmWorkJobVO(com.cloud.framework.jobs.impl.VmWorkJobVO)

Example 15 with CallContext

use of com.cloud.context.CallContext in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImpl method orchestrateRemoveNicFromVm.

private boolean orchestrateRemoveNicFromVm(final VirtualMachine vm, final Nic nic) throws ConcurrentOperationException, ResourceUnavailableException {
    final CallContext cctx = CallContext.current();
    final VMInstanceVO vmVO = _vmDao.findById(vm.getId());
    final NetworkVO network = _networkDao.findById(nic.getNetworkId());
    final ReservationContext context = new ReservationContextImpl(null, null, cctx.getCallingUser(), cctx.getCallingAccount());
    final VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vmVO, null, null, null, null);
    final Zone zone = _zoneRepository.findOne(network.getDataCenterId());
    final Host host = _hostDao.findById(vm.getHostId());
    final DeployDestination dest = new DeployDestination(zone, null, null, host);
    final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType());
    final VirtualMachineTO vmTO = hvGuru.implement(vmProfile);
    final NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), _networkModel.getNetworkRate(network.getId(), vm.getId()), _networkModel.getNetworkTag(vmProfile.getVirtualMachine().getHypervisorType(), network));
    // 1) Unplug the nic
    if (vm.getState() == State.Running) {
        final NicTO nicTO = toNicTO(nicProfile, vmProfile.getVirtualMachine().getHypervisorType());
        s_logger.debug("Un-plugging nic " + nic + " for vm " + vm + " from network " + network);
        final boolean result = unplugNic(network, nicTO, vmTO, context, dest);
        if (result) {
            s_logger.debug("Nic is unplugged successfully for vm " + vm + " in network " + network);
            final long isDefault = nic.isDefaultNic() ? 1 : 0;
        } else {
            s_logger.warn("Failed to unplug nic for the vm " + vm + " from network " + network);
            return false;
        }
    } else if (vm.getState() != State.Stopped) {
        s_logger.warn("Unable to remove vm " + vm + " from network  " + network);
        throw new ResourceUnavailableException("Unable to remove vm " + vm + " from network, is not in the right state", DataCenter.class, vm.getDataCenterId());
    }
    // 2) Release the nic
    _networkMgr.releaseNic(vmProfile, nic);
    s_logger.debug("Successfully released nic " + nic + "for vm " + vm);
    // 3) Remove the nic
    _networkMgr.removeNic(vmProfile, nic);
    _nicsDao.expunge(nic.getId());
    return true;
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) Zone(com.cloud.db.model.Zone) TimeZone(java.util.TimeZone) Host(com.cloud.host.Host) CallContext(com.cloud.context.CallContext) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) DataCenter(com.cloud.dc.DataCenter) DeployDestination(com.cloud.deploy.DeployDestination) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) NicTO(com.cloud.agent.api.to.NicTO)

Aggregations

CallContext (com.cloud.context.CallContext)72 Account (com.cloud.user.Account)41 User (com.cloud.user.User)26 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)26 VmWorkJobVO (com.cloud.framework.jobs.impl.VmWorkJobVO)22 ActionEvent (com.cloud.event.ActionEvent)20 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)19 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)19 DB (com.cloud.utils.db.DB)12 LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)10 VMInstanceVO (com.cloud.vm.VMInstanceVO)10 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)9 ServerApiException (com.cloud.api.ServerApiException)8 FirewallRule (com.cloud.network.rules.FirewallRule)8 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)6 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)6 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)6 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)6 Network (com.cloud.network.Network)6 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)5