mirror of
https://github.com/vmware/vsphere-automation-sdk-python.git
synced 2024-11-29 20:09:58 -05:00
224 lines
18 KiB
HTML
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 — 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> »</li>
|
||
|
<li><a href="../../../index.html" accesskey="U">Module code</a> »</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">"""</span>
|
||
|
<span class="sd">vAPI Connection factory</span>
|
||
|
<span class="sd">"""</span>
|
||
|
|
||
|
<span class="n">__author__</span> <span class="o">=</span> <span class="s">'VMware, Inc.'</span>
|
||
|
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s">'Copyright 2011-2014 VMware, Inc. All rights reserved. -- VMware Confidential'</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">"""</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"> """</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">'No such file'</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">"""</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 'http', 'https' and 'requests'</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 'json'.</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"> """</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">'http'</span><span class="p">,</span> <span class="s">'https'</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">'url'</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">'url'</span><span class="p">)</span>
|
||
|
<span class="n">rpc_provider_args</span><span class="p">[</span><span class="s">'ssl_args'</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="s">'ssl_context'</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">'requests'</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">'url'</span><span class="p">,</span> <span class="s">'session'</span><span class="p">,</span> <span class="s">'timeout'</span><span class="p">,</span> <span class="s">'pool_size'</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">'Invalid RPC Provider specified </span><span class="si">%s</span><span class="s">'</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">'config'</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">'use_saml_token'</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">'vmware.vapi.protocol.client.msg.json_connector'</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">'vmware.vapi.protocol.client.msg.json_connector'</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">'vmware.vapi.protocol.client.msg.json_connector'</span><span class="p">,</span>
|
||
|
<span class="s">'processors'</span><span class="p">,</span>
|
||
|
<span class="s">'vmware.vapi.security.sso.JSONSSOSigner'</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">'json'</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">"""</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 'http', 'https' or 'requests'</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 'json'.</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"> """</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">'json'</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">"""</span>
|
||
|
<span class="sd"> Create a connection that uses 'requests' 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 'json'.</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"> """</span>
|
||
|
<span class="k">return</span> <span class="n">get_connector</span><span class="p">(</span><span class="s">'requests'</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> »</li>
|
||
|
<li><a href="../../../index.html" >Module code</a> »</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
<div class="footer">
|
||
|
© Copyright 2014, VMware, Inc..
|
||
|
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|