Skip to content

Commit

Permalink
Change the termination error to forbidden
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerwowen committed Jul 26, 2024
1 parent cddbb72 commit e5ba5b5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -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);
}
Expand Down Expand Up @@ -534,15 +524,21 @@ public void ensureHostsOwnedByEnv(EnvironBean environBean, Collection<String> 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -67,7 +79,8 @@ void stopServiceOnHost_withReplaceHost_hostBeanStateIsPendingTerminate() throws
}

@Test
void stopServiceOnHost_withoutReplaceHost_hostBeanStateIsPendingTerminateNoReplace() throws Exception {
void stopServiceOnHost_withoutReplaceHost_hostBeanStateIsPendingTerminateNoReplace()
throws Exception {
ArgumentCaptor<HostBean> argument = ArgumentCaptor.forClass(HostBean.class);

environHandler.stopServiceOnHost(DEFAULT_HOST_ID, false);
Expand All @@ -87,15 +100,19 @@ 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
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
Expand All @@ -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));
}
}

0 comments on commit e5ba5b5

Please sign in to comment.