From e5ba5b5310ace67d541dddf93a45661984b28dcc Mon Sep 17 00:00:00 2001 From: Tyler Ouyang Date: Thu, 25 Jul 2024 18:04:45 -0700 Subject: [PATCH] Change the termination error to forbidden --- .../deployservice/handler/EnvironHandler.java | 38 ++++++------- .../handler/EnvironHandlerTest.java | 53 +++++++++++++------ 2 files changed, 53 insertions(+), 38 deletions(-) diff --git a/deploy-service/common/src/main/java/com/pinterest/deployservice/handler/EnvironHandler.java b/deploy-service/common/src/main/java/com/pinterest/deployservice/handler/EnvironHandler.java index 3e44121fca..fa1836d99b 100644 --- a/deploy-service/common/src/main/java/com/pinterest/deployservice/handler/EnvironHandler.java +++ b/deploy-service/common/src/main/java/com/pinterest/deployservice/handler/EnvironHandler.java @@ -1,5 +1,5 @@ -/* - * Copyright 2016 Pinterest, Inc. +/** + * Copyright (c) 2016-2024 Pinterest, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,21 +22,15 @@ import com.pinterest.deployservice.dao.EnvironDAO; import com.pinterest.deployservice.dao.GroupDAO; import com.pinterest.deployservice.dao.HostDAO; -import com.pinterest.deployservice.dao.PindeployDAO; import com.pinterest.deployservice.dao.PromoteDAO; -import com.pinterest.deployservice.dao.ScheduleDAO; - - -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.sql.SQLException; import java.util.*; - -import javax.ws.rs.NotAllowedException; +import javax.ws.rs.ForbiddenException; import javax.ws.rs.NotFoundException; import javax.ws.rs.WebApplicationException; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class EnvironHandler { private static final Logger LOG = LoggerFactory.getLogger(EnvironHandler.class); @@ -46,8 +40,6 @@ public class EnvironHandler { private AgentDAO agentDAO; private GroupDAO groupDAO; private HostDAO hostDAO; - private PindeployDAO pindeployDAO; - private ScheduleDAO scheduleDAO; private CommonHandler commonHandler; private DataHandler dataHandler; @@ -57,8 +49,6 @@ public EnvironHandler(ServiceContext serviceContext) { agentDAO = serviceContext.getAgentDAO(); groupDAO = serviceContext.getGroupDAO(); hostDAO = serviceContext.getHostDAO(); - pindeployDAO = serviceContext.getPindeployDAO(); - scheduleDAO = serviceContext.getScheduleDAO(); commonHandler = new CommonHandler(serviceContext); dataHandler = new DataHandler(serviceContext); } @@ -534,15 +524,21 @@ public void ensureHostsOwnedByEnv(EnvironBean environBean, Collection ho EnvironBean mainEnv = environDAO.getMainEnvByHostId(hostId); if (mainEnv == null) { throw new NotFoundException( - String.format("No main environment found for host %s, refuse to proceed", hostId)); + String.format( + "No main environment found for host %s, refuse to proceed", + hostId)); } if (!mainEnv.getEnv_id().equals(environBean.getEnv_id())) { - throw new NotAllowedException(String.format("%s/%s is not the owning environment of host %s", - environBean.getEnv_name(), environBean.getStage_name(), hostId)); + throw new ForbiddenException( + String.format( + "%s/%s is not the owning environment of host %s", + environBean.getEnv_name(), + environBean.getStage_name(), + hostId)); } } catch (SQLException e) { - throw new WebApplicationException(String.format("Failed to get main environment for host %s", hostId), - e); + throw new WebApplicationException( + String.format("Failed to get main environment for host %s", hostId), e); } } } diff --git a/deploy-service/common/src/test/java/com/pinterest/deployservice/handler/EnvironHandlerTest.java b/deploy-service/common/src/test/java/com/pinterest/deployservice/handler/EnvironHandlerTest.java index b414819866..f8da667fe0 100644 --- a/deploy-service/common/src/test/java/com/pinterest/deployservice/handler/EnvironHandlerTest.java +++ b/deploy-service/common/src/test/java/com/pinterest/deployservice/handler/EnvironHandlerTest.java @@ -1,3 +1,18 @@ +/** + * Copyright (c) 2024 Pinterest, Inc. + * + * Licensed 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 com.pinterest.deployservice.handler; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; @@ -9,18 +24,6 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import java.sql.SQLException; -import java.util.Arrays; -import java.util.List; - -import javax.ws.rs.NotAllowedException; -import javax.ws.rs.NotFoundException; -import javax.ws.rs.WebApplicationException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; - import com.pinterest.deployservice.ServiceContext; import com.pinterest.deployservice.bean.EnvType; import com.pinterest.deployservice.bean.EnvironBean; @@ -29,9 +32,18 @@ import com.pinterest.deployservice.dao.AgentDAO; import com.pinterest.deployservice.dao.EnvironDAO; import com.pinterest.deployservice.dao.HostDAO; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.List; +import javax.ws.rs.ForbiddenException; +import javax.ws.rs.NotFoundException; +import javax.ws.rs.WebApplicationException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; class EnvironHandlerTest { - private final static String DEFAULT_HOST_ID = "hostId"; + private static final String DEFAULT_HOST_ID = "hostId"; private EnvironHandler environHandler; @@ -67,7 +79,8 @@ void stopServiceOnHost_withReplaceHost_hostBeanStateIsPendingTerminate() throws } @Test - void stopServiceOnHost_withoutReplaceHost_hostBeanStateIsPendingTerminateNoReplace() throws Exception { + void stopServiceOnHost_withoutReplaceHost_hostBeanStateIsPendingTerminateNoReplace() + throws Exception { ArgumentCaptor argument = ArgumentCaptor.forClass(HostBean.class); environHandler.stopServiceOnHost(DEFAULT_HOST_ID, false); @@ -87,7 +100,9 @@ void updateStage_type_enables_private_build() throws Exception { @Test void ensureHostsOwnedByEnv_noMainEnv() throws Exception { - assertThrows(NotFoundException.class, () -> environHandler.ensureHostsOwnedByEnv(new EnvironBean(), hostIds)); + assertThrows( + NotFoundException.class, + () -> environHandler.ensureHostsOwnedByEnv(new EnvironBean(), hostIds)); } @Test @@ -95,7 +110,9 @@ void ensureHostsOwnedByEnv_differentMainEnv() throws Exception { EnvironBean envBean = new EnvironBean(); envBean.setEnv_id("envId"); when(environDAO.getMainEnvByHostId(anyString())).thenReturn(envBean); - assertThrows(NotAllowedException.class, () -> environHandler.ensureHostsOwnedByEnv(new EnvironBean(), hostIds)); + assertThrows( + ForbiddenException.class, + () -> environHandler.ensureHostsOwnedByEnv(new EnvironBean(), hostIds)); } @Test @@ -109,6 +126,8 @@ void ensureHostsOwnedByEnv_sameMainEnv() throws Exception { @Test void ensureHostsOwnedByEnv_sqlException() throws Exception { when(environDAO.getMainEnvByHostId(anyString())).thenThrow(SQLException.class); - assertThrows(WebApplicationException.class, () -> environHandler.ensureHostsOwnedByEnv(new EnvironBean(), hostIds)); + assertThrows( + WebApplicationException.class, + () -> environHandler.ensureHostsOwnedByEnv(new EnvironBean(), hostIds)); } }