diff --git a/.gitignore b/.gitignore index 6e671afb..56a465dd 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,6 @@ docs/_build/ # OS X .DS_Store *.orig + +# ignore flight recorder YAML file +*/hello_world_vcenter.yaml diff --git a/requirements.txt b/requirements.txt index 6084b8bf..afc3a201 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ argparse -pyvmomi +pyvmomi>=5.5.0.2014.1.1 requests suds>=0.4,<0.7 +vcrpy>=1.1.1 diff --git a/samples/hello_world_vcenter_with_yaml_recorder.py b/samples/hello_world_vcenter_with_yaml_recorder.py new file mode 100755 index 00000000..83bf5573 --- /dev/null +++ b/samples/hello_world_vcenter_with_yaml_recorder.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python +# VMware vSphere Python SDK +# Copyright (c) 2008-2014 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 to authenticate and print +a friendly encouragement to joining the community! +""" + +import atexit +import argparse +import getpass + +import vcr + +from pyVim import connect +from pyVmomi import vmodl + + +def get_args(): + """Get command line args from the user. + """ + 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') + + args = parser.parse_args() + + if not args.password: + args.password = getpass.getpass( + prompt='Enter password for host {0} and user {1}: '.format( + args.host, args.user)) + return args + + +def main(): + """ + Simple command-line program for listing the virtual machines on a system. + """ + + args = get_args() + + try: + my_vcr = vcr.VCR() + # use the vcr instance to setup an instance of service_instance + with my_vcr.use_cassette('hello_world_vcenter.yaml', + record_mode='all'): + service_instance = connect.SmartConnect(host=args.host, + user=args.user, + pwd=args.password, + port=int(args.port)) + # the recording will show up in the working directory + + atexit.register(connect.Disconnect, service_instance) + + print "\nHello World!\n" + print "If you got here, you authenticted into vCenter." + print "The server is {0}!".format(args.host) + # NOTE (hartsock): only a successfully authenticated session has a + # session key aka session id. + session_id = service_instance.content.sessionManager.currentSession.key + print "current session id: {0}".format(session_id) + print "Well done!" + print "\n" + print "Download, learn and contribute back:" + print "https://github.com/vmware/pyvmomi-community-samples" + print "\n\n" + + except vmodl.MethodFault as error: + print "Caught vmodl fault : " + error.msg + return -1 + + return 0 + +# Start program +if __name__ == "__main__": + main()