Skip to content

Commit 2d232fd

Browse files
authored
Merge pull request #27 from pgbidkar/master
Sample to demonstrate Cert Verification while connecting to vCenter s…
2 parents a94fff4 + 7720424 commit 2d232fd

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

samples/vsphere/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The following table shows the sample sub-directories and their contents.
44

55
Directory | Description
66
----------------| -------------
7-
common | Samples common helper classes and abstractions; This package does NOT contain any sample
7+
common | Samples common helper classes and abstractions; This package contains one sample 'connect_with_cert.py', to demonstrate how to connect with valid Cert Verification
88
contentlibrary | Samples for Content Library APIs
99
tagging | Samples for Tagging APIs
1010
vcenter | Samples for managing vSphere infrastructure and virtual machines
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
* *******************************************************
5+
* Copyright (c) VMware, Inc. 2017. All Rights Reserved.
6+
* SPDX-License-Identifier: MIT
7+
* *******************************************************
8+
*
9+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
11+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
12+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
13+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
14+
"""
15+
16+
__author__ = 'VMware, Inc.'
17+
__copyright__ = 'Copyright 2017 VMware, Inc. All rights reserved.'
18+
__vcenter_version__ = '6.0+'
19+
20+
import atexit
21+
from com.vmware.cis.tagging_client import (Category, CategoryModel)
22+
from samples.vsphere.common import vapiconnect
23+
from samples.vsphere.common.sample_util import process_cli_args
24+
from samples.vsphere.common.sample_cli import build_arg_parser
25+
26+
27+
class CertConnect(object):
28+
"""
29+
Demonstrates how to Connect to vCenter vAPI service with
30+
with Valid Cert
31+
"""
32+
33+
def __init__(self):
34+
self.server = None
35+
self.username = None
36+
self.password = None
37+
self.stub_config = None
38+
self.cleardata = None
39+
self.skip_verification = False
40+
self.cert_path = None
41+
self.category_svc = None
42+
self.category_id = None
43+
44+
def setup(self):
45+
parser = build_arg_parser()
46+
parser.add_argument('-cpath', '--cert_path',
47+
action='store',
48+
help='path to a CA_BUNDLE file or directory with certificates of trusted CAs')
49+
args = parser.parse_args()
50+
51+
self.server, self.username, self.password, self.cleardata, self.skip_verification = \
52+
process_cli_args(args)
53+
54+
if args.cert_path:
55+
self.cert_path = args.cert_path
56+
57+
def run(self):
58+
print('\n\n#### Example: Login to vCenter server with '
59+
'Valid Cert Verification')
60+
# Connect to VAPI
61+
self.stub_config = vapiconnect.connect(self.server, self.username, self.password,
62+
self.skip_verification,
63+
cert_path=self.cert_path)
64+
atexit.register(vapiconnect.logout, self.stub_config)
65+
66+
# Create and Delete TagCategory to Verify connection is successful
67+
print('\nStep 3: Creating and Deleting Tag Category...\n')
68+
self.category_svc = Category(self.stub_config)
69+
70+
self.category_id = self.create_tag_category('TestTagCat', 'TestTagDesc',
71+
CategoryModel.Cardinality.MULTIPLE)
72+
assert self.category_id is not None
73+
print('Tag category created; Id: {0}\n'.format(self.category_id))
74+
75+
# Delete TagCategory
76+
self.category_svc.delete(self.category_id)
77+
78+
print('VAPI session disconnected successfully...')
79+
80+
def create_tag_category(self, name, description, cardinality):
81+
"""create a category. User who invokes this needs create category privilege."""
82+
create_spec = self.category_svc.CreateSpec()
83+
create_spec.name = name
84+
create_spec.description = description
85+
create_spec.cardinality = cardinality
86+
associableTypes = set()
87+
create_spec.associable_types = associableTypes
88+
return self.category_svc.create(create_spec)
89+
90+
91+
def main():
92+
connect_with_cert = CertConnect()
93+
connect_with_cert.setup()
94+
connect_with_cert.run()
95+
96+
97+
# Start program
98+
if __name__ == '__main__':
99+
main()

samples/vsphere/common/vapiconnect.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def get_jsonrpc_endpoint_url(host):
3131
return "https://{}/api".format(host)
3232

3333

34-
def connect(host, user, pwd, skip_verification=False, suppress_warning=True):
34+
def connect(host, user, pwd, skip_verification=False, cert_path=None, suppress_warning=True):
3535
"""
3636
Create an authenticated stub configuration object that can be used to issue
3737
requests against vCenter.
@@ -44,6 +44,8 @@ def connect(host, user, pwd, skip_verification=False, suppress_warning=True):
4444
session = requests.Session()
4545
if skip_verification:
4646
session = create_unverified_session(session, suppress_warning)
47+
elif cert_path:
48+
session.verify = cert_path
4749
connector = get_requests_connector(session=session, url=host_url)
4850
stub_config = StubConfigurationFactory.new_std_configuration(connector)
4951

0 commit comments

Comments
 (0)