Search in sources :

Example 11 with Project

use of com.cloud.projects.Project in project cloudstack by apache.

the class CreateSnapshotCmd method getEntityOwnerId.

@Override
public long getEntityOwnerId() {
    Volume volume = _entityMgr.findById(Volume.class, getVolumeId());
    if (volume == null) {
        throw new InvalidParameterValueException("Unable to find volume by id=" + volumeId);
    }
    Account account = _accountService.getAccount(volume.getAccountId());
    //Can create templates for enabled projects/accounts only
    if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
        Project project = _projectService.findByProjectAccountId(volume.getAccountId());
        if (project.getState() != Project.State.Active) {
            throw new PermissionDeniedException("Can't add resources to the project id=" + project.getId() + " in state=" + project.getState() + " as it's no longer active");
        }
    } else if (account.getState() == Account.State.disabled) {
        throw new PermissionDeniedException("The owner of template is disabled: " + account);
    }
    return volume.getAccountId();
}
Also used : Account(com.cloud.user.Account) Project(com.cloud.projects.Project) Volume(com.cloud.storage.Volume) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException)

Example 12 with Project

use of com.cloud.projects.Project in project cloudstack by apache.

the class CreateProjectCmd method create.

@Override
public void create() throws ResourceAllocationException {
    CallContext.current().setEventDetails("Project Name: " + getName());
    Project project = _projectService.createProject(getName(), getDisplayText(), getAccountName(), getDomainId());
    if (project != null) {
        this.setEntityId(project.getId());
        this.setEntityUuid(project.getUuid());
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create a project");
    }
}
Also used : Project(com.cloud.projects.Project) ServerApiException(org.apache.cloudstack.api.ServerApiException)

Example 13 with Project

use of com.cloud.projects.Project in project cloudstack by apache.

the class UpdateProjectCmd method execute.

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public void execute() throws ResourceAllocationException {
    CallContext.current().setEventDetails("Project id: " + getId());
    Project project = _projectService.updateProject(getId(), getDisplayText(), getAccountName());
    if (project != null) {
        ProjectResponse response = _responseGenerator.createProjectResponse(project);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update a project");
    }
}
Also used : Project(com.cloud.projects.Project) ProjectResponse(org.apache.cloudstack.api.response.ProjectResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException)

Example 14 with Project

use of com.cloud.projects.Project 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)

Example 15 with Project

use of com.cloud.projects.Project in project cloudstack by apache.

the class AddAccountToProjectCmd method getEntityOwnerId.

@Override
public long getEntityOwnerId() {
    Project project = _projectService.getProject(getProjectId());
    //verify input parameters
    if (project == null) {
        InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find project with specified ID");
        ex.addProxyObject(getProjectId().toString(), "projectId");
        throw ex;
    }
    return _projectService.getProjectOwner(getProjectId()).getId();
}
Also used : Project(com.cloud.projects.Project) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException)

Aggregations

Project (com.cloud.projects.Project)48 Account (com.cloud.user.Account)30 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)25 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)16 Domain (com.cloud.domain.Domain)10 ArrayList (java.util.ArrayList)10 DomainVO (com.cloud.domain.DomainVO)7 Volume (com.cloud.storage.Volume)7 Pair (com.cloud.utils.Pair)6 List (java.util.List)6 ServerApiException (com.cloud.api.ServerApiException)5 ProjectResponse (com.cloud.api.response.ProjectResponse)5 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)5 ProjectAccount (com.cloud.projects.ProjectAccount)5 UserAccount (com.cloud.user.UserAccount)5 ServerApiException (org.apache.cloudstack.api.ServerApiException)5 DataCenterVO (com.cloud.dc.DataCenterVO)4 DB (com.cloud.utils.db.DB)4 Filter (com.cloud.utils.db.Filter)4 VlanVO (com.cloud.dc.VlanVO)3