Search in sources :

Example 21 with VMTemplateZoneVO

use of com.cloud.storage.VMTemplateZoneVO in project cloudstack by apache.

the class VMTemplateZoneDaoImpl method deleteByZoneId.

@Override
public void deleteByZoneId(long zoneId) {
    SearchCriteria<VMTemplateZoneVO> sc = ZoneSearch.create();
    sc.setParameters("zone_id", zoneId);
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    txn.start();
    remove(sc);
    txn.commit();
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO)

Example 22 with VMTemplateZoneVO

use of com.cloud.storage.VMTemplateZoneVO in project cloudstack by apache.

the class VMTemplateZoneDaoImpl method deletePrimaryRecordsForTemplate.

@Override
public void deletePrimaryRecordsForTemplate(long templateId) {
    SearchCriteria<VMTemplateZoneVO> sc = TemplateSearch.create();
    sc.setParameters("template_id", templateId);
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    txn.start();
    remove(sc);
    txn.commit();
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO)

Example 23 with VMTemplateZoneVO

use of com.cloud.storage.VMTemplateZoneVO in project cloudstack by apache.

the class TemplateServiceImpl method associateTemplateToZone.

// persist entry in template_zone_ref table. zoneId can be empty for
// region-wide image store, in that case,
// we will associate the template to all the zones.
@Override
public void associateTemplateToZone(long templateId, Long zoneId) {
    List<Long> dcs = new ArrayList<Long>();
    if (zoneId != null) {
        dcs.add(zoneId);
    } else {
        List<DataCenterVO> zones = _dcDao.listAll();
        for (DataCenterVO zone : zones) {
            dcs.add(zone.getId());
        }
    }
    for (Long id : dcs) {
        VMTemplateZoneVO tmpltZoneVO = _vmTemplateZoneDao.findByZoneTemplate(id, templateId);
        if (tmpltZoneVO == null) {
            tmpltZoneVO = new VMTemplateZoneVO(id, templateId, new Date());
            _vmTemplateZoneDao.persist(tmpltZoneVO);
        } else {
            tmpltZoneVO.setLastUpdated(new Date());
            _vmTemplateZoneDao.update(tmpltZoneVO.getId(), tmpltZoneVO);
        }
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 24 with VMTemplateZoneVO

use of com.cloud.storage.VMTemplateZoneVO in project cloudstack by apache.

the class TemplateServiceImpl method associateCrosszoneTemplatesToZone.

// update template_zone_ref for cross-zone template for newly added zone
@Override
public void associateCrosszoneTemplatesToZone(long dcId) {
    VMTemplateZoneVO tmpltZone;
    List<VMTemplateVO> allTemplates = _templateDao.listAll();
    for (VMTemplateVO vt : allTemplates) {
        if (vt.isCrossZones()) {
            tmpltZone = _vmTemplateZoneDao.findByZoneTemplate(dcId, vt.getId());
            if (tmpltZone == null) {
                VMTemplateZoneVO vmTemplateZone = new VMTemplateZoneVO(dcId, vt.getId(), new Date());
                _vmTemplateZoneDao.persist(vmTemplateZone);
            }
        }
    }
}
Also used : VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) Date(java.util.Date)

Example 25 with VMTemplateZoneVO

use of com.cloud.storage.VMTemplateZoneVO in project cloudstack by apache.

the class BareMetalTemplateAdapter method delete.

@Override
@DB
public boolean delete(TemplateProfile profile) {
    VMTemplateVO template = profile.getTemplate();
    Long templateId = template.getId();
    boolean success = true;
    String zoneName;
    if (profile.getZoneIdList() != null && profile.getZoneIdList().size() > 1)
        throw new CloudRuntimeException("Operation is not supported for more than one zone id at a time");
    if (!template.isCrossZones() && profile.getZoneIdList() != null) {
        // get the first element in the list
        zoneName = profile.getZoneIdList().get(0).toString();
    } else {
        zoneName = "all zones";
    }
    s_logger.debug("Attempting to mark template host refs for template: " + template.getName() + " as destroyed in zone: " + zoneName);
    Account account = _accountDao.findByIdIncludingRemoved(template.getAccountId());
    String eventType = EventTypes.EVENT_TEMPLATE_DELETE;
    List<TemplateDataStoreVO> templateHostVOs = this._tmpltStoreDao.listByTemplate(templateId);
    for (TemplateDataStoreVO vo : templateHostVOs) {
        TemplateDataStoreVO lock = null;
        try {
            lock = _tmpltStoreDao.acquireInLockTable(vo.getId());
            if (lock == null) {
                s_logger.debug("Failed to acquire lock when deleting templateDataStoreVO with ID: " + vo.getId());
                success = false;
                break;
            }
            vo.setDestroyed(true);
            _tmpltStoreDao.update(vo.getId(), vo);
        } finally {
            if (lock != null) {
                _tmpltStoreDao.releaseFromLockTable(lock.getId());
            }
        }
    }
    if (profile.getZoneIdList() != null) {
        UsageEventVO usageEvent = new UsageEventVO(eventType, account.getId(), profile.getZoneIdList().get(0), templateId, null);
        _usageEventDao.persist(usageEvent);
        VMTemplateZoneVO templateZone = _tmpltZoneDao.findByZoneTemplate(profile.getZoneIdList().get(0), templateId);
        if (templateZone != null) {
            _tmpltZoneDao.remove(templateZone.getId());
        }
    } else {
        List<DataCenterVO> dcs = _dcDao.listAllIncludingRemoved();
        for (DataCenterVO dc : dcs) {
            UsageEventVO usageEvent = new UsageEventVO(eventType, account.getId(), dc.getId(), templateId, null);
            _usageEventDao.persist(usageEvent);
        }
    }
    s_logger.debug("Successfully marked template host refs for template: " + template.getName() + " as destroyed in zone: " + zoneName);
    // If there are no more non-destroyed template host entries for this template, delete it
    if (success && (_tmpltStoreDao.listByTemplate(templateId).size() == 0)) {
        long accountId = template.getAccountId();
        VMTemplateVO lock = _tmpltDao.acquireInLockTable(templateId);
        try {
            if (lock == null) {
                s_logger.debug("Failed to acquire lock when deleting template with ID: " + templateId);
                success = false;
            } else if (_tmpltDao.remove(templateId)) {
                // Decrement the number of templates and total secondary storage space used by the account.
                _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template);
                _resourceLimitMgr.recalculateResourceCount(accountId, template.getDomainId(), ResourceType.secondary_storage.getOrdinal());
            }
        } finally {
            if (lock != null) {
                _tmpltDao.releaseFromLockTable(lock.getId());
            }
        }
        s_logger.debug("Removed template: " + template.getName() + " because all of its template host refs were marked as destroyed.");
    }
    return success;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) UsageEventVO(com.cloud.event.UsageEventVO) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DB(com.cloud.utils.db.DB)

Aggregations

VMTemplateZoneVO (com.cloud.storage.VMTemplateZoneVO)25 VMTemplateVO (com.cloud.storage.VMTemplateVO)18 Date (java.util.Date)14 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)11 DB (com.cloud.utils.db.DB)10 ArrayList (java.util.ArrayList)7 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)4 Account (com.cloud.user.Account)4 Pair (com.cloud.utils.Pair)4 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)4 ExecutionException (java.util.concurrent.ExecutionException)4 DedicatedResourceVO (com.cloud.dc.DedicatedResourceVO)3 DomainVO (com.cloud.domain.DomainVO)3 ActionEvent (com.cloud.event.ActionEvent)3 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)3 CloudException (com.cloud.exception.CloudException)3 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)3 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)3 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)3