Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@

import org.apache.bigtop.manager.common.enums.Command;
import org.apache.bigtop.manager.dao.po.ClusterPO;
import org.apache.bigtop.manager.dao.po.HostPO;
import org.apache.bigtop.manager.dao.repository.ClusterDao;
import org.apache.bigtop.manager.dao.repository.HostDao;
import org.apache.bigtop.manager.server.command.CommandIdentifier;
import org.apache.bigtop.manager.server.enums.ApiExceptionEnum;
import org.apache.bigtop.manager.server.enums.CommandLevel;
import org.apache.bigtop.manager.server.exception.ApiException;
import org.apache.bigtop.manager.server.model.dto.command.ClusterCommandDTO;

import org.apache.commons.collections4.CollectionUtils;

import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;
Expand All @@ -38,6 +42,9 @@ public class ClusterAddValidator implements CommandValidator {
@Resource
private ClusterDao clusterDao;

@Resource
private HostDao hostDao;

@Override
public List<CommandIdentifier> getCommandIdentifiers() {
return List.of(new CommandIdentifier(CommandLevel.CLUSTER, Command.ADD));
Expand All @@ -53,5 +60,15 @@ public void validate(ValidatorContext context) {
if (clusterPO != null) {
throw new ApiException(ApiExceptionEnum.CLUSTER_EXISTS, clusterName);
}

List<String> hostnames = clusterCommand.getHosts().stream()
.flatMap(hostCommandDTO -> hostCommandDTO.getHostnames().stream())
.toList();
List<HostPO> hostPOList = hostDao.findAllByHostnames(hostnames);
if (CollectionUtils.isNotEmpty(hostPOList)) {
List<String> existsHostnames =
hostPOList.stream().map(HostPO::getHostname).toList();
throw new ApiException(ApiExceptionEnum.HOST_ASSIGNED, String.join(",", existsHostnames));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,11 @@ public ResponseEntity<Boolean> installDependencies(@RequestBody @Validated List<
public ResponseEntity<List<InstalledStatusVO>> installedStatus() {
return ResponseEntity.success(hostService.installedStatus());
}

@Operation(summary = "check-duplicate", description = "check hostname duplicate")
@PostMapping("/check-duplicate")
public ResponseEntity<Boolean> checkDuplicate(@RequestBody @Validated HostReq hostReq) {
HostDTO hostDTO = HostConverter.INSTANCE.fromReq2DTO(hostReq);
return ResponseEntity.success(hostService.checkDuplicate(hostDTO));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,10 @@ public interface HostService {
* @return installed status
*/
List<InstalledStatusVO> installedStatus();

/**
* validate host exists
* @param hostDTO hostnames
*/
Boolean checkDuplicate(HostDTO hostDTO);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import org.apache.bigtop.manager.server.utils.PageUtils;
import org.apache.bigtop.manager.server.utils.RemoteSSHUtils;

import org.apache.commons.collections4.CollectionUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -252,6 +254,17 @@ public List<InstalledStatusVO> installedStatus() {
return installedStatus;
}

@Override
public Boolean checkDuplicate(HostDTO hostDTO) {
List<HostPO> existsHostList = hostDao.findAllByHostnames(hostDTO.getHostnames());
if (CollectionUtils.isNotEmpty(existsHostList)) {
List<String> existsHostnames =
existsHostList.stream().map(HostPO::getHostname).toList();
throw new ApiException(ApiExceptionEnum.HOST_ASSIGNED, String.join(",", existsHostnames));
}
return true;
}

public void installDependencies(HostDTO hostDTO, String hostname, InstalledStatusVO installedStatusVO) {
String path = hostDTO.getAgentDir();
String repoUrl = toolDao.findByName("agent").getBaseUrl();
Expand Down
Loading