Skip to content

Commit

Permalink
update teletraan api to disable keel pipeline by pipeline link instea…
Browse files Browse the repository at this point in the history
…d 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
  • Loading branch information
liyaqin1 authored May 20, 2024
1 parent 266965b commit ed7b265
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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=?";

Expand All @@ -45,9 +45,8 @@ public PindeployBean get(String envId) throws Exception {
}

@Override
public void delete(String envId) throws Exception {
ResultSetHandler<PindeployBean> h = new BeanHandler<PindeployBean>(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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -78,15 +75,13 @@ 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;


public EnvStages(@Context TeletraanServiceContext context) {
environDAO = context.getEnvironDAO();
pindeployDAO = context.getPindeployDAO();
environHandler = new EnvironHandler(context);
configHistoryHandler = new ConfigHistoryHandler(context);
tagHandler = new EnvTagHandler(context);
Expand All @@ -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",
Expand Down Expand Up @@ -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<EnvType, String> stageTypeCategory = new HashMap<>();
stageTypeCategory.put(EnvType.DEFAULT, "PRODUCTION");
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit ed7b265

Please sign in to comment.