Skip to content

Commit

Permalink
if convert template to vm, do not check if it is template
Browse files Browse the repository at this point in the history
  • Loading branch information
h56983577 committed Dec 31, 2023
1 parent 1100c79 commit db69bb1
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/service/Vm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class VmService(val call: ApplicationCall) : IService {
?: throw NotFoundException("VirtualMachine($uuid) is not found")
}

fun getVmByUUid(uuid: String): VirtualMachineCrd {
return vmKubeClient.inAnyNamespace().withField("status.uuid", uuid).list().items.filterNot { it.spec.deleted }
fun getTemplateByUUID(uuid: String): VirtualMachineCrd {
return vmKubeClient.inAnyNamespace().list().items.filter { it.status.uuid == uuid }.filterNot { it.spec.deleted }
.firstOrNull()
?: throw NotFoundException("VirtualMachine($uuid) is not found")
?: throw NotFoundException("Template($uuid) is not found")
}

fun deleteVm(id: String) {
Expand Down Expand Up @@ -345,15 +345,19 @@ class VmService(val call: ApplicationCall) : IService {
if (vm.powerState.value.lowercase() != "poweredoff") {
throw BadRequestException("VM is not powered off")
}
if (vm.isTemplate) {
if (isTemplate && vm.isTemplate) {
throw BadRequestException("VM is already a template")
}
// 检查template名称是否重复
// if (mysql.virtualMachines.exists { it.isTemplate.eq(true) and it.name.eq(name) }) {
// throw BadRequestException("template name already exists")
// }
val vmCrd = getVmByUUid(uuid)
vmCrd.spec = vmCrd.spec.copy(template = true)
val vmCrd = if (isTemplate) {
getVmByUUID(crdId)
} else {
getTemplateByUUID(uuid)
}
vmCrd.spec = vmCrd.spec.copy(template = isTemplate)
vmKubeClient.resource(vmCrd).patch()
val owner = call.user()
val ownerId = if (owner.id == "admin") {
Expand Down

0 comments on commit db69bb1

Please sign in to comment.