Search in sources :

Example 56 with Project

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.");
    }
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) HashMap(java.util.HashMap) EventBus(com.cloud.framework.events.EventBus) CallContext(com.cloud.context.CallContext) EventBusException(com.cloud.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Date(java.util.Date) Project(com.cloud.projects.Project) EventBusException(com.cloud.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 57 with Project

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);
    }
}
Also used : CloudException(com.cloud.exception.CloudException) CallContext(com.cloud.context.CallContext) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException) Project(com.cloud.projects.Project) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) DispatchTask(com.cloud.api.dispatch.DispatchTask)

Example 58 with Project

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());
    }
}
Also used : UserAccount(com.cloud.user.UserAccount) Account(com.cloud.user.Account) Project(com.cloud.projects.Project)

Example 59 with Project

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;
}
Also used : UserAccount(com.cloud.user.UserAccount) Account(com.cloud.user.Account) Project(com.cloud.projects.Project) VirtualMachineTemplate(com.cloud.template.VirtualMachineTemplate) ArrayList(java.util.ArrayList) Domain(com.cloud.domain.Domain) TemplatePermissionsResponse(com.cloud.api.response.TemplatePermissionsResponse)

Example 60 with Project

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());
}
Also used : UserAccount(com.cloud.user.UserAccount) Account(com.cloud.user.Account) Project(com.cloud.projects.Project) Domain(com.cloud.domain.Domain)

Aggregations

Project (com.cloud.projects.Project)89 Account (com.cloud.user.Account)55 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)28 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)27 Domain (com.cloud.domain.Domain)20 ArrayList (java.util.ArrayList)20 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)18 DomainVO (com.cloud.domain.DomainVO)12 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)11 Pair (com.cloud.utils.Pair)11 List (java.util.List)11 ServerApiException (com.cloud.api.ServerApiException)10 ProjectAccount (com.cloud.projects.ProjectAccount)10 Volume (com.cloud.storage.Volume)10 UserAccount (com.cloud.user.UserAccount)10 DB (com.cloud.utils.db.DB)10 ProjectResponse (com.cloud.api.response.ProjectResponse)9 Filter (com.cloud.utils.db.Filter)9 DataCenterVO (com.cloud.dc.DataCenterVO)8 VlanVO (com.cloud.dc.VlanVO)6