mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Update extraconfig for platform param in xen/xcpng (#9248)
* Update extraconfig for platform param in xen/xcpng * Fix map param key, not to replace '-' with '_' (replace only applicable to param / map-param) * Added unit tests * Add license for tests file
This commit is contained in:
parent
7c5b7ca077
commit
cc52b38e54
@ -16,13 +16,13 @@
|
|||||||
// under the License.
|
// under the License.
|
||||||
package org.apache.cloudstack.hypervisor.xenserver;
|
package org.apache.cloudstack.hypervisor.xenserver;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.apache.xmlrpc.XmlRpcException;
|
import org.apache.xmlrpc.XmlRpcException;
|
||||||
|
|
||||||
import com.cloud.exception.InvalidParameterValueException;
|
import com.cloud.exception.InvalidParameterValueException;
|
||||||
|
import com.cloud.utils.Pair;
|
||||||
import com.cloud.utils.exception.CloudRuntimeException;
|
import com.cloud.utils.exception.CloudRuntimeException;
|
||||||
import com.xensource.xenapi.Connection;
|
import com.xensource.xenapi.Connection;
|
||||||
import com.xensource.xenapi.Types;
|
import com.xensource.xenapi.Types;
|
||||||
@ -35,16 +35,22 @@ public class ExtraConfigurationUtility {
|
|||||||
Map<String, Object> recordMap = vmr.toMap();
|
Map<String, Object> recordMap = vmr.toMap();
|
||||||
for (String key : extraConfig.keySet()) {
|
for (String key : extraConfig.keySet()) {
|
||||||
String cfg = extraConfig.get(key);
|
String cfg = extraConfig.get(key);
|
||||||
Map<String, String> configParams = prepareKeyValuePair(cfg);
|
// cfg is either param=value or map-param:key=value
|
||||||
|
Pair<String, String> configParam = prepareKeyValuePair(cfg);
|
||||||
|
if (configParam == null) {
|
||||||
|
LOG.warn("Invalid extra config passed: " + cfg);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// paramKey is either param or param:key for map parameters
|
// paramKey is either param or map-param:key for map parameters
|
||||||
String paramKey = configParams.keySet().toString().replaceAll("[\\[\\]]", "");
|
String paramKey = configParam.first();
|
||||||
String paramValue = configParams.get(paramKey);
|
String paramValue = configParam.second();
|
||||||
|
|
||||||
//Map params
|
|
||||||
if (paramKey.contains(":")) {
|
if (paramKey.contains(":")) {
|
||||||
|
// Map params - paramKey is map-param:key
|
||||||
applyConfigWithNestedKeyValue(conn, vm, recordMap, paramKey, paramValue);
|
applyConfigWithNestedKeyValue(conn, vm, recordMap, paramKey, paramValue);
|
||||||
} else {
|
} else {
|
||||||
|
// Params - paramKey is param
|
||||||
applyConfigWithKeyValue(conn, vm, recordMap, paramKey, paramValue);
|
applyConfigWithKeyValue(conn, vm, recordMap, paramKey, paramValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,6 +64,7 @@ public class ExtraConfigurationUtility {
|
|||||||
* Nested keys contain ":" between the paramKey and need to split into operation param and key
|
* Nested keys contain ":" between the paramKey and need to split into operation param and key
|
||||||
* */
|
* */
|
||||||
private static void applyConfigWithNestedKeyValue(Connection conn, VM vm, Map<String, Object> recordMap, String paramKey, String paramValue) {
|
private static void applyConfigWithNestedKeyValue(Connection conn, VM vm, Map<String, Object> recordMap, String paramKey, String paramValue) {
|
||||||
|
// paramKey is map-param:key
|
||||||
int i = paramKey.indexOf(":");
|
int i = paramKey.indexOf(":");
|
||||||
String actualParam = paramKey.substring(0, i);
|
String actualParam = paramKey.substring(0, i);
|
||||||
String keyName = paramKey.substring(i + 1);
|
String keyName = paramKey.substring(i + 1);
|
||||||
@ -68,12 +75,13 @@ public class ExtraConfigurationUtility {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// map-param param with '_'
|
||||||
switch (actualParam) {
|
switch (actualParam) {
|
||||||
case "VCPUs_params":
|
case "VCPUs_params":
|
||||||
vm.addToVCPUsParams(conn, keyName, paramValue);
|
vm.addToVCPUsParams(conn, keyName, paramValue);
|
||||||
break;
|
break;
|
||||||
case "platform":
|
case "platform":
|
||||||
vm.addToOtherConfig(conn, keyName, paramValue);
|
vm.addToPlatform(conn, keyName, paramValue);
|
||||||
break;
|
break;
|
||||||
case "HVM_boot_params":
|
case "HVM_boot_params":
|
||||||
vm.addToHVMBootParams(conn, keyName, paramValue);
|
vm.addToHVMBootParams(conn, keyName, paramValue);
|
||||||
@ -101,6 +109,7 @@ public class ExtraConfigurationUtility {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// param with '_'
|
||||||
switch (paramKey) {
|
switch (paramKey) {
|
||||||
case "HVM_boot_policy":
|
case "HVM_boot_policy":
|
||||||
vm.setHVMBootPolicy(conn, paramValue);
|
vm.setHVMBootPolicy(conn, paramValue);
|
||||||
@ -144,7 +153,7 @@ public class ExtraConfigurationUtility {
|
|||||||
case "VCPUs_at_startup":
|
case "VCPUs_at_startup":
|
||||||
vm.setVCPUsAtStartup(conn, Long.valueOf(paramValue));
|
vm.setVCPUsAtStartup(conn, Long.valueOf(paramValue));
|
||||||
break;
|
break;
|
||||||
case "is-a-template":
|
case "is_a_template":
|
||||||
vm.setIsATemplate(conn, Boolean.valueOf(paramValue));
|
vm.setIsATemplate(conn, Boolean.valueOf(paramValue));
|
||||||
break;
|
break;
|
||||||
case "memory_static_max":
|
case "memory_static_max":
|
||||||
@ -169,12 +178,28 @@ public class ExtraConfigurationUtility {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<String, String> prepareKeyValuePair(String cfg) {
|
protected static Pair<String, String> prepareKeyValuePair(String cfg) {
|
||||||
Map<String, String> configKeyPair = new HashMap<>();
|
// cfg is either param=value or map-param:key=value
|
||||||
int indexOfEqualSign = cfg.indexOf("=");
|
int indexOfEqualSign = cfg.indexOf("=");
|
||||||
String key = cfg.substring(0, indexOfEqualSign).replace("-", "_");
|
if (indexOfEqualSign <= 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String key;
|
||||||
|
// Replace '-' with '_' in param / map-param only
|
||||||
|
if (cfg.contains(":")) {
|
||||||
|
int indexOfColon = cfg.indexOf(":");
|
||||||
|
if (indexOfColon <= 0 || indexOfEqualSign < indexOfColon) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String mapParam = cfg.substring(0, indexOfColon).replace("-", "_");
|
||||||
|
String paramKey = cfg.substring(indexOfColon + 1, indexOfEqualSign);
|
||||||
|
key = mapParam + ":" + paramKey;
|
||||||
|
} else {
|
||||||
|
key = cfg.substring(0, indexOfEqualSign).replace("-", "_");
|
||||||
|
}
|
||||||
|
|
||||||
String value = cfg.substring(indexOfEqualSign + 1);
|
String value = cfg.substring(indexOfEqualSign + 1);
|
||||||
configKeyPair.put(key, value);
|
return new Pair<>(key, value);
|
||||||
return configKeyPair;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* 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.hypervisor.xenserver;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import com.cloud.utils.Pair;
|
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class ExtraConfigurationUtilityTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void prepareKeyValuePairTest() {
|
||||||
|
// Map params
|
||||||
|
verifyKeyValuePairForConfigParam("platform:exp-nested-hvm=true", "platform:exp-nested-hvm", "true");
|
||||||
|
verifyKeyValuePairForConfigParam("other_config:my_key=my_value", "other_config:my_key", "my_value");
|
||||||
|
verifyKeyValuePairForConfigParam("test-config:test-key=test-value", "test_config:test-key", "test-value");
|
||||||
|
|
||||||
|
// Params
|
||||||
|
verifyKeyValuePairForConfigParam("is_a_template=true", "is_a_template", "true");
|
||||||
|
verifyKeyValuePairForConfigParam("is-a-template=true", "is_a_template", "true");
|
||||||
|
verifyKeyValuePairForConfigParam("memory_dynamic_min=536870912", "memory_dynamic_min", "536870912");
|
||||||
|
verifyKeyValuePairForConfigParam("VCPUs_at_startup=2", "VCPUs_at_startup", "2");
|
||||||
|
verifyKeyValuePairForConfigParam("VCPUs-max=4", "VCPUs_max", "4");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void verifyKeyValuePairForConfigParam(String cfg, String expectedKey, String expectedValue) {
|
||||||
|
Pair<String, String> keyValuePair = ExtraConfigurationUtility.prepareKeyValuePair(cfg);
|
||||||
|
Assert.assertEquals(expectedKey, keyValuePair.first());
|
||||||
|
Assert.assertEquals(expectedValue, keyValuePair.second());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user