Skip to content

Commit 0e8b0b8

Browse files
authored
Allow counters to be created with same name, provider and source as a deleted one (#10223)
1 parent f4b6a74 commit 0e8b0b8

File tree

7 files changed

+66
-4
lines changed

7 files changed

+66
-4
lines changed

api/src/main/java/com/cloud/network/as/AutoScaleService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public interface AutoScaleService {
7070

7171
Counter createCounter(CreateCounterCmd cmd);
7272

73+
Counter getCounter(long counterId);
74+
7375
boolean deleteCounter(long counterId) throws ResourceInUseException;
7476

7577
List<? extends Counter> listCounters(ListCountersCmd cmd);

api/src/main/java/org/apache/cloudstack/api/command/admin/autoscale/CreateCounterCmd.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.cloudstack.api.command.admin.autoscale;
1919

20+
import org.apache.cloudstack.context.CallContext;
2021

2122
import org.apache.cloudstack.api.APICommand;
2223
import org.apache.cloudstack.api.ApiCommandResourceType;
@@ -89,16 +90,18 @@ public void create() {
8990
if (ctr != null) {
9091
this.setEntityId(ctr.getId());
9192
this.setEntityUuid(ctr.getUuid());
92-
CounterResponse response = _responseGenerator.createCounterResponse(ctr);
93-
response.setResponseName(getCommandName());
94-
this.setResponseObject(response);
9593
} else {
9694
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create Counter with name " + getName());
9795
}
9896
}
9997

10098
@Override
10199
public void execute() {
100+
CallContext.current().setEventDetails("Counter ID: " + getEntityId());
101+
Counter ctr = _autoScaleService.getCounter(getEntityId());
102+
CounterResponse response = _responseGenerator.createCounterResponse(ctr);
103+
response.setResponseName(getCommandName());
104+
this.setResponseObject(response);
102105
}
103106

104107
@Override

engine/schema/src/main/java/com/cloud/network/as/dao/CounterDao.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.cloud.utils.db.GenericDao;
2525

2626
public interface CounterDao extends GenericDao<CounterVO, Long> {
27+
CounterVO findByNameProviderValue(String name, String value, String provider);
2728
public List<CounterVO> listCounters(Long id, String name, String source, String provider, String keyword, Filter filter);
2829

2930
}

engine/schema/src/main/java/com/cloud/network/as/dao/CounterDaoImpl.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
@Component
3333
public class CounterDaoImpl extends GenericDaoBase<CounterVO, Long> implements CounterDao {
3434
final SearchBuilder<CounterVO> AllFieldsSearch;
35+
final SearchBuilder<CounterVO> CounterValueSearch;
3536

3637
protected CounterDaoImpl() {
3738
AllFieldsSearch = createSearchBuilder();
@@ -40,6 +41,21 @@ protected CounterDaoImpl() {
4041
AllFieldsSearch.and("source", AllFieldsSearch.entity().getSource(), Op.EQ);
4142
AllFieldsSearch.and("provider", AllFieldsSearch.entity().getProvider(), Op.EQ);
4243
AllFieldsSearch.done();
44+
45+
CounterValueSearch = createSearchBuilder();
46+
CounterValueSearch.and("name", CounterValueSearch.entity().getName(), Op.EQ);
47+
CounterValueSearch.and("value", CounterValueSearch.entity().getValue(), Op.EQ);
48+
CounterValueSearch.and("provider", CounterValueSearch.entity().getProvider(), Op.EQ);
49+
CounterValueSearch.done();
50+
}
51+
52+
@Override
53+
public CounterVO findByNameProviderValue(String name, String value, String provider) {
54+
SearchCriteria<CounterVO> sc = CounterValueSearch.create();
55+
sc.setParameters("name", name);
56+
sc.setParameters("value", value);
57+
sc.setParameters("provider", provider);
58+
return findOneBy(sc);
4359
}
4460

4561
@Override
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
18+
-- in cloud
19+
DROP PROCEDURE IF EXISTS `cloud`.`IDEMPOTENT_DROP_UNIQUE_KEY`;
20+
21+
CREATE PROCEDURE `cloud`.`IDEMPOTENT_DROP_UNIQUE_KEY` (
22+
IN in_table_name VARCHAR(200),
23+
IN in_index_name VARCHAR(200)
24+
)
25+
BEGIN
26+
DECLARE CONTINUE HANDLER FOR 1091, 1025 BEGIN END; SET @ddl = CONCAT('ALTER TABLE ', in_table_name, ' DROP KEY ', in_index_name); PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt; END;

engine/schema/src/main/resources/META-INF/db/schema-42100to42200.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,6 @@ CREATE TABLE IF NOT EXISTS `cloud`.`import_vm_task`(
8383

8484
CALL `cloud`.`INSERT_EXTENSION_IF_NOT_EXISTS`('MaaS', 'Baremetal Extension for Canonical MaaS written in Python', 'MaaS/maas.py');
8585
CALL `cloud`.`INSERT_EXTENSION_DETAIL_IF_NOT_EXISTS`('MaaS', 'orchestratorrequirespreparevm', 'true', 0);
86+
87+
CALL `cloud`.`IDEMPOTENT_DROP_UNIQUE_KEY`('counter', 'uc_counter__provider__source__value');
88+
CALL `cloud`.`IDEMPOTENT_ADD_UNIQUE_KEY`('cloud.counter', 'uc_counter__provider__source__value__removed', '(provider, source, value, removed)');

server/src/main/java/com/cloud/network/as/AutoScaleManagerImpl.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,7 @@ public AutoScaleVmGroup disableAutoScaleVmGroup(Long id) {
14571457
public Counter createCounter(CreateCounterCmd cmd) {
14581458
String source = cmd.getSource().toUpperCase();
14591459
String name = cmd.getName();
1460+
String value = cmd.getValue();
14601461
Counter.Source src;
14611462
// Validate Source
14621463
try {
@@ -1473,13 +1474,23 @@ public Counter createCounter(CreateCounterCmd cmd) {
14731474

14741475
CounterVO counter = null;
14751476

1477+
CounterVO existingCounter = counterDao.findByNameProviderValue(name, value, provider.getName());
1478+
if (existingCounter != null) {
1479+
throw new InvalidParameterValueException(String.format("Counter with name %s and value %s already exists. ", name,value));
1480+
}
14761481
logger.debug("Adding Counter " + name);
1477-
counter = counterDao.persist(new CounterVO(src, name, cmd.getValue(), provider));
1482+
counter = counterDao.persist(new CounterVO(src, name, value, provider));
14781483

14791484
CallContext.current().setEventDetails(" Id: " + counter.getId() + " Name: " + name);
14801485
return counter;
14811486
}
14821487

1488+
@Override
1489+
@ActionEvent(eventType = EventTypes.EVENT_COUNTER_CREATE, eventDescription = "Creating a counter", async = true)
1490+
public Counter getCounter(long counterId) {
1491+
return counterDao.findById(counterId);
1492+
}
1493+
14831494
@Override
14841495
@ActionEvent(eventType = EventTypes.EVENT_CONDITION_CREATE, eventDescription = "Condition", create = true)
14851496
public Condition createCondition(CreateConditionCmd cmd) {

0 commit comments

Comments
 (0)