|
| 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() |
0 commit comments