Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加了删除镜像接口 #29

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 52 additions & 10 deletions openapi/cloudapi_v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ paths:
description: 获取课程信息
security:
- Authorization: []
'/courses':
/courses:
get:
summary: 管理员获取所有课程
tags:
Expand All @@ -455,7 +455,7 @@ paths:
$ref: '#/components/schemas/CourseResponse'
operationId: get-course-list
security:
- Authorization: [ ]
- Authorization: []
description: 管理员获取所有课程
'/stat/exp/{expId}/assignments':
parameters:
Expand Down Expand Up @@ -1490,6 +1490,29 @@ paths:
description: 获取当前项目中的所有可用镜像
tags:
- 镜像
delete:
summary: 删除镜像
operationId: delete-project-projectId-images
responses:
'200':
description: OK
description: ' 删除镜像。'
security:
- Authorization: []
requestBody:
content:
application/json:
schema:
type: object
properties:
repo:
type: string
tag:
type: string
required:
- repo
- tag
description: ''
'/project/{projectId}/containers':
parameters:
- schema:
Expand Down Expand Up @@ -1626,6 +1649,25 @@ paths:
description: 获取当前项目下的所有Harbor镜像仓库
security:
- Authorization: []
delete:
summary: 删除镜像仓库
operationId: delete-project-projectId-imageRepos
responses:
'200':
description: OK
description: 删除镜像仓库
requestBody:
content:
application/json:
schema:
type: object
properties:
repo:
type: string
required:
- repo
security:
- Authorization: []
/captcha:
get:
summary: 获取验证码
Expand Down Expand Up @@ -1869,7 +1911,7 @@ paths:
tags:
- 容器服务
security:
- Authorization: [ ]
- Authorization: []
get:
summary: 获取容器服务
operationId: get-project-projectId-containers-containerServiceId
Expand All @@ -1881,7 +1923,7 @@ paths:
schema:
$ref: '#/components/schemas/ContainerServiceResponse'
security:
- Authorization: [ ]
- Authorization: []
description: 根据ID获取容器服务
tags:
- 容器服务
Expand All @@ -1906,7 +1948,7 @@ paths:
operationId: get-resourceUsedRecords-resourceUsedRecordId
description: 获取资源使用记录
security:
- Authorization: [ ]
- Authorization: []
parameters:
- schema:
type: boolean
Expand Down Expand Up @@ -1939,7 +1981,7 @@ paths:
operationId: get-resourcePool-resourcePoolId
description: 通过ID获取资源池信息
security:
- Authorization: [ ]
- Authorization: []
'/stat/resourcePools/{resourcePoolId}/used':
parameters:
- schema:
Expand Down Expand Up @@ -1973,7 +2015,7 @@ paths:
operationId: get-stat-resourcePools-resourcePoolId-used
description: 统计资源池的资源使用情况
security:
- Authorization: [ ]
- Authorization: []
/containerServiceTemplates:
get:
summary: 获取容器服务模板列表
Expand Down Expand Up @@ -2011,7 +2053,7 @@ paths:
operationId: get-containerServiceTemplates
description: 获取容器服务模板列表
security:
- Authorization: [ ]
- Authorization: []
/containerServices:
get:
summary: 获取所有容器服务列表
Expand All @@ -2029,7 +2071,7 @@ paths:
operationId: get-containerServices
description: 获取当前用户所归属的所有项目的所有容器服务列表的总和
security:
- Authorization: [ ]
- Authorization: []
'/project/{projectId}/containers/fromTemplate':
parameters:
- schema:
Expand All @@ -2047,7 +2089,7 @@ paths:
- 容器服务
description: 从模板创建容器服务
security:
- Authorization: [ ]
- Authorization: []
requestBody:
content:
application/json:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* cloudapi_v2
* buaa scs cloud api v2
*
* The version of the OpenAPI document: 2.0
* Contact: loheagn@icloud.com
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package cn.edu.buaa.scs.controller.models


/**
*
* @param repo
*/
data class DeleteProjectProjectIdImageReposRequest(
val repo: kotlin.String
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* cloudapi_v2
* buaa scs cloud api v2
*
* The version of the OpenAPI document: 2.0
* Contact: loheagn@icloud.com
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package cn.edu.buaa.scs.controller.models


/**
*
* @param repo
* @param tag
*/
data class DeleteProjectProjectIdImagesRequest(
val repo: kotlin.String,
val tag: kotlin.String
)

34 changes: 17 additions & 17 deletions src/main/kotlin/cn/edu/buaa/scs/controller/models/Project.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/**
* cloudapi_v2
* buaa scs cloud api v2
*
* The version of the OpenAPI document: 2.0
* Contact: loheagn@icloud.com
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
* cloudapi_v2
* buaa scs cloud api v2
*
* The version of the OpenAPI document: 2.0
* Contact: loheagn@icloud.com
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package cn.edu.buaa.scs.controller.models


/**
*
* @param id
* @param name
* @param owner
*
* @param id
* @param name
* @param owner
* @param token 当前该用户访问该Project中的资源,例如clone代码仓库、push和pull镜像等,所需要使用的token,即密码
* @param createdTime
* @param createdTime
* @param displayName 区别于name之外的一个格式随便的,便于记忆和展示的名字
* @param description
* @param expId
* @param description
* @param expId
*/
data class Project(
val id: kotlin.Long,
Expand Down
17 changes: 14 additions & 3 deletions src/main/kotlin/cn/edu/buaa/scs/harbor/HarborClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import cn.edu.buaa.scs.sdk.harbor.infrastructure.ClientException
import cn.edu.buaa.scs.sdk.harbor.models.*
import io.ktor.http.*


object HarborClient : IProjectManager {

private val projectClient by lazy { ProjectApi() }
Expand All @@ -27,7 +28,19 @@ object HarborClient : IProjectManager {
).filter { artifact -> artifact.type?.lowercase() == "image" }
}
}

fun deleteImageRepo(projectName:String,repositoryName: String): Result<Unit> = runCatching {
repoClient.deleteRepository(
projectName = projectName,
repositoryName = repositoryName
)
}
fun deleteImage(projectName: String, repositoryName: String,reference: String ): Result<Unit> = runCatching {
artifactClient.deleteArtifact(
projectName = projectName,
repositoryName = repositoryName,
reference = reference
)
}
override suspend fun createUser(userID: String, realName: String, email: String, password: String): Result<String> =
runCatching {
if (userClient.searchUsers(userID).isNotEmpty()) return Result.success(userID)
Expand Down Expand Up @@ -73,9 +86,7 @@ object HarborClient : IProjectManager {
deleteProjectMember(projectName, memberID)
}

fun deleteTag() {

}

private fun existProject(projectName: String): Boolean {
return try {
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/cn/edu/buaa/scs/model/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ interface User : Entity<User>, IEntity {
.and(it.role.eq(ProjectRole.ADMIN) or it.role.eq(ProjectRole.OWNER))
} ||
isAdmin()
fun isProjectMember(projectID: Long): Boolean = isProjectMember(Project.id(projectID))

fun isProjectMember(project: Project): Boolean =
isProjectAdmin(project)||mysql.projectMembers.exists {
it.projectId.eq(project.id) and it.userId.eq(this.id)
}

fun personalProjectName() = "personal-${this.id.lowercase()}"

Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/cn/edu/buaa/scs/route/Project.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import cn.edu.buaa.scs.service.id
import cn.edu.buaa.scs.service.project
import cn.edu.buaa.scs.storage.mysql
import cn.edu.buaa.scs.utils.user
import com.fkorotkov.openshift.newAWSResourceTag
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
Expand Down Expand Up @@ -112,6 +113,12 @@ fun Route.projectRoute() {
convertImageBuildTaskResponse(imageMeta, taskData)
)
}
delete{
val projectID = call.getProjectID()
val req =call.receive<DeleteProjectProjectIdImagesRequest>()
call.project.deleteImage(projectID, req.repo, req.tag)
call.respond("OK")
}
}

route("/imageBuildTasks") {
Expand All @@ -128,6 +135,12 @@ fun Route.projectRoute() {
call.project.getImageReposByProject(call.getProjectID())
)
}
delete{
val projectID = call.getProjectID()
val req = call.receive<DeleteProjectProjectIdImageReposRequest>();
call.project.deleteImageRepo(projectID,req.repo)
call.respond("OK")
}
}

route("/containers") {
Expand Down
16 changes: 16 additions & 0 deletions src/main/kotlin/cn/edu/buaa/scs/service/Project.kt
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,15 @@ class ProjectService(val call: ApplicationCall) : IService, FileService.FileDeco
pullCommand = "docker pull ${ImageMeta.hostPrefix}/${repoName}@${this.digest}",
)


fun deleteImage(projectID: Long, repositoryName: String,reference: String ){
val project = Project.id(projectID)
if(!call.user().isProjectMember(project)){
throw AuthorizationException("You are not the project member")
}
HarborClient.deleteImage(project.name,repositoryName,reference)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里虽然不用检查是不是Admin,但必须检查是不是project的member。下面那个函数也是。

}

fun getImagesByProject(projectID: Long): List<Image> {
val project = Project.id(projectID)
call.user().assertRead(project)
Expand All @@ -373,6 +382,13 @@ class ProjectService(val call: ApplicationCall) : IService, FileService.FileDeco
)
}
}
fun deleteImageRepo(projectID: Long, repositoryName: String){
val project = Project.id(projectID)
if(!call.user().isProjectMember(project)){
throw AuthorizationException("You are not the project member")
}
HarborClient.deleteImageRepo(project.name,repositoryName)
}

fun getImageBuildTasksByProject(projectID: Long): List<Pair<ImageMeta, TaskData>> {
val project = Project.id(projectID)
Expand Down