1
0
mirror of https://github.com/vmware/vsphere-automation-sdk-python.git synced 2024-11-26 19:29:58 -05:00
vsphere-automation-sdk-python/vsphere/6.0/_modules/vmware/vapi/lib/connect.html
2018-08-07 16:42:25 -07:00

224 lines
18 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>vmware.vapi.lib.connect &mdash; vCloud Suite SDK for Python 6.0.0 documentation</title>
<link rel="stylesheet" href="../../../../_static/vapitheme.css" type="text/css" />
<link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../../../../',
VERSION: '6.0.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../../../../_static/jquery.js"></script>
<script type="text/javascript" src="../../../../_static/underscore.js"></script>
<script type="text/javascript" src="../../../../_static/doctools.js"></script>
<link rel="top" title="vCloud Suite SDK for Python 6.0.0 documentation" href="../../../../index.html" />
<link rel="up" title="Module code" href="../../../index.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li><a href="../../../../index.html">vCloud Suite SDK for Python 6.0.0 documentation</a> &raquo;</li>
<li><a href="../../../index.html" accesskey="U">Module code</a> &raquo;</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<h1>Source code for vmware.vapi.lib.connect</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">vAPI Connection factory</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="n">__author__</span> <span class="o">=</span> <span class="s">&#39;VMware, Inc.&#39;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s">&#39;Copyright 2011-2014 VMware, Inc. All rights reserved. -- VMware Confidential&#39;</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">six.moves</span> <span class="kn">import</span> <span class="n">configparser</span>
<span class="kn">from</span> <span class="nn">vmware.vapi.protocol.client.rpc.provider_factory</span> <span class="kn">import</span> <span class="n">RpcProviderFactory</span>
<span class="kn">from</span> <span class="nn">vmware.vapi.protocol.client.msg.connector_factory</span> <span class="kn">import</span> <span class="n">ProtocolConnectorFactory</span>
<span class="kn">from</span> <span class="nn">vmware.vapi.settings</span> <span class="kn">import</span> <span class="n">config</span>
<div class="viewcode-block" id="check_file_exists"><a class="viewcode-back" href="../../../../vmware.vapi.lib.html#vmware.vapi.lib.connect.check_file_exists">[docs]</a><span class="k">def</span> <span class="nf">check_file_exists</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Check if name is a file and exists</span>
<span class="sd"> :type :class:`str`</span>
<span class="sd"> :param file name</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">OSError</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="s">&#39;No such file&#39;</span><span class="p">,</span> <span class="n">filename</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="get_connector"><a class="viewcode-back" href="../../../../vmware.vapi.lib.html#vmware.vapi.lib.connect.get_connector">[docs]</a><span class="k">def</span> <span class="nf">get_connector</span><span class="p">(</span><span class="n">rpc_provider_name</span><span class="p">,</span> <span class="n">msg_protocol</span><span class="p">,</span> <span class="n">ssl_context</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create a connection to the vAPI Provider using the specified arguments</span>
<span class="sd"> :type rpc_provider_name: :class:`str`</span>
<span class="sd"> :param rpc_provider_name: The RPC provider to be used for the connection.</span>
<span class="sd"> Valid values are &#39;http&#39;, &#39;https&#39; and &#39;requests&#39;</span>
<span class="sd"> :type msg_protocol: :class:`str`</span>
<span class="sd"> :param msg_protocol: The message protocol to be used for the connection.</span>
<span class="sd"> Valid values are &#39;json&#39;.</span>
<span class="sd"> :type ssl_context: :class:`OpenSSL.SSL.Context`</span>
<span class="sd"> :param ssl_context: SSL context to use for SSL connections</span>
<span class="sd"> :param kwargs: Additional arguments for the RPC provider</span>
<span class="sd"> :rtype: :class:`vmware.vapi.protocol.client.connector.Connector`</span>
<span class="sd"> :return: Connection to the vAPI provider</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c">#RPC provider</span>
<span class="n">rpc_provider_args</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">if</span> <span class="n">rpc_provider_name</span> <span class="ow">in</span> <span class="p">(</span><span class="s">&#39;http&#39;</span><span class="p">,</span> <span class="s">&#39;https&#39;</span><span class="p">):</span>
<span class="c"># provider url</span>
<span class="n">rpc_provider_args</span><span class="p">[</span><span class="s">&#39;url&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;url&#39;</span><span class="p">)</span>
<span class="n">rpc_provider_args</span><span class="p">[</span><span class="s">&#39;ssl_args&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;ssl_context&#39;</span><span class="p">:</span> <span class="n">ssl_context</span><span class="p">}</span>
<span class="k">elif</span> <span class="n">rpc_provider_name</span> <span class="ow">in</span> <span class="p">(</span><span class="s">&#39;requests&#39;</span><span class="p">):</span>
<span class="k">for</span> <span class="n">arg_name</span> <span class="ow">in</span> <span class="p">(</span><span class="s">&#39;url&#39;</span><span class="p">,</span> <span class="s">&#39;session&#39;</span><span class="p">,</span> <span class="s">&#39;timeout&#39;</span><span class="p">,</span> <span class="s">&#39;pool_size&#39;</span><span class="p">):</span>
<span class="n">rpc_provider_args</span><span class="p">[</span><span class="n">arg_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">arg_name</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;Invalid RPC Provider specified </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">rpc_provider_name</span><span class="p">)</span>
<span class="c"># Accept additional configuration using a client config file</span>
<span class="k">if</span> <span class="n">config</span><span class="o">.</span><span class="n">cfg</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">config</span><span class="o">.</span><span class="n">cfg</span> <span class="o">=</span> <span class="n">configparser</span><span class="o">.</span><span class="n">SafeConfigParser</span><span class="p">()</span>
<span class="n">config_file</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;config&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">config_file</span><span class="p">:</span>
<span class="n">check_file_exists</span><span class="p">(</span><span class="n">config_file</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">cfg</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">config_file</span><span class="p">)</span>
<span class="c"># Using SAML based authentication</span>
<span class="c"># If the configuration does not have SSO signer, add it</span>
<span class="n">use_saml_token</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;use_saml_token&#39;</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="n">use_saml_token</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">config</span><span class="o">.</span><span class="n">cfg</span><span class="o">.</span><span class="n">has_section</span><span class="p">(</span>
<span class="s">&#39;vmware.vapi.protocol.client.msg.json_connector&#39;</span><span class="p">)):</span>
<span class="n">config</span><span class="o">.</span><span class="n">cfg</span><span class="o">.</span><span class="n">add_section</span><span class="p">(</span><span class="s">&#39;vmware.vapi.protocol.client.msg.json_connector&#39;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">cfg</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s">&#39;vmware.vapi.protocol.client.msg.json_connector&#39;</span><span class="p">,</span>
<span class="s">&#39;processors&#39;</span><span class="p">,</span>
<span class="s">&#39;vmware.vapi.security.sso.JSONSSOSigner&#39;</span><span class="p">)</span>
<span class="n">rpc_factory</span> <span class="o">=</span> <span class="n">RpcProviderFactory</span><span class="p">()</span>
<span class="n">rpc_provider</span> <span class="o">=</span> <span class="n">rpc_factory</span><span class="o">.</span><span class="n">get_rpc_provider</span><span class="p">(</span><span class="n">rpc_provider_name</span><span class="p">,</span>
<span class="o">**</span><span class="n">rpc_provider_args</span><span class="p">)</span>
<span class="c"># Msg protocol provider</span>
<span class="n">connector_factory</span> <span class="o">=</span> <span class="n">ProtocolConnectorFactory</span><span class="p">()</span>
<span class="n">connector</span> <span class="o">=</span> <span class="n">connector_factory</span><span class="o">.</span><span class="n">get_connector</span><span class="p">(</span><span class="n">msg_protocol</span><span class="p">,</span> <span class="n">rpc_provider</span><span class="p">)</span>
<span class="k">return</span> <span class="n">connector</span>
</div>
<div class="viewcode-block" id="get_saml_hok_connector"><a class="viewcode-back" href="../../../../vmware.vapi.lib.html#vmware.vapi.lib.connect.get_saml_hok_connector">[docs]</a><span class="k">def</span> <span class="nf">get_saml_hok_connector</span><span class="p">(</span><span class="n">rpc_provider_name</span><span class="p">,</span> <span class="n">msg_protocol</span><span class="o">=</span><span class="s">&#39;json&#39;</span><span class="p">,</span> <span class="n">ssl_context</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create a connection that uses SAML Hok based authentication</span>
<span class="sd"> to connect to a vAPI Provider</span>
<span class="sd"> :type rpc_provider_name: :class:`str`</span>
<span class="sd"> :param rpc_provider_name: The RPC provider to be used for the connection.</span>
<span class="sd"> Valid values are &#39;http&#39;, &#39;https&#39; or &#39;requests&#39;</span>
<span class="sd"> :type msg_protocol: :class:`str`</span>
<span class="sd"> :param msg_protocol: The message protocol to be used for the connection.</span>
<span class="sd"> Valid values are &#39;json&#39;.</span>
<span class="sd"> :type ssl_context: :class:`OpenSSL.SSL.Context`</span>
<span class="sd"> :param ssl_context: SSL context to use for SSL connections</span>
<span class="sd"> :type kwargs: :class:`dict` of :class:`str` and :class:`object`</span>
<span class="sd"> :param kwargs: Additional arguments for the RPC provider</span>
<span class="sd"> :rtype: :class:`vmware.vapi.protocol.client.connector.Connector`</span>
<span class="sd"> :return: Connection to the vAPI provider</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">get_connector</span><span class="p">(</span><span class="n">rpc_provider_name</span><span class="p">,</span> <span class="n">msg_protocol</span><span class="p">,</span> <span class="n">ssl_context</span><span class="p">,</span>
<span class="n">use_saml_token</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="get_requests_connector"><a class="viewcode-back" href="../../../../vmware.vapi.lib.html#vmware.vapi.lib.connect.get_requests_connector">[docs]</a><span class="k">def</span> <span class="nf">get_requests_connector</span><span class="p">(</span><span class="n">session</span><span class="p">,</span> <span class="n">msg_protocol</span><span class="o">=</span><span class="s">&#39;json&#39;</span><span class="p">,</span> <span class="n">url</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
<span class="n">timeout</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">pool_size</span><span class="o">=</span><span class="mi">8</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create a connection that uses &#39;requests&#39; library for http connections</span>
<span class="sd"> :type session: :class:`requests.Session`</span>
<span class="sd"> :param session: Session object</span>
<span class="sd"> :type msg_protocol: :class:`str`</span>
<span class="sd"> :param msg_protocol: Message protocol to be used for the connection. Valid</span>
<span class="sd"> values are &#39;json&#39;.</span>
<span class="sd"> :type url: :class:`str`</span>
<span class="sd"> :param url: HTTP(S) URL to be used</span>
<span class="sd"> :type timeout: :class:`int`</span>
<span class="sd"> :param timeout: Request timeout</span>
<span class="sd"> :type pool_size: :class:`int`</span>
<span class="sd"> :param pool_size: Connection pool size to be used</span>
<span class="sd"> :rtype: :class:`vmware.vapi.protocol.client.connector.Connector`</span>
<span class="sd"> :return: Connection to the vAPI provider</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">get_connector</span><span class="p">(</span><span class="s">&#39;requests&#39;</span><span class="p">,</span> <span class="n">msg_protocol</span><span class="p">,</span>
<span class="n">url</span><span class="o">=</span><span class="n">url</span><span class="p">,</span> <span class="n">session</span><span class="o">=</span><span class="n">session</span><span class="p">,</span>
<span class="n">timeout</span><span class="o">=</span><span class="n">timeout</span><span class="p">,</span> <span class="n">pool_size</span><span class="o">=</span><span class="n">pool_size</span><span class="p">)</span></div>
</pre></div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li><a href="../../../../index.html">vCloud Suite SDK for Python 6.0.0 documentation</a> &raquo;</li>
<li><a href="../../../index.html" >Module code</a> &raquo;</li>
</ul>
</div>
<div class="footer">
&copy; Copyright 2014, VMware, Inc..
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
</div>
</body>
</html>