Search in sources :

Example 6 with CallContext

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

the class VpcManagerImpl method deleteVpc.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VPC_DELETE, eventDescription = "deleting VPC")
public boolean deleteVpc(final long vpcId) throws ConcurrentOperationException, ResourceUnavailableException {
    CallContext.current().setEventDetails(" Id: " + vpcId);
    final CallContext ctx = CallContext.current();
    // Verify vpc id
    final Vpc vpc = _vpcDao.findById(vpcId);
    if (vpc == null) {
        throw new InvalidParameterValueException("unable to find VPC id=" + vpcId);
    }
    // verify permissions
    _accountMgr.checkAccess(ctx.getCallingAccount(), null, false, vpc);
    return destroyVpc(vpc, ctx.getCallingAccount(), ctx.getCallingUserId());
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CallContext(org.apache.cloudstack.context.CallContext) ActionEvent(com.cloud.event.ActionEvent)

Example 7 with CallContext

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

the class VpcManagerImpl method startVpc.

@Override
public boolean startVpc(final long vpcId, final boolean destroyOnFailure) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    final CallContext ctx = CallContext.current();
    final Account caller = ctx.getCallingAccount();
    final User callerUser = _accountMgr.getActiveUser(ctx.getCallingUserId());
    // check if vpc exists
    final Vpc vpc = getActiveVpc(vpcId);
    if (vpc == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find Enabled VPC by id specified");
        ex.addProxyObject(String.valueOf(vpcId), "VPC");
        throw ex;
    }
    // permission check
    _accountMgr.checkAccess(caller, null, false, vpc);
    final DataCenter dc = _entityMgr.findById(DataCenter.class, vpc.getZoneId());
    final DeployDestination dest = new DeployDestination(dc, null, null, null);
    final ReservationContext context = new ReservationContextImpl(null, null, callerUser, _accountMgr.getAccount(vpc.getAccountId()));
    boolean result = true;
    try {
        if (!startVpc(vpc, dest, context)) {
            s_logger.warn("Failed to start vpc " + vpc);
            result = false;
        }
    } catch (final Exception ex) {
        s_logger.warn("Failed to start vpc " + vpc + " due to ", ex);
        result = false;
    } finally {
        // do cleanup
        if (!result && destroyOnFailure) {
            s_logger.debug("Destroying vpc " + vpc + " that failed to start");
            if (destroyVpc(vpc, caller, callerUser.getId())) {
                s_logger.warn("Successfully destroyed vpc " + vpc + " that failed to start");
            } else {
                s_logger.warn("Failed to destroy vpc " + vpc + " that failed to start");
            }
        }
    }
    return result;
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) DataCenter(com.cloud.dc.DataCenter) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DeployDestination(com.cloud.deploy.DeployDestination) CallContext(org.apache.cloudstack.context.CallContext) ReservationContextImpl(com.cloud.vm.ReservationContextImpl) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ReservationContext(com.cloud.vm.ReservationContext)

Example 8 with CallContext

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

the class ActionEventInterceptor method interceptStart.

@Override
public Object interceptStart(Method method, Object target) {
    EventVO event = null;
    for (ActionEvent actionEvent : getActionEvents(method)) {
        boolean async = actionEvent.async();
        if (async) {
            CallContext ctx = CallContext.current();
            String eventDescription = getEventDescription(actionEvent, ctx);
            String eventType = getEventType(actionEvent, ctx);
            boolean isEventDisplayEnabled = ctx.isEventDisplayEnabled();
            ActionEventUtils.onStartedActionEventFromContext(eventType, eventDescription, isEventDisplayEnabled);
        }
    }
    return event;
}
Also used : CallContext(org.apache.cloudstack.context.CallContext)

Example 9 with CallContext

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

the class ActionEventUtils method populateFirstClassEntities.

private static void populateFirstClassEntities(Map<String, String> eventDescription) {
    CallContext context = CallContext.current();
    Map<Object, Object> contextMap = context.getContextParameters();
    for (Map.Entry<Object, Object> entry : contextMap.entrySet()) {
        try {
            Class<?> clz = (Class<?>) entry.getKey();
            if (clz != null && Identity.class.isAssignableFrom(clz)) {
                String uuid = getEntityUuid(clz, entry.getValue());
                eventDescription.put(ReflectUtil.getEntityName(clz), uuid);
            }
        } catch (Exception e) {
            s_logger.trace("Caught exception while populating first class entities for event bus, moving on");
        }
    }
}
Also used : Identity(org.apache.cloudstack.api.Identity) CallContext(org.apache.cloudstack.context.CallContext) HashMap(java.util.HashMap) Map(java.util.Map) EventBusException(org.apache.cloudstack.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 10 with CallContext

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

the class ActionEventUtils method publishOnEventBus.

private static void publishOnEventBus(long userId, long accountId, String eventCategory, String eventType, Event.State state, String description) {
    String configKey = Config.PublishActionEvent.key();
    String value = s_configDao.getValue(configKey);
    boolean configValue = Boolean.parseBoolean(value);
    if (!configValue)
        return;
    try {
        s_eventBus = ComponentContext.getComponent(EventBus.class);
    } catch (NoSuchBeanDefinitionException nbe) {
        // no provider is configured to provide events bus, so just return
        return;
    }
    // get the entity details for which ActionEvent is generated
    String entityType = null;
    String entityUuid = null;
    CallContext context = CallContext.current();
    //Get entity Class(Example - VirtualMachine.class) from the event Type eg. - VM.CREATE
    Class<?> entityClass = EventTypes.getEntityClassForEvent(eventType);
    if (entityClass != null) {
        //Get uuid from id
        Object param = context.getContextParameter(entityClass);
        if (param != null) {
            try {
                entityUuid = getEntityUuid(entityClass, param);
                entityType = entityClass.getName();
            } catch (Exception e) {
                s_logger.debug("Caught exception while finding entityUUID, moving on");
            }
        }
    }
    org.apache.cloudstack.framework.events.Event event = new org.apache.cloudstack.framework.events.Event(ManagementService.Name, eventCategory, eventType, EventTypes.getEntityForEvent(eventType), entityUuid);
    Map<String, String> eventDescription = new HashMap<String, String>();
    Project project = s_projectDao.findByProjectAccountId(accountId);
    Account account = s_accountDao.findById(accountId);
    User user = s_userDao.findById(userId);
    // if account has been deleted, this might be called during cleanup of resources and results in null pointer
    if (account == null)
        return;
    if (user == null)
        return;
    if (project != null)
        eventDescription.put("project", project.getUuid());
    eventDescription.put("user", user.getUuid());
    eventDescription.put("account", account.getUuid());
    eventDescription.put("event", eventType);
    eventDescription.put("status", state.toString());
    eventDescription.put("entity", entityType);
    eventDescription.put("entityuuid", entityUuid);
    //Put all the first class entities that are touched during the action. For now atleast put in the vmid.
    populateFirstClassEntities(eventDescription);
    eventDescription.put("description", description);
    String eventDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").format(new Date());
    eventDescription.put("eventDateTime", eventDate);
    event.setDescription(eventDescription);
    try {
        s_eventBus.publish(event);
    } catch (EventBusException e) {
        s_logger.warn("Failed to publish action event on the the event bus.");
    }
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) HashMap(java.util.HashMap) EventBus(org.apache.cloudstack.framework.events.EventBus) CallContext(org.apache.cloudstack.context.CallContext) EventBusException(org.apache.cloudstack.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Date(java.util.Date) Project(com.cloud.projects.Project) EventBusException(org.apache.cloudstack.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

CallContext (org.apache.cloudstack.context.CallContext)76 Account (com.cloud.user.Account)45 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)29 User (com.cloud.user.User)26 ActionEvent (com.cloud.event.ActionEvent)22 VmWorkJobVO (org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO)22 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)21 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)21 DB (com.cloud.utils.db.DB)13 LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)12 VMInstanceVO (com.cloud.vm.VMInstanceVO)10 FirewallRule (com.cloud.network.rules.FirewallRule)8 ArrayList (java.util.ArrayList)8 ServerApiException (org.apache.cloudstack.api.ServerApiException)8 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)7 Network (com.cloud.network.Network)7 TransactionStatus (com.cloud.utils.db.TransactionStatus)6 DeployDestination (com.cloud.deploy.DeployDestination)5 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)5 IPAddressVO (com.cloud.network.dao.IPAddressVO)5