#!/usr/bin/env python # VMware vSphere Python SDK # Copyright (c) 2008-2013 VMware, Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Python program for updting load balancer teaming policy to Route based on Physical NIC Load """ import re import atexit from pyVim import connect from pyVmomi import vmodl from pyVmomi import vim #import tools.cli as cli from optparse import OptionParser, make_option from pyVim.connect import SmartConnectNoSSL, Disconnect from pyVmomi import vmodl from pyVmomi import vim from datetime import timedelta import argparse import atexit import sys import datetime def get_obj(content, vimtype, name): """ Get the vsphere object associated with a given text name """ obj = None container = content.viewManager.CreateContainerView( content.rootFolder, vimtype, True) for c in container.view: if c.name == name: obj = c break return obj def build_arg_parser(): """ Builds a standard argument parser with arguments for talking to vCenter -s service_host_name_or_ip -o optional_port_number -u required_user -p optional_password -pg dvsportgroup """ parser = argparse.ArgumentParser( description='Standard Arguments for talking to vCenter') # because -h is reserved for 'help' we use -s for service parser.add_argument('-s', '--host', required=True, action='store', help='vSphere service to connect to') # because we want -p for password, we use -o for port parser.add_argument('-o', '--port', type=int, default=443, action='store', help='Port to connect on') parser.add_argument('-u', '--user', required=True, action='store', help='User name to use when connecting to host') parser.add_argument('-p', '--password', required=False, action='store', help='Password to use when connecting to host') parser.add_argument('-pg', '--dvsportgroup', required=False, action='store', help='Portgroup name to update the Load balance policy') parser.add_argument('-S', '--disable_ssl_verification', required=False, action='store_true', help='Disable ssl host certificate verification') return parser def prompt_for_password(args): """ if no password is specified on the command line, prompt for it """ if not args.password: args.password = getpass.getpass( prompt='Enter password for host %s and user %s: ' % (args.host, args.user)) return args def get_args(): """ Supports the command-line arguments needed to form a connection to vSphere. """ parser = build_arg_parser() args = parser.parse_args() return prompt_for_password(args) def get_args(): parser = build_arg_parser() parser.add_argument('-f', '--find', required=False, action='store', help='String to match VM names') args = parser.parse_args() return prompt_for_password(args) try: from pyVmomi import vim, vmodl except ImportError as e: pass def main(): """ Simple command-line program for listing the virtual machines on a system. """ args = get_args() try: if args.disable_ssl_verification: service_instance = connect.SmartConnectNoSSL(host=args.host, user=args.user, pwd=args.password, # dv_port_name=args.dvsportgroup, port=int(args.port)) else: service_instance = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password, #dv_port_name=args.dvsportgroup, port=int(args.port)) atexit.register(connect.Disconnect, service_instance) content = service_instance.RetrieveContent() dv_port_name=args.dvsportgroup teamingPolicy = vim.dvs.VmwareDistributedVirtualSwitch.UplinkPortTeamingPolicy() dv_pg = get_obj(content, [vim.dvs.DistributedVirtualPortgroup], dv_port_name) dv_pg_spec = vim.dvs.DistributedVirtualPortgroup.ConfigSpec() dv_pg_spec.defaultPortConfig = vim.dvs.VmwareDistributedVirtualSwitch.VmwarePortConfigPolicy() dv_pg_spec.configVersion = dv_pg.config.configVersion dv_pg_spec.defaultPortConfig.uplinkTeamingPolicy = vim.dvs.VmwareDistributedVirtualSwitch.UplinkPortTeamingPolicy() dv_pg_spec.defaultPortConfig.uplinkTeamingPolicy.policy = vim.StringPolicy(value = "loadbalance_loadbased") task = dv_pg.ReconfigureDVPortgroup_Task(dv_pg_spec) except vmodl.MethodFault as error: print("Caught vmodl fault : " + error.msg) return -1 return 0 # Start program if __name__ == "__main__": main()