Skip to content

Commit a94fff4

Browse files
authored
Update sample template (#25)
Use service manager to connect to both vAPI and VIM services Signed-off-by: Tianhao He <[email protected]>
1 parent 44a785b commit a94fff4

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

samples/vsphere/common/sample_template.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616
__author__ = 'VMware, Inc.'
1717
__vcenter_version__ = '6.5+'
1818

19-
import atexit
20-
2119
from com.vmware.vcenter_client import VM
22-
from samples.vsphere.common import vapiconnect
2320
from samples.vsphere.common.sample_util import parse_cli_args
21+
from samples.vsphere.common.service_manager_factory import ServiceManagerFactory
2422

2523

2624
class Sample:
@@ -33,21 +31,36 @@ class Sample:
3331

3432
def __init__(self):
3533
self.vm_service = None # Service used by the sample code.
34+
self.stub_config = None
35+
self.si = None
3636
self.cleardata = None
3737

3838
def setup(self):
3939
server, username, password, cleardata, skip_verification = \
4040
parse_cli_args()
41-
stub_config = vapiconnect.connect(server, username, password,
42-
skip_verification)
43-
self.vm_service = VM(stub_config)
4441
self.cleardata = cleardata
45-
atexit.register(vapiconnect.logout, stub_config)
42+
43+
# Connect to both Vim and vAPI services
44+
service_manager = ServiceManagerFactory.get_service_manager(
45+
server,
46+
username,
47+
password,
48+
skip_verification)
49+
50+
# Get the vAPI stub
51+
self.stub_config = service_manager.stub_config
52+
self.vm_service = VM(self.stub_config)
53+
54+
# Get VIM service instance (pyVmomi)
55+
self.si = service_manager.si
4656

4757
def run(self):
4858
vms = self.vm_service.list()
4959
print(vms)
5060

61+
current_time = self.si.CurrentTime()
62+
print(current_time)
63+
5164
def cleanup(self):
5265
if self.cleardata:
5366
pass
@@ -60,6 +73,5 @@ def main():
6073
sample.cleanup()
6174

6275

63-
# Start program
6476
if __name__ == '__main__':
6577
main()

0 commit comments

Comments
 (0)