2016-10-26 19:08:23 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
* *******************************************************
|
|
|
|
* Copyright (c) VMware, Inc. 2017. All Rights Reserved.
|
2017-03-15 18:36:13 -04:00
|
|
|
* SPDX-License-Identifier: MIT
|
2016-10-26 19:08:23 -04:00
|
|
|
* *******************************************************
|
|
|
|
*
|
|
|
|
* 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
|
|
|
from vmware.vapi.vsphere.client import create_vsphere_client
|
|
|
|
|
2017-08-18 02:09:38 -04:00
|
|
|
from com.vmware.cis.tagging_client import (Category, CategoryModel)
|
2016-10-26 19:08:23 -04:00
|
|
|
|
2017-08-18 02:09:38 -04:00
|
|
|
from samples.vsphere.common import sample_cli
|
|
|
|
from samples.vsphere.common import sample_util
|
|
|
|
from samples.vsphere.common import sso
|
2016-10-26 19:08:23 -04:00
|
|
|
from samples.vsphere.common.ssl_helper import get_unverified_context
|
2018-03-08 16:02:07 -05:00
|
|
|
from samples.vsphere.common.ssl_helper import get_unverified_session
|
2016-10-26 19:08:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
class EmbeddedPscSsoWorkflow(object):
|
|
|
|
"""
|
|
|
|
Demonstrates how to Login to vCenter vAPI service with
|
|
|
|
embedded Platform Services Controller.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2017-08-18 02:09:38 -04:00
|
|
|
parser = sample_cli.build_arg_parser()
|
|
|
|
self.args = sample_util.process_cli_args(parser.parse_args())
|
2016-10-26 19:08:23 -04:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
print('\n\n#### Example: Login to vCenter server with '
|
|
|
|
'embedded Platform Services Controller')
|
|
|
|
|
|
|
|
# Since the platform services controller is embedded, the sso server
|
|
|
|
# is the same as the vCenter server.
|
2018-03-08 16:02:07 -05:00
|
|
|
sso_url = 'https://{}/sts/STSService'.format(self.args.server)
|
2016-10-26 19:08:23 -04:00
|
|
|
|
|
|
|
print('\nStep 1: Connect to the Single Sign-On URL and '
|
|
|
|
'retrieve the SAML bearer token.')
|
|
|
|
|
2018-03-08 16:02:07 -05:00
|
|
|
authenticator = sso.SsoAuthenticator(sso_url)
|
2016-10-26 19:08:23 -04:00
|
|
|
context = None
|
2017-08-18 02:09:38 -04:00
|
|
|
if self.args.skipverification:
|
2016-10-26 19:08:23 -04:00
|
|
|
context = get_unverified_context()
|
|
|
|
bearer_token = authenticator.get_bearer_saml_assertion(
|
2017-08-18 02:09:38 -04:00
|
|
|
self.args.username,
|
|
|
|
self.args.password,
|
2016-10-26 19:08:23 -04:00
|
|
|
delegatable=True,
|
|
|
|
ssl_context=context)
|
|
|
|
|
2018-03-08 16:02:07 -05:00
|
|
|
session = get_unverified_session() if self.args.skipverification else None
|
2016-10-26 19:08:23 -04:00
|
|
|
|
2018-03-08 16:02:07 -05:00
|
|
|
# Connect to vSphere client
|
|
|
|
client = create_vsphere_client(server=self.args.server,
|
|
|
|
bearer_token=bearer_token,
|
|
|
|
session=session)
|
2016-10-26 19:08:23 -04:00
|
|
|
|
2017-05-03 04:30:48 -04:00
|
|
|
# Create and Delete TagCategory to Verify connection is successful
|
|
|
|
print('\nStep 3: Creating and Deleting Tag Category...\n')
|
2018-03-08 16:02:07 -05:00
|
|
|
create_spec = client.tagging.Category.CreateSpec()
|
|
|
|
create_spec.name = 'TestTag_embeded_psc_sso_workflow'
|
|
|
|
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))
|
2016-10-26 19:08:23 -04:00
|
|
|
|
2017-05-03 04:30:48 -04:00
|
|
|
# Delete TagCategory
|
2018-03-08 16:02:07 -05:00
|
|
|
client.tagging.Category.delete(category_id)
|
2017-05-03 04:30:48 -04:00
|
|
|
|
2016-10-26 19:08:23 -04:00
|
|
|
|
|
|
|
def main():
|
|
|
|
embedded_psc_sso_workflow = EmbeddedPscSsoWorkflow()
|
|
|
|
embedded_psc_sso_workflow.run()
|
|
|
|
|
|
|
|
|
|
|
|
# Start program
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|