1
0
mirror of https://github.com/vmware/vsphere-automation-sdk-python.git synced 2024-11-21 17:29:59 -05:00

Add connect to vmc vsphere sample

Sample output:

VM ID    VM Name
-------  ----------------------------------------------------------
vm-35    vcenter
vm-50    nsx-manager
vm-51    Node-7c1f558c-d026-4e48-be2b-f0bb6e6c220f-NSX-controller-1
vm-52    Node-db8fda3f-f611-4631-a3ac-700ab6412ea0-NSX-controller-2
vm-53    Node-844750bc-0191-45da-9a0d-1f849de2ea08-NSX-controller-3

Signed-off-by: Tianhao He <het@vmware.com>
This commit is contained in:
Tianhao He 2018-03-23 16:26:49 -07:00 committed by Steve Trefethen
parent 474eb8f901
commit eda57e2a07

View File

@ -0,0 +1,101 @@
#!/usr/bin/env python
"""
* *******************************************************
* Copyright (c) VMware, Inc. 2018. All Rights Reserved.
* 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__ = 'VMware Cloud on AWS'
import argparse
from com.vmware.vapi.std.errors_client import NotFound
from com.vmware.vmc.model_client import ErrorResponse
from six.moves.urllib import parse
from tabulate import tabulate
from vmware.vapi.vmc.client import create_vmc_client
from vmware.vapi.vsphere.client import create_vsphere_client
class ConnectTovSphereWithDefaultCredentials(object):
"""
Demonstrates how to connect to a vSphere in a SDDC
using the initial cloud admin credentials.
Sample Prerequisites:
- A SDDC in the org
- A firewall rule to access the vSphere
"""
def __init__(self):
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--refresh-token',
required=True,
help='VMware Cloud API refresh token')
parser.add_argument('-o', '--org-id',
required=True,
help='Organization identifier.')
parser.add_argument('-s', '--sddc-id',
required=True,
help='Sddc Identifier.')
args = parser.parse_args()
self.refresh_token = args.refresh_token
self.org_id = args.org_id
self.sddc_id = args.sddc_id
def run(self):
# Connect to VMware Cloud on AWS
vmc_client = create_vmc_client(self.refresh_token)
print('\n# Example: Successfully login to VMware Cloud on AWS instance')
# Check if the organization exists
orgs = vmc_client.Orgs.list()
if self.org_id not in [org.id for org in orgs]:
raise ValueError("Org with ID {} doesn't exist".format(self.org_id))
# Check if the SDDC exists
try:
sddc = vmc_client.orgs.Sddcs.get(self.org_id, self.sddc_id)
except NotFound as e:
error_response = e.data.convert_to(ErrorResponse)
raise ValueError(error_response.error_messages)
# Get VC hostname
server = parse.urlparse(sddc.resource_config.vc_url).hostname
# Connect to vSphere client using the initial cloud admin credentials.
# Please use the new credentials to login after you reset the default one.
vsphere_client = create_vsphere_client(server,
username=sddc.resource_config.cloud_username,
password=sddc.resource_config.cloud_password)
print("\n# Example: Successfully connect to vCenter at '{}'".format(server))
# List VMs in the vSphere instance
vms = vsphere_client.vcenter.VM.list()
table = []
for vm_summary in vms:
table.append([vm_summary.vm, vm_summary.name])
print('\n# Example: List VMs in the vSphere')
print(tabulate(table, ['VM ID', 'VM Name']))
def main():
connect_to_vsphere = ConnectTovSphereWithDefaultCredentials()
connect_to_vsphere.run()
if __name__ == '__main__':
main()