Skip to content

Commit 275206b

Browse files
committed
Create api
1 parent ac3c49e commit 275206b

File tree

9 files changed

+320
-0
lines changed

9 files changed

+320
-0
lines changed

framework/db/src/main/java/com/cloud/utils/db/GenericSearchBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public GenericSearchBuilder<T, K> and(String name, Object field, Op op) {
8080
return this;
8181
}
8282

83+
public GenericSearchBuilder<T, K> and(String name, Op op) {
84+
return and(name, null, op);
85+
}
86+
8387
/**
8488
* Adds an AND condition. Some prefer this method because it looks like
8589
* the actual SQL query.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.quota.dao;
18+
19+
import com.cloud.utils.db.GenericDao;
20+
import org.apache.cloudstack.quota.vo.QuotaEmailConfigurationVO;
21+
22+
public interface QuotaEmailConfigurationDao extends GenericDao<QuotaEmailConfigurationVO, Long> {
23+
24+
QuotaEmailConfigurationVO findByIds(long accountId, long emailTemplateId);
25+
26+
QuotaEmailConfigurationVO updateQuotaEmailConfiguration(final QuotaEmailConfigurationVO quotaEmailConfigurationVO);
27+
28+
QuotaEmailConfigurationVO persistQuotaEmailConfiguration(QuotaEmailConfigurationVO quotaEmailConfigurationVO);
29+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.quota.dao;
18+
19+
import com.cloud.utils.db.GenericDaoBase;
20+
import com.cloud.utils.db.SearchBuilder;
21+
import com.cloud.utils.db.SearchCriteria;
22+
import com.cloud.utils.db.Transaction;
23+
import com.cloud.utils.db.TransactionCallback;
24+
import com.cloud.utils.db.TransactionLegacy;
25+
import com.cloud.utils.db.TransactionStatus;
26+
import org.apache.cloudstack.quota.vo.QuotaEmailConfigurationVO;
27+
import org.apache.log4j.Logger;
28+
29+
public class QuotaEmailConfigurationDaoImpl extends GenericDaoBase<QuotaEmailConfigurationVO, Long> implements QuotaEmailConfigurationDao {
30+
31+
private SearchBuilder<QuotaEmailConfigurationVO> searchBuilderFindByIds;
32+
33+
private SearchBuilder<QuotaEmailConfigurationVO> searchBuilderUpdate;
34+
35+
private static Logger LOGGER = Logger.getLogger(QuotaEmailConfigurationDaoImpl.class);
36+
37+
public QuotaEmailConfigurationDaoImpl() {
38+
super();
39+
searchBuilderFindByIds = createSearchBuilder();
40+
searchBuilderFindByIds.and("account_id", SearchCriteria.Op.EQ);
41+
searchBuilderFindByIds.and("email_template_id", SearchCriteria.Op.EQ);
42+
searchBuilderFindByIds.done();
43+
}
44+
45+
public QuotaEmailConfigurationVO findByIds(long accountId, long emailTemplateId) {
46+
SearchCriteria<QuotaEmailConfigurationVO> sc = searchBuilderFindByIds.create();
47+
sc.setParameters("account_id", accountId);
48+
sc.setParameters("email_template_id", emailTemplateId);
49+
return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback<QuotaEmailConfigurationVO>() {
50+
@Override public QuotaEmailConfigurationVO doInTransaction(TransactionStatus status) {
51+
return findOneBy(sc);
52+
}
53+
});
54+
}
55+
56+
public QuotaEmailConfigurationVO updateQuotaEmailConfiguration(final QuotaEmailConfigurationVO quotaEmailConfigurationVO) {
57+
SearchCriteria<QuotaEmailConfigurationVO> sc = searchBuilderFindByIds.create();
58+
sc.setParameters("account_id", quotaEmailConfigurationVO.getAccountId());
59+
sc.setParameters("email_template_id", quotaEmailConfigurationVO.getEmailTemplateId());
60+
int result = Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback<Integer>() {
61+
@Override public Integer doInTransaction(TransactionStatus status) {
62+
return update(quotaEmailConfigurationVO, sc);
63+
}
64+
});
65+
66+
if (result == 0) {
67+
LOGGER.debug(String.format("Unable to update [%s]", quotaEmailConfigurationVO));
68+
return null;
69+
}
70+
71+
return quotaEmailConfigurationVO;
72+
}
73+
74+
public QuotaEmailConfigurationVO persistQuotaEmailConfiguration(QuotaEmailConfigurationVO quotaEmailConfigurationVO) {
75+
return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback<QuotaEmailConfigurationVO>() {
76+
@Override public QuotaEmailConfigurationVO doInTransaction(TransactionStatus status) {
77+
return persist(quotaEmailConfigurationVO);
78+
}
79+
});
80+
}
81+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.quota.vo;
18+
19+
import javax.persistence.Column;
20+
import javax.persistence.Entity;
21+
import javax.persistence.Table;
22+
23+
@Entity
24+
@Table(name = "usage_network")
25+
public class QuotaEmailConfigurationVO {
26+
27+
@Column(name = "account_id")
28+
private long accountId;
29+
30+
@Column(name = "email_template_id")
31+
private long emailTemplateId;
32+
33+
@Column(name = "enable")
34+
private boolean enable;
35+
36+
public QuotaEmailConfigurationVO(long accountId, long emailTemplateId, boolean enable) {
37+
this.accountId = accountId;
38+
this.emailTemplateId = emailTemplateId;
39+
this.enable = enable;
40+
}
41+
42+
public long getAccountId() {
43+
return accountId;
44+
}
45+
46+
public void setAccountId(long accountId) {
47+
this.accountId = accountId;
48+
}
49+
50+
public long getEmailTemplateId() {
51+
return emailTemplateId;
52+
}
53+
54+
public void setEmailTemplateId(long emailTemplateId) {
55+
this.emailTemplateId = emailTemplateId;
56+
}
57+
58+
public boolean isEnable() {
59+
return enable;
60+
}
61+
62+
public void setEnable(boolean enable) {
63+
this.enable = enable;
64+
}
65+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//Licensed to the Apache Software Foundation (ASF) under one
2+
//or more contributor license agreements. See the NOTICE file
3+
//distributed with this work for additional information
4+
//regarding copyright ownership. The ASF licenses this file
5+
//to you under the Apache License, Version 2.0 (the
6+
//"License"); you may not use this file except in compliance
7+
//with the License. You may obtain a copy of the License at
8+
//
9+
//http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
//Unless required by applicable law or agreed to in writing,
12+
//software distributed under the License is distributed on an
13+
//"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
//KIND, either express or implied. See the License for the
15+
//specific language governing permissions and limitations
16+
//under the License.
17+
package org.apache.cloudstack.api.command;
18+
19+
import org.apache.cloudstack.api.APICommand;
20+
import org.apache.cloudstack.api.ApiConstants;
21+
import org.apache.cloudstack.api.BaseCmd;
22+
import org.apache.cloudstack.api.Parameter;
23+
import org.apache.cloudstack.api.response.QuotaConfigureEmailResponse;
24+
import org.apache.cloudstack.api.response.QuotaResponseBuilder;
25+
26+
import javax.inject.Inject;
27+
28+
@APICommand(name = "quotaConfigureEmail", responseObject = QuotaConfigureEmailResponse.class, description = "Configure a quota email template", since = "4.17.10",
29+
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
30+
public class QuotaConfigureEmailCmd extends BaseCmd {
31+
32+
@Parameter(name = ApiConstants.ACCOUNT_ID, type = CommandType.UUID, required = true, description = "Account ID for which to configure quota template email or min balance")
33+
private long accountId;
34+
35+
@Parameter(name = ApiConstants.TEMPLATE_NAME, type = CommandType.STRING, description = "Quota email template name which should be configured")
36+
private String templateName;
37+
38+
@Parameter(name = ApiConstants.ENABLE, type = CommandType.BOOLEAN, description = "If the quota email template should be enabled")
39+
private Boolean enable;
40+
41+
@Parameter(name = "minbalance", type = CommandType.DOUBLE, description = "New quota account min balance")
42+
private Double minBalance;
43+
44+
@Inject
45+
private QuotaResponseBuilder responseBuilder;
46+
47+
@Override public void execute() {
48+
responseBuilder.configureQuotaEmail(this);
49+
}
50+
51+
@Override public String getCommandName() {
52+
return "quotaconfigureemailresponse";
53+
}
54+
55+
@Override public long getEntityOwnerId() {
56+
return accountId;
57+
}
58+
59+
public long getAccountId() {
60+
return accountId;
61+
}
62+
63+
public String getTemplateName() {
64+
return templateName;
65+
}
66+
67+
public Boolean getEnable() {
68+
return enable;
69+
}
70+
71+
public Double getMinBalance() {
72+
return minBalance;
73+
}
74+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//Licensed to the Apache Software Foundation (ASF) under one
2+
//or more contributor license agreements. See the NOTICE file
3+
//distributed with this work for additional information
4+
//regarding copyright ownership. The ASF licenses this file
5+
//to you under the Apache License, Version 2.0 (the
6+
//"License"); you may not use this file except in compliance
7+
//with the License. You may obtain a copy of the License at
8+
//
9+
//http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
//Unless required by applicable law or agreed to in writing,
12+
//software distributed under the License is distributed on an
13+
//"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
//KIND, either express or implied. See the License for the
15+
//specific language governing permissions and limitations
16+
//under the License.
17+
package org.apache.cloudstack.api.response;
18+
19+
import org.apache.cloudstack.api.BaseResponse;
20+
21+
public class QuotaConfigureEmailResponse extends BaseResponse {
22+
}

plugins/database/quota/src/main/java/org/apache/cloudstack/api/response/QuotaResponseBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
package org.apache.cloudstack.api.response;
1818

1919
import org.apache.cloudstack.api.command.QuotaBalanceCmd;
20+
import org.apache.cloudstack.api.command.QuotaConfigureEmailCmd;
2021
import org.apache.cloudstack.api.command.QuotaEmailTemplateListCmd;
2122
import org.apache.cloudstack.api.command.QuotaEmailTemplateUpdateCmd;
2223
import org.apache.cloudstack.api.command.QuotaStatementCmd;
2324
import org.apache.cloudstack.api.command.QuotaTariffListCmd;
2425
import org.apache.cloudstack.api.command.QuotaTariffUpdateCmd;
2526
import org.apache.cloudstack.quota.vo.QuotaBalanceVO;
27+
import org.apache.cloudstack.quota.vo.QuotaEmailConfigurationVO;
2628
import org.apache.cloudstack.quota.vo.QuotaTariffVO;
2729
import org.apache.cloudstack.quota.vo.QuotaUsageVO;
2830

@@ -64,4 +66,6 @@ public interface QuotaResponseBuilder {
6466
Date startOfNextDay(Date dt);
6567

6668
Date startOfNextDay();
69+
70+
QuotaEmailConfigurationVO configureQuotaEmail(QuotaConfigureEmailCmd cmd);
6771
}

plugins/database/quota/src/main/java/org/apache/cloudstack/api/response/QuotaResponseBuilderImpl.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import javax.inject.Inject;
3535

3636
import org.apache.cloudstack.api.command.QuotaBalanceCmd;
37+
import org.apache.cloudstack.api.command.QuotaConfigureEmailCmd;
3738
import org.apache.cloudstack.api.command.QuotaEmailTemplateListCmd;
3839
import org.apache.cloudstack.api.command.QuotaEmailTemplateUpdateCmd;
3940
import org.apache.cloudstack.api.command.QuotaStatementCmd;
@@ -47,12 +48,14 @@
4748
import org.apache.cloudstack.quota.dao.QuotaAccountDao;
4849
import org.apache.cloudstack.quota.dao.QuotaBalanceDao;
4950
import org.apache.cloudstack.quota.dao.QuotaCreditsDao;
51+
import org.apache.cloudstack.quota.dao.QuotaEmailConfigurationDao;
5052
import org.apache.cloudstack.quota.dao.QuotaEmailTemplatesDao;
5153
import org.apache.cloudstack.quota.dao.QuotaTariffDao;
5254
import org.apache.cloudstack.quota.dao.QuotaUsageDao;
5355
import org.apache.cloudstack.quota.vo.QuotaAccountVO;
5456
import org.apache.cloudstack.quota.vo.QuotaBalanceVO;
5557
import org.apache.cloudstack.quota.vo.QuotaCreditsVO;
58+
import org.apache.cloudstack.quota.vo.QuotaEmailConfigurationVO;
5659
import org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO;
5760
import org.apache.cloudstack.quota.vo.QuotaTariffVO;
5861
import org.apache.cloudstack.quota.vo.QuotaUsageVO;
@@ -104,6 +107,9 @@ public class QuotaResponseBuilderImpl implements QuotaResponseBuilder {
104107
@Inject
105108
private QuotaManager _quotaManager;
106109

110+
@Inject
111+
private QuotaEmailConfigurationDao quotaEmailConfigurationDao;
112+
107113
@Override
108114
public QuotaTariffResponse createQuotaTariffResponse(QuotaTariffVO tariff) {
109115
final QuotaTariffResponse response = new QuotaTariffResponse();
@@ -541,6 +547,39 @@ public Date startOfNextDay() {
541547
return createDateAtTheStartOfNextDay(localDate);
542548
}
543549

550+
@Override
551+
public QuotaEmailConfigurationVO configureQuotaEmail(QuotaConfigureEmailCmd cmd) {
552+
if (cmd.getTemplateName() == null && cmd.getMinBalance() == null) {
553+
throw new InvalidParameterValueException("You should inform at least the minbalance or the templatename and enable parameters.");
554+
}
555+
556+
if (cmd.getTemplateName() != null && cmd.getEnable() == null) {
557+
throw new InvalidParameterValueException("If you inform the template name, you must also inform the enable parameter");
558+
}
559+
560+
if (cmd.getMinBalance() != null) {
561+
_quotaService.setMinBalance(cmd.getAccountId(), cmd.getMinBalance());
562+
}
563+
564+
if (cmd.getTemplateName() != null) {
565+
List<QuotaEmailTemplatesVO> templateVO = _quotaEmailTemplateDao.listAllQuotaEmailTemplates(cmd.getTemplateName());
566+
if (templateVO.isEmpty()) {
567+
throw new InvalidParameterValueException(String.format("Could not find template with name [%s].", cmd.getTemplateName()));
568+
}
569+
QuotaEmailConfigurationVO configurationVO = quotaEmailConfigurationDao.findByIds(cmd.getAccountId(), templateVO.get(0).getId());
570+
571+
if (configurationVO == null) {
572+
configurationVO = new QuotaEmailConfigurationVO(cmd.getAccountId(), templateVO.get(0).getId(), cmd.getEnable());
573+
return quotaEmailConfigurationDao.persistQuotaEmailConfiguration(configurationVO);
574+
}
575+
576+
configurationVO.setEnable(cmd.getEnable());
577+
return quotaEmailConfigurationDao.updateQuotaEmailConfiguration(configurationVO);
578+
}
579+
580+
return null;
581+
}
582+
544583
private Date createDateAtTheStartOfNextDay(LocalDate localDate) {
545584
LocalDate nextDayLocalDate = localDate.plusDays(1);
546585
return Date.from(nextDayLocalDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());

plugins/database/quota/src/main/java/org/apache/cloudstack/quota/QuotaServiceImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import javax.naming.ConfigurationException;
2929

3030
import org.apache.cloudstack.api.command.QuotaBalanceCmd;
31+
import org.apache.cloudstack.api.command.QuotaConfigureEmailCmd;
3132
import org.apache.cloudstack.api.command.QuotaCreditsCmd;
3233
import org.apache.cloudstack.api.command.QuotaEmailTemplateListCmd;
3334
import org.apache.cloudstack.api.command.QuotaEmailTemplateUpdateCmd;
@@ -126,6 +127,7 @@ public List<Class<?>> getCommands() {
126127
cmdList.add(QuotaCreditsCmd.class);
127128
cmdList.add(QuotaEmailTemplateListCmd.class);
128129
cmdList.add(QuotaEmailTemplateUpdateCmd.class);
130+
cmdList.add(QuotaConfigureEmailCmd.class);
129131
return cmdList;
130132
}
131133

0 commit comments

Comments
 (0)