{"id":1846,"date":"2019-05-30T17:40:19","date_gmt":"2019-05-30T16:40:19","guid":{"rendered":"http:\/\/read.virtualizeplanet.com\/?p=1846"},"modified":"2019-05-30T17:51:29","modified_gmt":"2019-05-30T16:51:29","slug":"python-and-vmware-sdk","status":"publish","type":"post","link":"https:\/\/read.virtualizeplanet.com\/?p=1846","title":{"rendered":"Python and VMware SDK"},"content":{"rendered":"\n<p>So I\u2019m making a big push to learn Python. In IT don\u2019t think you can survive without it. For those who don\u2019t know me, I\u2019ve never been a professional programmer. I learned enough C#.net 13 years ago to be dangerous. I did it so I could learn how to automate VMware by building my own tools. Thirteen years later and a plethora of tools later including <a rel=\"noreferrer noopener\" aria-label=\"vDisk Informer (opens in a new tab)\" href=\"http:\/\/read.virtualizeplanet.com\/?p=366\" target=\"_blank\">vDisk Informer<\/a>, <a rel=\"noreferrer noopener\" aria-label=\"vSphere Plugin Wizard (opens in a new tab)\" href=\"http:\/\/read.virtualizeplanet.com\/?p=381\" target=\"_blank\">vSphere Plugin Wizard<\/a> and <a rel=\"noreferrer noopener\" aria-label=\"vRealize Automation icon changer  (opens in a new tab)\" href=\"http:\/\/read.virtualizeplanet.com\/?p=1539\" target=\"_blank\">vRealize Automation icon changer <\/a>I&#8217;m looking at starting again. This time with python.  The first job as last time, the VMware programmer\u2019s equivalent of \u201cHello World\u201d which is to list the VMs on your vSphere environment. To make my life easier, I decided upon the Python SDK wrapper <a rel=\"noreferrer noopener\" aria-label=\"pyvmomi (opens in a new tab)\" href=\"https:\/\/github.com\/vmware\/pyvmomi\" target=\"_blank\">pyvmomi<\/a> .  So here\u2019s how to do it in my favourite python IDE PyCharm. <br> Step pip install <a href=\"https:\/\/github.com\/vmware\/pyvmomi\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"pyvmomi  (opens in a new tab)\">pyvmomi <\/a>using the below command: pip install \u2013upgrade pyvmomi<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"499\" height=\"332\" src=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/1.png\" alt=\"\" class=\"wp-image-1847\" srcset=\"https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/1.png 499w, https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/1-300x200.png 300w\" sizes=\"auto, (max-width: 499px) 100vw, 499px\" \/><\/figure>\n\n\n\n<p>Now fire up Pycharm and create a new project.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"779\" height=\"492\" src=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/2.png\" alt=\"\" class=\"wp-image-1848\" srcset=\"https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/2.png 779w, https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/2-300x189.png 300w, https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/2-768x485.png 768w\" sizes=\"auto, (max-width: 779px) 100vw, 779px\" \/><\/figure>\n\n\n\n<p>Now the next step caught me out as a novice. It&#8217;s not enough to import the pyvmomi library in your code, you also have to add it through pycharm in your project.<br>\nClick on File &gt; Settings &gt;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"269\" height=\"461\" src=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/4.png\" alt=\"\" class=\"wp-image-1849\" srcset=\"https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/4.png 269w, https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/4-175x300.png 175w\" sizes=\"auto, (max-width: 269px) 100vw, 269px\" \/><\/figure>\n\n\n\n<p>Now expand your Project and click on \u201cProject Interrupter\u201d <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"204\" height=\"515\" src=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/5.png\" alt=\"\" class=\"wp-image-1850\" srcset=\"https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/5.png 204w, https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/5-119x300.png 119w\" sizes=\"auto, (max-width: 204px) 100vw, 204px\" \/><\/figure>\n\n\n\n<p>Click on the + symbol on the right<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"182\" height=\"266\" src=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/6.png\" alt=\"\" class=\"wp-image-1851\"\/><\/figure>\n\n\n\n<p>Now in the search box search for pyvmomi and select it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"414\" height=\"181\" src=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/7.png\" alt=\"\" class=\"wp-image-1852\" srcset=\"https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/7.png 414w, https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/7-300x131.png 300w\" sizes=\"auto, (max-width: 414px) 100vw, 414px\" \/><\/figure>\n\n\n\n<p>Now click on Install Package<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"177\" height=\"61\" src=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/8.png\" alt=\"\" class=\"wp-image-1853\"\/><\/figure>\n\n\n\n<p>Once it&#8217;s finished installing it should look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"999\" height=\"711\" src=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/9.png\" alt=\"\" class=\"wp-image-1854\" srcset=\"https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/9.png 999w, https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/9-300x214.png 300w, https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/9-768x547.png 768w\" sizes=\"auto, (max-width: 999px) 100vw, 999px\" \/><\/figure>\n\n\n\n<p>Now underneath your project, create a new python file and let&#8217;s get coding.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"606\" height=\"214\" src=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/10.png\" alt=\"\" class=\"wp-image-1855\" srcset=\"https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/10.png 606w, https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2019\/05\/10-300x106.png 300w\" sizes=\"auto, (max-width: 606px) 100vw, 606px\" \/><\/figure>\n\n\n\n<p>Cut and paste the following code into your newly created python file. Make sure you change the hostname, port, username and password to reflect your environment. I have added comments to explain how to code works inline with the code. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># first import the sdk wrapper library\nfrom pyVim.connect import SmartConnect, Disconnect\nfrom pyVmomi import vim\n\n# import the ssl library to handle http\nimport ssl\n\n# now setup your connection properties\nhost = \"vcsa6.demo.local\"\nport = 443\nuser = \"administrator@vsphere.local\"\npassword = \"PassW0rd123\"\n\n# now connect to your vCenter\/vSphere\ncontext = None\nif hasattr(ssl, '_create_unverified_context'):\n    context = ssl._create_unverified_context()\nsi = SmartConnect(host=host,\n                  user=user,\n                  pwd=password,\n                  port=port,\n                  sslContext=context)\nif not si:\n    print(\"Could not connect to the specified host using specified \"\n            \"username and password\")\n\n# no grab the data model of the SDK tree\ncontent = si.RetrieveContent()\n\n# now loop through any object that is tagged as \"vmFolder\"\nfor child in content.rootFolder.childEntity:\n    if hasattr(child, 'vmFolder'):\n       datacenter = child\n       vmFolder = datacenter.vmFolder\n       vmList = vmFolder.childEntity\n\n       # now grab the child nodes of any vmFolder object\n\n       for vm in vmList:\n           if hasattr(vm, 'childEntity'):\n               vmList2 = vm.childEntity\n\n               # now loop through the child ojbects printing their user friendly name\n               for c in vmList2:\n                   vmproperties = c.summary\n                   print(vmproperties.config.name)<\/code><\/pre>\n\n\n\n<p>It can be difficult to visualize what is going on since you don\u2019t have a picture of the data model. I learned this many years ago and to give you help, you should watch my video on automating VMware using the REST API. Pyvmomi is not using the REST API, but my video also gives some clue to how the data model is laid out. You find it useful. <a rel=\"noreferrer noopener\" aria-label=\"Click HERE (opens in a new tab)\" href=\"https:\/\/www.youtube.com\/watch?v=SsYrIGk9kfw&amp;t=1329s\" target=\"_blank\">Click HERE<\/a> to watch. Remember MoB is your friend!<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So I\u2019m making a big push to learn Python. In IT don\u2019t think you can survive without it. For those who don\u2019t know me, I\u2019ve never been a professional programmer. I learned enough C#.net 13 years ago to be dangerous. I did it so I could learn how to automate VMware by building my own [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[98,129,4],"tags":[130,133,131,132],"class_list":["post-1846","post","type-post","status-publish","format-standard","hentry","category-automation","category-python-tips-corner","category-vmware","tag-python","tag-pyvmomi","tag-vmware-api","tag-vmware-sdk"],"aioseo_notices":[],"views":7925,"_links":{"self":[{"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/1846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1846"}],"version-history":[{"count":4,"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/1846\/revisions"}],"predecessor-version":[{"id":1860,"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/1846\/revisions\/1860"}],"wp:attachment":[{"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}