From ed7b2652560bec5d0ce3da3f439561d0bcbd221a Mon Sep 17 00:00:00 2001 From: liyaqin1 <42289525+liyaqin1@users.noreply.github.com> Date: Mon, 20 May 2024 14:34:18 -0700 Subject: [PATCH] update teletraan api to disable keel pipeline by pipeline link instead of env/stage name (#1619) * update teletraan api to disable keel pipeline by pipeline link instead of env/stage name * move disable to environs.java to get rid of env/stage name * debug * debug * debug * move pindeploy related to a new file * register new class * fix a sql syntax error * fix a sql error * remove log --- .../deployservice/dao/PindeployDAO.java | 2 +- .../deployservice/db/DBPindeployDAOImpl.java | 7 +- .../deployservice/handler/EnvironHandler.java | 4 - .../pinterest/teletraan/TeletraanService.java | 1 + .../teletraan/resource/EnvStages.java | 47 -------- .../teletraan/resource/Pindeploy.java | 104 ++++++++++++++++++ 6 files changed, 109 insertions(+), 56 deletions(-) create mode 100644 deploy-service/teletraanservice/src/main/java/com/pinterest/teletraan/resource/Pindeploy.java diff --git a/deploy-service/common/src/main/java/com/pinterest/deployservice/dao/PindeployDAO.java b/deploy-service/common/src/main/java/com/pinterest/deployservice/dao/PindeployDAO.java index 0a34f9f58e..90e9840796 100644 --- a/deploy-service/common/src/main/java/com/pinterest/deployservice/dao/PindeployDAO.java +++ b/deploy-service/common/src/main/java/com/pinterest/deployservice/dao/PindeployDAO.java @@ -23,7 +23,7 @@ public interface PindeployDAO { PindeployBean get(String envId) throws Exception; - void delete(String envId) throws Exception; + void delete(String pipeline) throws Exception; void insertOrUpdate(PindeployBean pindeployBean) throws Exception; } \ No newline at end of file diff --git a/deploy-service/common/src/main/java/com/pinterest/deployservice/db/DBPindeployDAOImpl.java b/deploy-service/common/src/main/java/com/pinterest/deployservice/db/DBPindeployDAOImpl.java index 1ccd996fa6..a511de0235 100644 --- a/deploy-service/common/src/main/java/com/pinterest/deployservice/db/DBPindeployDAOImpl.java +++ b/deploy-service/common/src/main/java/com/pinterest/deployservice/db/DBPindeployDAOImpl.java @@ -28,7 +28,7 @@ public class DBPindeployDAOImpl implements PindeployDAO { private static final String GET_PINDEPLOY = "SELECT * FROM pindeploy WHERE env_id=?"; private static final String DELETE_PINDEPLOY = - "DELETE * FROM pindeploy WHERE env_id=?"; + "DELETE FROM pindeploy WHERE pipeline=?"; private static final String INSERT_OR_UPDATE_PINDEPLOY = "INSERT INTO pindeploy SET %s ON DUPLICATE KEY UPDATE pipeline=?, is_pindeploy=?"; @@ -45,9 +45,8 @@ public PindeployBean get(String envId) throws Exception { } @Override - public void delete(String envId) throws Exception { - ResultSetHandler h = new BeanHandler(PindeployBean.class); - new QueryRunner(dataSource).query(DELETE_PINDEPLOY, h, envId); + public void delete(String pipeline) throws Exception { + new QueryRunner(dataSource).update(DELETE_PINDEPLOY, pipeline); } @Override 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 b13fdc3f1a..891fca5103 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 @@ -336,10 +336,6 @@ public void updateEnvPromote(EnvironBean envBean, PromoteBean promoteBean, Strin } } - public void updatePindeploy(PindeployBean pindeployBean) throws Exception { - pindeployDAO.insertOrUpdate(pindeployBean); - } - public String resume(EnvironBean envBean, String operator) throws Exception { envBean.setEnv_state(EnvState.NORMAL); updateStage(envBean, operator); diff --git a/deploy-service/teletraanservice/src/main/java/com/pinterest/teletraan/TeletraanService.java b/deploy-service/teletraanservice/src/main/java/com/pinterest/teletraan/TeletraanService.java index af458d0060..ad90e20073 100644 --- a/deploy-service/teletraanservice/src/main/java/com/pinterest/teletraan/TeletraanService.java +++ b/deploy-service/teletraanservice/src/main/java/com/pinterest/teletraan/TeletraanService.java @@ -90,6 +90,7 @@ public void run(TeletraanServiceConfiguration configuration, Environment environ environment.jersey().register(EnvGroupRoles.class); environment.jersey().register(Hosts.class); environment.jersey().register(Systems.class); + environment.jersey().register(Pindeploy.class); // Support pings as well environment.jersey().register(Pings.class); diff --git a/deploy-service/teletraanservice/src/main/java/com/pinterest/teletraan/resource/EnvStages.java b/deploy-service/teletraanservice/src/main/java/com/pinterest/teletraan/resource/EnvStages.java index e23de08b39..166272a78c 100644 --- a/deploy-service/teletraanservice/src/main/java/com/pinterest/teletraan/resource/EnvStages.java +++ b/deploy-service/teletraanservice/src/main/java/com/pinterest/teletraan/resource/EnvStages.java @@ -43,15 +43,12 @@ import com.pinterest.deployservice.bean.EnvType; import com.pinterest.deployservice.bean.EnvironBean; -import com.pinterest.deployservice.bean.PindeployBean; import com.pinterest.deployservice.bean.TagBean; -import com.pinterest.deployservice.bean.PindeployBean; import com.pinterest.deployservice.bean.TagTargetType; import com.pinterest.deployservice.bean.TagValue; import com.pinterest.deployservice.bean.TeletraanPrincipalRole; import com.pinterest.deployservice.common.Constants; import com.pinterest.deployservice.dao.EnvironDAO; -import com.pinterest.deployservice.dao.PindeployDAO; import com.pinterest.deployservice.handler.ConfigHistoryHandler; import com.pinterest.deployservice.handler.EnvTagHandler; import com.pinterest.deployservice.handler.EnvironHandler; @@ -78,7 +75,6 @@ public enum ActionType { private static final Logger LOG = LoggerFactory.getLogger(EnvStages.class); private EnvironDAO environDAO; - private PindeployDAO pindeployDAO; private EnvironHandler environHandler; private ConfigHistoryHandler configHistoryHandler; private TagHandler tagHandler; @@ -86,7 +82,6 @@ public enum ActionType { public EnvStages(@Context TeletraanServiceContext context) { environDAO = context.getEnvironDAO(); - pindeployDAO = context.getPindeployDAO(); environHandler = new EnvironHandler(context); configHistoryHandler = new ConfigHistoryHandler(context); tagHandler = new EnvTagHandler(context); @@ -103,19 +98,6 @@ public EnvironBean get( return Utils.getEnvStage(environDAO, envName, stageName); } - @GET - @Path("/pindeployPipeline") - @ApiOperation( - value = "Get pindeploy related info", - notes = "Return is_pindeploy and pipeline given the environment id", - response = PindeployBean.class) - public PindeployBean getPindeployInfo( - @ApiParam(value = "Environment name", required = true)@PathParam("envName") String envName, - @ApiParam(value = "Stage name", required = true)@PathParam("stageName") String stageName) throws Exception { - EnvironBean envBean = Utils.getEnvStage(environDAO, envName, stageName); - return pindeployDAO.get(envBean.getEnv_id()); - } - @PUT @ApiOperation( value = "Update an environment", @@ -265,35 +247,6 @@ public void action(@Context SecurityContext sc, LOG.info(String.format("Successfully updated action %s for %s/%s by %s", actionType, envName, stageName, operator)); } - @POST - @Path("/pindeployPipeline/action") - @RolesAllowed(TeletraanPrincipalRole.Names.EXECUTE) - @ResourceAuthZInfo(type = AuthZResource.Type.ENV_STAGE, idLocation = Location.PATH) - public void pindeployPipelineAction(@Context SecurityContext sc, - @PathParam("envName") String envName, - @PathParam("stageName") String stageName, - @NotNull @QueryParam("actionType") ActionType actionType, - @NotEmpty @QueryParam("pipeline") String pipeline) throws Exception { - EnvironBean envBean = Utils.getEnvStage(environDAO, envName, stageName); - String operator = sc.getUserPrincipal().getName(); - - PindeployBean pindeployBean = new PindeployBean(); - pindeployBean.setEnv_id(envBean.getEnv_id()); - pindeployBean.setPipeline(pipeline); - switch (actionType) { - case ENABLE: - pindeployBean.setIs_pindeploy(true); - break; - case DISABLE: - pindeployBean.setIs_pindeploy(false); - break; - default: - throw new WebApplicationException("No action found.", Response.Status.BAD_REQUEST); - } - environHandler.updatePindeploy(pindeployBean); - LOG.info(String.format("Successfully updated pindeploy pipeline action %s for %s/%s by %s", actionType, envName, stageName, operator)); - } - private void stageTypeValidate(EnvironBean origBean, EnvironBean newBean) throws Exception { Map stageTypeCategory = new HashMap<>(); stageTypeCategory.put(EnvType.DEFAULT, "PRODUCTION"); diff --git a/deploy-service/teletraanservice/src/main/java/com/pinterest/teletraan/resource/Pindeploy.java b/deploy-service/teletraanservice/src/main/java/com/pinterest/teletraan/resource/Pindeploy.java new file mode 100644 index 0000000000..92a9bdeaa1 --- /dev/null +++ b/deploy-service/teletraanservice/src/main/java/com/pinterest/teletraan/resource/Pindeploy.java @@ -0,0 +1,104 @@ +/** + * Copyright 2016 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.teletraan.resource; + +import com.google.common.base.Optional; +import com.pinterest.deployservice.bean.TeletraanPrincipalRole; +import com.pinterest.deployservice.bean.PindeployBean; +import com.pinterest.deployservice.bean.EnvironBean; +import com.pinterest.deployservice.dao.PindeployDAO; +import com.pinterest.deployservice.dao.EnvironDAO; +import com.pinterest.teletraan.TeletraanServiceContext; +import com.pinterest.teletraan.universal.security.ResourceAuthZInfo; +import com.pinterest.teletraan.universal.security.ResourceAuthZInfo.Location; +import com.pinterest.teletraan.universal.security.bean.AuthZResource; +import com.pinterest.teletraan.universal.security.bean.UserPrincipal; + +import io.swagger.annotations.*; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.security.PermitAll; +import javax.annotation.security.RolesAllowed; +import javax.validation.Valid; +import javax.ws.rs.*; +import javax.ws.rs.core.*; +import java.net.URI; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +@PermitAll +@Path("/v1/pindeploy") +@Api(tags = "Pindeploy") +@SwaggerDefinition( + tags = { + @Tag(name = "Pindeploy", description = "Pindeploy related APIs"), + } +) +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class Pindeploy { + private static final Logger LOG = LoggerFactory.getLogger(Pindeploy.class); + private PindeployDAO pindeployDAO; + private EnvironDAO environDAO; + + public Pindeploy(@Context TeletraanServiceContext context) throws Exception { + pindeployDAO = context.getPindeployDAO(); + environDAO = context.getEnvironDAO(); + } + + @GET + @ApiOperation( + value = "Get pindeploy related info", + notes = "Return is_pindeploy and pipeline given the environment id", + response = PindeployBean.class) + public PindeployBean getPindeployInfo( + @NotEmpty @QueryParam("envName") String envName, + @NotEmpty @QueryParam("stageName") String stageName) throws Exception { + EnvironBean envBean = Utils.getEnvStage(environDAO, envName, stageName); + return pindeployDAO.get(envBean.getEnv_id()); + } + + @DELETE + @Path("/disable") + public void disablePindeployPipeline(@Context SecurityContext sc, + @NotEmpty @QueryParam("pipeline") String pipeline) throws Exception { + String operator = sc.getUserPrincipal().getName(); + pindeployDAO.delete(pipeline); + LOG.info(String.format("Successfully disabled pindeploy pipeline %s by %s", pipeline, operator)); + } + + @POST + @Path("/enable") + public void enablePindeployPipeline(@Context SecurityContext sc, + @NotEmpty @QueryParam("envName") String envName, + @NotEmpty @QueryParam("stageName") String stageName, + @NotEmpty @QueryParam("pipeline") String pipeline) throws Exception { + EnvironBean envBean = Utils.getEnvStage(environDAO, envName, stageName); + String operator = sc.getUserPrincipal().getName(); + PindeployBean pindeployBean = new PindeployBean(); + pindeployBean.setEnv_id(envBean.getEnv_id()); + pindeployBean.setPipeline(pipeline); + pindeployBean.setIs_pindeploy(true); + pindeployDAO.insertOrUpdate(pindeployBean); + LOG.info(String.format("Successfully updated pindeploy pipeline for %s/%s by %s", envName, stageName, operator)); + } +} \ No newline at end of file