Skip to content

Commit

Permalink
🚧 Try to connect the vCenter service
Browse files Browse the repository at this point in the history
  • Loading branch information
loheagn committed Apr 13, 2022
1 parent 1d8f77a commit 0519a1a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
package cn.edu.buaa.scs.controller.plugins

import cn.edu.buaa.scs.utils.InstantSerializer
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import io.ktor.application.*
import io.ktor.features.*
import io.ktor.jackson.*
import org.ktorm.jackson.KtormModule
import java.time.Instant

fun Application.configureContentNegotiation() {
install(ContentNegotiation) {
jackson {
registerModule(KtormModule())
registerModule(JavaTimeModule().apply {
addSerializer(
Instant::class.java,
InstantSerializer()
)
})
registerModule(Jdk8Module())
}
}
}
12 changes: 0 additions & 12 deletions src/main/kotlin/cn/edu/buaa/scs/utils/Time.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package cn.edu.buaa.scs.utils

import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter

const val defaultDateTimeFormatter = "yyyy/MM/dd HH:mm:ss"
Expand All @@ -27,12 +23,4 @@ fun Long.formatDateTime(formatString: String = defaultDateTimeFormatter): String
object TimeUtil {
fun currentDateTime(): String =
System.currentTimeMillis().formatDateTime()
}

class InstantSerializer : StdSerializer<Instant>(Instant::class.java) {
override fun serialize(value: Instant?, gen: JsonGenerator?, provider: SerializerProvider?) {
val formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")
.withZone(ZoneOffset.ofHours(8))
gen?.writeString(value?.let { formatter.format(it) })
}
}
4 changes: 4 additions & 0 deletions src/main/kotlin/cn/edu/buaa/scs/vm/IVmClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package cn.edu.buaa.scs.vm

interface IVmClient {
}
77 changes: 77 additions & 0 deletions src/main/kotlin/cn/edu/buaa/scs/vm/OldVmClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cn.edu.buaa.scs.vm

import cn.edu.buaa.scs.utils.getConfigString
import io.ktor.application.*
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.features.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.http.content.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.ByteArrayInputStream
import java.net.URLDecoder
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory

object OldVmClient : IVmClient {
internal lateinit var svcPath: String

private val client: HttpClient by lazy {
HttpClient(CIO) {
engine {
maxConnectionsCount = 1000
endpoint {
maxConnectionsPerRoute = 100
pipelineMaxSize = 20
keepAliveTime = 5000
connectTimeout = 5000
connectAttempts = 5
}

}
defaultRequest {
header("SOAPAction", "\"\"")
}
}
}

private suspend fun <T> baseAction(req: String, respTag: String, convert: (String) -> T): T {
val resp = client.post<String>(svcPath) {
body = TextContent(req, contentType = ContentType.Text.Xml)
}
val builder: DocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
val doc = withContext(Dispatchers.IO) {
ByteArrayInputStream(resp.toByteArray(Charsets.UTF_8)).use {
builder.parse(it)
}
}
val node = doc.getElementsByTagName(respTag).item(0)
return convert(URLDecoder.decode(node.textContent, Charsets.UTF_8))
}

suspend fun getVmInfo(name: String): String {
return baseAction(
"""
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:vcm="http://vm.manager.buaa.edu.cn/VCManager">
<soapenv:Header/>
<soapenv:Body>
<vcm:GetVMInfo>
<name>$name</name>
</vcm:GetVMInfo>
</soapenv:Body>
</soapenv:Envelope>
""".trimIndent(),
"GetVMInfoReturn"
) {
it
}
}
}

@Suppress("unused")
fun Application.oldVmClientModule() {
val endpoint = getConfigString("oldVmManager.endpoint")
OldVmClient.svcPath = endpoint
}
17 changes: 17 additions & 0 deletions src/test/kotlin/cn/edu/buaa/scs/vm/OldVmClientTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cn.edu.buaa.scs.vm

import cn.edu.buaa.scs.testEnv
import io.ktor.server.testing.*
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test

class OldVmClientTest {
@Test
fun testGetVmInfo() {
withApplication(testEnv) {
runBlocking {
println(OldVmClient.getVmInfo("debian11-ks-2"))
}
}
}
}

0 comments on commit 0519a1a

Please sign in to comment.