# 第三方工具API集成
# 环境准备
参考 开发环境搭建
# 第三方工具集成开发
# 新建项目
DevOps源码中内置模板项目'devops-integration-null'
克隆项目并重命名
eg.
'devops-integration-null'->'devops-integration-demo'
重命名构建包名
eg.
'com.primeton.devops.integration.null'->'com.primeton.devops.integration.demo'
重命名 pom.xml 中对应的属性
eg.
'devops-integration-null'->'devops-integration-demo'
'com.primeton.devops.integration.null'->'com.primeton.devops.integration.demo'
重命名 MANIFEST.MF 中对应的属性
eg.
'com.primeton.devops.integration.null'->'com.primeton.devops.integration.demo'
'integration/null'->'integration/demo'
打开源码路径,更新文件夹名称
eg.
'com.primeton.devops.integration.null'->'com.primeton.devops.integration.demo'
更新源码路径下 .classpath 文件内容
eg.
'com.primeton.devops.integration.null/bin'->'com.primeton.devops.integration.demo/bin'
'com.primeton.devops.integration.null/src'->'com.primeton.devops.integration.demo/src'
更新源码路径下 .project 文件内容
eg.
'devops-integration-null'->'devops-integration-demo'
# 后端集成开发
在新建的项目中创建包
eg.
'com.primeton.devops.integration.demo'
创建集成服务连通性测试类:'DemoServiceTest'
更新'DemoServiceTest'测试类,需要实现'com.primeton.devops.specs.api.pcm.ISystemServiceTest'接口的test方法,如下为示例写法:
在配置路径中创建文件夹'services',并在'services'文件夹中增加测试配置文件'com.primeton.devops.specs.api.pcm.ISystemServiceTest'
更新配置文件'com.primeton.devops.specs.api.pcm.ISystemServiceTest'内容
说明:SystemType以及Category请参考'com.primeton.devops.specs.constant.pcm.SystemType'枚举类,支持用户自定义
对应业务字典如下,如果需要自定义请按已有规则添加字典项
在新创建的包中创建集成服务类以及接口
eg.
'IntegrationDemoController'、'DemoService'
'IntegrationDemoController'示例如下
package com.primeton.devops.integration.demo; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.DELETE; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.Consumes; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import com.primeton.components.rest.annotation.JSONParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; /** * IntegrationDemoController. * */ @Path("/api/integration/demos") @Consumes({ MediaType.APPLICATION_JSON }) @Produces(MediaType.APPLICATION_JSON) @Api(description = "Demo服务集成管理接口") @Controller public class IntegrationDemoController { @Autowired private DemoService demoService; /** * 根据ID查询Demo信息 * * @param demoId * @return Demo信息 * @throws Exception */ @GET @Path("/{demoId}") @ApiOperation("根据ID查询Demo信息") Demo getDemo(@PathParam("demoId") @ApiParam("DemoID") String demoId, @QueryParam("systemId") @ApiParam("系统Id") String systemId) throws Exception { return demoService.getDemo(systemId, demoId); } /** * 新建Demo * * @param demo Demo信息 * @return * @throws Exception */ @POST @ApiOperation("新建Demo") Demo createDemo(@JSONParam("demo") @ApiParam("Demo") Demo demo) throws Exception { return demoService.createDemo(demo); } /** * 修改Demo * * @param demoId DemoID * @param demo Demo信息 * @return * @throws Exception */ @PUT @Path("/{demoId}") @ApiOperation("修改Demo") Demo modifyDemo(@PathParam("demoId") @ApiParam("DemoID") String demoId, @JSONParam("demo") @ApiParam("Demo") Demo demo) throws Exception { return demoService.modifyDemo(demoId, demo); } /** * 删除Demo * * @param demoId DemoID * @throws Exception */ @DELETE @Path("/{demoId}") @ApiOperation("删除Demo") Demo removeDemo(@PathParam("demoId") @ApiParam("DemoId") String demoId) throws Exception { return demoService.removeDemo(demoId); } }
'DemoService'示例如下
package com.primeton.devops.integration.demo; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.primeton.devops.common.dao.TenantBaseDao; import com.primeton.devops.common.util.ExceptionUtil; import com.primeton.devops.common.util.HttpClient; import com.primeton.devops.common.util.JsonUtil; import com.primeton.devops.common.util.HttpClient.Response; import com.primeton.devops.specs.api.pcm.service.ISysConfService; import com.primeton.devops.specs.exception.DevOps; import com.primeton.devops.specs.exception.DevOpsException; import com.primeton.devops.specs.model.pcm.Infra3rdSystem; @Service public class DemoService { private static final String TOKEN_NAME = "Authorization"; @Autowired private ISysConfService sysConfService; @Autowired private TenantBaseDao baseDao; private <T> T get(String rootUrl, String apiToken, String restUri, Map<String, ?> queryParams, Class<T> retClass) throws Exception { return request(rootUrl, apiToken, restUri, queryParams, HttpClient.GET, null, retClass); } private <T> T post(String rootUrl, String apiToken, String restUri, Map<String, ?> queryParams, Object body, Class<T> retClass) throws Exception { return request(rootUrl, apiToken, restUri, queryParams, HttpClient.POST, body, retClass); } private <T> T put(String rootUrl, String apiToken, String restUri, Map<String, ?> queryParams, Object body, Class<T> retClass) throws Exception { return request(rootUrl, apiToken, restUri, queryParams, HttpClient.PUT, body, retClass); } private <T> T delete(String rootUrl, String apiToken, String restUri, Map<String, ?> queryParams, Object body, Class<T> retClass) throws Exception { return request(rootUrl, apiToken, restUri, queryParams, HttpClient.DELETE, body, retClass); } private <T> T request(String rootUrl, String apiToken, String restUri, Map<String, ?> queryParams, String requestMethod, Object body, Class<T> retClass) throws Exception { try { HttpClient httpClient = new HttpClient(rootUrl + restUri).head(TOKEN_NAME, apiToken); if (queryParams != null && !queryParams.isEmpty()) { for (Entry<String, ?> entry : queryParams.entrySet().toArray(new Entry[0])) { if (entry.getValue() == null || StringUtils.isBlank(String.valueOf(entry.getValue()))) { queryParams.remove(entry.getKey()); } } } httpClient = httpClient.queryParams(queryParams).method(requestMethod); if (body != null) { httpClient.body(JsonUtil.toJson(body)); } if (retClass != null && retClass == byte[].class) { ByteArrayOutputStream responseOutput = new ByteArrayOutputStream(); httpClient.setResponseOutput(responseOutput); httpClient.request(-1); return (T)responseOutput.toByteArray(); } Response response = httpClient.request(-1); if (retClass == null) { return null; } return response.getBodyObject(retClass); } catch (Exception e) { if ((HttpClient.GET.equals(requestMethod) || HttpClient.DELETE.equals(requestMethod)) && (e.getMessage().contains("[statusCode=400]") || e.getCause() instanceof FileNotFoundException)) { if (retClass == null) { return null; } if (retClass.isArray()) { return (T)Array.newInstance(retClass.getComponentType(), 0); } else if (List.class.isAssignableFrom(retClass)) { return (T)new ArrayList<>(); } return null; } throw e; } } public Demo getDemo(String systemId, String demoId) throws Exception { Infra3rdSystem sysConf = sysConfService.getSysConf(systemId); Map queryParams = new HashMap<>(); return toDemo(get(sysConf.getAddress(), sysConf.getApiToken(), "", queryParams, Map.class)); } private Demo toDemo(Map map) throws Exception { return null; } }
pom更新
'devops-dailybuild\backend\pom.xml'
'devops-dailybuild\swagger\pom.xml'
'devops-dailybuild\war\pom.xml'
# 前端集成开发
1、修改 scripts/buildApi.js 中 swagger.json 的在线地址
(function () {
// 读取 在线 swagger
var url = "http://10.15.15.109:8080/swagger/swagger.json";
execute(url).then((data) => {
console.log(`read ${url} ...`)
mapAction(data)
}).then(() => {
console.log('create controller')
createController()
})
})()
2、运行 npm run build:api
以'IBomController'为示例:
运行 npm run build:api 后自动生成下列'IBomController'内容
← Groovy 执行脚本实现 后端开发 →