VMware {code} Community
vedaniks
Enthusiast
Enthusiast
Jump to solution

Clone Session using Python

Hello,

I am trying to clone session from plugin UI to back end using python for back-end development. [basically this]

I've successfully retrieved the clone ticket from vCenter API endpoint and but struggling to clone the session using it.

I've come across this post which details the problem of cloning the session using clone ticket with python.

I've seen in the pyvmomi sample code where there is code to convert python suds cookies over to pyvmomi session cookies. 
The comments in the pyvmomi code note that there is no way to connect to call the CloneSession() function on vCenter through
pyvmomi without having already been logged in. Is this actually true?

This seems like a chicken or the egg problem. How can I call CloneSession() without already being logged in?
Because if I could be already logged then I wouldn't need to call CloneSession().

Any suggestions on what course to take to resolve this little puzzle?

 

If anyone has done the "Clone Session using Python" already, can you please show how to do it?

Any help is appreciated. Thanks in advance!

0 Kudos
1 Solution

Accepted Solutions
vedaniks
Enthusiast
Enthusiast
Jump to solution

The below code worked for me:

#imports
from pyVmomi import vim, vmodl
from pyVim import connect
import ssl

#Use below code after fetching session clone ticket from "/vcenter/session/clone-ticket" API
    if hasattr(ssl, '_create_unverified_context'):
        ssl._create_default_https_context = ssl._create_unverified_context
    stub = connect.SmartStubAdapter(host="vcenter-fqdn/IP")
    si = vim.ServiceInstance("ServiceInstance", stub)
    content = si.RetrieveContent()
    try:
        user_session = content.sessionManager.CloneSession(session_clone_ticket)
        session_id = si.content.sessionManager.currentSession.key
        auth_manager = si.content.authorizationManager
        root_folder = si.content.rootFolder
        privileges = ["VcIntegrity.lifecycleSettings.Read","VcIntegrity.Baseline.com.vmware.vcIntegrity.AssignBaselines","VcIntegrity.Baseline.com.vmware.vcIntegrity.ManageBaselines","VcIntegrity.General.com.vmware.vcIntegrity.Configure"]
        result = auth_manager.HasPrivilegeOnEntity(root_folder, session_id, privileges)
    except Exception as ex:
        print(f'Something went wrong cloning the session {ex}')

 

View solution in original post

Tags (1)
0 Kudos
2 Replies
vedaniks
Enthusiast
Enthusiast
Jump to solution

The below code worked for me:

#imports
from pyVmomi import vim, vmodl
from pyVim import connect
import ssl

#Use below code after fetching session clone ticket from "/vcenter/session/clone-ticket" API
    if hasattr(ssl, '_create_unverified_context'):
        ssl._create_default_https_context = ssl._create_unverified_context
    stub = connect.SmartStubAdapter(host="vcenter-fqdn/IP")
    si = vim.ServiceInstance("ServiceInstance", stub)
    content = si.RetrieveContent()
    try:
        user_session = content.sessionManager.CloneSession(session_clone_ticket)
        session_id = si.content.sessionManager.currentSession.key
        auth_manager = si.content.authorizationManager
        root_folder = si.content.rootFolder
        privileges = ["VcIntegrity.lifecycleSettings.Read","VcIntegrity.Baseline.com.vmware.vcIntegrity.AssignBaselines","VcIntegrity.Baseline.com.vmware.vcIntegrity.ManageBaselines","VcIntegrity.General.com.vmware.vcIntegrity.Configure"]
        result = auth_manager.HasPrivilegeOnEntity(root_folder, session_id, privileges)
    except Exception as ex:
        print(f'Something went wrong cloning the session {ex}')

 

Tags (1)
0 Kudos
Denis_Chorbadzh
VMware Employee
VMware Employee
Jump to solution

Hi vedaniks,

 

Thanks a lot for sharing a working python example for cloning a user session.

 

Best Regards,

Denis

0 Kudos