Need to map groovy code for -d part of this API from vmware

49 views Asked by At

curl -X PATCH -H "vmware-api-session-id: b00db39f948d13ea1e59b4d6fce56389" -H "Content-Type: application/json" -d '{"spec":{"cores_per_socket":0,"count":0,"hot_add_enabled":false,"hot_remove_enabled":false}}' https://{api_host}/rest/vcenter/vm/{vm}/hardware/cpu

I have the code for session id and URL , I need to map the code for -d part where inputs are to be provided like cpu count and all.

1

There are 1 answers

0
tim_yates On

This should do it, so long as you're running on Java 11+

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()