-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚧 Try to connect the vCenter service
- Loading branch information
Showing
5 changed files
with
98 additions
and
23 deletions.
There are no files selected for viewing
11 changes: 0 additions & 11 deletions
11
src/main/kotlin/cn/edu/buaa/scs/controller/plugins/ContentNegotiation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package cn.edu.buaa.scs.vm | ||
|
||
interface IVmClient { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} | ||
} | ||
} | ||
} |