Search in sources :

Example 96 with ActionEvent

use of com.cloud.event.ActionEvent in project cloudstack by apache.

the class ManagementServerImpl method removeGuestOs.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_GUEST_OS_REMOVE, eventDescription = "removing guest OS type", async = true)
public boolean removeGuestOs(final RemoveGuestOsCmd cmd) {
    final Long id = cmd.getId();
    //check if guest OS exists
    final GuestOS guestOs = ApiDBUtils.findGuestOSById(id);
    if (guestOs == null) {
        throw new InvalidParameterValueException("Guest OS not found. Please specify a valid ID for the Guest OS");
    }
    if (!guestOs.getIsUserDefined()) {
        throw new InvalidParameterValueException("Unable to remove system defined guest OS");
    }
    return _guestOSDao.remove(id);
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) GuestOS(com.cloud.storage.GuestOS) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 97 with ActionEvent

use of com.cloud.event.ActionEvent in project cloudstack by apache.

the class VolumeApiServiceImpl method takeSnapshot.

@Override
@ActionEvent(eventType = EventTypes.EVENT_SNAPSHOT_CREATE, eventDescription = "taking snapshot", async = true)
public Snapshot takeSnapshot(Long volumeId, Long policyId, Long snapshotId, Account account, boolean quiescevm, Snapshot.LocationType locationType) throws ResourceAllocationException {
    VolumeInfo volume = volFactory.getVolume(volumeId);
    if (volume == null) {
        throw new InvalidParameterValueException("Creating snapshot failed due to volume:" + volumeId + " doesn't exist");
    }
    if (volume.getState() != Volume.State.Ready) {
        throw new InvalidParameterValueException("VolumeId: " + volumeId + " is not in " + Volume.State.Ready + " state but " + volume.getState() + ". Cannot take snapshot.");
    }
    StoragePoolVO storagePoolVO = _storagePoolDao.findById(volume.getPoolId());
    if (storagePoolVO.isManaged() && locationType == null) {
        locationType = Snapshot.LocationType.PRIMARY;
    }
    VMInstanceVO vm = null;
    if (volume.getInstanceId() != null)
        vm = _vmInstanceDao.findById(volume.getInstanceId());
    if (vm != null) {
        // serialize VM operation
        AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext();
        if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) {
            // avoid re-entrance
            VmWorkJobVO placeHolder = null;
            placeHolder = createPlaceHolderWork(vm.getId());
            try {
                return orchestrateTakeVolumeSnapshot(volumeId, policyId, snapshotId, account, quiescevm, locationType);
            } finally {
                _workJobDao.expunge(placeHolder.getId());
            }
        } else {
            Outcome<Snapshot> outcome = takeVolumeSnapshotThroughJobQueue(vm.getId(), volumeId, policyId, snapshotId, account.getId(), quiescevm, locationType);
            try {
                outcome.get();
            } catch (InterruptedException e) {
                throw new RuntimeException("Operation is interrupted", e);
            } catch (java.util.concurrent.ExecutionException e) {
                throw new RuntimeException("Execution excetion", e);
            }
            Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob());
            if (jobResult != null) {
                if (jobResult instanceof ConcurrentOperationException)
                    throw (ConcurrentOperationException) jobResult;
                else if (jobResult instanceof ResourceAllocationException)
                    throw (ResourceAllocationException) jobResult;
                else if (jobResult instanceof Throwable)
                    throw new RuntimeException("Unexpected exception", (Throwable) jobResult);
            }
            return _snapshotDao.findById(snapshotId);
        }
    } else {
        CreateSnapshotPayload payload = new CreateSnapshotPayload();
        payload.setSnapshotId(snapshotId);
        payload.setSnapshotPolicyId(policyId);
        payload.setAccount(account);
        payload.setQuiescevm(quiescevm);
        volume.addPayload(payload);
        return volService.takeSnapshot(volume);
    }
}
Also used : AsyncJobExecutionContext(org.apache.cloudstack.framework.jobs.AsyncJobExecutionContext) VMInstanceVO(com.cloud.vm.VMInstanceVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VmWorkJobVO(org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO) ExecutionException(java.util.concurrent.ExecutionException) VmWorkTakeVolumeSnapshot(com.cloud.vm.VmWorkTakeVolumeSnapshot) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ActionEvent(com.cloud.event.ActionEvent)

Example 98 with ActionEvent

use of com.cloud.event.ActionEvent in project cloudstack by apache.

the class GlobalLoadBalancingRulesServiceImpl method assignToGlobalLoadBalancerRule.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_ASSIGN_TO_GLOBAL_LOAD_BALANCER_RULE, eventDescription = "Assigning a load balancer rule to global load balancer rule", async = true)
public boolean assignToGlobalLoadBalancerRule(AssignToGlobalLoadBalancerRuleCmd assignToGslbCmd) {
    CallContext ctx = CallContext.current();
    Account caller = ctx.getCallingAccount();
    final long gslbRuleId = assignToGslbCmd.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: " + gslbRule.getUuid() + " is in revoked state");
    }
    final List<Long> newLbRuleIds = assignToGslbCmd.getLoadBalancerRulesIds();
    if (newLbRuleIds == null || newLbRuleIds.isEmpty()) {
        throw new InvalidParameterValueException("empty list of load balancer rule Ids specified to be assigned" + " global load balancer rule");
    }
    List<Long> oldLbRuleIds = new ArrayList<Long>();
    List<Long> oldZones = new ArrayList<Long>();
    List<Long> newZones = new ArrayList<Long>(oldZones);
    List<Pair<Long, Long>> physcialNetworks = new ArrayList<Pair<Long, Long>>();
    // get the list of load balancer rules id's that are assigned currently to GSLB rule and corresponding zone id's
    List<GlobalLoadBalancerLbRuleMapVO> gslbLbMapVos = _gslbLbMapDao.listByGslbRuleId(gslbRuleId);
    if (gslbLbMapVos != null) {
        for (GlobalLoadBalancerLbRuleMapVO gslbLbMapVo : gslbLbMapVos) {
            LoadBalancerVO loadBalancer = _lbDao.findById(gslbLbMapVo.getLoadBalancerId());
            Network network = _networkDao.findById(loadBalancer.getNetworkId());
            oldZones.add(network.getDataCenterId());
            oldLbRuleIds.add(gslbLbMapVo.getLoadBalancerId());
        }
    }
    /* check each of the load balancer rule id passed in the 'AssignToGlobalLoadBalancerRuleCmd' command is
         *     valid ID
         *     caller has access to the rule
         *     check rule is not revoked
         *     no two rules are in same zone
         *     rule is not already assigned to gslb rule
         */
    for (Long lbRuleId : newLbRuleIds) {
        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);
        if (gslbRule.getAccountId() != loadBalancer.getAccountId()) {
            throw new InvalidParameterValueException("GSLB rule and load balancer rule does not belong to same account");
        }
        if (loadBalancer.getState() == LoadBalancer.State.Revoke) {
            throw new InvalidParameterValueException("Load balancer ID " + loadBalancer.getUuid() + " is in revoke state");
        }
        if (oldLbRuleIds != null && oldLbRuleIds.contains(loadBalancer.getId())) {
            throw new InvalidParameterValueException("Load balancer ID " + loadBalancer.getUuid() + " is already assigned");
        }
        Network network = _networkDao.findById(loadBalancer.getNetworkId());
        if (oldZones != null && oldZones.contains(network.getDataCenterId()) || newZones != null && newZones.contains(network.getDataCenterId())) {
            throw new InvalidParameterValueException("Load balancer rule specified should be in unique zone");
        }
        newZones.add(network.getDataCenterId());
        physcialNetworks.add(new Pair<Long, Long>(network.getDataCenterId(), network.getPhysicalNetworkId()));
    }
    // for each of the physical network check if GSLB service provider configured
    for (Pair<Long, Long> physicalNetwork : physcialNetworks) {
        if (!checkGslbServiceEnabledInZone(physicalNetwork.first(), physicalNetwork.second())) {
            throw new InvalidParameterValueException("GSLB service is not enabled in the Zone:" + physicalNetwork.first() + " and physical network " + physicalNetwork.second());
        }
    }
    final Map<Long, Long> lbRuleWeightMap = assignToGslbCmd.getLoadBalancerRuleWeightMap();
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            // persist the mapping for the new Lb rule that needs to assigned to a gslb rule
            for (Long lbRuleId : newLbRuleIds) {
                GlobalLoadBalancerLbRuleMapVO newGslbLbMap = new GlobalLoadBalancerLbRuleMapVO();
                newGslbLbMap.setGslbLoadBalancerId(gslbRuleId);
                newGslbLbMap.setLoadBalancerId(lbRuleId);
                if (lbRuleWeightMap != null && lbRuleWeightMap.get(lbRuleId) != null) {
                    newGslbLbMap.setWeight(lbRuleWeightMap.get(lbRuleId));
                }
                _gslbLbMapDao.persist(newGslbLbMap);
            }
            // mark the gslb rule state as add
            if (gslbRule.getState() == GlobalLoadBalancerRule.State.Staged || gslbRule.getState() == GlobalLoadBalancerRule.State.Active) {
                gslbRule.setState(GlobalLoadBalancerRule.State.Add);
                _gslbRuleDao.update(gslbRule.getId(), gslbRule);
            }
        }
    });
    boolean success = false;
    try {
        s_logger.debug("Configuring gslb rule configuration on the gslb service providers in the participating zones");
        // apply the gslb rule on to the back end gslb service providers on zones participating in gslb
        if (!applyGlobalLoadBalancerRuleConfig(gslbRuleId, false)) {
            s_logger.warn("Failed to add load balancer rules " + newLbRuleIds + " to global load balancer rule id " + gslbRuleId);
            CloudRuntimeException ex = new CloudRuntimeException("Failed to add load balancer rules to GSLB rule ");
            throw ex;
        }
        // on success set state to Active
        gslbRule.setState(GlobalLoadBalancerRule.State.Active);
        _gslbRuleDao.update(gslbRule.getId(), gslbRule);
        success = true;
    } catch (ResourceUnavailableException e) {
        throw new CloudRuntimeException("Failed to apply new GSLB configuration while assigning new LB rules to GSLB rule.");
    }
    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) Pair(com.cloud.utils.Pair) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 99 with ActionEvent

use of com.cloud.event.ActionEvent in project cloudstack by apache.

the class GlobalLoadBalancingRulesServiceImpl method createGlobalLoadBalancerRule.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_GLOBAL_LOAD_BALANCER_CREATE, eventDescription = "creating global load " + "balancer rule", create = true)
public GlobalLoadBalancerRule createGlobalLoadBalancerRule(CreateGlobalLoadBalancerRuleCmd newRule) {
    final Integer regionId = newRule.getRegionId();
    final String algorithm = newRule.getAlgorithm();
    final String stickyMethod = newRule.getStickyMethod();
    final String name = newRule.getName();
    final String description = newRule.getDescription();
    final String domainName = newRule.getServiceDomainName();
    final String serviceType = newRule.getServiceType();
    final Account gslbOwner = _accountMgr.getAccount(newRule.getEntityOwnerId());
    if (!GlobalLoadBalancerRule.Algorithm.isValidAlgorithm(algorithm)) {
        throw new InvalidParameterValueException("Invalid Algorithm: " + algorithm);
    }
    if (!GlobalLoadBalancerRule.Persistence.isValidPersistence(stickyMethod)) {
        throw new InvalidParameterValueException("Invalid persistence: " + stickyMethod);
    }
    if (!GlobalLoadBalancerRule.ServiceType.isValidServiceType(serviceType)) {
        throw new InvalidParameterValueException("Invalid service type: " + serviceType);
    }
    if (!NetUtils.verifyDomainName(domainName)) {
        throw new InvalidParameterValueException("Invalid domain name : " + domainName);
    }
    GlobalLoadBalancerRuleVO gslbRuleWithDomainName = _gslbRuleDao.findByDomainName(domainName);
    if (gslbRuleWithDomainName != null) {
        throw new InvalidParameterValueException("Domain name " + domainName + "is in use");
    }
    Region region = _regionDao.findById(regionId);
    if (region == null) {
        throw new InvalidParameterValueException("Invalid region ID: " + regionId);
    }
    String providerDnsName = _globalConfigDao.getValue(Config.CloudDnsName.key());
    if (!region.checkIfServiceEnabled(Region.Service.Gslb) || (providerDnsName == null)) {
        throw new CloudRuntimeException("GSLB service is not enabled in region : " + region.getName());
    }
    GlobalLoadBalancerRuleVO newGslbRule = Transaction.execute(new TransactionCallback<GlobalLoadBalancerRuleVO>() {

        @Override
        public GlobalLoadBalancerRuleVO doInTransaction(TransactionStatus status) {
            GlobalLoadBalancerRuleVO newGslbRule = new GlobalLoadBalancerRuleVO(name, description, domainName, algorithm, stickyMethod, serviceType, regionId, gslbOwner.getId(), gslbOwner.getDomainId(), GlobalLoadBalancerRule.State.Staged);
            _gslbRuleDao.persist(newGslbRule);
            UsageEventUtils.publishUsageEvent(EventTypes.EVENT_GLOBAL_LOAD_BALANCER_CREATE, newGslbRule.getAccountId(), 0, newGslbRule.getId(), name, GlobalLoadBalancerRule.class.getName(), newGslbRule.getUuid());
            return newGslbRule;
        }
    });
    s_logger.debug("successfully created new global load balancer rule for the account " + gslbOwner.getId());
    return newGslbRule;
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Region(org.apache.cloudstack.region.Region) TransactionStatus(com.cloud.utils.db.TransactionStatus) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 100 with ActionEvent

use of com.cloud.event.ActionEvent in project cloudstack by apache.

the class GlobalLoadBalancingRulesServiceImpl method deleteGlobalLoadBalancerRule.

@Override
@ActionEvent(eventType = EventTypes.EVENT_GLOBAL_LOAD_BALANCER_DELETE, eventDescription = "Delete global load balancer rule")
public boolean deleteGlobalLoadBalancerRule(DeleteGlobalLoadBalancerRuleCmd deleteGslbCmd) {
    CallContext ctx = CallContext.current();
    Account caller = ctx.getCallingAccount();
    long gslbRuleId = deleteGslbCmd.getGlobalLoadBalancerId();
    try {
        revokeGslbRule(gslbRuleId, caller);
    } catch (Exception e) {
        s_logger.warn("Failed to delete GSLB rule due to" + e.getMessage());
        return false;
    }
    return true;
}
Also used : Account(com.cloud.user.Account) CallContext(org.apache.cloudstack.context.CallContext) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ActionEvent(com.cloud.event.ActionEvent)

Aggregations

ActionEvent (com.cloud.event.ActionEvent)209 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)174 Account (com.cloud.user.Account)114 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)80 DB (com.cloud.utils.db.DB)79 TransactionStatus (com.cloud.utils.db.TransactionStatus)40 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)32 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)32 ArrayList (java.util.ArrayList)31 CallContext (org.apache.cloudstack.context.CallContext)22 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)20 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)20 DataCenterVO (com.cloud.dc.DataCenterVO)18 Network (com.cloud.network.Network)18 LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)17 InvalidParameterException (java.security.InvalidParameterException)16 List (java.util.List)16 NetworkVO (com.cloud.network.dao.NetworkVO)15 ConfigurationException (javax.naming.ConfigurationException)15 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)14