Question

Entity CAS version mismatch when "Invoke-WebRequest" - Vms

  • 14 October 2019
  • 1 reply
  • 1731 views

Badge
We have below script which is changing VM's project from default to some other project but after we execute invoke-webrequest, we notice that it will change Vm's "spec_version" to different number than it's in out $body variable. That's the reason why invoke-webrequest is given below error. Just before we run invoke-webrequest, we checked that vm's configuration metadata "spec_version" is same than in $body that we are trying invoke. So it seems that invoke-webrequest is somehow first changing the vm's spec_version in vm's configuration data (in our case to 18) and after that trying to invoke spec_version = 16 on top of that. But because the version should be same, it's failing. Same error occured when you are trying the same in REST Explorer - "vms".

SCRIPT:
Import-Module d:\temp\PSNutanixPrismCentral.psm1
Invoke-NutanixPrismCentralLogin -Username "username@xxx.local" -Uri "https://prismcentral.xxxx.local:9440"

# Get non default projects
$Projects = Get-NutanixPrismCentralProjects
$NonDefaultProjects = $Projects| Where-Object {$_.Name -ne "default"}

# Get VMs on project default
$VMs = Get-NutanixPrismCentralVMs
$VMsOnDefaultProject = $VMs | Where-Object {$_.Project -eq "default"}

# Change project for testmachine
$vm = $vms |?{$_.name -eq "testmachine"}
$ProjectId = 1

$VM.config.metadata.project_reference.name = $Projects[$ProjectId].name
$VM.config.metadata.project_reference.uuid = $Projects[$ProjectId].uuid

$Body = $VM.config | ConvertTo-JSON -Depth 10

Invoke-WebRequest -Uri "$NutanixPrismCentralUrl/api/nutanix/v3/vms/$($VM.uuid)" -Headers $NutanixAuthenticationHeaders -Method PUT -Body $Body -ContentType "application/json"

ERROR:
Invoke-WebRequest : { "api_version": "3.1", "code": 409, "message_list": [ { "message": "Edit conflict: please retry change. Entity CAS version mismatch.", "reason": "CONCURRENT_REQUESTS_NOT_ALLOWED" } ], "state": "ERROR" }
At D:\temp\kopratest.ps1:63 char:2
+ Invoke-WebRequest -Uri "$NutanixPrismCentralUrl/api/nutanix/v3/vms/$ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand


BODY (variable that we are trying to invoke):
{
"spec": {
"cluster_reference": {
"kind": "cluster",
"name": "our cluster name",
"uuid": "00077c5f-2e66-241a-461e-ac1fgbc0c11f"
},
"description": "Used for testing project script)",
"resources": {
"num_threads_per_core": 1,
"vnuma_config": {
"num_vnuma_nodes": 0
},
"serial_port_list": [
],
"nic_list": [
],
"num_vcpus_per_socket": 1,
"num_sockets": 1,
"vga_console_enabled": true,
"gpu_list": [
],
"is_agent_vm": false,
"memory_size_mib": 3,
"boot_config": {
"boot_device_order_list": [
"CDROM",
"DISK",
"NETWORK"
],
"boot_type": "LEGACY"
},
"hardware_clock_timezone": "UTC",
"disable_branding": false,
"power_state": "OFF",
"power_state_mechanism": {
"guest_transition_config": {
"should_fail_on_script_failure": false,
"enable_script_exec": false
},
"mechanism": "HARD"
},
"disk_list": [
]
},
"name": "testmachine"
},
"metadata": {
"last_update_time": "2019-10-10T10:05:06Z",
"kind": "vm",
"uuid": "25e8ff83-258c-4a77-b716-b46af3777899",
"project_reference": {
"kind": "project",
"name": "projectname",
"uuid": "4e66fe37-04fb-4555-9b70-c7ab069eb267"
},
"creation_time": "2019-10-10T08:08:45Z",
"spec_version": 16,
"categories_mapping": {
"ServerType": [
"Other"
]
},
"owner_reference": {
"kind": "user",
"uuid": "00000000-0000-0000-0000-000000000000",
"name": "admin"
},
"categories": {
"ServerType": "Other"
}
}
}

1 reply

Badge

Hi, 

Actually, I tried different things with theses API and it’s looks that nothing works really well.

I don’t know if it’s a good idea to use their rest api.

Following the instruction of Nutanix and trying only with REST API, i did :

  • Call REST API : GET to get all the informations of the vm
  • Edit the Json with the desired informations + remove the “status” part of the Json (the spec_version is already in the json)
  • Call REST API : PUT to update the vms information

After this, the scripts works and the vms are updated. But the updated vms are in status “Entity CAS version mismatch” with a warning in Prism central.

Hope it can be helpfull, Silvin

 

Reply