Search in sources :

Example 26 with ActionEvent

use of com.cloud.event.ActionEvent in project cloudstack by apache.

the class ProjectManagerImpl method activateProject.

@Override
@ActionEvent(eventType = EventTypes.EVENT_PROJECT_ACTIVATE, eventDescription = "activating project")
@DB
public Project activateProject(final long projectId) {
    Account caller = CallContext.current().getCallingAccount();
    //check that the project exists
    final ProjectVO project = getProject(projectId);
    if (project == null) {
        InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find project with specified id");
        ex.addProxyObject(String.valueOf(projectId), "projectId");
        throw ex;
    }
    //verify permissions
    _accountMgr.checkAccess(caller, AccessType.ModifyProject, true, _accountMgr.getAccount(project.getProjectAccountId()));
    //allow project activation only when it's in Suspended state
    Project.State currentState = project.getState();
    if (currentState == State.Active) {
        s_logger.debug("The project id=" + projectId + " is already active, no need to activate it again");
        return project;
    }
    if (currentState != State.Suspended) {
        throw new InvalidParameterValueException("Can't activate the project in " + currentState + " state");
    }
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            project.setState(Project.State.Active);
            _projectDao.update(projectId, project);
            _accountMgr.enableAccount(project.getProjectAccountId());
        }
    });
    return _projectDao.findById(projectId);
}
Also used : Account(com.cloud.user.Account) State(com.cloud.projects.Project.State) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 27 with ActionEvent

use of com.cloud.event.ActionEvent in project cloudstack by apache.

the class Site2SiteVpnManagerImpl method updateCustomerGateway.

@Override
@ActionEvent(eventType = EventTypes.EVENT_S2S_VPN_CUSTOMER_GATEWAY_UPDATE, eventDescription = "update s2s vpn customer gateway", create = true)
public Site2SiteCustomerGateway updateCustomerGateway(UpdateVpnCustomerGatewayCmd cmd) {
    CallContext.current().setEventDetails(" Id: " + cmd.getId());
    Account caller = CallContext.current().getCallingAccount();
    Long id = cmd.getId();
    Site2SiteCustomerGatewayVO gw = _customerGatewayDao.findById(id);
    if (gw == null) {
        throw new InvalidParameterValueException("Find to find customer gateway with id " + id);
    }
    _accountMgr.checkAccess(caller, null, false, gw);
    List<Site2SiteVpnConnectionVO> conns = _vpnConnectionDao.listByCustomerGatewayId(id);
    if (conns != null) {
        for (Site2SiteVpnConnection conn : conns) {
            if (conn.getState() != State.Error) {
                throw new InvalidParameterValueException("Unable to update customer gateway with connections in non-Error state!");
            }
        }
    }
    String name = cmd.getName();
    String gatewayIp = cmd.getGatewayIp();
    if (!NetUtils.isValidIp(gatewayIp)) {
        throw new InvalidParameterValueException("The customer gateway ip " + gatewayIp + " is invalid!");
    }
    if (name == null) {
        name = "VPN-" + gatewayIp;
    }
    String guestCidrList = cmd.getGuestCidrList();
    if (!NetUtils.validateGuestCidrList(guestCidrList)) {
        throw new InvalidParameterValueException("The customer gateway guest cidr list " + guestCidrList + " contains invalid guest cidr!");
    }
    String ipsecPsk = cmd.getIpsecPsk();
    String ikePolicy = cmd.getIkePolicy();
    String espPolicy = cmd.getEspPolicy();
    if (!NetUtils.isValidS2SVpnPolicy("ike", ikePolicy)) {
        throw new InvalidParameterValueException("The customer gateway IKE policy" + ikePolicy + " is invalid!  Verify the required Diffie Hellman (DH) group is specified.");
    }
    if (!NetUtils.isValidS2SVpnPolicy("esp", espPolicy)) {
        throw new InvalidParameterValueException("The customer gateway ESP policy" + espPolicy + " is invalid!");
    }
    Long ikeLifetime = cmd.getIkeLifetime();
    if (ikeLifetime == null) {
        // Default value of lifetime is 1 day
        ikeLifetime = (long) 86400;
    }
    if (ikeLifetime > 86400) {
        throw new InvalidParameterValueException("The IKE lifetime " + ikeLifetime + " of vpn connection is invalid!");
    }
    Long espLifetime = cmd.getEspLifetime();
    if (espLifetime == null) {
        // Default value of lifetime is 1 hour
        espLifetime = (long) 3600;
    }
    if (espLifetime > 86400) {
        throw new InvalidParameterValueException("The ESP lifetime " + espLifetime + " of vpn connection is invalid!");
    }
    Boolean dpd = cmd.getDpd();
    if (dpd == null) {
        dpd = false;
    }
    Boolean encap = cmd.getEncap();
    if (encap == null) {
        encap = false;
    }
    checkCustomerGatewayCidrList(guestCidrList);
    long accountId = gw.getAccountId();
    Site2SiteCustomerGatewayVO existedGw = _customerGatewayDao.findByGatewayIpAndAccountId(gatewayIp, accountId);
    if (existedGw != null && existedGw.getId() != gw.getId()) {
        throw new InvalidParameterValueException("The customer gateway with ip " + gatewayIp + " already existed in the system!");
    }
    existedGw = _customerGatewayDao.findByNameAndAccountId(name, accountId);
    if (existedGw != null && existedGw.getId() != gw.getId()) {
        throw new InvalidParameterValueException("The customer gateway with name " + name + " already existed!");
    }
    gw.setName(name);
    gw.setGatewayIp(gatewayIp);
    gw.setGuestCidrList(guestCidrList);
    gw.setIkePolicy(ikePolicy);
    gw.setEspPolicy(espPolicy);
    gw.setIpsecPsk(ipsecPsk);
    gw.setIkeLifetime(ikeLifetime);
    gw.setEspLifetime(espLifetime);
    gw.setDpd(dpd);
    gw.setEncap(encap);
    _customerGatewayDao.persist(gw);
    return gw;
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Site2SiteCustomerGatewayVO(com.cloud.network.dao.Site2SiteCustomerGatewayVO) Site2SiteVpnConnectionVO(com.cloud.network.dao.Site2SiteVpnConnectionVO) Site2SiteVpnConnection(com.cloud.network.Site2SiteVpnConnection) ActionEvent(com.cloud.event.ActionEvent)

Example 28 with ActionEvent

use of com.cloud.event.ActionEvent in project cloudstack by apache.

the class Site2SiteVpnManagerImpl method deleteCustomerGateway.

@Override
@ActionEvent(eventType = EventTypes.EVENT_S2S_VPN_CUSTOMER_GATEWAY_DELETE, eventDescription = "deleting s2s vpn customer gateway", create = true)
public boolean deleteCustomerGateway(DeleteVpnCustomerGatewayCmd cmd) {
    CallContext.current().setEventDetails(" Id: " + cmd.getId());
    Account caller = CallContext.current().getCallingAccount();
    Long id = cmd.getId();
    Site2SiteCustomerGateway customerGateway = _customerGatewayDao.findById(id);
    if (customerGateway == null) {
        throw new InvalidParameterValueException("Fail to find customer gateway with " + id + " !");
    }
    _accountMgr.checkAccess(caller, null, false, customerGateway);
    return doDeleteCustomerGateway(customerGateway);
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Site2SiteCustomerGateway(com.cloud.network.Site2SiteCustomerGateway) ActionEvent(com.cloud.event.ActionEvent)

Example 29 with ActionEvent

use of com.cloud.event.ActionEvent in project cloudstack by apache.

the class Site2SiteVpnManagerImpl method updateVpnGateway.

@Override
@ActionEvent(eventType = EventTypes.EVENT_S2S_VPN_GATEWAY_UPDATE, eventDescription = "updating s2s vpn gateway", async = true)
public Site2SiteVpnGateway updateVpnGateway(Long id, String customId, Boolean forDisplay) {
    Account caller = CallContext.current().getCallingAccount();
    Site2SiteVpnGatewayVO vpnGateway = _vpnGatewayDao.findById(id);
    if (vpnGateway == null) {
        throw new InvalidParameterValueException("Fail to find vpn gateway with " + id);
    }
    _accountMgr.checkAccess(caller, null, false, vpnGateway);
    if (customId != null) {
        vpnGateway.setUuid(customId);
    }
    if (forDisplay != null) {
        vpnGateway.setDisplay(forDisplay);
    }
    _vpnGatewayDao.update(id, vpnGateway);
    return _vpnGatewayDao.findById(id);
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Site2SiteVpnGatewayVO(com.cloud.network.dao.Site2SiteVpnGatewayVO) ActionEvent(com.cloud.event.ActionEvent)

Example 30 with ActionEvent

use of com.cloud.event.ActionEvent in project cloudstack by apache.

the class ProjectManagerImpl method createProject.

@Override
@ActionEvent(eventType = EventTypes.EVENT_PROJECT_CREATE, eventDescription = "creating project", create = true)
@DB
public Project createProject(final String name, final String displayText, String accountName, final Long domainId) throws ResourceAllocationException {
    Account caller = CallContext.current().getCallingAccount();
    Account owner = caller;
    //check if the user authorized to create the project
    if (_accountMgr.isNormalUser(caller.getId()) && !_allowUserToCreateProject) {
        throw new PermissionDeniedException("Regular user is not permitted to create a project");
    }
    //Verify request parameters
    if ((accountName != null && domainId == null) || (domainId != null && accountName == null)) {
        throw new InvalidParameterValueException("Account name and domain id must be specified together");
    }
    if (accountName != null) {
        owner = _accountMgr.finalizeOwner(caller, accountName, domainId, null);
    }
    //don't allow 2 projects with the same name inside the same domain
    if (_projectDao.findByNameAndDomain(name, owner.getDomainId()) != null) {
        throw new InvalidParameterValueException("Project with name " + name + " already exists in domain id=" + owner.getDomainId());
    }
    //do resource limit check
    _resourceLimitMgr.checkResourceLimit(owner, ResourceType.project);
    final Account ownerFinal = owner;
    return Transaction.execute(new TransactionCallback<Project>() {

        @Override
        public Project doInTransaction(TransactionStatus status) {
            //Create an account associated with the project
            StringBuilder acctNm = new StringBuilder("PrjAcct-");
            acctNm.append(name).append("-").append(ownerFinal.getDomainId());
            Account projectAccount = _accountMgr.createAccount(acctNm.toString(), Account.ACCOUNT_TYPE_PROJECT, null, domainId, null, null, UUID.randomUUID().toString());
            Project project = _projectDao.persist(new ProjectVO(name, displayText, ownerFinal.getDomainId(), projectAccount.getId()));
            //assign owner to the project
            assignAccountToProject(project, ownerFinal.getId(), ProjectAccount.Role.Admin);
            if (project != null) {
                CallContext.current().setEventDetails("Project id=" + project.getId());
                CallContext.current().putContextParameter(Project.class, project.getUuid());
            }
            //Increment resource count
            _resourceLimitMgr.incrementResourceCount(ownerFinal.getId(), ResourceType.project);
            return project;
        }
    });
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) TransactionStatus(com.cloud.utils.db.TransactionStatus) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Aggregations

ActionEvent (com.cloud.event.ActionEvent)209 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)174 Account (com.cloud.user.Account)114 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)80 DB (com.cloud.utils.db.DB)79 TransactionStatus (com.cloud.utils.db.TransactionStatus)40 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)32 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)32 ArrayList (java.util.ArrayList)31 CallContext (org.apache.cloudstack.context.CallContext)22 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)20 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)20 DataCenterVO (com.cloud.dc.DataCenterVO)18 Network (com.cloud.network.Network)18 LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)17 InvalidParameterException (java.security.InvalidParameterException)16 List (java.util.List)16 NetworkVO (com.cloud.network.dao.NetworkVO)15 ConfigurationException (javax.naming.ConfigurationException)15 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)14