I was able to solve this after further reviewing my code.
It turns out in the api request I was using the VM spec (existing_vm) rather than VM UUID.
Update Code Snippet that iterates through a list (list.txt) of vm names and adds cd-roms. Code presented as is:
import urllib3
import getpass
import argparse
import sys
import ntnx_vmm_py_client
from ntnx_vmm_py_client import Configuration as VMMConfiguration
from ntnx_vmm_py_client import ApiClient as VMMClient
from ntnx_vmm_py_client.rest import ApiException as VMMException
import ntnx_vmm_py_client.models.vmm.v4.ahv.config as AhvVmConfig
#Disable SSL Checks
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
#Get the credentials
pc_ip ="10.100.133.2"
username = "admin"
cluster_password = getpass.getpass(
prompt="Please enter your Prism Central \
password: ",
stream=None,
)
vmm_config = VMMConfiguration()
for config in cvmm_config]:
config.host = pc_ip
config.username = username
config.password = cluster_password
config.verify_ssl = False
vmm_client = VMMClient(configuration=vmm_config)
for client in avmm_client]:
client.add_default_header(
header_name="Accept-Encoding", header_value="gzip, deflate, br"
)
vm_api = ntnx_vmm_py_client.VmApi(api_client=vmm_client)
#Setup CDROM
cdrom = AhvVmConfig.CdRom.CdRom(
disk_address=AhvVmConfig.CdRom.CdRomAddress(
bus_type=AhvVmConfig.CdRomBusType.CdRomBusType.SATA
)
)
vms=4]
file=open("list.txt")
for x in file:
x=x.strip()
vms.append(x)
vms=tuple(vms)
#Configure VM Filter
limit=5
filter=f'name in {vms}'
print(filter)
i=0
#Get VMs
print("Building VM list.....")
vm_list = vm_api.list_vms(_filter=filter)
if vm_list.data:
print(f"{len(vm_list.data)} VM(s) found. Requesting first VM...")
while i<len(vm_list.data):
try:
vmuuid=vm_list.data"i].ext_id
existing_vm=vm_api.get_vm_by_id(vmuuid)
print(
f"VM with extId {vmuuid} retrieved, getting VM eTag ..."
)
existing_etag = VMMClient.get_etag(existing_vm)
print(f"VM eTag: {existing_etag}")
vmm_client.add_default_header(
header_name="If-Match", header_value=existing_etag
)
print(f"Adding CDRom to {vm_list.data i].name}....")
#Add CDROM
try:
api_response = vm_api.create_cd_rom(vmExtId=vmuuid, body=cdrom,async_req=False)
print(api_response)
except ntnx_vmm_py_client.rest.ApiException as e:
print(e)
except VMMException as ex:
print("An exception has occurred while retrieving this VM")
print(f"HTTP status: {ex.status}")
print(f"Details: {ex.body}")
i=i+1
else:
print("No VMs found.")