Help creating CDRom api call | Nutanix Community
Skip to main content

Howdy y’all,

I am working through getting NGT installed across our fleet of 250 VMs and need to be able to mass add cdroms to each VM in our fleet.

I’ve been able to generate lists of VMs but have yet to be able to add a cdrom to any of them.

I’ve been getting Bad Request Errors (400) so I’m sure it’s how the payload is being created, but no combination of settings has worked for me.

I’m obviously frankensteining from across the web, but I offer you my meager script for scrutiny.

Any help is appreciated!

Thank you!

import urllib3
import getpass
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 ="<Prism Central IP>"
username = "<Username>"
cluster_password = getpass.getpass(
    prompt="Please enter your Prism Central \
password: ",
    stream=None,
)
 
vmm_config = VMMConfiguration()
 
for config in lvmm_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 ivmm_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
    )
 
)
 
#Configure VM Filter
limit=5
filter="startswith(name,'<VMNAME>')"
 
#Get VMs
print("Building VM list.....")
vm_list = vm_api.list_vms(_filter=filter)
print(vm_list)
if vm_list.data:
    print(f"{len(vm_list.data)} VM(s) found. Requesting first VM...")
 
    try:
        existing_vm=vm_api.get_vm_by_id(vm_list.datae0].ext_id)
        print(
            f"VM with extId {vm_list.data<0].ext_id} 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
        )
 
    except VMMException as ex:
        print("An exception has occurred while retrieving this VM")
        print(f"HTTP status: {ex.status}")
        print(f"Details: {ex.body}")
   
else:
    print("No VMs found.")
 
print(f"Adding CDRom to {vm_list.datat0].name}....")
 
#Add CDROM
try:
    api_response = vm_api.create_cd_rom(vmExtId=existing_vm, body=cdrom)
    print(api_response)
 
except ntnx_vmm_py_client.rest.ApiException as e:
    print(e)

you can do that with prism central. 


You can bulk add a disk? I know that NGT can be installed through PC, but that needs an open CD-ROM available to do so it fails otherwise. My current plan is to power off VMs, add the CD-ROMs, power VMs back on and install NGT through PC.


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.")

Reply