需要从vmwareMap此API的-d部分的groovy代码

qncylg1j  于 2022-11-01  发布在  其他
关注(0)|答案(1)|浏览(131)

curl -X PATCH -H“VMware应用程序工作阶段识别码:”-H“内容类型:应用程序/json”-d '{“规格”:{“每个插槽的核心数”:0,“计数”:0,“启用热添加”:假,“启用热删除”:假}}' https://{api_host}/rest/vcenter/vm/{vm}/硬件/cpu
我有会话ID和URL的代码,我需要Map-d部分的代码,其中输入将被提供,如cpu计数和所有。

czq61nw1

czq61nw11#

只要您运行的是Java11+,就应该这样做

import groovy.json.JsonOutput

import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse

def body = [
        spec: [
                cores_per_socket  : 0,
                count             : 0,
                hot_add_enabled   : false,
                hot_remove_enabled: false
        ]
]

def payload = HttpRequest.BodyPublishers.ofString(JsonOutput.toJson(body))

HttpClient httpClient = HttpClient.newHttpClient()

HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://{api_host}/rest/vcenter/vm/{vm}/hardware/cpu"))
        .method("PATCH", payload)
        .header("Content-Type", "application/json")
        .header("vmware-api-session-id", "b00db39f948d13ea1e59b4d6fce56389")
        .build();

HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());

println response.body()

相关问题