Skip to content

Refactor Quota Summary API #10505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
public class ApiConstants {
public static final String ACCOUNT = "account";
public static final String ACCOUNTS = "accounts";
public static final String ACCOUNT_STATE_TO_SHOW = "accountstatetoshow";
public static final String ACCOUNT_TYPE = "accounttype";
public static final String ACCOUNT_ID = "accountid";
public static final String ACCOUNT_IDS = "accountids";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public boolean isChildDomain(Long parentId, Long childId) {
SearchCriteria<DomainVO> sc = DomainPairSearch.create();
sc.setParameters("id", parentId, childId);

List<DomainVO> domainPair = listBy(sc);
List<DomainVO> domainPair = listIncludingRemovedBy(sc);

if ((domainPair != null) && (domainPair.size() == 2)) {
DomainVO d1 = domainPair.get(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.

-- cloud_usage.quota_summary_view source

-- Create view for quota summary
DROP VIEW IF EXISTS `cloud_usage`.`quota_summary_view`;
CREATE VIEW `cloud_usage`.`quota_summary_view` AS
SELECT
cloud_usage.quota_account.account_id AS account_id,
cloud_usage.quota_account.quota_balance AS quota_balance,
cloud_usage.quota_account.quota_balance_date AS quota_balance_date,
cloud_usage.quota_account.quota_enforce AS quota_enforce,
cloud_usage.quota_account.quota_min_balance AS quota_min_balance,
cloud_usage.quota_account.quota_alert_date AS quota_alert_date,
cloud_usage.quota_account.quota_alert_type AS quota_alert_type,
cloud_usage.quota_account.last_statement_date AS last_statement_date,
cloud.account.uuid AS account_uuid,
cloud.account.account_name AS account_name,
cloud.account.state AS account_state,
cloud.account.removed AS account_removed,
cloud.domain.id AS domain_id,
cloud.domain.uuid AS domain_uuid,
cloud.domain.name AS domain_name,
cloud.domain.path AS domain_path,
cloud.domain.removed AS domain_removed,
cloud.projects.uuid AS project_uuid,
cloud.projects.name AS project_name,
cloud.projects.removed AS project_removed
FROM
cloud_usage.quota_account
INNER JOIN cloud.account ON (cloud.account.id = cloud_usage.quota_account.account_id)
INNER JOIN cloud.domain ON (cloud.domain.id = cloud.account.domain_id)
LEFT JOIN cloud.projects ON (cloud.account.type = 5 AND cloud.account.id = cloud.projects.project_account_id);
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.quota;

public enum QuotaAccountStateFilter {
ALL, ACTIVE, REMOVED;

public static QuotaAccountStateFilter getValue(String value) {
for (QuotaAccountStateFilter state : values()) {
if (state.name().equalsIgnoreCase(value)) {
return state;
}
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.cloudstack.quota.dao;

import java.util.List;

import org.apache.cloudstack.quota.QuotaAccountStateFilter;
import org.apache.cloudstack.quota.vo.QuotaSummaryVO;

import com.cloud.utils.Pair;
import com.cloud.utils.db.GenericDao;

public interface QuotaSummaryDao extends GenericDao<QuotaSummaryVO, Long> {

Pair<List<QuotaSummaryVO>, Integer> listQuotaSummariesForAccountAndOrDomain(Long accountId, String accountName, Long domainId, String domainPath,
QuotaAccountStateFilter accountStateFilter, Long startIndex, Long pageSize);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.cloudstack.quota.dao;

import java.util.List;

import org.apache.cloudstack.quota.QuotaAccountStateFilter;
import org.apache.cloudstack.quota.vo.QuotaSummaryVO;

import com.cloud.utils.Pair;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.db.TransactionCallback;
import com.cloud.utils.db.TransactionLegacy;

public class QuotaSummaryDaoImpl extends GenericDaoBase<QuotaSummaryVO, Long> implements QuotaSummaryDao {

@Override
public Pair<List<QuotaSummaryVO>, Integer> listQuotaSummariesForAccountAndOrDomain(Long accountId, String accountName, Long domainId, String domainPath,
QuotaAccountStateFilter accountStateFilter, Long startIndex, Long pageSize) {
SearchCriteria<QuotaSummaryVO> searchCriteria = createListQuotaSummariesSearchCriteria(accountId, accountName, domainId, domainPath, accountStateFilter);
Filter filter = new Filter(QuotaSummaryVO.class, "accountName", true, startIndex, pageSize);

return Transaction.execute(TransactionLegacy.USAGE_DB, (TransactionCallback<Pair<List<QuotaSummaryVO>, Integer>>) status -> searchAndCount(searchCriteria, filter));
}

protected SearchCriteria<QuotaSummaryVO> createListQuotaSummariesSearchCriteria(Long accountId, String accountName, Long domainId, String domainPath,
QuotaAccountStateFilter accountStateFilter) {
SearchCriteria<QuotaSummaryVO> searchCriteria = createListQuotaSummariesSearchBuilder(accountStateFilter).create();

searchCriteria.setParametersIfNotNull("accountId", accountId);
searchCriteria.setParametersIfNotNull("domainId", domainId);

if (accountName != null) {
searchCriteria.setParameters("accountName", "%" + accountName + "%");
}

if (domainPath != null) {
searchCriteria.setParameters("domainPath", domainPath + "%");
}

return searchCriteria;
}

protected SearchBuilder<QuotaSummaryVO> createListQuotaSummariesSearchBuilder(QuotaAccountStateFilter accountStateFilter) {
SearchBuilder<QuotaSummaryVO> searchBuilder = createSearchBuilder();

searchBuilder.and("accountId", searchBuilder.entity().getAccountId(), SearchCriteria.Op.EQ);
searchBuilder.and("accountName", searchBuilder.entity().getAccountName(), SearchCriteria.Op.LIKE);
searchBuilder.and("domainId", searchBuilder.entity().getDomainId(), SearchCriteria.Op.EQ);
searchBuilder.and("domainPath", searchBuilder.entity().getDomainPath(), SearchCriteria.Op.LIKE);

if (QuotaAccountStateFilter.REMOVED.equals(accountStateFilter)) {
searchBuilder.and("accountRemoved", searchBuilder.entity().getAccountRemoved(), SearchCriteria.Op.NNULL);
} else if (QuotaAccountStateFilter.ACTIVE.equals(accountStateFilter)) {
searchBuilder.and("accountRemoved", searchBuilder.entity().getAccountRemoved(), SearchCriteria.Op.NULL);
}

return searchBuilder;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.cloudstack.quota.vo;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.cloud.user.Account;

@Entity
@Table(name = "quota_summary_view")
public class QuotaSummaryVO {

@Id
@Column(name = "account_id")
private Long accountId = null;

@Column(name = "quota_enforce")
private Integer quotaEnforce = 0;

@Column(name = "quota_balance")
private BigDecimal quotaBalance;

@Column(name = "quota_balance_date")
@Temporal(value = TemporalType.TIMESTAMP)
private Date quotaBalanceDate = null;

@Column(name = "quota_min_balance")
private BigDecimal quotaMinBalance;

@Column(name = "quota_alert_type")
private Integer quotaAlertType = null;

@Column(name = "quota_alert_date")
@Temporal(value = TemporalType.TIMESTAMP)
private Date quotaAlertDate = null;

@Column(name = "last_statement_date")
@Temporal(value = TemporalType.TIMESTAMP)
private Date lastStatementDate = null;

@Column(name = "account_uuid")
private String accountUuid;

@Column(name = "account_name")
private String accountName;

@Column(name = "account_state")
@Enumerated(EnumType.STRING)
private Account.State accountState;

@Column(name = "account_removed")
private Date accountRemoved;

@Column(name = "domain_id")
private Long domainId;

@Column(name = "domain_uuid")
private String domainUuid;

@Column(name = "domain_name")
private String domainName;

@Column(name = "domain_path")
private String domainPath;

@Column(name = "domain_removed")
private Date domainRemoved;

@Column(name = "project_uuid")
private String projectUuid;

@Column(name = "project_name")
private String projectName;

@Column(name = "project_removed")
private Date projectRemoved;

public Long getAccountId() {
return accountId;
}

public BigDecimal getQuotaBalance() {
return quotaBalance;
}

public String getAccountUuid() {
return accountUuid;
}

public String getAccountName() {
return accountName;
}

public Date getAccountRemoved() {
return accountRemoved;
}

public Account.State getAccountState() {
return accountState;
}

public Long getDomainId() {
return domainId;
}

public String getDomainUuid() {
return domainUuid;
}

public String getDomainPath() {
return domainPath;
}

public Date getDomainRemoved() {
return domainRemoved;
}

public String getProjectUuid() {
return projectUuid;
}

public String getProjectName() {
return projectName;
}

public Date getProjectRemoved() {
return projectRemoved;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

<bean id="presetVariableHelper" class="org.apache.cloudstack.quota.activationrule.presetvariables.PresetVariableHelper" />
<bean id="QuotaTariffDao" class="org.apache.cloudstack.quota.dao.QuotaTariffDaoImpl" />
<bean id="QuotaAccountDao" class="org.apache.cloudstack.quota.dao.QuotaAccountDaoImpl" />
<bean id="QuotaSummaryDao" class="org.apache.cloudstack.quota.dao.QuotaSummaryDaoImpl" />
<bean id="QuotaAccountDao" class="org.apache.cloudstack.quota.dao.QuotaAccountDaoImpl" />
<bean id="QuotaBalanceDao" class="org.apache.cloudstack.quota.dao.QuotaBalanceDaoImpl" />
<bean id="QuotaCreditsDao" class="org.apache.cloudstack.quota.dao.QuotaCreditsDaoImpl" />
<bean id="QuotaEmailTemplatesDao"
Expand Down
Loading
Loading