pythonvmwarevspherevcenter

How to check for vCenter Server updates with the vSphere Python API?


I want to automate the checking of new vCenter Server updates with the vSphere Python API with the intent in mind to then be able to decide with a prompt if I should update or not.

I checked for existing scripts and went through the API documentation but the python examples are scarce and the vCenter Server update/upgrade category is very lackluster and lacks any python code. IS it even possible to achieve what I want from python? Even if it's something else, it would still be fine, but the goal is not to have to upgrade from the Vsphere client itself.


Solution

  • Without leveraging the Python API but using the REST API instead, I managed to achieve my goal using this:

    import requests
    import json
    
    import config
    
    def main():
        session = requests.Session()
    
        sess = session.post(f"https://{config.server}/rest/com/vmware/cis/session", auth=(config.username, config.password), verify=False)
        session_id = sess.json()['value']
    
        get_upgrade_url = f"https://{config.server}/api/vcenter/deployment/upgrade"
        response = session.get(get_upgrade_url, headers={'vmware-api-session-id': session_id}, verify=False)
    
        ########### JSON Handling
        json_object = json.loads(response.text)
        json_formatted_str = json.dumps(json_object, indent=2)
        print(json_formatted_str)
        ######################
    
        check_upgrade_url = f"https://{config.server}/rest/appliance/update/pending?source_type=LAST_CHECK"
        check_response = session.get(check_upgrade_url, headers={'vmware-api-session-id': session_id}, verify=False)
        print(check_response)
    
        ########### JSON Handling
        json_object = json.loads(check_response.text)
        json_formatted_str = json.dumps(json_object, indent=2)
        print(json_formatted_str)
        ######################
    
    if __name__ == "__main__":
        main()