Skip to content

Commit 2bd1dc1

Browse files
raveningRakesh VenkateshDaanHoogland
authored
Enable resetting config values to default value (#4230)
* Enable resetting config values to default value Provide reset button to zone,cluster,domain,account, primary and secondary storage so that config values can be reset to default value * fix ui issue * Update test/integration/smoke/test_reset_configuration_settings.py * Update test/integration/smoke/test_reset_configuration_settings.py Co-authored-by: Rakesh Venkatesh <rakeshv@apache.org> Co-authored-by: dahn <daan.hoogland@gmail.com>
1 parent e06a66b commit 2bd1dc1

File tree

12 files changed

+849
-9
lines changed

12 files changed

+849
-9
lines changed

api/src/main/java/com/cloud/configuration/ConfigurationService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.List;
2020

21+
import org.apache.cloudstack.api.command.admin.config.ResetCfgCmd;
2122
import org.apache.cloudstack.api.command.admin.config.UpdateCfgCmd;
2223
import org.apache.cloudstack.api.command.admin.network.CreateManagementNetworkIpRangeCmd;
2324
import org.apache.cloudstack.api.command.admin.network.CreateNetworkOfferingCmd;
@@ -76,6 +77,15 @@ public interface ConfigurationService {
7677
*/
7778
Configuration updateConfiguration(UpdateCfgCmd cmd) throws InvalidParameterValueException;
7879

80+
/**
81+
* Resets a configuration entry with default value
82+
*
83+
* @param cmd
84+
* - the command wrapping name parameter
85+
* @return updated configuration object if successful
86+
*/
87+
Pair<Configuration, String> resetConfiguration(ResetCfgCmd cmd) throws InvalidParameterValueException;
88+
7989
/**
8090
* Create a service offering through the API
8191
*

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@ public class ApiConstants {
714714
public static final String VM_SNAPSHOT_MEMORY = "snapshotmemory";
715715
public static final String VM_SNAPSHOT_QUIESCEVM = "quiescevm";
716716
public static final String IMAGE_STORE_UUID = "imagestoreuuid";
717+
public static final String IMAGE_STORE_ID = "imagestoreid";
717718
public static final String GUEST_VM_CIDR = "guestvmcidr";
718719
public static final String NETWORK_CIDR = "networkcidr";
719720
public static final String RESERVED_IP_RANGE = "reservediprange";
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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.admin.config;
18+
19+
import org.apache.cloudstack.api.APICommand;
20+
import org.apache.cloudstack.api.ApiArgValidator;
21+
import org.apache.cloudstack.api.ApiConstants;
22+
import org.apache.cloudstack.api.ApiErrorCode;
23+
import org.apache.cloudstack.api.BaseCmd;
24+
import org.apache.cloudstack.api.Parameter;
25+
import org.apache.cloudstack.api.ServerApiException;
26+
import org.apache.cloudstack.api.response.ImageStoreResponse;
27+
import org.apache.cloudstack.framework.config.ConfigKey;
28+
import org.apache.log4j.Logger;
29+
30+
import org.apache.cloudstack.api.response.AccountResponse;
31+
import org.apache.cloudstack.api.response.ClusterResponse;
32+
import org.apache.cloudstack.api.response.ConfigurationResponse;
33+
import org.apache.cloudstack.api.response.DomainResponse;
34+
import org.apache.cloudstack.api.response.StoragePoolResponse;
35+
import org.apache.cloudstack.api.response.ZoneResponse;
36+
import org.apache.cloudstack.config.Configuration;
37+
38+
import com.cloud.user.Account;
39+
import com.cloud.utils.Pair;
40+
41+
@APICommand(name = "resetConfiguration", description = "Resets a configuration. The configuration will be set to default value for global setting, and removed from account_details or domain_details for Account/Domain settings", responseObject = ConfigurationResponse.class,
42+
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false, since = "4.16.0")
43+
public class ResetCfgCmd extends BaseCmd {
44+
public static final Logger s_logger = Logger.getLogger(ResetCfgCmd.class.getName());
45+
private static final String s_name = "resetconfigurationresponse";
46+
47+
/////////////////////////////////////////////////////
48+
//////////////// API parameters /////////////////////
49+
/////////////////////////////////////////////////////
50+
51+
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required = true, description = "the name of the configuration", validations = {ApiArgValidator.NotNullOrEmpty})
52+
private String cfgName;
53+
54+
@Parameter(name = ApiConstants.ZONE_ID,
55+
type = CommandType.UUID,
56+
entityType = ZoneResponse.class,
57+
description = "the ID of the Zone to reset the parameter value for corresponding zone")
58+
private Long zoneId;
59+
60+
@Parameter(name = ApiConstants.CLUSTER_ID,
61+
type = CommandType.UUID,
62+
entityType = ClusterResponse.class,
63+
description = "the ID of the Cluster to reset the parameter value for corresponding cluster")
64+
private Long clusterId;
65+
66+
@Parameter(name = ApiConstants.STORAGE_ID,
67+
type = CommandType.UUID,
68+
entityType = StoragePoolResponse.class,
69+
description = "the ID of the Storage pool to reset the parameter value for corresponding storage pool")
70+
private Long storagePoolId;
71+
72+
@Parameter(name = ApiConstants.DOMAIN_ID,
73+
type = CommandType.UUID,
74+
entityType = DomainResponse.class,
75+
description = "the ID of the Domain to reset the parameter value for corresponding domain")
76+
private Long domainId;
77+
78+
@Parameter(name = ApiConstants.ACCOUNT_ID,
79+
type = CommandType.UUID,
80+
entityType = AccountResponse.class,
81+
description = "the ID of the Account to reset the parameter value for corresponding account")
82+
private Long accountId;
83+
84+
@Parameter(name = ApiConstants.IMAGE_STORE_ID,
85+
type = CommandType.UUID,
86+
entityType = ImageStoreResponse.class,
87+
description = "the ID of the Image Store to reset the parameter value for corresponding image store")
88+
private Long imageStoreId;
89+
90+
/////////////////////////////////////////////////////
91+
/////////////////// Accessors ///////////////////////
92+
/////////////////////////////////////////////////////
93+
94+
public String getCfgName() {
95+
return cfgName;
96+
}
97+
98+
public Long getZoneId() {
99+
return zoneId;
100+
}
101+
102+
public Long getClusterId() {
103+
return clusterId;
104+
}
105+
106+
public Long getStoragepoolId() {
107+
return storagePoolId;
108+
}
109+
110+
public Long getDomainId() {
111+
return domainId;
112+
}
113+
114+
public Long getAccountId() {
115+
return accountId;
116+
}
117+
118+
public Long getImageStoreId() {
119+
return imageStoreId;
120+
}
121+
122+
/////////////////////////////////////////////////////
123+
/////////////// API Implementation///////////////////
124+
/////////////////////////////////////////////////////
125+
126+
@Override
127+
public String getCommandName() {
128+
return s_name;
129+
}
130+
131+
@Override
132+
public long getEntityOwnerId() {
133+
return Account.ACCOUNT_ID_SYSTEM;
134+
}
135+
136+
@Override
137+
public void execute() {
138+
Pair<Configuration, String> cfg = _configService.resetConfiguration(this);
139+
if (cfg != null) {
140+
ConfigurationResponse response = _responseGenerator.createConfigurationResponse(cfg.first());
141+
response.setResponseName(getCommandName());
142+
if (getZoneId() != null) {
143+
response.setScope(ConfigKey.Scope.Zone.name());
144+
}
145+
if (getClusterId() != null) {
146+
response.setScope(ConfigKey.Scope.Cluster.name());
147+
}
148+
if (getStoragepoolId() != null) {
149+
response.setScope(ConfigKey.Scope.StoragePool.name());
150+
}
151+
if (getDomainId() != null) {
152+
response.setScope(ConfigKey.Scope.Domain.name());
153+
}
154+
if (getAccountId() != null) {
155+
response.setScope(ConfigKey.Scope.Account.name());
156+
}
157+
if (getImageStoreId() != null) {
158+
response.setScope(ConfigKey.Scope.ImageStore.name());
159+
}
160+
response.setValue(cfg.second());
161+
this.setResponseObject(response);
162+
} else {
163+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to reset config");
164+
}
165+
}
166+
}

engine/schema/src/main/java/com/cloud/dc/ClusterDetailsDaoImpl.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public class ClusterDetailsDaoImpl extends GenericDaoBase<ClusterDetailsVO, Long
3535
protected final SearchBuilder<ClusterDetailsVO> ClusterSearch;
3636
protected final SearchBuilder<ClusterDetailsVO> DetailSearch;
3737

38+
private final String CpuOverprovisioningFactor = "cpu.overprovisioning.factor";
39+
private final String MemoryOverprovisioningFactor = "mem.overprovisioning.factor";
40+
private final String CpuOverCommitRatio = "cpuOvercommitRatio";
41+
private final String MemoryOverCommitRatio = "memoryOvercommitRatio";
42+
3843
protected ClusterDetailsDaoImpl() {
3944
ClusterSearch = createSearchBuilder();
4045
ClusterSearch.and("clusterId", ClusterSearch.entity().getClusterId(), SearchCriteria.Op.EQ);
@@ -50,12 +55,7 @@ protected ClusterDetailsDaoImpl() {
5055
public ClusterDetailsVO findDetail(long clusterId, String name) {
5156
SearchCriteria<ClusterDetailsVO> sc = DetailSearch.create();
5257
// This is temporary fix to support list/update configuration api for cpu and memory overprovisioning ratios
53-
if (name.equalsIgnoreCase("cpu.overprovisioning.factor")) {
54-
name = "cpuOvercommitRatio";
55-
}
56-
if (name.equalsIgnoreCase("mem.overprovisioning.factor")) {
57-
name = "memoryOvercommitRatio";
58-
}
58+
name = getCpuMemoryOvercommitRatio(name);
5959
sc.setParameters("clusterId", clusterId);
6060
sc.setParameters("name", name);
6161

@@ -103,18 +103,21 @@ public void persist(long clusterId, Map<String, String> details) {
103103
expunge(sc);
104104

105105
for (Map.Entry<String, String> detail : details.entrySet()) {
106+
String name = detail.getKey();
107+
name = getCpuMemoryOvercommitRatio(name);
106108
String value = detail.getValue();
107109
if ("password".equals(detail.getKey())) {
108110
value = DBEncryptionUtil.encrypt(value);
109111
}
110-
ClusterDetailsVO vo = new ClusterDetailsVO(clusterId, detail.getKey(), value);
112+
ClusterDetailsVO vo = new ClusterDetailsVO(clusterId, name, value);
111113
persist(vo);
112114
}
113115
txn.commit();
114116
}
115117

116118
@Override
117119
public void persist(long clusterId, String name, String value) {
120+
name = getCpuMemoryOvercommitRatio(name);
118121
TransactionLegacy txn = TransactionLegacy.currentTxn();
119122
txn.start();
120123
SearchCriteria<ClusterDetailsVO> sc = DetailSearch.create();
@@ -147,4 +150,15 @@ public String getVmwareDcName(Long clusterId) {
147150
dcName = tokens[3];
148151
return dcName;
149152
}
153+
154+
private String getCpuMemoryOvercommitRatio(String name) {
155+
if (name.equalsIgnoreCase(CpuOverprovisioningFactor)) {
156+
name = CpuOverCommitRatio;
157+
}
158+
if (name.equalsIgnoreCase(MemoryOverprovisioningFactor)) {
159+
name = MemoryOverCommitRatio;
160+
}
161+
162+
return name;
163+
}
150164
}

0 commit comments

Comments
 (0)