do not end name with ip when name is unique
Resolves
@@ -30,7 +30,6 @@ import org.zstack.header.network.l2.L2NetworkType;
import org.zstack.header.rest.RESTFacade;
import org.zstack.header.rest.SyncHttpCallHandler;
import org.zstack.header.tag.FormTagExtensionPoint;
-import org.zstack.header.tag.SystemTagValidator;
import org.zstack.header.volume.MaxDataVolumeNumberExtensionPoint;
import org.zstack.header.volume.VolumeConstant;
import org.zstack.header.volume.VolumeFormat;
@@ -95,15 +94,33 @@ public class KVMHostFactory extends AbstractService implements HypervisorFactory
@Override
public List<AddHostMsg> buildMessageFromFile(String content) {
try {
- return loadMsgFromFile(content).stream()
- .peek(it -> it.setName((StringUtils.isEmpty(it.getName()) ? "HOST" : it.getName()) + "-" + it.getManagementIp()))
- .collect(Collectors.toList());
+ List<AddKVMHostMsg> msgs = loadMsgFromFile(content);
+ return prepareMsgHostName(msgs);
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new OperationFailureException(operr("fail to load host info from file. because\n%s", e.getMessage()));
}
}
+ private List<AddHostMsg> prepareMsgHostName(List<AddKVMHostMsg> msgs) {
+ Map<String, List<AddKVMHostMsg>> nameMsgsMap = new HashMap<>();
+ msgs.forEach(it -> nameMsgsMap.computeIfAbsent(it.getName(), k -> new ArrayList<>()).add(it));
+
+ List<AddHostMsg> renamed = nameMsgsMap.entrySet().stream()
+ .filter(e -> e.getValue().size() > 1 || StringUtils.isEmpty(e.getKey()))
+ .flatMap(e -> e.getValue().stream())
+ .peek(msg -> msg.setName((StringUtils.isEmpty(msg.getName()) ? "HOST" : msg.getName()) + "-" + msg.getManagementIp()))
+ .collect(Collectors.toList());
+
+ List<AddHostMsg> origins = nameMsgsMap.entrySet().stream()
+ .filter(e -> e.getValue().size() == 1 && !StringUtils.isEmpty(e.getKey()))
+ .flatMap(e -> e.getValue().stream())
+ .collect(Collectors.toList());
+
+ renamed.addAll(origins);
+ return renamed;
+ }
+
private List<AddKVMHostMsg> loadMsgFromFile(String content) throws IOException {
Map<String, Function<String, String>> extensionTagMappers = new HashMap<>();
pluginRgty.getExtensionList(FormTagExtensionPoint.class).forEach(it -> extensionTagMappers.putAll(it.getTagMappers(AddKVMHostMsg.class)));
