diff --git a/wasin/src/main/java/com/wasin/wasin/_core/exception/BaseException.java b/wasin/src/main/java/com/wasin/wasin/_core/exception/BaseException.java index e209981..49a9884 100644 --- a/wasin/src/main/java/com/wasin/wasin/_core/exception/BaseException.java +++ b/wasin/src/main/java/com/wasin/wasin/_core/exception/BaseException.java @@ -78,6 +78,9 @@ public enum BaseException { POWER_SAVING_MODE_CHANGE_FAIL("파워 세이빙 모드로 바꾸는 데 실패했습니다.", HttpStatus.INTERNAL_SERVER_ERROR), SSH_CONNECTION_FAIL("SSH 연결에 실패했습니다.", HttpStatus.INTERNAL_SERVER_ERROR), + // 모니터링 + MONITOR_PERMISSION_NONE("모니터링 권한이 없습니다.", HttpStatus.FORBIDDEN), + ; diff --git a/wasin/src/main/java/com/wasin/wasin/controller/MonitoringController.java b/wasin/src/main/java/com/wasin/wasin/controller/MonitoringController.java index 4e82f40..d6c75f8 100644 --- a/wasin/src/main/java/com/wasin/wasin/controller/MonitoringController.java +++ b/wasin/src/main/java/com/wasin/wasin/controller/MonitoringController.java @@ -26,4 +26,11 @@ public ResponseEntity monitorById(@RequestParam(value = "routerId") Long rout return ResponseEntity.ok().body(ApiUtils.success(response)); } + // 관리자가 속한 라우터 그룹의 모든 라우터 조회 + @GetMapping("/routers") + public ResponseEntity findAllRouter(@AuthenticationPrincipal CustomUserDetails userDetails) { + MonitorResponse.FindAllRouter response = monitoringService.findAllRouter(userDetails.getUser()); + return ResponseEntity.ok().body(ApiUtils.success(response)); + } + } diff --git a/wasin/src/main/java/com/wasin/wasin/domain/dto/MonitorResponse.java b/wasin/src/main/java/com/wasin/wasin/domain/dto/MonitorResponse.java index 7ffc4b2..348852a 100644 --- a/wasin/src/main/java/com/wasin/wasin/domain/dto/MonitorResponse.java +++ b/wasin/src/main/java/com/wasin/wasin/domain/dto/MonitorResponse.java @@ -24,4 +24,15 @@ public record MonitorGraph( ) { } + public record FindAllRouter ( + List routerList + ) { + } + + public record MonitorRouter( + Long routerId, + String name, + String instance + ) { + } } diff --git a/wasin/src/main/java/com/wasin/wasin/domain/mapper/MonitoringMapper.java b/wasin/src/main/java/com/wasin/wasin/domain/mapper/MonitoringMapper.java index fa035aa..a4271c8 100644 --- a/wasin/src/main/java/com/wasin/wasin/domain/mapper/MonitoringMapper.java +++ b/wasin/src/main/java/com/wasin/wasin/domain/mapper/MonitoringMapper.java @@ -3,6 +3,7 @@ import com.wasin.wasin.domain._const.Metric; import com.wasin.wasin.domain.dto.MonitorResponse; import com.wasin.wasin.domain.dto.RouterResponse; +import com.wasin.wasin.domain.entity.Router; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; @@ -42,4 +43,16 @@ private List getMetricList() { ) ).toList(); } + + public MonitorResponse.FindAllRouter toRouterListDTO(List routerList) { + return new MonitorResponse.FindAllRouter( + routerList.stream().map(it -> + new MonitorResponse.MonitorRouter( + it.getId(), + it.getName(), + it.getInstance() + ) + ).toList() + ); + } } diff --git a/wasin/src/main/java/com/wasin/wasin/domain/validation/MonitoringValidation.java b/wasin/src/main/java/com/wasin/wasin/domain/validation/MonitoringValidation.java new file mode 100644 index 0000000..ed25aed --- /dev/null +++ b/wasin/src/main/java/com/wasin/wasin/domain/validation/MonitoringValidation.java @@ -0,0 +1,22 @@ +package com.wasin.wasin.domain.validation; + +import com.wasin.wasin._core.exception.BaseException; +import com.wasin.wasin._core.exception.error.ForbiddenException; +import com.wasin.wasin.domain.entity.Router; +import com.wasin.wasin.domain.entity.User; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class MonitoringValidation { + + + public void checkRouterExistInGroup(Router router, User user) { + Long routerCompanyId = router.getCompany().getId(); + Long userCompanyId = user.getCompany().getId(); + if (!routerCompanyId.equals(userCompanyId)) { + throw new ForbiddenException(BaseException.MONITOR_PERMISSION_NONE); + } + } +} diff --git a/wasin/src/main/java/com/wasin/wasin/service/MonitoringService.java b/wasin/src/main/java/com/wasin/wasin/service/MonitoringService.java index d219f0c..58d6804 100644 --- a/wasin/src/main/java/com/wasin/wasin/service/MonitoringService.java +++ b/wasin/src/main/java/com/wasin/wasin/service/MonitoringService.java @@ -6,4 +6,6 @@ public interface MonitoringService { MonitorResponse.FindById monitorById(Long routerId, Long metricId, Long time, User user); + + MonitorResponse.FindAllRouter findAllRouter(User user); } diff --git a/wasin/src/main/java/com/wasin/wasin/service/impl/MonitoringServiceImpl.java b/wasin/src/main/java/com/wasin/wasin/service/impl/MonitoringServiceImpl.java index 5b94f4e..bb19b4e 100644 --- a/wasin/src/main/java/com/wasin/wasin/service/impl/MonitoringServiceImpl.java +++ b/wasin/src/main/java/com/wasin/wasin/service/impl/MonitoringServiceImpl.java @@ -6,36 +6,58 @@ import com.wasin.wasin.domain._const.Metric; import com.wasin.wasin.domain.dto.MonitorResponse; import com.wasin.wasin.domain.dto.RouterResponse; +import com.wasin.wasin.domain.entity.Company; import com.wasin.wasin.domain.entity.Router; import com.wasin.wasin.domain.entity.User; import com.wasin.wasin.domain.mapper.MonitoringMapper; +import com.wasin.wasin.domain.validation.MonitoringValidation; import com.wasin.wasin.repository.RouterJPARepository; +import com.wasin.wasin.repository.UserJPARepository; import com.wasin.wasin.service.MonitoringService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; + @RequiredArgsConstructor @Transactional(readOnly = true) @Service public class MonitoringServiceImpl implements MonitoringService { + private final UserJPARepository userJPARepository; private final RouterJPARepository routerJPARepository; private final MonitoringApiUtil monitoringApiUtil; private final MonitoringMapper monitoringMapper; + private final MonitoringValidation monitoringValidation; - public MonitorResponse.FindById monitorById(Long routerId, Long metricId, Long time, User user) { - // default values + public MonitorResponse.FindById monitorById(Long routerId, Long metricId, Long time, User _user) { if (metricId == null) metricId = 0L; if (time == null) time = 60L; Metric metric = Metric.findByMetricId(metricId); Router router = findByRouterId(routerId); + User user = findUserByUserId(_user.getId()); + + monitoringValidation.checkRouterExistInGroup(router, user); RouterResponse.MonitorResult results = monitoringApiUtil.getMetric(metric, router, time); return monitoringMapper.resultToDTO(results, metricId, time); } + public MonitorResponse.FindAllRouter findAllRouter(User _user) { + User user = findUserByUserId(_user.getId()); + Company company = user.getCompany(); + List routerList = routerJPARepository.findAllRouterByCompanyId(company.getId()); + return monitoringMapper.toRouterListDTO(routerList); + } + + private User findUserByUserId(Long userId) { + return userJPARepository.findActiveUserWithCompanyById(userId).orElseThrow( + () -> new NotFoundException(BaseException.USER_NOT_FOUND) + ); + } + private Router findByRouterId(Long routerId) { return routerJPARepository.findById(routerId).orElseThrow( () -> new NotFoundException(BaseException.ROUTER_NOT_EXIST_IN_DB)