use of com.cloud.projects.Project in project cosmic by MissionCriticalCloud.
the class ActionEventUtils method publishOnEventBus.
private static void publishOnEventBus(final long userId, final long accountId, final String eventCategory, final String eventType, final Event.State state, final String description) {
final String configKey = Config.PublishActionEvent.key();
final String value = s_configDao.getValue(configKey);
final boolean configValue = Boolean.parseBoolean(value);
if (!configValue) {
return;
}
try {
s_eventBus = ComponentContext.getComponent(EventBus.class);
} catch (final 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;
final CallContext context = CallContext.current();
// Get entity Class(Example - VirtualMachine.class) from the event Type eg. - VM.CREATE
final Class<?> entityClass = EventTypes.getEntityClassForEvent(eventType);
if (entityClass != null) {
// Get uuid from id
final Object param = context.getContextParameter(entityClass);
if (param != null) {
try {
entityUuid = getEntityUuid(entityClass, param);
entityType = entityClass.getName();
} catch (final Exception e) {
s_logger.debug("Caught exception while finding entityUUID, moving on");
}
}
}
final com.cloud.framework.events.Event event = new com.cloud.framework.events.Event(ManagementService.Name, eventCategory, eventType, EventTypes.getEntityForEvent(eventType), entityUuid);
final Map<String, String> eventDescription = new HashMap<>();
final Project project = s_projectDao.findByProjectAccountId(accountId);
final Account account = s_accountDao.findById(accountId);
final 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);
final 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 (final EventBusException e) {
s_logger.warn("Failed to publish action event on the the event bus.");
}
}
use of com.cloud.projects.Project in project cosmic by MissionCriticalCloud.
the class ApiDispatcher method dispatch.
public void dispatch(final BaseCmd cmd, final Map<String, String> params, final boolean execute) throws CloudException {
// Let the chain of responsibility dispatch gradually
standardDispatchChain.dispatch(new DispatchTask(cmd, params));
final CallContext ctx = CallContext.current();
ctx.setEventDisplayEnabled(cmd.isDisplay());
if (params.get(ApiConstants.PROJECT_ID) != null) {
final Project project = _entityMgr.findByUuidIncludingRemoved(Project.class, params.get(ApiConstants.PROJECT_ID));
ctx.setProject(project);
}
// TODO This if shouldn't be here. Use polymorphism and move it to validateSpecificParameters
if (cmd instanceof BaseAsyncCmd) {
final BaseAsyncCmd asyncCmd = (BaseAsyncCmd) cmd;
final String startEventId = params.get(ApiConstants.CTX_START_EVENT_ID);
ctx.setStartEventId(Long.parseLong(startEventId));
// Synchronise job on the object if needed
if (asyncCmd.getJob() != null && asyncCmd.getSyncObjId() != null && asyncCmd.getSyncObjType() != null) {
final Long queueSizeLimit;
if (asyncCmd.getSyncObjType() != null && asyncCmd.getSyncObjType().equalsIgnoreCase(BaseAsyncCmd.snapshotHostSyncObject)) {
queueSizeLimit = _createSnapshotQueueSizeLimit;
} else {
queueSizeLimit = 1L;
}
if (queueSizeLimit != null) {
if (!execute) {
// if we are not within async-execution context, enqueue the command
_asyncMgr.syncAsyncJobExecution((AsyncJob) asyncCmd.getJob(), asyncCmd.getSyncObjType(), asyncCmd.getSyncObjId().longValue(), queueSizeLimit);
return;
}
} else {
s_logger.trace("The queue size is unlimited, skipping the synchronizing");
}
}
}
// TODO This if shouldn't be here. Use polymorphism and move it to validateSpecificParameters
if (cmd instanceof BaseAsyncCustomIdCmd) {
((BaseAsyncCustomIdCmd) cmd).checkUuid();
} else if (cmd instanceof BaseCustomIdCmd) {
((BaseCustomIdCmd) cmd).checkUuid();
}
try {
cmd.execute();
} catch (ResourceUnavailableException | InsufficientCapacityException | ResourceAllocationException | NetworkRuleConflictException e) {
throw new CloudException("Caught exception while executing command", e);
}
}
use of com.cloud.projects.Project in project cosmic by MissionCriticalCloud.
the class ApiResponseHelper method populateAccount.
private void populateAccount(final ControlledEntityResponse response, final long accountId) {
final Account account = ApiDBUtils.findAccountById(accountId);
if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
// find the project
final Project project = ApiDBUtils.findProjectByProjectAccountId(account.getId());
if (project != null) {
response.setProjectId(project.getUuid());
response.setProjectName(project.getName());
response.setAccountName(account.getAccountName());
}
} else {
response.setAccountName(account.getAccountName());
}
}
use of com.cloud.projects.Project in project cosmic by MissionCriticalCloud.
the class ApiResponseHelper method createTemplatePermissionsResponse.
@Override
public TemplatePermissionsResponse createTemplatePermissionsResponse(final ResponseView view, final List<String> accountNames, final Long id) {
Long templateOwnerDomain = null;
final VirtualMachineTemplate template = ApiDBUtils.findTemplateById(id);
final Account templateOwner = ApiDBUtils.findAccountById(template.getAccountId());
if (view == ResponseView.Full) {
// from that
if (templateOwner != null) {
templateOwnerDomain = templateOwner.getDomainId();
}
}
final TemplatePermissionsResponse response = new TemplatePermissionsResponse();
response.setId(template.getUuid());
response.setPublicTemplate(template.isPublicTemplate());
if (view == ResponseView.Full && templateOwnerDomain != null) {
final Domain domain = ApiDBUtils.findDomainById(templateOwnerDomain);
if (domain != null) {
response.setDomainId(domain.getUuid());
}
}
// Set accounts
final List<String> projectIds = new ArrayList<>();
final List<String> regularAccounts = new ArrayList<>();
for (final String accountName : accountNames) {
final Account account = ApiDBUtils.findAccountByNameDomain(accountName, templateOwner.getDomainId());
if (account.getType() != Account.ACCOUNT_TYPE_PROJECT) {
regularAccounts.add(accountName);
} else {
// convert account to projectIds
final Project project = ApiDBUtils.findProjectByProjectAccountId(account.getId());
if (project.getUuid() != null && !project.getUuid().isEmpty()) {
projectIds.add(project.getUuid());
} else {
projectIds.add(String.valueOf(project.getId()));
}
}
}
if (!projectIds.isEmpty()) {
response.setProjectIds(projectIds);
}
if (!regularAccounts.isEmpty()) {
response.setAccountNames(regularAccounts);
}
response.setObjectName("templatepermission");
return response;
}
use of com.cloud.projects.Project in project cosmic by MissionCriticalCloud.
the class ApiResponseHelper method populateOwner.
// TODO: we may need to refactor once ControlledEntityResponse and
// ControlledEntity id to uuid conversion are all done.
// currently code is scattered in
private void populateOwner(final ControlledEntityResponse response, final ControlledEntity object) {
final Account account = ApiDBUtils.findAccountById(object.getAccountId());
if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
// find the project
final Project project = ApiDBUtils.findProjectByProjectAccountId(account.getId());
if (project != null) {
response.setProjectId(project.getUuid());
response.setProjectName(project.getName());
}
} else {
response.setAccountName(account.getAccountName());
}
final Domain domain = ApiDBUtils.findDomainById(object.getDomainId());
response.setDomainId(domain.getUuid());
response.setDomainName(domain.getName());
}
Aggregations