2017-07-18 03:17:05 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
* *******************************************************
|
2018-03-08 16:02:07 -05:00
|
|
|
* Copyright (c) VMware, Inc. 2017, 2018. All Rights Reserved.
|
2017-07-18 03:17:05 -04:00
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
* *******************************************************
|
|
|
|
*
|
|
|
|
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
|
|
|
|
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
|
|
|
|
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
|
|
|
|
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'VMware, Inc.'
|
|
|
|
__vcenter_version__ = '6.0+'
|
|
|
|
|
2018-03-08 16:02:07 -05:00
|
|
|
import requests
|
|
|
|
|
|
|
|
from vmware.vapi.vsphere.client import create_vsphere_client
|
|
|
|
|
|
|
|
from com.vmware.cis.tagging_client import CategoryModel
|
|
|
|
|
2017-07-18 03:17:05 -04:00
|
|
|
from samples.vsphere.common.sample_util import process_cli_args
|
2018-03-08 16:02:07 -05:00
|
|
|
from samples.vsphere.common import sample_cli
|
2017-07-18 03:17:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
class CertConnect(object):
|
|
|
|
"""
|
|
|
|
Demonstrates how to Connect to vCenter vAPI service with
|
|
|
|
with Valid Cert
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2018-03-08 16:02:07 -05:00
|
|
|
parser = sample_cli.build_arg_parser()
|
2017-07-18 03:17:05 -04:00
|
|
|
parser.add_argument('-cpath', '--cert_path',
|
|
|
|
action='store',
|
|
|
|
help='path to a CA_BUNDLE file or directory with certificates of trusted CAs')
|
|
|
|
args = parser.parse_args()
|
2018-03-08 16:02:07 -05:00
|
|
|
self.args = process_cli_args(args)
|
2017-07-18 03:17:05 -04:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
print('\n\n#### Example: Login to vCenter server with '
|
|
|
|
'Valid Cert Verification')
|
|
|
|
|
2018-03-08 16:02:07 -05:00
|
|
|
# Create a requests session and load the CA cert
|
|
|
|
session = requests.session()
|
|
|
|
session.verify = self.args.cert_path
|
2017-07-18 03:17:05 -04:00
|
|
|
|
2018-03-08 16:02:07 -05:00
|
|
|
# Connect to VAPI
|
|
|
|
client = create_vsphere_client(server=self.args.server,
|
|
|
|
username=self.args.username,
|
|
|
|
password=self.args.password,
|
|
|
|
session=session)
|
|
|
|
|
|
|
|
# List Tag Categories to verify the connection is successful
|
|
|
|
print('\nStep 3: Listing the Tag Categories...\n')
|
|
|
|
create_spec = client.tagging.Category.CreateSpec()
|
|
|
|
create_spec.name = 'TestTag_connect_with_cert'
|
|
|
|
create_spec.description = 'TestTagDesc'
|
|
|
|
create_spec.cardinality = CategoryModel.Cardinality.MULTIPLE
|
|
|
|
create_spec.associable_types = set()
|
|
|
|
category_id = client.tagging.Category.create(create_spec)
|
|
|
|
assert category_id is not None
|
|
|
|
print('Tag category created; Id: {0}\n'.format(category_id))
|
2017-07-18 03:17:05 -04:00
|
|
|
|
|
|
|
# Delete TagCategory
|
2018-03-08 16:02:07 -05:00
|
|
|
client.tagging.Category.delete(category_id)
|
2017-07-18 03:17:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
connect_with_cert = CertConnect()
|
|
|
|
connect_with_cert.run()
|
|
|
|
|
|
|
|
|
|
|
|
# Start program
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|