Search in sources :

Example 81 with CallContext

use of org.apache.cloudstack.context.CallContext in project cloudstack by apache.

the class VMSnapshotManagerImpl method revertToVMSnapshotThroughJobQueue.

public Outcome<VMSnapshot> revertToVMSnapshotThroughJobQueue(final Long vmId, final Long vmSnapshotId) {
    final CallContext context = CallContext.current();
    final User callingUser = context.getCallingUser();
    final Account callingAccount = context.getCallingAccount();
    final VMInstanceVO vm = _vmInstanceDao.findById(vmId);
    VmWorkJobVO workJob = new VmWorkJobVO(context.getContextId());
    workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER);
    workJob.setCmd(VmWorkRevertToVMSnapshot.class.getName());
    workJob.setAccountId(callingAccount.getId());
    workJob.setUserId(callingUser.getId());
    workJob.setStep(VmWorkJobVO.Step.Starting);
    workJob.setVmType(VirtualMachine.Type.Instance);
    workJob.setVmInstanceId(vm.getId());
    workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
    // save work context info (there are some duplications)
    VmWorkRevertToVMSnapshot workInfo = new VmWorkRevertToVMSnapshot(callingUser.getId(), callingAccount.getId(), vm.getId(), VMSnapshotManagerImpl.VM_WORK_JOB_HANDLER, vmSnapshotId);
    workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo));
    _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId());
    AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId());
    return new VmJobVMSnapshotOutcome(workJob, vmSnapshotId);
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) VMInstanceVO(com.cloud.vm.VMInstanceVO) CallContext(org.apache.cloudstack.context.CallContext) VmWorkJobVO(org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO)

Example 82 with CallContext

use of org.apache.cloudstack.context.CallContext in project cloudstack by apache.

the class GlobalLoadBalancingRulesServiceImpl method removeFromGlobalLoadBalancerRule.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_REMOVE_FROM_GLOBAL_LOAD_BALANCER_RULE, eventDescription = "Removing a load balancer rule to be part of global load balancer rule")
public boolean removeFromGlobalLoadBalancerRule(RemoveFromGlobalLoadBalancerRuleCmd removeFromGslbCmd) {
    CallContext ctx = CallContext.current();
    Account caller = ctx.getCallingAccount();
    final long gslbRuleId = removeFromGslbCmd.getGlobalLoadBalancerRuleId();
    final GlobalLoadBalancerRuleVO gslbRule = _gslbRuleDao.findById(gslbRuleId);
    if (gslbRule == null) {
        throw new InvalidParameterValueException("Invalid global load balancer rule id: " + gslbRuleId);
    }
    _accountMgr.checkAccess(caller, SecurityChecker.AccessType.OperateEntry, true, gslbRule);
    if (gslbRule.getState() == GlobalLoadBalancerRule.State.Revoke) {
        throw new InvalidParameterValueException("global load balancer rule id: " + gslbRuleId + " is already in revoked state");
    }
    final List<Long> lbRuleIdsToremove = removeFromGslbCmd.getLoadBalancerRulesIds();
    if (lbRuleIdsToremove == null || lbRuleIdsToremove.isEmpty()) {
        throw new InvalidParameterValueException("empty list of load balancer rule Ids specified to be un-assigned" + " to global load balancer rule");
    }
    // get the active list of LB rule id's that are assigned currently to GSLB rule and corresponding zone id's
    List<Long> oldLbRuleIds = new ArrayList<Long>();
    List<Long> oldZones = new ArrayList<Long>();
    List<GlobalLoadBalancerLbRuleMapVO> gslbLbMapVos = _gslbLbMapDao.listByGslbRuleId(gslbRuleId);
    if (gslbLbMapVos == null) {
        throw new InvalidParameterValueException(" There are no load balancer rules that are assigned to global " + " load balancer rule id: " + gslbRule.getUuid() + " that are available for deletion");
    }
    for (Long lbRuleId : lbRuleIdsToremove) {
        LoadBalancerVO loadBalancer = _lbDao.findById(lbRuleId);
        if (loadBalancer == null) {
            throw new InvalidParameterValueException("Specified load balancer rule ID does not exist.");
        }
        _accountMgr.checkAccess(caller, null, true, loadBalancer);
    }
    for (GlobalLoadBalancerLbRuleMapVO gslbLbMapVo : gslbLbMapVos) {
        LoadBalancerVO loadBalancer = _lbDao.findById(gslbLbMapVo.getLoadBalancerId());
        Network network = _networkDao.findById(loadBalancer.getNetworkId());
        oldLbRuleIds.add(gslbLbMapVo.getLoadBalancerId());
        oldZones.add(network.getDataCenterId());
    }
    for (Long lbRuleId : lbRuleIdsToremove) {
        LoadBalancerVO loadBalancer = _lbDao.findById(lbRuleId);
        if (oldLbRuleIds != null && !oldLbRuleIds.contains(loadBalancer.getId())) {
            throw new InvalidParameterValueException("Load balancer ID " + loadBalancer.getUuid() + " is not assigned" + " to global load balancer rule: " + gslbRule.getUuid());
        }
    }
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            // update the mapping of gslb rule to Lb rule, to revoke state
            for (Long lbRuleId : lbRuleIdsToremove) {
                GlobalLoadBalancerLbRuleMapVO removeGslbLbMap = _gslbLbMapDao.findByGslbRuleIdAndLbRuleId(gslbRuleId, lbRuleId);
                removeGslbLbMap.setRevoke(true);
                _gslbLbMapDao.update(removeGslbLbMap.getId(), removeGslbLbMap);
            }
            // mark the gslb rule state as add
            if (gslbRule.getState() == GlobalLoadBalancerRule.State.Staged) {
                gslbRule.setState(GlobalLoadBalancerRule.State.Add);
                _gslbRuleDao.update(gslbRule.getId(), gslbRule);
            }
        }
    });
    boolean success = false;
    try {
        s_logger.debug("Attempting to configure global load balancer rule configuration on the gslb service providers ");
        // apply the gslb rule on to the back end gslb service providers
        if (!applyGlobalLoadBalancerRuleConfig(gslbRuleId, false)) {
            s_logger.warn("Failed to remove load balancer rules " + lbRuleIdsToremove + " from global load balancer rule id " + gslbRuleId);
            CloudRuntimeException ex = new CloudRuntimeException("Failed to remove load balancer rule ids from GSLB rule ");
            throw ex;
        }
        Transaction.execute(new TransactionCallbackNoReturn() {

            @Override
            public void doInTransactionWithoutResult(TransactionStatus status) {
                // remove the mappings of gslb rule to Lb rule that are in revoked state
                for (Long lbRuleId : lbRuleIdsToremove) {
                    GlobalLoadBalancerLbRuleMapVO removeGslbLbMap = _gslbLbMapDao.findByGslbRuleIdAndLbRuleId(gslbRuleId, lbRuleId);
                    _gslbLbMapDao.remove(removeGslbLbMap.getId());
                }
                // on success set state back to Active
                gslbRule.setState(GlobalLoadBalancerRule.State.Active);
                _gslbRuleDao.update(gslbRule.getId(), gslbRule);
            }
        });
        success = true;
    } catch (ResourceUnavailableException e) {
        throw new CloudRuntimeException("Failed to update removed load balancer details from gloabal load balancer");
    }
    return success;
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) CallContext(org.apache.cloudstack.context.CallContext) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 83 with CallContext

use of org.apache.cloudstack.context.CallContext in project cloudstack by apache.

the class GlobalLoadBalancingRulesServiceImpl method listGlobalLoadBalancerRule.

@Override
public List<GlobalLoadBalancerRule> listGlobalLoadBalancerRule(ListGlobalLoadBalancerRuleCmd listGslbCmd) {
    CallContext ctx = CallContext.current();
    Account caller = ctx.getCallingAccount();
    Integer regionId = listGslbCmd.getRegionId();
    Long ruleId = listGslbCmd.getId();
    List<GlobalLoadBalancerRule> response = new ArrayList<GlobalLoadBalancerRule>();
    if (regionId == null && ruleId == null) {
        throw new InvalidParameterValueException("Invalid arguments. At least one of region id, " + "rule id must be specified");
    }
    if (regionId != null && ruleId != null) {
        throw new InvalidParameterValueException("Invalid arguments. Only one of region id, " + "rule id must be specified");
    }
    if (ruleId != null) {
        GlobalLoadBalancerRule gslbRule = _gslbRuleDao.findById(ruleId);
        if (gslbRule == null) {
            throw new InvalidParameterValueException("Invalid gslb rule id specified");
        }
        _accountMgr.checkAccess(caller, org.apache.cloudstack.acl.SecurityChecker.AccessType.UseEntry, false, gslbRule);
        response.add(gslbRule);
        return response;
    }
    if (regionId != null) {
        List<GlobalLoadBalancerRuleVO> gslbRules = _gslbRuleDao.listByAccount(caller.getAccountId());
        if (gslbRules != null) {
            response.addAll(gslbRules);
        }
        return response;
    }
    return null;
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) GlobalLoadBalancerRule(com.cloud.region.ha.GlobalLoadBalancerRule) ArrayList(java.util.ArrayList) CallContext(org.apache.cloudstack.context.CallContext)

Example 84 with CallContext

use of org.apache.cloudstack.context.CallContext in project cloudstack by apache.

the class GlobalLoadBalancingRulesServiceImpl method updateGlobalLoadBalancerRule.

@Override
public GlobalLoadBalancerRule updateGlobalLoadBalancerRule(UpdateGlobalLoadBalancerRuleCmd updateGslbCmd) {
    String algorithm = updateGslbCmd.getAlgorithm();
    String stickyMethod = updateGslbCmd.getStickyMethod();
    String description = updateGslbCmd.getDescription();
    long gslbRuleId = updateGslbCmd.getId();
    GlobalLoadBalancerRuleVO gslbRule = _gslbRuleDao.findById(gslbRuleId);
    if (gslbRule == null) {
        throw new InvalidParameterValueException("Invalid global load balancer rule id: " + gslbRuleId);
    }
    CallContext ctx = CallContext.current();
    Account caller = ctx.getCallingAccount();
    _accountMgr.checkAccess(caller, SecurityChecker.AccessType.OperateEntry, true, gslbRule);
    if (algorithm != null && !GlobalLoadBalancerRule.Algorithm.isValidAlgorithm(algorithm)) {
        throw new InvalidParameterValueException("Invalid Algorithm: " + algorithm);
    }
    if (stickyMethod != null && !GlobalLoadBalancerRule.Persistence.isValidPersistence(stickyMethod)) {
        throw new InvalidParameterValueException("Invalid persistence: " + stickyMethod);
    }
    if (algorithm != null) {
        gslbRule.setAlgorithm(algorithm);
    }
    if (stickyMethod != null) {
        gslbRule.setPersistence(stickyMethod);
    }
    if (description != null) {
        gslbRule.setDescription(description);
    }
    gslbRule.setState(GlobalLoadBalancerRule.State.Add);
    _gslbRuleDao.update(gslbRule.getId(), gslbRule);
    try {
        s_logger.debug("Updating global load balancer with id " + gslbRule.getUuid());
        // apply the gslb rule on to the back end gslb service providers on zones participating in gslb
        applyGlobalLoadBalancerRuleConfig(gslbRuleId, false);
        // on success set state to Active
        gslbRule.setState(GlobalLoadBalancerRule.State.Active);
        _gslbRuleDao.update(gslbRule.getId(), gslbRule);
        return gslbRule;
    } catch (ResourceUnavailableException e) {
        throw new CloudRuntimeException("Failed to configure gslb config due to " + e.getMessage());
    }
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CallContext(org.apache.cloudstack.context.CallContext)

Example 85 with CallContext

use of org.apache.cloudstack.context.CallContext in project cloudstack by apache.

the class KubernetesClusterManagerImpl method listKubernetesClusters.

@Override
public ListResponse<KubernetesClusterResponse> listKubernetesClusters(ListKubernetesClustersCmd cmd) {
    if (!KubernetesServiceEnabled.value()) {
        logAndThrow(Level.ERROR, "Kubernetes Service plugin is disabled");
    }
    final CallContext ctx = CallContext.current();
    final Account caller = ctx.getCallingAccount();
    final Long clusterId = cmd.getId();
    final String state = cmd.getState();
    final String name = cmd.getName();
    final String keyword = cmd.getKeyword();
    List<KubernetesClusterResponse> responsesList = new ArrayList<KubernetesClusterResponse>();
    List<Long> permittedAccounts = new ArrayList<Long>();
    Ternary<Long, Boolean, Project.ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, Project.ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
    accountManager.buildACLSearchParameters(caller, clusterId, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
    Long domainId = domainIdRecursiveListProject.first();
    Boolean isRecursive = domainIdRecursiveListProject.second();
    Project.ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter searchFilter = new Filter(KubernetesClusterVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    SearchBuilder<KubernetesClusterVO> sb = kubernetesClusterDao.createSearchBuilder();
    accountManager.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
    sb.and("keyword", sb.entity().getName(), SearchCriteria.Op.LIKE);
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.IN);
    SearchCriteria<KubernetesClusterVO> sc = sb.create();
    accountManager.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (state != null) {
        sc.setParameters("state", state);
    }
    if (keyword != null) {
        sc.setParameters("keyword", "%" + keyword + "%");
    }
    if (clusterId != null) {
        sc.setParameters("id", clusterId);
    }
    if (name != null) {
        sc.setParameters("name", name);
    }
    List<KubernetesClusterVO> kubernetesClusters = kubernetesClusterDao.search(sc, searchFilter);
    for (KubernetesClusterVO cluster : kubernetesClusters) {
        KubernetesClusterResponse clusterResponse = createKubernetesClusterResponse(cluster.getId());
        responsesList.add(clusterResponse);
    }
    ListResponse<KubernetesClusterResponse> response = new ListResponse<KubernetesClusterResponse>();
    response.setResponses(responsesList);
    return response;
}
Also used : UserAccount(com.cloud.user.UserAccount) Account(com.cloud.user.Account) KubernetesClusterResponse(org.apache.cloudstack.api.response.KubernetesClusterResponse) ListResponse(org.apache.cloudstack.api.response.ListResponse) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) CallContext(org.apache.cloudstack.context.CallContext) Project(com.cloud.projects.Project) Filter(com.cloud.utils.db.Filter)

Aggregations

CallContext (org.apache.cloudstack.context.CallContext)85 Account (com.cloud.user.Account)46 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)33 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)25 ActionEvent (com.cloud.event.ActionEvent)23 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)23 User (com.cloud.user.User)18 DB (com.cloud.utils.db.DB)15 VmWorkJobVO (org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO)15 LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)12 ArrayList (java.util.ArrayList)11 VMInstanceVO (com.cloud.vm.VMInstanceVO)10 FirewallRule (com.cloud.network.rules.FirewallRule)9 Network (com.cloud.network.Network)8 ServerApiException (org.apache.cloudstack.api.ServerApiException)8 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)7 TransactionStatus (com.cloud.utils.db.TransactionStatus)7 DeployDestination (com.cloud.deploy.DeployDestination)6 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)6 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)6