Skip to content

Commit

Permalink
Merge pull request vmware#122 from hartsock/issues/121
Browse files Browse the repository at this point in the history
[sample] vcrpy as flight recorder
  • Loading branch information
hartsock committed Oct 9, 2014
2 parents 554e0b4 + 36f65e3 commit 51e92b2
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ docs/_build/
# OS X
.DS_Store
*.orig

# ignore flight recorder YAML file
*/hello_world_vcenter.yaml
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
argparse
pyvmomi
pyvmomi>=5.5.0.2014.1.1
requests
suds>=0.4,<0.7
vcrpy>=1.1.1
111 changes: 111 additions & 0 deletions samples/hello_world_vcenter_with_yaml_recorder.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 51e92b2

Please sign in to comment.