0

I am trying to build a web application to automate the creation of some resource pools, folder and VMs on my vCenter. I have a datacenter and inside that a cluster of 3 hosts. This is how it looks like :

  • vcs.domain
    • datacenter
      • cluster
        • resource pool 1
        • resource pool 2

I want to create a third resource pool using the REST API, but i can't make it to work. I am struggling to find what the "parent" field is.

I don't understand what this "parent" field means. I am also struggling to create vm-folders and dvs-portgroups

What I already do :

  • authenticate and get a token to talk
  • GET requests work as expected
  • POST request to /api/vcenter/resource-pool with several bodies for ex : {"name": "test", "parent": "cluster"} even with all non-required fields

I am getting this response :

{
    "error_type": "NOT_FOUND",
    "messages": [
        {
            "args": [],
            "default_message": "Resource pool with identifier '{}' does not exist.",
            "id": "com.vmware.api.vcenter.resourcepool.not_found"
        }
    ]
}

1 Answers1

0

According to the documentation:

[...] the field must be an identifier for the resource type: ResourcePool.[...]

So you need to pass the parent id (not the name) of a ResourcePool:

What you could try is to create a resource pool and insert the others inside like so:

  • cluster
    • ResourcePools
      • resource pool 1
      • resource pool 2

Then you can retrieve the identifier from it doing a call to: {api_host}/api/vcenter/resource-pool

Then deserialize the response to get a list of resource pools. The identifier comes under the attribute "resource_pool"

ResourcePool.cs class in C#

using Newtonsoft.Json;

namespace ProjectName.Models
{
    public class ResourcePool
    {
        [JsonProperty("resource_pool")]
        public string ID { get; set; }
        public string Name { get; set; }
    }
}

API Call in C#:

using HttpClientHandler httpClientHandler = new HttpClientHandler();
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
using HttpClient client = new HttpClient(httpClientHandler);

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, VSphereAddress + "/rest/vcenter/resource-pool");
request.Headers.Add("vmware-api-session-id", SessionID);

Task<HttpResponseMessage> task = client.SendAsync(request);
HttpResponseMessage response = task.Result;
response.EnsureSuccessStatusCode();

string responseBody = response.Content.ReadAsStringAsync().Result;
List<ResourcePool> pools = JsonConvert.DeserializeObject<List<ResourcePool>>(JObject.Parse(responseBody)["value"].ToString());

ResourcePool resourcePool = pools.Where(pool => pool.Name.Equals("ResourcePools")).FirstOrDefault();
string identifier = resourcePool.ID;  

Then use this identifier in your API post call

Marc
  • 19
  • 6