diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 0568e8c497..23e79bed96 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -27,3 +27,7 @@ jobs: working-directory: deploy-service run: | mvn clean package --file pom.xml -P-pinterest-dependenies,exclude-pinterest-only-classes + - name: Build with Maven + working-directory: deploy-service + run: | + mvn spotless:check --file pom.xml diff --git a/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/HostAgentBean.java b/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/HostAgentBean.java index 9171801628..5caa35beaf 100644 --- a/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/HostAgentBean.java +++ b/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/HostAgentBean.java @@ -18,11 +18,10 @@ import com.fasterxml.jackson.annotation.JsonProperty; import lombok.AllArgsConstructor; import lombok.Builder; +import lombok.Data; import lombok.NoArgsConstructor; import org.apache.commons.lang3.builder.ReflectionToStringBuilder; -import lombok.Data; - @Builder @NoArgsConstructor @AllArgsConstructor diff --git a/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/HostBean.java b/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/HostBean.java index d938a2b83c..0ebd7a335a 100644 --- a/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/HostBean.java +++ b/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/HostBean.java @@ -18,7 +18,6 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.AllArgsConstructor; -import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; diff --git a/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/HostBeanWithStatuses.java b/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/HostBeanWithStatuses.java index d6d8cba9b7..5f08cd7579 100644 --- a/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/HostBeanWithStatuses.java +++ b/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/HostBeanWithStatuses.java @@ -17,7 +17,6 @@ import com.fasterxml.jackson.annotation.JsonProperty; import lombok.AllArgsConstructor; -import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; @@ -27,7 +26,8 @@ @AllArgsConstructor @Data public class HostBeanWithStatuses extends HostBean { - // Normandie and Knox Statuses are NOT in the hosts table, but instead on the hosts_and_agents table + // Normandie and Knox Statuses are NOT in the hosts table, but instead on the hosts_and_agents + // table @JsonProperty("normandieStatus") private NormandieStatus normandie_status; diff --git a/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/KnoxStatus.java b/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/KnoxStatus.java index 283010aeae..7be90b7a4a 100644 --- a/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/KnoxStatus.java +++ b/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/KnoxStatus.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.bean; public enum KnoxStatus { diff --git a/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/NormandieStatus.java b/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/NormandieStatus.java index d1e0a657ad..3e9c75e81f 100644 --- a/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/NormandieStatus.java +++ b/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/NormandieStatus.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.bean; public enum NormandieStatus { diff --git a/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/PingRequestBean.java b/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/PingRequestBean.java index 2fd71d11dd..1076179e6b 100644 --- a/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/PingRequestBean.java +++ b/deploy-service/common/src/main/java/com/pinterest/deployservice/bean/PingRequestBean.java @@ -18,7 +18,6 @@ import java.util.List; import java.util.Set; import javax.validation.constraints.NotEmpty; - import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; diff --git a/deploy-service/common/src/main/java/com/pinterest/deployservice/dao/HostDAO.java b/deploy-service/common/src/main/java/com/pinterest/deployservice/dao/HostDAO.java index 42e13329cc..4cd491cc5f 100644 --- a/deploy-service/common/src/main/java/com/pinterest/deployservice/dao/HostDAO.java +++ b/deploy-service/common/src/main/java/com/pinterest/deployservice/dao/HostDAO.java @@ -17,7 +17,6 @@ import com.pinterest.deployservice.bean.HostBean; import com.pinterest.deployservice.bean.HostBeanWithStatuses; - import java.sql.SQLException; import java.util.Collection; import java.util.List; @@ -72,7 +71,8 @@ void insertOrUpdate( HostBean getByEnvIdAndHostId(String envId, String hostId) throws Exception; - Collection getByEnvIdAndHostName(String envId, String hostName) throws Exception; + Collection getByEnvIdAndHostName(String envId, String hostName) + throws Exception; Collection getToBeRetiredHostIdsByGroup(String groupName) throws Exception; diff --git a/deploy-service/common/src/main/java/com/pinterest/deployservice/db/DBHostDAOImpl.java b/deploy-service/common/src/main/java/com/pinterest/deployservice/db/DBHostDAOImpl.java index ca03015f22..5be79b9ed3 100644 --- a/deploy-service/common/src/main/java/com/pinterest/deployservice/db/DBHostDAOImpl.java +++ b/deploy-service/common/src/main/java/com/pinterest/deployservice/db/DBHostDAOImpl.java @@ -244,7 +244,8 @@ public List getHostsByGroup(String groupName, long pageIndex, int page @Override public List getHosts(String hostName) throws Exception { - ResultSetHandler> h = new BeanListHandler<>(HostBeanWithStatuses.class); + ResultSetHandler> h = + new BeanListHandler<>(HostBeanWithStatuses.class); return new QueryRunner(dataSource).query(GET_HOST_BY_NAME, h, hostName); } @@ -324,7 +325,8 @@ public HostBean getByEnvIdAndHostId(String envId, String hostId) throws Exceptio @Override public Collection getByEnvIdAndHostName(String envId, String hostName) throws Exception { - ResultSetHandler> h = new BeanListHandler<>(HostBeanWithStatuses.class); + ResultSetHandler> h = + new BeanListHandler<>(HostBeanWithStatuses.class); Collection hostBeans = new QueryRunner(dataSource) .query(GET_HOST_BY_ENVID_AND_HOSTNAME1, h, envId, hostName); diff --git a/deploy-service/common/src/main/java/com/pinterest/deployservice/handler/PingHandler.java b/deploy-service/common/src/main/java/com/pinterest/deployservice/handler/PingHandler.java index 28e6b844c7..42eb24b24c 100644 --- a/deploy-service/common/src/main/java/com/pinterest/deployservice/handler/PingHandler.java +++ b/deploy-service/common/src/main/java/com/pinterest/deployservice/handler/PingHandler.java @@ -211,8 +211,13 @@ void updateHosts( } void updateHostStatus( - String hostId, String hostName, String hostIp, String agentVersion, String asg, - NormandieStatus normandieStatus, KnoxStatus knoxStatus) + String hostId, + String hostName, + String hostIp, + String agentVersion, + String asg, + NormandieStatus normandieStatus, + KnoxStatus knoxStatus) throws Exception { HostAgentBean hostAgentBean = hostAgentDAO.getHostById(hostId); long currentTime = System.currentTimeMillis(); @@ -835,10 +840,17 @@ public PingResult ping(PingRequestBean pingRequest, boolean rate_limited) throws String agentVersion = pingRequest.getAgentVersion() != null ? pingRequest.getAgentVersion() : "UNKNOWN"; - NormandieStatus normandieStatus = pingRequest.getNormandieStatus() != null ? pingRequest.getNormandieStatus() : NormandieStatus.UNKNOWN; - KnoxStatus knoxStatus = pingRequest.getKnoxStatus() != null ? pingRequest.getKnoxStatus() : KnoxStatus.UNKNOWN; - - this.updateHostStatus(hostId, hostName, hostIp, agentVersion, asg, normandieStatus, knoxStatus); + NormandieStatus normandieStatus = + pingRequest.getNormandieStatus() != null + ? pingRequest.getNormandieStatus() + : NormandieStatus.UNKNOWN; + KnoxStatus knoxStatus = + pingRequest.getKnoxStatus() != null + ? pingRequest.getKnoxStatus() + : KnoxStatus.UNKNOWN; + + this.updateHostStatus( + hostId, hostName, hostIp, agentVersion, asg, normandieStatus, knoxStatus); // update the host <-> groups mapping this.updateHosts(hostName, hostIp, hostId, groups, accountId); diff --git a/deploy-service/common/src/test/java/com/pinterest/deployservice/db/DBAgentDAOImplTest.java b/deploy-service/common/src/test/java/com/pinterest/deployservice/db/DBAgentDAOImplTest.java index cc4a28b897..79dc7c2d61 100644 --- a/deploy-service/common/src/test/java/com/pinterest/deployservice/db/DBAgentDAOImplTest.java +++ b/deploy-service/common/src/test/java/com/pinterest/deployservice/db/DBAgentDAOImplTest.java @@ -1,21 +1,35 @@ +/** + * 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.db; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + import com.pinterest.deployservice.bean.HostAgentBean; import com.pinterest.deployservice.bean.KnoxStatus; import com.pinterest.deployservice.bean.NormandieStatus; import com.pinterest.deployservice.dao.HostAgentDAO; +import java.util.List; +import java.util.UUID; import org.apache.commons.dbcp.BasicDataSource; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import java.util.List; -import java.util.UUID; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - public class DBAgentDAOImplTest { private static BasicDataSource dataSource; @@ -33,7 +47,6 @@ void tearDown() throws Exception { DBUtils.truncateAllTables(dataSource); } - @Test public void testHostAgentDAO() throws Exception { final String hostId = "host-1"; @@ -59,15 +72,18 @@ public void testHostAgentDAO() throws Exception { assertEquals(1, hostCount); // Test getStaleHosts - List staleHosts = hostAgentDAO.getStaleHosts(System.currentTimeMillis() - 100_000); + List staleHosts = + hostAgentDAO.getStaleHosts(System.currentTimeMillis() - 100_000); assertTrue(staleHosts.isEmpty()); - List staleHosts2 = hostAgentDAO.getStaleHosts(System.currentTimeMillis() + 100_000); + List staleHosts2 = + hostAgentDAO.getStaleHosts(System.currentTimeMillis() + 100_000); assertEquals(1, staleHosts2.size()); assertEquals(hostAgentBean, staleHosts2.get(0)); - List staleHosts3 = hostAgentDAO.getStaleHosts( - System.currentTimeMillis() - 100_000, System.currentTimeMillis() + 100_000); + List staleHosts3 = + hostAgentDAO.getStaleHosts( + System.currentTimeMillis() - 100_000, System.currentTimeMillis() + 100_000); assertEquals(1, staleHosts3.size()); assertEquals(hostAgentBean, staleHosts3.get(0)); @@ -81,16 +97,15 @@ public void testHostAgentDAO() throws Exception { private HostAgentBean genDefaultHostAgentBean(String hostId) { return HostAgentBean.builder() - .ip("127.0.0.1") - .host_id(hostId) - .host_name(UUID.randomUUID().toString()) - .create_date(System.currentTimeMillis()) - .last_update(System.currentTimeMillis()) - .agent_version("1.0") - .auto_scaling_group("auto-scaling-group") - .normandie_status(NormandieStatus.OK) - .knox_status(KnoxStatus.OK) - .build(); + .ip("127.0.0.1") + .host_id(hostId) + .host_name(UUID.randomUUID().toString()) + .create_date(System.currentTimeMillis()) + .last_update(System.currentTimeMillis()) + .agent_version("1.0") + .auto_scaling_group("auto-scaling-group") + .normandie_status(NormandieStatus.OK) + .knox_status(KnoxStatus.OK) + .build(); } - } diff --git a/deploy-service/common/src/test/java/com/pinterest/deployservice/db/DBDAOTest.java b/deploy-service/common/src/test/java/com/pinterest/deployservice/db/DBDAOTest.java index 0df329d291..2230909919 100644 --- a/deploy-service/common/src/test/java/com/pinterest/deployservice/db/DBDAOTest.java +++ b/deploy-service/common/src/test/java/com/pinterest/deployservice/db/DBDAOTest.java @@ -86,7 +86,6 @@ import java.util.HashSet; import java.util.List; import java.util.Set; - import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.lang3.builder.EqualsBuilder; import org.joda.time.DateTime; @@ -870,13 +869,19 @@ public void testHostDAO() throws Exception { // Test support for normandie and knox statuses in hostDAO.getHosts hostDAO.insertOrUpdate( - "host-normandie", "1.1.1.1", "id-normandie", HostState.ACTIVE.toString(), groups, "test"); - hostAgentDAO.insert(HostAgentBean.builder() - .host_name("host-normandie") - .host_id("id-normandie") - .normandie_status(NormandieStatus.OK) - .knox_status(KnoxStatus.ERROR) - .build()); + "host-normandie", + "1.1.1.1", + "id-normandie", + HostState.ACTIVE.toString(), + groups, + "test"); + hostAgentDAO.insert( + HostAgentBean.builder() + .host_name("host-normandie") + .host_id("id-normandie") + .normandie_status(NormandieStatus.OK) + .knox_status(KnoxStatus.ERROR) + .build()); List hostBeansNormandie = hostDAO.getHosts("host-normandie"); assertEquals(1, hostBeansNormandie.size()); @@ -888,7 +893,7 @@ public void testHostDAO() throws Exception { // Test support for normandie and knox statuses in hostDAO.getByEnvIdAndHostName groupDAO.addHostCapacity("e-3", "host-normandie"); Collection hostBeansNormandie2 = - hostDAO.getByEnvIdAndHostName("e-3", "host-normandie"); + hostDAO.getByEnvIdAndHostName("e-3", "host-normandie"); assertEquals(1, hostBeansNormandie2.size()); HostBeanWithStatuses hostBeanNormandie2 = hostBeansNormandie2.iterator().next(); assertEquals(hostBeanNormandie2.getHost_name(), "host-normandie"); diff --git a/deploy-service/teletraanservice/src/test/java/com/pinterest/teletraan/resource/HostsTest.java b/deploy-service/teletraanservice/src/test/java/com/pinterest/teletraan/resource/HostsTest.java index 3b461327b5..8644192db4 100644 --- a/deploy-service/teletraanservice/src/test/java/com/pinterest/teletraan/resource/HostsTest.java +++ b/deploy-service/teletraanservice/src/test/java/com/pinterest/teletraan/resource/HostsTest.java @@ -15,6 +15,15 @@ */ package com.pinterest.teletraan.resource; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.refEq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableList; import com.pinterest.deployservice.bean.HostBean; @@ -27,6 +36,11 @@ import com.pinterest.teletraan.universal.security.AnonymousAuthFilter; import io.dropwizard.testing.junit5.DropwizardExtensionsSupport; import io.dropwizard.testing.junit5.ResourceExtension; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.Response; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -34,25 +48,6 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import javax.ws.rs.client.Entity; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.SecurityContext; -import java.security.Principal; -import java.util.List; -import java.util.Map; -import java.util.stream.Stream; - -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.argThat; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.ArgumentMatchers.refEq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.reset; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - @ExtendWith(DropwizardExtensionsSupport.class) public class HostsTest { @@ -68,81 +63,91 @@ public class HostsTest { hostDAOMock = mock(HostDAO.class); context.setHostDAO(hostDAOMock); - resourceExtension = ResourceExtension.builder() - .addResource(new Hosts(context)) - .addProvider(new AnonymousAuthFilter()) - .build(); + resourceExtension = + ResourceExtension.builder() + .addResource(new Hosts(context)) + .addProvider(new AnonymousAuthFilter()) + .build(); } @BeforeEach public void setup() { - // the mock is static due to resourceExtension requirements, so we need to reset it before each test + // the mock is static due to resourceExtension requirements, so we need to reset it before + // each test reset(hostDAOMock); } @ParameterizedTest @MethodSource("validHostBeansSource") public void postValidHostBeans() throws Exception { - HostBean hostBean = HostBean.builder() - .host_id("hostId") - .host_name("hostName") - .state(HostState.ACTIVE) - .account_id("accountId") - .group_name("groupName") - .build(); + HostBean hostBean = + HostBean.builder() + .host_id("hostId") + .host_name("hostName") + .state(HostState.ACTIVE) + .account_id("accountId") + .group_name("groupName") + .build(); final Response put = - resourceExtension.target(Target.V1_HOSTS).request().post(Entity.json(hostBean)); + resourceExtension.target(Target.V1_HOSTS).request().post(Entity.json(hostBean)); assertNotEquals(422, put.getStatus()); - verify(hostDAOMock).insert( - refEq(hostBean, "create_date", "last_update")); + verify(hostDAOMock).insert(refEq(hostBean, "create_date", "last_update")); } @ParameterizedTest @MethodSource("validHostBeansSource") public void putValidHostBeans() throws Exception { - HostBean hostBean = HostBean.builder() - .host_id("hostId") - .host_name("hostName") - .state(HostState.ACTIVE) - .account_id("accountId") - .group_name("groupName") - .build(); + HostBean hostBean = + HostBean.builder() + .host_id("hostId") + .host_name("hostName") + .state(HostState.ACTIVE) + .account_id("accountId") + .group_name("groupName") + .build(); final Response put = - resourceExtension.target(Target.V1_HOSTS + "/" + "hostId").request().put(Entity.json(hostBean)); + resourceExtension + .target(Target.V1_HOSTS + "/" + "hostId") + .request() + .put(Entity.json(hostBean)); assertNotEquals(422, put.getStatus()); - verify(hostDAOMock).updateHostById( - eq(hostBean.getHost_id()), - refEq(hostBean, "last_update")); + verify(hostDAOMock) + .updateHostById(eq(hostBean.getHost_id()), refEq(hostBean, "last_update")); } @Test public void getByHostName() throws Exception { - HostBeanWithStatuses hostBean = HostBeanWithStatuses.builder() - .host_id("hostId") - .host_name("hostName") - .state(HostState.ACTIVE) - .account_id("accountId") - .group_name("groupName") - .normandie_status(NormandieStatus.OK) - .knox_status(KnoxStatus.ERROR) - .build(); + HostBeanWithStatuses hostBean = + HostBeanWithStatuses.builder() + .host_id("hostId") + .host_name("hostName") + .state(HostState.ACTIVE) + .account_id("accountId") + .group_name("groupName") + .normandie_status(NormandieStatus.OK) + .knox_status(KnoxStatus.ERROR) + .build(); when(hostDAOMock.getHosts("hostName")).thenReturn(ImmutableList.of(hostBean)); final Response get = - resourceExtension.target(Target.V1_HOSTS + "/" + hostBean.getHost_name()).request().get(); + resourceExtension + .target(Target.V1_HOSTS + "/" + hostBean.getHost_name()) + .request() + .get(); assertNotEquals(422, get.getStatus()); // GET returns a list of maps, so we need to convert it to a HostBeanWithStatuses List list = get.readEntity(List.class); - HostBeanWithStatuses resultBean = mapper.convertValue(list.get(0), HostBeanWithStatuses.class); + HostBeanWithStatuses resultBean = + mapper.convertValue(list.get(0), HostBeanWithStatuses.class); assertEquals(hostBean, resultBean); } @@ -152,19 +157,17 @@ private static Stream validHostBeansSource() { // All fields are null - host is still valid HostBean hostBean2 = HostBean.builder().build(); - return Stream.of( - Arguments.of(hostBean), - Arguments.of(hostBean2)); + return Stream.of(Arguments.of(hostBean), Arguments.of(hostBean2)); } private static HostBean validHostBean() { return HostBean.builder() - .host_id("hostId") - .host_name("hostName") - .state(HostState.ACTIVE) - .account_id("accountId") - .group_name("groupName") - .build(); + .host_id("hostId") + .host_name("hostName") + .state(HostState.ACTIVE) + .account_id("accountId") + .group_name("groupName") + .build(); } private static class Target {