Harbor api 2.0 操作
harbor
Harbor 是一个开源的容器镜像仓库,用于存储和管理 Docker 镜像和其他容器镜像。 * 容器镜像仓库、存储和管理 Docker 镜像和其他容器镜像 * 有什么特点:支持多种镜像格式、易于使用、安全性和访问控制
项目地址:https://gitcode.com/gh_mirrors/ha/harbor
免费下载资源
·
Harbor api 2.0 操作
点关注不迷路,欢迎再访!
精简博客内容,尽量已行业术语来分享。
努力做到对每一位认可自己的读者负责。
帮助别人的同时更是丰富自己的良机。
目录
一.依赖
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-java-client</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
二.常用操作API
2.1. 获取harbor中所有的项目
/**
*
* @param env 环境
* @return
*/
public void getAllProject(String env) {
ApiClient apiClient = new ApiClient();
//指定环境
apiClient.setBasePath(env);
ArtifactApi apiInstance = new ArtifactApi(apiClient);
try {
List<io.swagger.client.model.Project> projects = apiInstance.listProjects();
logger.info("获取harbor中所有的项目成功");
} catch (ApiException e) {
logger.error("连接harbor API异常");
logger.error("异常:" + e);
}
}
2.2. 根据项目id获取该项目下所有的镜像仓库
/**
*
* @param env 环境
* @param projectId 项目id
* @return
*/
public void getAllProjectDetail(String projectId, String env) {
ApiClient apiClient = new ApiClient();
apiClient.setBasePath(env);
try {
RepositoryApi repositoryApi = new RepositoryApi(apiClient);
ArtifactApi apiInstance = new ArtifactApi(apiClient);
//查询所有项目
List<io.swagger.client.model.Project> projects= apiInstance.listProjects();
//根据projectId过滤项目
projects = projects.stream().filter(project -> project.getProjectId() == Integer.parseInt(projectId)).collect(Collectors.toList());
if(projects != null && projects.size() > 0){
Project project = projects.get(0);
//分页查询
List<Repository> repositorys = repositoryApi.listRepositories(project.getName(), null, null, 1L, 50L);
logger.info("获取该项目下所有的镜像仓库成功");
}
} catch (ApiException e) {
logger.error("连接harbor API异常");
logger.error("异常:"+e);
}
}
2.3. 根据应用(镜像)名称查询harbor私有仓库镜像镜像
/**
*
* @param env 环境
* @param repoName 应用名
* @return
*/
public List<DockerHarborImage> getImagesRepoName(String env, String repoName) {
List<HarborProject> projects = getAllProject(env);
List<HarborProjectDetail> projectDetails = new ArrayList<HarborProjectDetail>();
for (HarborProject project : projects) {
for(String name:names){
projectDetails.addAll(getAllProjectDetail(String.valueOf(project.getProjectId()), env));
}
}
List<DockerHarborImage> harborImages = new ArrayList<DockerHarborImage>();
for (HarborProjectDetail projectDetail : projectDetails) {
String name = projectDetail.getName();
if (repoName.equals(name.split(CommonConstant.IMAGE_SIGN_TWO)[1])) {
harborImages.addAll(getAllHarborImage(projectDetail.getName(), env));
}
}
return harborImages;
}
/**
*
* @param projectId
* @param env
* @author andy
* @return
*/
private List<HarborProjectDetail> getAllProjectDetail(String projectId, String env) {
List<HarborProjectDetail> harborProjectDetail = null;
ApiClient apiClient = new ApiClient();
apiClient.setBasePath(env);
try {
RepositoryApi repositoryApi = new RepositoryApi(apiClient);
ArtifactApi apiInstance = new ArtifactApi(apiClient);
List<io.swagger.client.model.Project> projects= apiInstance.listProjects();
projects = projects.stream().filter(project -> project.getProjectId() == Integer.parseInt(projectId)).collect(Collectors.toList());
if(projects != null && projects.size() > 0){
Project project = projects.get(0);
List<Repository> repositorys = repositoryApi.listRepositories(project.getName(), null, null, 1L, 200L);
harborProjectDetail = changeRepository(repositorys);
logger.info("获取该项目下所有的镜像仓库成功");
}
} catch (ApiException e) {
logger.error("连接harbor API异常");
logger.error("异常:"+e);
}
return harborProjectDetail;
}
/**
*
* @param projectDetailName 镜像仓库名称
* @param env 环境
* @author andy
* @return
*/
public List<DockerHarborImage> getAllHarborImage(String projectDetailName, String env) {
List<DockerHarborImage> imageList = null;
ApiClient apiClient = new ApiClient();
apiClient.setBasePath(env);
try {
ArtifactApi apiInstance = new ArtifactApi(apiClient);
if (StringUtils.isNotBlank(projectDetailName) && projectDetailName.contains(CommonConstant.IMAGE_SIGN_ONE)) {
projectDetailName = projectDetailName.replace(CommonConstant.IMAGE_SIGN_ONE, CommonConstant.IMAGE_SIGN_TWO);
}
String projectName = projectDetailName.substring(CommonConstant.NUMBER_ZERO, projectDetailName.indexOf(CommonConstant.IMAGE_SIGN_TWO));
String repositoryName = projectDetailName.substring(projectDetailName.indexOf(CommonConstant.IMAGE_SIGN_TWO)+CommonConstant.NUMBER_ONE);
//分页
List<Artifact> artifactlist = apiInstance.listArtifacts(projectName, repositoryName, null, null, 1L, 50L, null, null, null, null, null);
//封装镜像信息,略过
imageList = getImagesArtifact(artifactlist,projectDetailName);
//过滤tag编号为空的镜像
imageList = imageList.stream().filter(image -> image.getTag() != null).collect(Collectors.toList());
logger.info("根据镜像仓库名称获取所有的镜像成功");
} catch (ApiException e) {
logger.error("连接harbor API异常");
logger.error("异常:"+e);
}
return imageList;
}
2.4. 删除镜像
/**
*
*<p>
*description: 删除 镜像
*</p>
*@param projectName 项目名,repositoryName 镜像仓库名
*@param tag 镜像tag:2.0.0.1
*@param env 环境
* @author andy
*/
public Response<Object> deleteImage(String projectName,String repositoryName,String tag, String env) {
ApiClient apiClient = new ApiClient();
apiClient.setBasePath(env);
//连接harbor认证
HttpBasicAuth basic = (HttpBasicAuth) apiClient.getAuthentication("basic");
basic.setUsername(用户名);
basic.setPassword(密码);
ArtifactApi apiInstance = new ArtifactApi(apiClient);
try {
apiInstance.deleteTag(projectName, repositoryName, tag, tag, null);
} catch (ApiException e) {
logger.error("连接harbor API异常");
logger.error("异常:"+e);
}
return new Response(1, "OK");
}
2.5. 推送镜像到harbor仓库
public Response<Object> pushImage(String name, String tag) {
//从对应位置先拉取镜像,然后推送到指定的harbor环境
docker push
return new Response<Object>(1, "镜像:" + nameTag + "推送成功!");
}
三.harbor api 2.0 源码分享
链接:https://pan.baidu.com/s/1TPiSy1SB6f42sbqizN9QJA
提取码:81yv
GitHub 加速计划 / ha / harbor
10
3
下载
Harbor 是一个开源的容器镜像仓库,用于存储和管理 Docker 镜像和其他容器镜像。 * 容器镜像仓库、存储和管理 Docker 镜像和其他容器镜像 * 有什么特点:支持多种镜像格式、易于使用、安全性和访问控制
最近提交(Master分支:4 个月前 )
a548ab70
Add the field extra_attrs to the p2p preheat policy for the provider to
define their specified parameters when preheating.
Signed-off-by: chlins <chlins.zhang@gmail.com> 9 天前
e4178753
The export CVE permission should be included in the project scope, as the API relies on project-level judgment.
Signed-off-by: wang yan <wangyan@vmware.com> 10 天前
更多推荐
已为社区贡献2条内容
所有评论(0)