Search in sources :

Example 1 with Account

use of com.cloud.user.Account in project CloudStack-archive by CloudStack-extras.

the class UsageManagerImpl method createSecurityGroupEvent.

private void createSecurityGroupEvent(UsageEventVO event) {
    long zoneId = -1L;
    long vmId = event.getResourceId();
    long sgId = event.getOfferingId();
    if (EventTypes.EVENT_SECURITY_GROUP_ASSIGN.equals(event.getType())) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Assigning : security group" + sgId + " to Vm: " + vmId + " for account: " + event.getAccountId());
        }
        zoneId = event.getZoneId();
        Account acct = m_accountDao.findByIdIncludingRemoved(event.getAccountId());
        UsageSecurityGroupVO securityGroup = new UsageSecurityGroupVO(zoneId, event.getAccountId(), acct.getDomainId(), vmId, sgId, event.getCreateDate(), null);
        m_usageSecurityGroupDao.persist(securityGroup);
    } else if (EventTypes.EVENT_SECURITY_GROUP_REMOVE.equals(event.getType())) {
        SearchCriteria<UsageSecurityGroupVO> sc = m_usageSecurityGroupDao.createSearchCriteria();
        sc.addAnd("accountId", SearchCriteria.Op.EQ, event.getAccountId());
        sc.addAnd("vmInstanceId", SearchCriteria.Op.EQ, vmId);
        sc.addAnd("securityGroupId", SearchCriteria.Op.EQ, sgId);
        sc.addAnd("deleted", SearchCriteria.Op.NULL);
        List<UsageSecurityGroupVO> sgVOs = m_usageSecurityGroupDao.search(sc, null);
        if (sgVOs.size() > 1) {
            s_logger.warn("More that one usage entry for security group: " + sgId + " for Vm: " + vmId + " assigned to account: " + event.getAccountId() + "; marking them all as deleted...");
        }
        for (UsageSecurityGroupVO sgVO : sgVOs) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("deleting security group: " + sgVO.getSecurityGroupId() + " from Vm: " + sgVO.getVmInstanceId());
            }
            // there really shouldn't be more than one
            sgVO.setDeleted(event.getCreateDate());
            m_usageSecurityGroupDao.update(sgVO);
        }
    }
}
Also used : Account(com.cloud.user.Account) List(java.util.List) SearchCriteria(com.cloud.utils.db.SearchCriteria)

Example 2 with Account

use of com.cloud.user.Account in project CloudStack-archive by CloudStack-extras.

the class UsageManagerImpl method createLoadBalancerHelperEvent.

private void createLoadBalancerHelperEvent(UsageEventVO event) {
    long zoneId = -1L;
    long id = event.getResourceId();
    if (EventTypes.EVENT_LOAD_BALANCER_CREATE.equals(event.getType())) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Creating load balancer : " + id + " for account: " + event.getAccountId());
        }
        zoneId = event.getZoneId();
        Account acct = m_accountDao.findByIdIncludingRemoved(event.getAccountId());
        UsageLoadBalancerPolicyVO lbVO = new UsageLoadBalancerPolicyVO(id, zoneId, event.getAccountId(), acct.getDomainId(), event.getCreateDate(), null);
        m_usageLoadBalancerPolicyDao.persist(lbVO);
    } else if (EventTypes.EVENT_LOAD_BALANCER_DELETE.equals(event.getType())) {
        SearchCriteria<UsageLoadBalancerPolicyVO> sc = m_usageLoadBalancerPolicyDao.createSearchCriteria();
        sc.addAnd("accountId", SearchCriteria.Op.EQ, event.getAccountId());
        sc.addAnd("id", SearchCriteria.Op.EQ, id);
        sc.addAnd("deleted", SearchCriteria.Op.NULL);
        List<UsageLoadBalancerPolicyVO> lbVOs = m_usageLoadBalancerPolicyDao.search(sc, null);
        if (lbVOs.size() > 1) {
            s_logger.warn("More that one usage entry for load balancer policy: " + id + " assigned to account: " + event.getAccountId() + "; marking them all as deleted...");
        }
        for (UsageLoadBalancerPolicyVO lbVO : lbVOs) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("deleting load balancer policy: " + lbVO.getId() + " from account: " + lbVO.getAccountId());
            }
            // there really shouldn't be more than one
            lbVO.setDeleted(event.getCreateDate());
            m_usageLoadBalancerPolicyDao.update(lbVO);
        }
    }
}
Also used : Account(com.cloud.user.Account) List(java.util.List) SearchCriteria(com.cloud.utils.db.SearchCriteria)

Example 3 with Account

use of com.cloud.user.Account in project CloudStack-archive by CloudStack-extras.

the class UsageManagerImpl method createPortForwardingHelperEvent.

private void createPortForwardingHelperEvent(UsageEventVO event) {
    long zoneId = -1L;
    long id = event.getResourceId();
    if (EventTypes.EVENT_NET_RULE_ADD.equals(event.getType())) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Creating port forwarding rule : " + id + " for account: " + event.getAccountId());
        }
        zoneId = event.getZoneId();
        Account acct = m_accountDao.findByIdIncludingRemoved(event.getAccountId());
        UsagePortForwardingRuleVO pfVO = new UsagePortForwardingRuleVO(id, zoneId, event.getAccountId(), acct.getDomainId(), event.getCreateDate(), null);
        m_usagePortForwardingRuleDao.persist(pfVO);
    } else if (EventTypes.EVENT_NET_RULE_DELETE.equals(event.getType())) {
        SearchCriteria<UsagePortForwardingRuleVO> sc = m_usagePortForwardingRuleDao.createSearchCriteria();
        sc.addAnd("accountId", SearchCriteria.Op.EQ, event.getAccountId());
        sc.addAnd("id", SearchCriteria.Op.EQ, id);
        sc.addAnd("deleted", SearchCriteria.Op.NULL);
        List<UsagePortForwardingRuleVO> pfVOs = m_usagePortForwardingRuleDao.search(sc, null);
        if (pfVOs.size() > 1) {
            s_logger.warn("More that one usage entry for port forwarding rule: " + id + " assigned to account: " + event.getAccountId() + "; marking them all as deleted...");
        }
        for (UsagePortForwardingRuleVO pfVO : pfVOs) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("deleting port forwarding rule: " + pfVO.getId() + " from account: " + pfVO.getAccountId());
            }
            // there really shouldn't be more than one
            pfVO.setDeleted(event.getCreateDate());
            m_usagePortForwardingRuleDao.update(pfVO);
        }
    }
}
Also used : Account(com.cloud.user.Account) List(java.util.List) SearchCriteria(com.cloud.utils.db.SearchCriteria)

Example 4 with Account

use of com.cloud.user.Account in project CloudStack-archive by CloudStack-extras.

the class UsageManagerImpl method createTemplateHelperEvent.

private void createTemplateHelperEvent(UsageEventVO event) {
    long templateId = -1L;
    long zoneId = -1L;
    long templateSize = -1L;
    templateId = event.getResourceId();
    zoneId = event.getZoneId();
    if (EventTypes.EVENT_TEMPLATE_CREATE.equals(event.getType()) || EventTypes.EVENT_TEMPLATE_COPY.equals(event.getType())) {
        templateSize = event.getSize();
        if (templateSize < 1) {
            s_logger.error("Incorrect size for template with Id " + templateId);
            return;
        }
        if (zoneId == -1L) {
            s_logger.error("Incorrect zoneId for template with Id " + templateId);
            return;
        }
    }
    if (EventTypes.EVENT_TEMPLATE_CREATE.equals(event.getType()) || EventTypes.EVENT_TEMPLATE_COPY.equals(event.getType())) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("create template with id : " + templateId + " for account: " + event.getAccountId());
        }
        List<UsageStorageVO> storageVOs = m_usageStorageDao.listByIdAndZone(event.getAccountId(), templateId, StorageTypes.TEMPLATE, zoneId);
        if (storageVOs.size() > 0) {
            s_logger.warn("Usage entry for Template: " + templateId + " assigned to account: " + event.getAccountId() + "already exists in zone " + zoneId);
            return;
        }
        Account acct = m_accountDao.findByIdIncludingRemoved(event.getAccountId());
        UsageStorageVO storageVO = new UsageStorageVO(templateId, zoneId, event.getAccountId(), acct.getDomainId(), StorageTypes.TEMPLATE, event.getTemplateId(), templateSize, event.getCreateDate(), null);
        m_usageStorageDao.persist(storageVO);
    } else if (EventTypes.EVENT_TEMPLATE_DELETE.equals(event.getType())) {
        List<UsageStorageVO> storageVOs;
        if (zoneId != -1L) {
            storageVOs = m_usageStorageDao.listByIdAndZone(event.getAccountId(), templateId, StorageTypes.TEMPLATE, zoneId);
        } else {
            storageVOs = m_usageStorageDao.listById(event.getAccountId(), templateId, StorageTypes.TEMPLATE);
        }
        if (storageVOs.size() > 1) {
            s_logger.warn("More that one usage entry for storage: " + templateId + " assigned to account: " + event.getAccountId() + "; marking them all as deleted...");
        }
        for (UsageStorageVO storageVO : storageVOs) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("deleting template: " + storageVO.getId() + " from account: " + storageVO.getAccountId());
            }
            // there really shouldn't be more than one
            storageVO.setDeleted(event.getCreateDate());
            m_usageStorageDao.update(storageVO);
        }
    }
}
Also used : Account(com.cloud.user.Account) List(java.util.List)

Example 5 with Account

use of com.cloud.user.Account in project CloudStack-archive by CloudStack-extras.

the class UsageManagerImpl method createVolumeHelperEvent.

private void createVolumeHelperEvent(UsageEventVO event) {
    Long doId = -1L;
    long zoneId = -1L;
    Long templateId = -1L;
    long size = -1L;
    long volId = event.getResourceId();
    if (EventTypes.EVENT_VOLUME_CREATE.equals(event.getType())) {
        doId = event.getOfferingId();
        zoneId = event.getZoneId();
        templateId = event.getTemplateId();
        size = event.getSize();
    }
    if (EventTypes.EVENT_VOLUME_CREATE.equals(event.getType())) {
        SearchCriteria<UsageVolumeVO> sc = m_usageVolumeDao.createSearchCriteria();
        sc.addAnd("accountId", SearchCriteria.Op.EQ, event.getAccountId());
        sc.addAnd("id", SearchCriteria.Op.EQ, volId);
        sc.addAnd("deleted", SearchCriteria.Op.NULL);
        List<UsageVolumeVO> volumesVOs = m_usageVolumeDao.search(sc, null);
        if (volumesVOs.size() > 0) {
            //This is a safeguard to avoid double counting of volumes.
            s_logger.error("Found duplicate usage entry for volume: " + volId + " assigned to account: " + event.getAccountId() + "; marking as deleted...");
        }
        for (UsageVolumeVO volumesVO : volumesVOs) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("deleting volume: " + volumesVO.getId() + " from account: " + volumesVO.getAccountId());
            }
            volumesVO.setDeleted(event.getCreateDate());
            m_usageVolumeDao.update(volumesVO);
        }
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("create volume with id : " + volId + " for account: " + event.getAccountId());
        }
        Account acct = m_accountDao.findByIdIncludingRemoved(event.getAccountId());
        UsageVolumeVO volumeVO = new UsageVolumeVO(volId, zoneId, event.getAccountId(), acct.getDomainId(), doId, templateId, size, event.getCreateDate(), null);
        m_usageVolumeDao.persist(volumeVO);
    } else if (EventTypes.EVENT_VOLUME_DELETE.equals(event.getType())) {
        SearchCriteria<UsageVolumeVO> sc = m_usageVolumeDao.createSearchCriteria();
        sc.addAnd("accountId", SearchCriteria.Op.EQ, event.getAccountId());
        sc.addAnd("id", SearchCriteria.Op.EQ, volId);
        sc.addAnd("deleted", SearchCriteria.Op.NULL);
        List<UsageVolumeVO> volumesVOs = m_usageVolumeDao.search(sc, null);
        if (volumesVOs.size() > 1) {
            s_logger.warn("More that one usage entry for volume: " + volId + " assigned to account: " + event.getAccountId() + "; marking them all as deleted...");
        }
        for (UsageVolumeVO volumesVO : volumesVOs) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("deleting volume: " + volumesVO.getId() + " from account: " + volumesVO.getAccountId());
            }
            // there really shouldn't be more than one
            volumesVO.setDeleted(event.getCreateDate());
            m_usageVolumeDao.update(volumesVO);
        }
    }
}
Also used : Account(com.cloud.user.Account) List(java.util.List) SearchCriteria(com.cloud.utils.db.SearchCriteria)

Aggregations

Account (com.cloud.user.Account)1088 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)308 ArrayList (java.util.ArrayList)293 ActionEvent (com.cloud.event.ActionEvent)243 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)216 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)207 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)170 User (com.cloud.user.User)149 List (java.util.List)147 DB (com.cloud.utils.db.DB)130 Test (org.junit.Test)123 Pair (com.cloud.utils.Pair)115 AccountVO (com.cloud.user.AccountVO)113 Network (com.cloud.network.Network)104 Filter (com.cloud.utils.db.Filter)103 TransactionStatus (com.cloud.utils.db.TransactionStatus)95 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)94 DomainVO (com.cloud.domain.DomainVO)91 Domain (com.cloud.domain.Domain)87 UserVO (com.cloud.user.UserVO)86