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());
}
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;
}
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;
}
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");
}
}
}
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.");
}
}
Aggregations