Skip to content

Commit 3997e59

Browse files
committed
Merge release branch 4.18 to 4.19
* 4.18: Update extraconfig for platform param in xen/xcpng (apache#9248)
2 parents 81269ad + cc52b38 commit 3997e59

File tree

2 files changed

+90
-13
lines changed

2 files changed

+90
-13
lines changed

plugins/hypervisors/xenserver/src/main/java/org/apache/cloudstack/hypervisor/xenserver/ExtraConfigurationUtility.java

+38-13
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
// under the License.
1717
package org.apache.cloudstack.hypervisor.xenserver;
1818

19-
import java.util.HashMap;
2019
import java.util.Map;
2120

2221
import org.apache.log4j.Logger;
2322
import org.apache.xmlrpc.XmlRpcException;
2423

2524
import com.cloud.exception.InvalidParameterValueException;
25+
import com.cloud.utils.Pair;
2626
import com.cloud.utils.exception.CloudRuntimeException;
2727
import com.xensource.xenapi.Connection;
2828
import com.xensource.xenapi.Types;
@@ -35,16 +35,22 @@ public static void setExtraConfigurationToVm(Connection conn, VM.Record vmr, VM
3535
Map<String, Object> recordMap = vmr.toMap();
3636
for (String key : extraConfig.keySet()) {
3737
String cfg = extraConfig.get(key);
38-
Map<String, String> configParams = prepareKeyValuePair(cfg);
38+
// cfg is either param=value or map-param:key=value
39+
Pair<String, String> configParam = prepareKeyValuePair(cfg);
40+
if (configParam == null) {
41+
LOG.warn("Invalid extra config passed: " + cfg);
42+
continue;
43+
}
3944

40-
// paramKey is either param or param:key for map parameters
41-
String paramKey = configParams.keySet().toString().replaceAll("[\\[\\]]", "");
42-
String paramValue = configParams.get(paramKey);
45+
// paramKey is either param or map-param:key for map parameters
46+
String paramKey = configParam.first();
47+
String paramValue = configParam.second();
4348

44-
//Map params
4549
if (paramKey.contains(":")) {
50+
// Map params - paramKey is map-param:key
4651
applyConfigWithNestedKeyValue(conn, vm, recordMap, paramKey, paramValue);
4752
} else {
53+
// Params - paramKey is param
4854
applyConfigWithKeyValue(conn, vm, recordMap, paramKey, paramValue);
4955
}
5056
}
@@ -58,6 +64,7 @@ private static boolean isValidOperation(Map<String, Object> recordMap, String ac
5864
* Nested keys contain ":" between the paramKey and need to split into operation param and key
5965
* */
6066
private static void applyConfigWithNestedKeyValue(Connection conn, VM vm, Map<String, Object> recordMap, String paramKey, String paramValue) {
67+
// paramKey is map-param:key
6168
int i = paramKey.indexOf(":");
6269
String actualParam = paramKey.substring(0, i);
6370
String keyName = paramKey.substring(i + 1);
@@ -68,12 +75,13 @@ private static void applyConfigWithNestedKeyValue(Connection conn, VM vm, Map<St
6875
}
6976

7077
try {
78+
// map-param param with '_'
7179
switch (actualParam) {
7280
case "VCPUs_params":
7381
vm.addToVCPUsParams(conn, keyName, paramValue);
7482
break;
7583
case "platform":
76-
vm.addToOtherConfig(conn, keyName, paramValue);
84+
vm.addToPlatform(conn, keyName, paramValue);
7785
break;
7886
case "HVM_boot_params":
7987
vm.addToHVMBootParams(conn, keyName, paramValue);
@@ -101,6 +109,7 @@ private static void applyConfigWithKeyValue(Connection conn, VM vm, Map<String,
101109
}
102110

103111
try {
112+
// param with '_'
104113
switch (paramKey) {
105114
case "HVM_boot_policy":
106115
vm.setHVMBootPolicy(conn, paramValue);
@@ -144,7 +153,7 @@ private static void applyConfigWithKeyValue(Connection conn, VM vm, Map<String,
144153
case "VCPUs_at_startup":
145154
vm.setVCPUsAtStartup(conn, Long.valueOf(paramValue));
146155
break;
147-
case "is-a-template":
156+
case "is_a_template":
148157
vm.setIsATemplate(conn, Boolean.valueOf(paramValue));
149158
break;
150159
case "memory_static_max":
@@ -169,12 +178,28 @@ private static void applyConfigWithKeyValue(Connection conn, VM vm, Map<String,
169178
}
170179
}
171180

172-
private static Map<String, String> prepareKeyValuePair(String cfg) {
173-
Map<String, String> configKeyPair = new HashMap<>();
181+
protected static Pair<String, String> prepareKeyValuePair(String cfg) {
182+
// cfg is either param=value or map-param:key=value
174183
int indexOfEqualSign = cfg.indexOf("=");
175-
String key = cfg.substring(0, indexOfEqualSign).replace("-", "_");
184+
if (indexOfEqualSign <= 0) {
185+
return null;
186+
}
187+
188+
String key;
189+
// Replace '-' with '_' in param / map-param only
190+
if (cfg.contains(":")) {
191+
int indexOfColon = cfg.indexOf(":");
192+
if (indexOfColon <= 0 || indexOfEqualSign < indexOfColon) {
193+
return null;
194+
}
195+
String mapParam = cfg.substring(0, indexOfColon).replace("-", "_");
196+
String paramKey = cfg.substring(indexOfColon + 1, indexOfEqualSign);
197+
key = mapParam + ":" + paramKey;
198+
} else {
199+
key = cfg.substring(0, indexOfEqualSign).replace("-", "_");
200+
}
201+
176202
String value = cfg.substring(indexOfEqualSign + 1);
177-
configKeyPair.put(key, value);
178-
return configKeyPair;
203+
return new Pair<>(key, value);
179204
}
180205
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.cloudstack.hypervisor.xenserver;
21+
22+
import org.junit.Assert;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.mockito.junit.MockitoJUnitRunner;
26+
27+
import com.cloud.utils.Pair;
28+
29+
@RunWith(MockitoJUnitRunner.class)
30+
public class ExtraConfigurationUtilityTest {
31+
32+
@Test
33+
public void prepareKeyValuePairTest() {
34+
// Map params
35+
verifyKeyValuePairForConfigParam("platform:exp-nested-hvm=true", "platform:exp-nested-hvm", "true");
36+
verifyKeyValuePairForConfigParam("other_config:my_key=my_value", "other_config:my_key", "my_value");
37+
verifyKeyValuePairForConfigParam("test-config:test-key=test-value", "test_config:test-key", "test-value");
38+
39+
// Params
40+
verifyKeyValuePairForConfigParam("is_a_template=true", "is_a_template", "true");
41+
verifyKeyValuePairForConfigParam("is-a-template=true", "is_a_template", "true");
42+
verifyKeyValuePairForConfigParam("memory_dynamic_min=536870912", "memory_dynamic_min", "536870912");
43+
verifyKeyValuePairForConfigParam("VCPUs_at_startup=2", "VCPUs_at_startup", "2");
44+
verifyKeyValuePairForConfigParam("VCPUs-max=4", "VCPUs_max", "4");
45+
}
46+
47+
private void verifyKeyValuePairForConfigParam(String cfg, String expectedKey, String expectedValue) {
48+
Pair<String, String> keyValuePair = ExtraConfigurationUtility.prepareKeyValuePair(cfg);
49+
Assert.assertEquals(expectedKey, keyValuePair.first());
50+
Assert.assertEquals(expectedValue, keyValuePair.second());
51+
}
52+
}

0 commit comments

Comments
 (0)