Search in sources :

Example 6 with VMTemplateVO

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

the class VMTemplateDaoImpl method updateState.

/*
     * @Override public Set<Pair<Long, Long>> searchS3Templates(final String
     * name, final String keyword, final TemplateFilter templateFilter, final
     * boolean isIso, final List<HypervisorType> hypers, final Boolean bootable,
     * final DomainVO domain, final Long pageSize, final Long startIndex, final
     * Long zoneId, final HypervisorType hyperType, final boolean onlyReady,
     * final boolean showDomr, final List<Account> permittedAccounts, final
     * Account caller, final Map<String, String> tags) {
     *
     * final String permittedAccountsStr = join(",", permittedAccounts);
     *
     * final TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start();
     *
     * Set<Pair<Long, Long>> templateZonePairList = new HashSet<Pair<Long,
     * Long>>(); PreparedStatement pstmt = null; ResultSet rs = null; try {
     *
     * final StringBuilder joinClause = new StringBuilder(); final StringBuilder
     * whereClause = new StringBuilder(" WHERE t.removed IS NULL");
     *
     * if (isIso) { whereClause.append(" AND t.format = 'ISO'"); if
     * (!hyperType.equals(HypervisorType.None)) { joinClause.append(
     * " INNER JOIN guest_os guestOS on (guestOS.id = t.guest_os_id) INNER JOIN guest_os_hypervisor goh on ( goh.guest_os_id = guestOS.id) "
     * ); whereClause.append(" AND goh.hypervisor_type = '");
     * whereClause.append(hyperType); whereClause.append("'"); } } else {
     * whereClause.append(" AND t.format <> 'ISO'"); if (hypers.isEmpty()) {
     * return templateZonePairList; } else { final StringBuilder relatedHypers =
     * new StringBuilder(); for (HypervisorType hyper : hypers) {
     * relatedHypers.append("'"); relatedHypers.append(hyper.toString());
     * relatedHypers.append("'"); relatedHypers.append(","); }
     * relatedHypers.setLength(relatedHypers.length() - 1);
     * whereClause.append(" AND t.hypervisor_type IN (");
     * whereClause.append(relatedHypers); whereClause.append(")"); } }
     *
     * joinClause.append(
     * " INNER JOIN  template_s3_ref tsr on (t.id = tsr.template_id)");
     *
     * whereClause.append("AND t.name LIKE \"%"); whereClause.append(keyword ==
     * null ? keyword : name); whereClause.append("%\"");
     *
     * if (bootable != null) { whereClause.append(" AND t.bootable = ");
     * whereClause.append(bootable); }
     *
     * if (!showDomr) { whereClause.append(" AND t.type != '");
     * whereClause.append(Storage.TemplateType.SYSTEM); whereClause.append("'");
     * }
     *
     * if (templateFilter == TemplateFilter.featured) {
     * whereClause.append(" AND t.public = 1 AND t.featured = 1"); } else if
     * ((templateFilter == TemplateFilter.self || templateFilter ==
     * TemplateFilter.selfexecutable) && caller.getType() !=
     * Account.ACCOUNT_TYPE_ADMIN) { if (caller.getType() ==
     * Account.ACCOUNT_TYPE_DOMAIN_ADMIN || caller.getType() ==
     * Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) { joinClause.append(
     * " INNER JOIN account a on (t.account_id = a.id) INNER JOIN domain d on (a.domain_id = d.id)"
     * ); whereClause.append("  AND d.path LIKE '");
     * whereClause.append(domain.getPath()); whereClause.append("%'"); } else {
     * whereClause.append(" AND t.account_id IN (");
     * whereClause.append(permittedAccountsStr); whereClause.append(")"); } }
     * else if (templateFilter == TemplateFilter.sharedexecutable &&
     * caller.getType() != Account.ACCOUNT_TYPE_ADMIN) { if (caller.getType() ==
     * Account.ACCOUNT_TYPE_NORMAL) { joinClause.append(
     * " LEFT JOIN launch_permission lp ON t.id = lp.template_id WHERE (t.account_id IN ("
     * ); joinClause.append(permittedAccountsStr);
     * joinClause.append(") OR lp.account_id IN (");
     * joinClause.append(permittedAccountsStr); joinClause.append("))"); } else
     * { joinClause.append(" INNER JOIN account a on (t.account_id = a.id) "); }
     * } else if (templateFilter == TemplateFilter.executable &&
     * !permittedAccounts.isEmpty()) {
     * whereClause.append(" AND (t.public = 1 OR t.account_id IN (");
     * whereClause.append(permittedAccountsStr); whereClause.append("))"); }
     * else if (templateFilter == TemplateFilter.community) {
     * whereClause.append(" AND t.public = 1 AND t.featured = 0"); } else if
     * (templateFilter == TemplateFilter.all && caller.getType() ==
     * Account.ACCOUNT_TYPE_ADMIN) { } else if (caller.getType() !=
     * Account.ACCOUNT_TYPE_ADMIN) { return templateZonePairList; }
     *
     * final StringBuilder sql = new StringBuilder(SELECT_TEMPLATE_S3_REF);
     * sql.append(joinClause); sql.append(whereClause);
     * sql.append(getOrderByLimit(pageSize, startIndex));
     *
     * pstmt = txn.prepareStatement(sql.toString()); rs = pstmt.executeQuery();
     * while (rs.next()) { final Pair<Long, Long> templateZonePair = new
     * Pair<Long, Long>( rs.getLong(1), -1L);
     * templateZonePairList.add(templateZonePair); } txn.commit(); } catch
     * (Exception e) { s_logger.warn("Error listing S3 templates", e); if (txn
     * != null) { txn.rollback(); } } finally { closeResources(pstmt, rs); if
     * (txn != null) { txn.close(); } }
     *
     * return templateZonePairList; }
     */
@Override
public boolean updateState(com.cloud.template.VirtualMachineTemplate.State currentState, com.cloud.template.VirtualMachineTemplate.Event event, com.cloud.template.VirtualMachineTemplate.State nextState, VirtualMachineTemplate vo, Object data) {
    Long oldUpdated = vo.getUpdatedCount();
    Date oldUpdatedTime = vo.getUpdated();
    SearchCriteria<VMTemplateVO> sc = AllFieldsSearch.create();
    sc.setParameters("id", vo.getId());
    sc.setParameters("state", currentState);
    sc.setParameters("updatedCount", vo.getUpdatedCount());
    vo.incrUpdatedCount();
    UpdateBuilder builder = getUpdateBuilder(vo);
    builder.set(vo, "state", nextState);
    builder.set(vo, "updated", new Date());
    int rows = update((VMTemplateVO) vo, sc);
    if (rows == 0 && s_logger.isDebugEnabled()) {
        VMTemplateVO dbTemplate = findByIdIncludingRemoved(vo.getId());
        if (dbTemplate != null) {
            StringBuilder str = new StringBuilder("Unable to update ").append(vo.toString());
            str.append(": DB Data={id=").append(dbTemplate.getId()).append("; state=").append(dbTemplate.getState()).append("; updatecount=").append(dbTemplate.getUpdatedCount()).append(";updatedTime=").append(dbTemplate.getUpdated());
            str.append(": New Data={id=").append(vo.getId()).append("; state=").append(nextState).append("; event=").append(event).append("; updatecount=").append(vo.getUpdatedCount()).append("; updatedTime=").append(vo.getUpdated());
            str.append(": stale Data={id=").append(vo.getId()).append("; state=").append(currentState).append("; event=").append(event).append("; updatecount=").append(oldUpdated).append("; updatedTime=").append(oldUpdatedTime);
        } else {
            s_logger.debug("Unable to update template: id=" + vo.getId() + ", as no such template exists in the database anymore");
        }
    }
    return rows > 0;
}
Also used : VMTemplateVO(com.cloud.storage.VMTemplateVO) UpdateBuilder(com.cloud.utils.db.UpdateBuilder) Date(java.util.Date)

Example 7 with VMTemplateVO

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

the class VMTemplateDaoImpl method findSystemVMReadyTemplate.

@Override
public VMTemplateVO findSystemVMReadyTemplate(long zoneId, HypervisorType hypervisorType) {
    SearchCriteria<VMTemplateVO> sc = readySystemTemplateSearch.create();
    sc.setParameters("templateType", Storage.TemplateType.SYSTEM);
    sc.setParameters("state", VirtualMachineTemplate.State.Active);
    sc.setJoinParameters("tmplHyper", "type", Host.Type.Routing);
    sc.setJoinParameters("tmplHyper", "zoneId", zoneId);
    sc.setJoinParameters("vmTemplateJoinTemplateStoreRef", "downloadState", VMTemplateStorageResourceAssoc.Status.DOWNLOADED);
    // order by descending order of id
    List<VMTemplateVO> tmplts = listBy(sc, new Filter(VMTemplateVO.class, "id", false, null, null));
    if (tmplts.size() > 0) {
        if (hypervisorType == HypervisorType.Any) {
            return tmplts.get(0);
        }
        for (VMTemplateVO tmplt : tmplts) {
            if (tmplt.getHypervisorType() == hypervisorType) {
                return tmplt;
            }
        }
    }
    return null;
}
Also used : Filter(com.cloud.utils.db.Filter) VMTemplateVO(com.cloud.storage.VMTemplateVO)

Example 8 with VMTemplateVO

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

the class VMTemplateDaoImpl method addTemplateToZone.

/*
     * @Override public Set<Pair<Long, Long>> searchSwiftTemplates(String name,
     * String keyword, TemplateFilter templateFilter, boolean isIso,
     * List<HypervisorType> hypers, Boolean bootable, DomainVO domain, Long
     * pageSize, Long startIndex, Long zoneId, HypervisorType hyperType, boolean
     * onlyReady, boolean showDomr, List<Account> permittedAccounts, Account
     * caller, Map<String, String> tags) {
     *
     * StringBuilder builder = new StringBuilder(); if
     * (!permittedAccounts.isEmpty()) { for (Account permittedAccount :
     * permittedAccounts) { builder.append(permittedAccount.getAccountId() +
     * ","); } }
     *
     * String permittedAccountsStr = builder.toString();
     *
     * if (permittedAccountsStr.length() > 0) { // chop the "," off
     * permittedAccountsStr = permittedAccountsStr.substring(0,
     * permittedAccountsStr.length() - 1); }
     *
     * TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start();
     *
     * Set<Pair<Long, Long>> templateZonePairList = new HashSet<Pair<Long,
     * Long>>(); PreparedStatement pstmt = null; ResultSet rs = null; String sql
     * = SELECT_TEMPLATE_SWIFT_REF; try { String joinClause = ""; String
     * whereClause = " WHERE t.removed IS NULL";
     *
     * if (isIso) { whereClause += " AND t.format = 'ISO'"; if
     * (!hyperType.equals(HypervisorType.None)) { joinClause =
     * " INNER JOIN guest_os guestOS on (guestOS.id = t.guest_os_id) INNER JOIN guest_os_hypervisor goh on ( goh.guest_os_id = guestOS.id) "
     * ; whereClause += " AND goh.hypervisor_type = '" + hyperType.toString() +
     * "'"; } } else { whereClause += " AND t.format <> 'ISO'"; if
     * (hypers.isEmpty()) { return templateZonePairList; } else { StringBuilder
     * relatedHypers = new StringBuilder(); for (HypervisorType hyper : hypers)
     * { relatedHypers.append("'"); relatedHypers.append(hyper.toString());
     * relatedHypers.append("'"); relatedHypers.append(","); }
     * relatedHypers.setLength(relatedHypers.length() - 1); whereClause +=
     * " AND t.hypervisor_type IN (" + relatedHypers + ")"; } } joinClause +=
     * " INNER JOIN  template_swift_ref tsr on (t.id = tsr.template_id)"; if
     * (keyword != null) { whereClause += " AND t.name LIKE \"%" + keyword +
     * "%\""; } else if (name != null) { whereClause += " AND t.name LIKE \"%" +
     * name + "%\""; }
     *
     * if (bootable != null) { whereClause += " AND t.bootable = " + bootable; }
     *
     * if (!showDomr) { whereClause += " AND t.type != '" +
     * Storage.TemplateType.SYSTEM.toString() + "'"; }
     *
     * if (templateFilter == TemplateFilter.featured) { whereClause +=
     * " AND t.public = 1 AND t.featured = 1"; } else if ((templateFilter ==
     * TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable)
     * && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) { if (caller.getType()
     * == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || caller.getType() ==
     * Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) { joinClause +=
     * " INNER JOIN account a on (t.account_id = a.id) INNER JOIN domain d on (a.domain_id = d.id)"
     * ; whereClause += "  AND d.path LIKE '" + domain.getPath() + "%'"; } else
     * { whereClause += " AND t.account_id IN (" + permittedAccountsStr + ")"; }
     * } else if ((templateFilter == TemplateFilter.shared || templateFilter ==
     * TemplateFilter.sharedexecutable) && caller.getType() !=
     * Account.ACCOUNT_TYPE_ADMIN) { if (caller.getType() ==
     * Account.ACCOUNT_TYPE_NORMAL) { joinClause +=
     * " LEFT JOIN launch_permission lp ON t.id = lp.template_id WHERE" +
     * " (t.account_id IN (" + permittedAccountsStr + ") OR" +
     * " lp.account_id IN (" + permittedAccountsStr + "))"; } else { joinClause
     * += " INNER JOIN account a on (t.account_id = a.id) "; } } else if
     * (templateFilter == TemplateFilter.executable &&
     * !permittedAccounts.isEmpty()) { whereClause +=
     * " AND (t.public = 1 OR t.account_id IN (" + permittedAccountsStr + "))";
     * } else if (templateFilter == TemplateFilter.community) { whereClause +=
     * " AND t.public = 1 AND t.featured = 0"; } else if (templateFilter ==
     * TemplateFilter.all && caller.getType() == Account.ACCOUNT_TYPE_ADMIN) { }
     * else if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) { return
     * templateZonePairList; }
     *
     * sql += joinClause + whereClause + getOrderByLimit(pageSize, startIndex);
     * pstmt = txn.prepareStatement(sql); rs = pstmt.executeQuery(); while
     * (rs.next()) { Pair<Long, Long> templateZonePair = new Pair<Long,
     * Long>(rs.getLong(1), -1L); templateZonePairList.add(templateZonePair); }
     *
     * } catch (Exception e) { s_logger.warn("Error listing templates", e); }
     * finally { try { if (rs != null) { rs.close(); } if (pstmt != null) {
     * pstmt.close(); } txn.commit(); } catch (SQLException sqle) {
     * s_logger.warn("Error in cleaning up", sqle); } }
     *
     * return templateZonePairList; }
     *
     *
     * @Override public Set<Pair<Long, Long>> searchTemplates(String name,
     * String keyword, TemplateFilter templateFilter, boolean isIso,
     * List<HypervisorType> hypers, Boolean bootable, DomainVO domain, Long
     * pageSize, Long startIndex, Long zoneId, HypervisorType hyperType, boolean
     * onlyReady, boolean showDomr,List<Account> permittedAccounts, Account
     * caller, ListProjectResourcesCriteria listProjectResourcesCriteria,
     * Map<String, String> tags, String zoneType) { StringBuilder builder = new
     * StringBuilder(); if (!permittedAccounts.isEmpty()) { for (Account
     * permittedAccount : permittedAccounts) {
     * builder.append(permittedAccount.getAccountId() + ","); } }
     *
     * String permittedAccountsStr = builder.toString();
     *
     * if (permittedAccountsStr.length() > 0) { //chop the "," off
     * permittedAccountsStr = permittedAccountsStr.substring(0,
     * permittedAccountsStr.length()-1); }
     *
     * TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start();
     *
     * // Use LinkedHashSet here to guarantee iteration order Set<Pair<Long,
     * Long>> templateZonePairList = new LinkedHashSet<Pair<Long, Long>>();
     * PreparedStatement pstmt = null; ResultSet rs = null; StringBuilder
     * relatedDomainIds = new StringBuilder(); String sql =
     * SELECT_TEMPLATE_ZONE_REF; String groupByClause = ""; try { //short
     * accountType; //String accountId = null; String guestOSJoin = "";
     * StringBuilder templateHostRefJoin = new StringBuilder(); String
     * dataCenterJoin = "", lpjoin = ""; String tagsJoin = "";
     *
     * if (isIso && !hyperType.equals(HypervisorType.None)) { guestOSJoin =
     * " INNER JOIN guest_os guestOS on (guestOS.id = t.guest_os_id) INNER JOIN guest_os_hypervisor goh on ( goh.guest_os_id = guestOS.id) "
     * ; } if (onlyReady){ templateHostRefJoin.append(
     * " INNER JOIN  template_host_ref thr on (t.id = thr.template_id) INNER JOIN host h on (thr.host_id = h.id)"
     * ); sql = SELECT_TEMPLATE_HOST_REF; groupByClause =
     * " GROUP BY t.id, h.data_center_id "; } if ((templateFilter ==
     * TemplateFilter.featured) || (templateFilter == TemplateFilter.community))
     * { dataCenterJoin =
     * " INNER JOIN data_center dc on (h.data_center_id = dc.id)"; }
     *
     * if (zoneType != null) { dataCenterJoin =
     * " INNER JOIN template_host_ref thr on (t.id = thr.template_id) INNER JOIN host h on (thr.host_id = h.id)"
     * ; dataCenterJoin +=
     * " INNER JOIN data_center dc on (h.data_center_id = dc.id)"; }
     *
     * if (templateFilter == TemplateFilter.sharedexecutable || templateFilter
     * == TemplateFilter.shared ){ lpjoin =
     * " INNER JOIN launch_permission lp ON t.id = lp.template_id "; }
     *
     * if (tags != null && !tags.isEmpty()) { tagsJoin =
     * " INNER JOIN resource_tags r ON t.id = r.resource_id "; }
     *
     * sql += guestOSJoin + templateHostRefJoin + dataCenterJoin + lpjoin +
     * tagsJoin; String whereClause = "";
     *
     * //All joins have to be made before we start setting the condition
     * settings if ((listProjectResourcesCriteria ==
     * ListProjectResourcesCriteria.SkipProjectResources ||
     * (!permittedAccounts.isEmpty() && !(templateFilter ==
     * TemplateFilter.community || templateFilter == TemplateFilter.featured)))
     * && !(caller.getType() != Account.ACCOUNT_TYPE_NORMAL && templateFilter ==
     * TemplateFilter.all)) { whereClause +=
     * " INNER JOIN account a on (t.account_id = a.id)"; if ((templateFilter ==
     * TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable)
     * && (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN ||
     * caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN)) {
     * whereClause +=
     * " INNER JOIN domain d on (a.domain_id = d.id) WHERE d.path LIKE '" +
     * domain.getPath() + "%'"; if (listProjectResourcesCriteria ==
     * ListProjectResourcesCriteria.SkipProjectResources) { whereClause +=
     * " AND a.type != " + Account.ACCOUNT_TYPE_PROJECT; } } else if
     * (listProjectResourcesCriteria ==
     * ListProjectResourcesCriteria.SkipProjectResources) { whereClause +=
     * " WHERE a.type != " + Account.ACCOUNT_TYPE_PROJECT; } }
     *
     * if (!permittedAccounts.isEmpty()) { for (Account account :
     * permittedAccounts) { //accountType = account.getType(); //accountId =
     * Long.toString(account.getId()); DomainVO accountDomain =
     * _domainDao.findById(account.getDomainId());
     *
     * // get all parent domain ID's all the way till root domain DomainVO
     * domainTreeNode = accountDomain; while (true) {
     * relatedDomainIds.append(domainTreeNode.getId());
     * relatedDomainIds.append(","); if (domainTreeNode.getParent() != null) {
     * domainTreeNode = _domainDao.findById(domainTreeNode.getParent()); } else
     * { break; } }
     *
     * // get all child domain ID's if (isAdmin(account.getType()) ) {
     * List<DomainVO> allChildDomains =
     * _domainDao.findAllChildren(accountDomain.getPath(),
     * accountDomain.getId()); for (DomainVO childDomain : allChildDomains) {
     * relatedDomainIds.append(childDomain.getId());
     * relatedDomainIds.append(","); } }
     * relatedDomainIds.setLength(relatedDomainIds.length()-1); } }
     *
     * String attr = " AND "; if (whereClause.endsWith(" WHERE ")) { attr +=
     * " WHERE "; }
     *
     * if (!isIso) { if ( hypers.isEmpty() ) { return templateZonePairList; }
     * else { StringBuilder relatedHypers = new StringBuilder(); for
     * (HypervisorType hyper : hypers ) { relatedHypers.append("'");
     * relatedHypers.append(hyper.toString()); relatedHypers.append("'");
     * relatedHypers.append(","); }
     * relatedHypers.setLength(relatedHypers.length()-1); whereClause += attr +
     * " t.hypervisor_type IN (" + relatedHypers + ")"; } }
     *
     * if (!permittedAccounts.isEmpty() && !(templateFilter ==
     * TemplateFilter.featured || templateFilter == TemplateFilter.community ||
     * templateFilter == TemplateFilter.executable || templateFilter ==
     * TemplateFilter.shared || templateFilter ==
     * TemplateFilter.sharedexecutable) && !isAdmin(caller.getType()) ) {
     * whereClause += attr + "t.account_id IN (" + permittedAccountsStr + ")"; }
     *
     * if (templateFilter == TemplateFilter.featured) { whereClause += attr +
     * "t.public = 1 AND t.featured = 1"; if (!permittedAccounts.isEmpty()) {
     * whereClause += attr + "(dc.domain_id IN (" + relatedDomainIds +
     * ") OR dc.domain_id is NULL)"; } } else if (templateFilter ==
     * TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable) {
     * whereClause += " AND t.account_id IN (" + permittedAccountsStr + ")"; }
     * else if (templateFilter == TemplateFilter.sharedexecutable ||
     * templateFilter == TemplateFilter.shared ) { whereClause += " AND " +
     * " (t.account_id IN (" + permittedAccountsStr + ") OR" +
     * " lp.account_id IN (" + permittedAccountsStr + "))"; } else if
     * (templateFilter == TemplateFilter.executable &&
     * !permittedAccounts.isEmpty()) { whereClause += attr +
     * "(t.public = 1 OR t.account_id IN (" + permittedAccountsStr + "))"; }
     * else if (templateFilter == TemplateFilter.community) { whereClause +=
     * attr + "t.public = 1 AND t.featured = 0"; if
     * (!permittedAccounts.isEmpty()) { whereClause += attr +
     * "(dc.domain_id IN (" + relatedDomainIds + ") OR dc.domain_id is NULL)"; }
     * } else if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN && !isIso) {
     * return templateZonePairList; }
     *
     * if (tags != null && !tags.isEmpty()) { whereClause += " AND ("; boolean
     * first = true; for (String key : tags.keySet()) { if (!first) {
     * whereClause += " OR "; } whereClause += "(r.key=\"" + key +
     * "\" and r.value=\"" + tags.get(key) + "\")"; first = false; } whereClause
     * += ")"; }
     *
     * if (whereClause.equals("")) { whereClause += " WHERE "; } else if
     * (!whereClause.equals(" WHERE ")) { whereClause += " AND "; }
     *
     * sql += whereClause + getExtrasWhere(templateFilter, name, keyword, isIso,
     * bootable, hyperType, zoneId, onlyReady, showDomr, zoneType) +
     * groupByClause + getOrderByLimit(pageSize, startIndex);
     *
     * pstmt = txn.prepareStatement(sql); rs = pstmt.executeQuery();
     *
     * while (rs.next()) { Pair<Long, Long> templateZonePair = new Pair<Long,
     * Long>(rs.getLong(1), rs.getLong(2));
     * templateZonePairList.add(templateZonePair); } //for now, defaulting
     * pageSize to a large val if null; may need to revisit post 2.2RC2 if(isIso
     * && templateZonePairList.size() < (pageSize != null ? pageSize : 500) &&
     * templateFilter != TemplateFilter.community && !(templateFilter ==
     * TemplateFilter.self && !BaseCmd.isRootAdmin(caller.getType())) ){
     * //evaluates to true If root admin and filter=self
     *
     * List<VMTemplateVO> publicIsos = publicIsoSearch(bootable, false, tags);
     * List<VMTemplateVO> userIsos = userIsoSearch(false);
     *
     * //Listing the ISOs according to the page size.Restricting the total no.
     * of ISOs on a page //to be less than or equal to the pageSize parameter
     *
     * int i=0;
     *
     * if (startIndex > userIsos.size()) { i=(int) (startIndex -
     * userIsos.size()); }
     *
     * for (; i < publicIsos.size(); i++) { if(templateZonePairList.size() >=
     * pageSize){ break; } else { if (keyword != null &&
     * publicIsos.get(i).getName().contains(keyword)) {
     * templateZonePairList.add(new Pair<Long,Long>(publicIsos.get(i).getId(),
     * null)); continue; } else if (name != null &&
     * publicIsos.get(i).getName().contains(name)) {
     * templateZonePairList.add(new Pair<Long,Long>(publicIsos.get(i).getId(),
     * null)); continue; } else if (keyword == null && name == null){
     * templateZonePairList.add(new Pair<Long,Long>(publicIsos.get(i).getId(),
     * null)); } } } } } catch (Exception e) {
     * s_logger.warn("Error listing templates", e); } finally { try { if (rs !=
     * null) { rs.close(); } if (pstmt != null) { pstmt.close(); } txn.commit();
     * } catch( SQLException sqle) { s_logger.warn("Error in cleaning up",
     * sqle); } }
     *
     * return templateZonePairList; }
     */
/*
     * private String getExtrasWhere(TemplateFilter templateFilter, String name,
     * String keyword, boolean isIso, Boolean bootable, HypervisorType
     * hyperType, Long zoneId, boolean onlyReady, boolean showDomr, String
     * zoneType) { String sql = ""; if (keyword != null) { sql +=
     * " t.name LIKE \"%" + keyword + "%\" AND"; } else if (name != null) { sql
     * += " t.name LIKE \"%" + name + "%\" AND"; }
     *
     * if (isIso) { sql += " t.format = 'ISO'"; if
     * (!hyperType.equals(HypervisorType.None)) { sql +=
     * " AND goh.hypervisor_type = '" + hyperType.toString() + "'"; } } else {
     * sql += " t.format <> 'ISO'"; if (!hyperType.equals(HypervisorType.None))
     * { sql += " AND t.hypervisor_type = '" + hyperType.toString() + "'"; } }
     *
     * if (bootable != null) { sql += " AND t.bootable = " + bootable; }
     *
     * if (onlyReady){ sql += " AND thr.download_state = '"
     * +Status.DOWNLOADED.toString() + "'" + " AND thr.destroyed=0 "; if (zoneId
     * != null){ sql += " AND h.data_center_id = " +zoneId; } }else if (zoneId
     * != null){ sql += " AND tzr.zone_id = " +zoneId+
     * " AND tzr.removed is null" ; }else{ sql += " AND tzr.removed is null "; }
     *
     * if (zoneType != null){ sql += " AND dc.networktype = '" + zoneType + "'";
     * }
     *
     * if (!showDomr){ sql += " AND t.type != '"
     * +Storage.TemplateType.SYSTEM.toString() + "'"; }
     *
     * sql += " AND t.removed IS NULL";
     *
     * return sql; }
     *
     * private String getOrderByLimit(Long pageSize, Long startIndex) { Boolean
     * isAscending =
     * Boolean.parseBoolean(_configDao.getValue("sortkey.algorithm"));
     * isAscending = (isAscending == null ? true : isAscending);
     *
     * String sql; if (isAscending) { sql = " ORDER BY t.sort_key ASC"; } else {
     * sql = " ORDER BY t.sort_key DESC"; }
     *
     * if ((pageSize != null) && (startIndex != null)) { sql += " LIMIT " +
     * startIndex.toString() + "," + pageSize.toString(); } return sql; }
     */
@SuppressWarnings("unchecked")
@Override
@DB
public long addTemplateToZone(VMTemplateVO tmplt, long zoneId) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    txn.start();
    VMTemplateVO tmplt2 = findById(tmplt.getId());
    if (tmplt2 == null) {
        if (persist(tmplt) == null) {
            throw new CloudRuntimeException("Failed to persist the template " + tmplt);
        }
        if (tmplt.getDetails() != null) {
            List<VMTemplateDetailVO> details = new ArrayList<VMTemplateDetailVO>();
            for (String key : tmplt.getDetails().keySet()) {
                details.add(new VMTemplateDetailVO(tmplt.getId(), key, tmplt.getDetails().get(key), true));
            }
            _templateDetailsDao.saveDetails(details);
        }
    }
    VMTemplateZoneVO tmpltZoneVO = _templateZoneDao.findByZoneTemplate(zoneId, tmplt.getId());
    if (tmpltZoneVO == null) {
        tmpltZoneVO = new VMTemplateZoneVO(zoneId, tmplt.getId(), new Date());
        _templateZoneDao.persist(tmpltZoneVO);
    } else {
        tmpltZoneVO.setRemoved(null);
        tmpltZoneVO.setLastUpdated(new Date());
        _templateZoneDao.update(tmpltZoneVO.getId(), tmpltZoneVO);
    }
    txn.commit();
    return tmplt.getId();
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) VMTemplateDetailVO(com.cloud.storage.VMTemplateDetailVO) VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMTemplateVO(com.cloud.storage.VMTemplateVO) ArrayList(java.util.ArrayList) Date(java.util.Date) DB(com.cloud.utils.db.DB)

Example 9 with VMTemplateVO

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

the class VMTemplateDaoImpl method findRoutingTemplate.

@Override
public VMTemplateVO findRoutingTemplate(HypervisorType hType, String templateName) {
    SearchCriteria<VMTemplateVO> sc = tmpltTypeHyperSearch2.create();
    sc.setParameters("templateType", TemplateType.ROUTING);
    sc.setParameters("hypervisorType", hType);
    if (templateName != null) {
        sc.setParameters("templateName", templateName);
    }
    // order by descending order of id and select the first (this is going
    // to be the latest)
    List<VMTemplateVO> tmplts = listBy(sc, new Filter(VMTemplateVO.class, "id", false, null, 1l));
    if (tmplts.size() > 0) {
        return tmplts.get(0);
    } else {
        sc = tmpltTypeHyperSearch2.create();
        sc.setParameters("templateType", TemplateType.SYSTEM);
        sc.setParameters("hypervisorType", hType);
        if (templateName != null) {
            sc.setParameters("templateName", templateName);
        }
        // order by descending order of id and select the first (this is going
        // to be the latest)
        tmplts = listBy(sc, new Filter(VMTemplateVO.class, "id", false, null, 1l));
        if (tmplts.size() > 0) {
            return tmplts.get(0);
        } else {
            return null;
        }
    }
}
Also used : Filter(com.cloud.utils.db.Filter) VMTemplateVO(com.cloud.storage.VMTemplateVO)

Example 10 with VMTemplateVO

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

the class LaunchPermissionDaoImpl method listPermittedTemplates.

@Override
public List<VMTemplateVO> listPermittedTemplates(long accountId) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    List<VMTemplateVO> permittedTemplates = new ArrayList<VMTemplateVO>();
    PreparedStatement pstmt = null;
    try {
        String sql = LIST_PERMITTED_TEMPLATES;
        pstmt = txn.prepareAutoCloseStatement(sql);
        pstmt.setLong(1, accountId);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            long id = rs.getLong(1);
            String uniqueName = rs.getString(2);
            String name = rs.getString(3);
            boolean isPublic = rs.getBoolean(4);
            String value = rs.getString(5);
            ImageFormat format = ImageFormat.valueOf(value);
            String tmpltType = rs.getString(6);
            boolean requiresHVM = rs.getBoolean(7);
            int bits = rs.getInt(8);
            String url = rs.getString(9);
            String createdTS = rs.getString(10);
            long templateAccountId = rs.getLong(11);
            String checksum = rs.getString(12);
            String displayText = rs.getString(13);
            boolean enablePassword = rs.getBoolean(14);
            long guestOSId = rs.getLong(15);
            boolean featured = rs.getBoolean(16);
            Date createdDate = null;
            if (createdTS != null) {
                createdDate = DateUtil.parseDateString(s_gmtTimeZone, createdTS);
            }
            if (isPublic) {
                // if it's public already, skip adding it to
                continue;
            // permitted templates as this for private
            // templates only
            }
            VMTemplateVO template = new VMTemplateVO(id, uniqueName, name, format, isPublic, featured, TemplateType.valueOf(tmpltType), url, createdDate, requiresHVM, bits, templateAccountId, checksum, displayText, enablePassword, guestOSId, true, null);
            permittedTemplates.add(template);
        }
    } catch (Exception e) {
        s_logger.warn("Error listing permitted templates", e);
    }
    return permittedTemplates;
}
Also used : ArrayList(java.util.ArrayList) VMTemplateVO(com.cloud.storage.VMTemplateVO) PreparedStatement(java.sql.PreparedStatement) Date(java.util.Date) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ImageFormat(com.cloud.storage.Storage.ImageFormat) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) ResultSet(java.sql.ResultSet)

Aggregations

VMTemplateVO (com.cloud.storage.VMTemplateVO)110 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)28 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)27 Account (com.cloud.user.Account)27 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)24 TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)22 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)17 Date (java.util.Date)16 ArrayList (java.util.ArrayList)15 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)13 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)13 DataCenterVO (com.cloud.dc.DataCenterVO)12 ActionEvent (com.cloud.event.ActionEvent)11 TemplateProfile (com.cloud.storage.TemplateProfile)11 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)11 VolumeVO (com.cloud.storage.VolumeVO)11 Test (org.junit.Test)11 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)10 VMTemplateZoneVO (com.cloud.storage.VMTemplateZoneVO)9 DB (com.cloud.utils.db.DB)9