http://git-wip-us.apache.org/repos/asf/incubator-airflow-site/blob/11437c14/_modules/airflow/contrib/hooks/redshift_hook.html ---------------------------------------------------------------------- diff --git a/_modules/airflow/contrib/hooks/redshift_hook.html b/_modules/airflow/contrib/hooks/redshift_hook.html new file mode 100644 index 0000000..314fd9e --- /dev/null +++ b/_modules/airflow/contrib/hooks/redshift_hook.html @@ -0,0 +1,348 @@ + + +<!DOCTYPE html> +<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]--> +<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]--> +<head> + <meta charset="utf-8"> + + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + + <title>airflow.contrib.hooks.redshift_hook — Airflow Documentation</title> + + + + + + + + + + + + + + + + + + <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" /> + + + + + + <link rel="index" title="Index" + href="../../../../genindex.html"/> + <link rel="search" title="Search" href="../../../../search.html"/> + <link rel="top" title="Airflow Documentation" href="../../../../index.html"/> + <link rel="up" title="Module code" href="../../../index.html"/> + + + <script src="../../../../_static/js/modernizr.min.js"></script> + +</head> + +<body class="wy-body-for-nav" role="document"> + + + <div class="wy-grid-for-nav"> + + + <nav data-toggle="wy-nav-shift" class="wy-nav-side"> + <div class="wy-side-scroll"> + <div class="wy-side-nav-search"> + + + + <a href="../../../../index.html" class="icon icon-home"> Airflow + + + + </a> + + + + + + + +<div role="search"> + <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get"> + <input type="text" name="q" placeholder="Search docs" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> +</div> + + + </div> + + <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation"> + + + + + + + <ul> +<li class="toctree-l1"><a class="reference internal" href="../../../../project.html">Project</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../license.html">License</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../start.html">Quick Start</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../installation.html">Installation</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../tutorial.html">Tutorial</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../howto/index.html">How-to Guides</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../ui.html">UI / Screenshots</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../concepts.html">Concepts</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../profiling.html">Data Profiling</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../cli.html">Command Line Interface</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../scheduler.html">Scheduling & Triggers</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../plugins.html">Plugins</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../security.html">Security</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../timezone.html">Time zones</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../api.html">Experimental Rest API</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../integration.html">Integration</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../lineage.html">Lineage</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../faq.html">FAQ</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../code.html">API Reference</a></li> +</ul> + + + + </div> + </div> + </nav> + + <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> + + + <nav class="wy-nav-top" role="navigation" aria-label="top navigation"> + + <i data-toggle="wy-nav-top" class="fa fa-bars"></i> + <a href="../../../../index.html">Airflow</a> + + </nav> + + + + <div class="wy-nav-content"> + <div class="rst-content"> + + + + + + + + + + + + + + + + +<div role="navigation" aria-label="breadcrumbs navigation"> + + <ul class="wy-breadcrumbs"> + + <li><a href="../../../../index.html">Docs</a> »</li> + + <li><a href="../../../index.html">Module code</a> »</li> + + <li>airflow.contrib.hooks.redshift_hook</li> + + + <li class="wy-breadcrumbs-aside"> + + + + </li> + + </ul> + + + <hr/> +</div> + <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article"> + <div itemprop="articleBody"> + + <h1>Source code for airflow.contrib.hooks.redshift_hook</h1><div class="highlight"><pre> +<span></span><span class="c1"># -*- coding: utf-8 -*-</span> +<span class="c1">#</span> +<span class="c1"># Licensed to the Apache Software Foundation (ASF) under one</span> +<span class="c1"># or more contributor license agreements. See the NOTICE file</span> +<span class="c1"># distributed with this work for additional information</span> +<span class="c1"># regarding copyright ownership. The ASF licenses this file</span> +<span class="c1"># to you under the Apache License, Version 2.0 (the</span> +<span class="c1"># "License"); you may not use this file except in compliance</span> +<span class="c1"># with the License. You may obtain a copy of the License at</span> +<span class="c1"># </span> +<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span> +<span class="c1"># </span> +<span class="c1"># Unless required by applicable law or agreed to in writing,</span> +<span class="c1"># software distributed under the License is distributed on an</span> +<span class="c1"># "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY</span> +<span class="c1"># KIND, either express or implied. See the License for the</span> +<span class="c1"># specific language governing permissions and limitations</span> +<span class="c1"># under the License.</span> + +<span class="kn">from</span> <span class="nn">airflow.contrib.hooks.aws_hook</span> <span class="k">import</span> <span class="n">AwsHook</span> + + +<div class="viewcode-block" id="RedshiftHook"><a class="viewcode-back" href="../../../../integration.html#airflow.contrib.hooks.redshift_hook.RedshiftHook">[docs]</a><span class="k">class</span> <span class="nc">RedshiftHook</span><span class="p">(</span><span class="n">AwsHook</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Interact with AWS Redshift, using the boto3 library</span> +<span class="sd"> """</span> + <span class="k">def</span> <span class="nf">get_conn</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> + <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_client_type</span><span class="p">(</span><span class="s1">'redshift'</span><span class="p">)</span> + + <span class="c1"># TODO: Wrap create_cluster_snapshot</span> +<div class="viewcode-block" id="RedshiftHook.cluster_status"><a class="viewcode-back" href="../../../../integration.html#airflow.contrib.hooks.redshift_hook.RedshiftHook.cluster_status">[docs]</a> <span class="k">def</span> <span class="nf">cluster_status</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cluster_identifier</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Return status of a cluster</span> + +<span class="sd"> :param cluster_identifier: unique identifier of a cluster</span> +<span class="sd"> :type cluster_identifier: str</span> +<span class="sd"> """</span> + <span class="n">conn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span> + <span class="k">try</span><span class="p">:</span> + <span class="n">response</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">describe_clusters</span><span class="p">(</span> + <span class="n">ClusterIdentifier</span><span class="o">=</span><span class="n">cluster_identifier</span><span class="p">)[</span><span class="s1">'Clusters'</span><span class="p">]</span> + <span class="k">return</span> <span class="n">response</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s1">'ClusterStatus'</span><span class="p">]</span> <span class="k">if</span> <span class="n">response</span> <span class="k">else</span> <span class="kc">None</span> + <span class="k">except</span> <span class="n">conn</span><span class="o">.</span><span class="n">exceptions</span><span class="o">.</span><span class="n">ClusterNotFoundFault</span><span class="p">:</span> + <span class="k">return</span> <span class="s1">'cluster_not_found'</span></div> + +<div class="viewcode-block" id="RedshiftHook.delete_cluster"><a class="viewcode-back" href="../../../../integration.html#airflow.contrib.hooks.redshift_hook.RedshiftHook.delete_cluster">[docs]</a> <span class="k">def</span> <span class="nf">delete_cluster</span><span class="p">(</span> + <span class="bp">self</span><span class="p">,</span> + <span class="n">cluster_identifier</span><span class="p">,</span> + <span class="n">skip_final_cluster_snapshot</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> + <span class="n">final_cluster_snapshot_identifier</span><span class="o">=</span><span class="s1">''</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Delete a cluster and optionally create a snapshot</span> + +<span class="sd"> :param cluster_identifier: unique identifier of a cluster</span> +<span class="sd"> :type cluster_identifier: str</span> +<span class="sd"> :param skip_final_cluster_snapshot: determines cluster snapshot creation</span> +<span class="sd"> :type skip_final_cluster_snapshot: bool</span> +<span class="sd"> :param final_cluster_snapshot_identifier: name of final cluster snapshot</span> +<span class="sd"> :type final_cluster_snapshot_identifier: str</span> +<span class="sd"> """</span> + <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span><span class="o">.</span><span class="n">delete_cluster</span><span class="p">(</span> + <span class="n">ClusterIdentifier</span><span class="o">=</span><span class="n">cluster_identifier</span><span class="p">,</span> + <span class="n">SkipFinalClusterSnapshot</span><span class="o">=</span><span class="n">skip_final_cluster_snapshot</span><span class="p">,</span> + <span class="n">FinalClusterSnapshotIdentifier</span><span class="o">=</span><span class="n">final_cluster_snapshot_identifier</span> + <span class="p">)</span> + <span class="k">return</span> <span class="n">response</span><span class="p">[</span><span class="s1">'Cluster'</span><span class="p">]</span> <span class="k">if</span> <span class="n">response</span><span class="p">[</span><span class="s1">'Cluster'</span><span class="p">]</span> <span class="k">else</span> <span class="kc">None</span></div> + +<div class="viewcode-block" id="RedshiftHook.describe_cluster_snapshots"><a class="viewcode-back" href="../../../../integration.html#airflow.contrib.hooks.redshift_hook.RedshiftHook.describe_cluster_snapshots">[docs]</a> <span class="k">def</span> <span class="nf">describe_cluster_snapshots</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cluster_identifier</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Gets a list of snapshots for a cluster</span> + +<span class="sd"> :param cluster_identifier: unique identifier of a cluster</span> +<span class="sd"> :type cluster_identifier: str</span> +<span class="sd"> """</span> + <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span><span class="o">.</span><span class="n">describe_cluster_snapshots</span><span class="p">(</span> + <span class="n">ClusterIdentifier</span><span class="o">=</span><span class="n">cluster_identifier</span> + <span class="p">)</span> + <span class="k">if</span> <span class="s1">'Snapshots'</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">response</span><span class="p">:</span> + <span class="k">return</span> <span class="kc">None</span> + <span class="n">snapshots</span> <span class="o">=</span> <span class="n">response</span><span class="p">[</span><span class="s1">'Snapshots'</span><span class="p">]</span> + <span class="n">snapshots</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="s1">'Status'</span><span class="p">],</span> <span class="n">snapshots</span><span class="p">)</span> + <span class="n">snapshots</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="s1">'SnapshotCreateTime'</span><span class="p">],</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> + <span class="k">return</span> <span class="n">snapshots</span></div> + +<div class="viewcode-block" id="RedshiftHook.restore_from_cluster_snapshot"><a class="viewcode-back" href="../../../../integration.html#airflow.contrib.hooks.redshift_hook.RedshiftHook.restore_from_cluster_snapshot">[docs]</a> <span class="k">def</span> <span class="nf">restore_from_cluster_snapshot</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cluster_identifier</span><span class="p">,</span> <span class="n">snapshot_identifier</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Restores a cluster from its snapshot</span> + +<span class="sd"> :param cluster_identifier: unique identifier of a cluster</span> +<span class="sd"> :type cluster_identifier: str</span> +<span class="sd"> :param snapshot_identifier: unique identifier for a snapshot of a cluster</span> +<span class="sd"> :type snapshot_identifier: str</span> +<span class="sd"> """</span> + <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span><span class="o">.</span><span class="n">restore_from_cluster_snapshot</span><span class="p">(</span> + <span class="n">ClusterIdentifier</span><span class="o">=</span><span class="n">cluster_identifier</span><span class="p">,</span> + <span class="n">SnapshotIdentifier</span><span class="o">=</span><span class="n">snapshot_identifier</span> + <span class="p">)</span> + <span class="k">return</span> <span class="n">response</span><span class="p">[</span><span class="s1">'Cluster'</span><span class="p">]</span> <span class="k">if</span> <span class="n">response</span><span class="p">[</span><span class="s1">'Cluster'</span><span class="p">]</span> <span class="k">else</span> <span class="kc">None</span></div> + +<div class="viewcode-block" id="RedshiftHook.create_cluster_snapshot"><a class="viewcode-back" href="../../../../integration.html#airflow.contrib.hooks.redshift_hook.RedshiftHook.create_cluster_snapshot">[docs]</a> <span class="k">def</span> <span class="nf">create_cluster_snapshot</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">snapshot_identifier</span><span class="p">,</span> <span class="n">cluster_identifier</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Creates a snapshot of a cluster</span> + +<span class="sd"> :param snapshot_identifier: unique identifier for a snapshot of a cluster</span> +<span class="sd"> :type snapshot_identifier: str</span> +<span class="sd"> :param cluster_identifier: unique identifier of a cluster</span> +<span class="sd"> :type cluster_identifier: str</span> +<span class="sd"> """</span> + <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span><span class="o">.</span><span class="n">create_cluster_snapshot</span><span class="p">(</span> + <span class="n">SnapshotIdentifier</span><span class="o">=</span><span class="n">snapshot_identifier</span><span class="p">,</span> + <span class="n">ClusterIdentifier</span><span class="o">=</span><span class="n">cluster_identifier</span><span class="p">,</span> + <span class="p">)</span> + <span class="k">return</span> <span class="n">response</span><span class="p">[</span><span class="s1">'Snapshot'</span><span class="p">]</span> <span class="k">if</span> <span class="n">response</span><span class="p">[</span><span class="s1">'Snapshot'</span><span class="p">]</span> <span class="k">else</span> <span class="kc">None</span></div></div> +</pre></div> + + </div> + <div class="articleComments"> + + </div> + </div> + <footer> + + + <hr/> + + <div role="contentinfo"> + <p> + + </p> + </div> + Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. + +</footer> + + </div> + </div> + + </section> + + </div> + + + + + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT:'../../../../', + VERSION:'', + COLLAPSE_INDEX:false, + FILE_SUFFIX:'.html', + HAS_SOURCE: true, + SOURCELINK_SUFFIX: '.txt' + }; + </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> + + + + + + <script type="text/javascript" src="../../../../_static/js/theme.js"></script> + + + + + <script type="text/javascript"> + jQuery(function () { + SphinxRtdTheme.StickyNav.enable(); + }); + </script> + + +</body> +</html> \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-airflow-site/blob/11437c14/_modules/airflow/contrib/hooks/sftp_hook.html ---------------------------------------------------------------------- diff --git a/_modules/airflow/contrib/hooks/sftp_hook.html b/_modules/airflow/contrib/hooks/sftp_hook.html new file mode 100644 index 0000000..6b048cb --- /dev/null +++ b/_modules/airflow/contrib/hooks/sftp_hook.html @@ -0,0 +1,404 @@ + + +<!DOCTYPE html> +<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]--> +<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]--> +<head> + <meta charset="utf-8"> + + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + + <title>airflow.contrib.hooks.sftp_hook — Airflow Documentation</title> + + + + + + + + + + + + + + + + + + <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" /> + + + + + + <link rel="index" title="Index" + href="../../../../genindex.html"/> + <link rel="search" title="Search" href="../../../../search.html"/> + <link rel="top" title="Airflow Documentation" href="../../../../index.html"/> + <link rel="up" title="Module code" href="../../../index.html"/> + + + <script src="../../../../_static/js/modernizr.min.js"></script> + +</head> + +<body class="wy-body-for-nav" role="document"> + + + <div class="wy-grid-for-nav"> + + + <nav data-toggle="wy-nav-shift" class="wy-nav-side"> + <div class="wy-side-scroll"> + <div class="wy-side-nav-search"> + + + + <a href="../../../../index.html" class="icon icon-home"> Airflow + + + + </a> + + + + + + + +<div role="search"> + <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get"> + <input type="text" name="q" placeholder="Search docs" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> +</div> + + + </div> + + <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation"> + + + + + + + <ul> +<li class="toctree-l1"><a class="reference internal" href="../../../../project.html">Project</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../license.html">License</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../start.html">Quick Start</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../installation.html">Installation</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../tutorial.html">Tutorial</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../howto/index.html">How-to Guides</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../ui.html">UI / Screenshots</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../concepts.html">Concepts</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../profiling.html">Data Profiling</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../cli.html">Command Line Interface</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../scheduler.html">Scheduling & Triggers</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../plugins.html">Plugins</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../security.html">Security</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../timezone.html">Time zones</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../api.html">Experimental Rest API</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../integration.html">Integration</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../lineage.html">Lineage</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../faq.html">FAQ</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../code.html">API Reference</a></li> +</ul> + + + + </div> + </div> + </nav> + + <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> + + + <nav class="wy-nav-top" role="navigation" aria-label="top navigation"> + + <i data-toggle="wy-nav-top" class="fa fa-bars"></i> + <a href="../../../../index.html">Airflow</a> + + </nav> + + + + <div class="wy-nav-content"> + <div class="rst-content"> + + + + + + + + + + + + + + + + +<div role="navigation" aria-label="breadcrumbs navigation"> + + <ul class="wy-breadcrumbs"> + + <li><a href="../../../../index.html">Docs</a> »</li> + + <li><a href="../../../index.html">Module code</a> »</li> + + <li>airflow.contrib.hooks.sftp_hook</li> + + + <li class="wy-breadcrumbs-aside"> + + + + </li> + + </ul> + + + <hr/> +</div> + <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article"> + <div itemprop="articleBody"> + + <h1>Source code for airflow.contrib.hooks.sftp_hook</h1><div class="highlight"><pre> +<span></span><span class="c1"># -*- coding: utf-8 -*-</span> +<span class="c1">#</span> +<span class="c1"># Licensed to the Apache Software Foundation (ASF) under one</span> +<span class="c1"># or more contributor license agreements. See the NOTICE file</span> +<span class="c1"># distributed with this work for additional information</span> +<span class="c1"># regarding copyright ownership. The ASF licenses this file</span> +<span class="c1"># to you under the Apache License, Version 2.0 (the</span> +<span class="c1"># "License"); you may not use this file except in compliance</span> +<span class="c1"># with the License. You may obtain a copy of the License at</span> +<span class="c1"># </span> +<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span> +<span class="c1"># </span> +<span class="c1"># Unless required by applicable law or agreed to in writing,</span> +<span class="c1"># software distributed under the License is distributed on an</span> +<span class="c1"># "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY</span> +<span class="c1"># KIND, either express or implied. See the License for the</span> +<span class="c1"># specific language governing permissions and limitations</span> +<span class="c1"># under the License.</span> + +<span class="kn">import</span> <span class="nn">stat</span> +<span class="kn">import</span> <span class="nn">pysftp</span> +<span class="kn">import</span> <span class="nn">logging</span> +<span class="kn">import</span> <span class="nn">datetime</span> +<span class="kn">from</span> <span class="nn">airflow.hooks.base_hook</span> <span class="k">import</span> <span class="n">BaseHook</span> + + +<div class="viewcode-block" id="SFTPHook"><a class="viewcode-back" href="../../../../code.html#airflow.contrib.hooks.sftp_hook.SFTPHook">[docs]</a><span class="k">class</span> <span class="nc">SFTPHook</span><span class="p">(</span><span class="n">BaseHook</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Interact with SFTP. Aims to be interchangeable with FTPHook.</span> + +<span class="sd"> Pitfalls: - In contrast with FTPHook describe_directory only returns size, type and</span> +<span class="sd"> modify. It doesn't return unix.owner, unix.mode, perm, unix.group and</span> +<span class="sd"> unique.</span> +<span class="sd"> - retrieve_file and store_file only take a local full path and not a</span> +<span class="sd"> buffer.</span> +<span class="sd"> - If no mode is passed to create_directory it will be created with 777</span> +<span class="sd"> permissions.</span> + +<span class="sd"> Errors that may occur throughout but should be handled downstream.</span> +<span class="sd"> """</span> + + <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ftp_conn_id</span><span class="o">=</span><span class="s1">'sftp_default'</span><span class="p">):</span> + <span class="bp">self</span><span class="o">.</span><span class="n">ftp_conn_id</span> <span class="o">=</span> <span class="n">ftp_conn_id</span> + <span class="bp">self</span><span class="o">.</span><span class="n">conn</span> <span class="o">=</span> <span class="kc">None</span> + +<div class="viewcode-block" id="SFTPHook.get_conn"><a class="viewcode-back" href="../../../../code.html#airflow.contrib.hooks.sftp_hook.SFTPHook.get_conn">[docs]</a> <span class="k">def</span> <span class="nf">get_conn</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Returns an SFTP connection object</span> +<span class="sd"> """</span> + <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">conn</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> + <span class="n">params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_connection</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ftp_conn_id</span><span class="p">)</span> + <span class="n">cnopts</span> <span class="o">=</span> <span class="n">pysftp</span><span class="o">.</span><span class="n">CnOpts</span><span class="p">()</span> + <span class="k">if</span> <span class="p">(</span><span class="s1">'ignore_hostkey_verification'</span> <span class="ow">in</span> <span class="n">params</span><span class="o">.</span><span class="n">extra_dejson</span> <span class="ow">and</span> + <span class="n">params</span><span class="o">.</span><span class="n">extra_dejson</span><span class="p">[</span><span class="s1">'ignore_hostkey_verification'</span><span class="p">]):</span> + <span class="n">cnopts</span><span class="o">.</span><span class="n">hostkeys</span> <span class="o">=</span> <span class="kc">None</span> + <span class="n">conn_params</span> <span class="o">=</span> <span class="p">{</span> + <span class="s1">'host'</span><span class="p">:</span> <span class="n">params</span><span class="o">.</span><span class="n">host</span><span class="p">,</span> + <span class="s1">'port'</span><span class="p">:</span> <span class="n">params</span><span class="o">.</span><span class="n">port</span><span class="p">,</span> + <span class="s1">'username'</span><span class="p">:</span> <span class="n">params</span><span class="o">.</span><span class="n">login</span><span class="p">,</span> + <span class="s1">'cnopts'</span><span class="p">:</span> <span class="n">cnopts</span> + <span class="p">}</span> + <span class="k">if</span> <span class="n">params</span><span class="o">.</span><span class="n">password</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> + <span class="n">conn_params</span><span class="p">[</span><span class="s1">'password'</span><span class="p">]</span> <span class="o">=</span> <span class="n">params</span><span class="o">.</span><span class="n">password</span> + <span class="k">if</span> <span class="s1">'private_key'</span> <span class="ow">in</span> <span class="n">params</span><span class="o">.</span><span class="n">extra_dejson</span><span class="p">:</span> + <span class="n">conn_params</span><span class="p">[</span><span class="s1">'private_key'</span><span class="p">]</span> <span class="o">=</span> <span class="n">params</span><span class="o">.</span><span class="n">extra_dejson</span><span class="p">[</span><span class="s1">'private_key'</span><span class="p">]</span> + <span class="k">if</span> <span class="s1">'private_key_pass'</span> <span class="ow">in</span> <span class="n">params</span><span class="o">.</span><span class="n">extra_dejson</span><span class="p">:</span> + <span class="n">conn_params</span><span class="p">[</span><span class="s1">'private_key_pass'</span><span class="p">]</span> <span class="o">=</span> <span class="n">params</span><span class="o">.</span><span class="n">extra_dejson</span><span class="p">[</span><span class="s1">'private_key_pass'</span><span class="p">]</span> + <span class="bp">self</span><span class="o">.</span><span class="n">conn</span> <span class="o">=</span> <span class="n">pysftp</span><span class="o">.</span><span class="n">Connection</span><span class="p">(</span><span class="o">**</span><span class="n">conn_params</span><span class="p">)</span> + <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">conn</span></div> + +<div class="viewcode-block" id="SFTPHook.close_conn"><a class="viewcode-back" href="../../../../code.html#airflow.contrib.hooks.sftp_hook.SFTPHook.close_conn">[docs]</a> <span class="k">def</span> <span class="nf">close_conn</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Closes the connection. An error will occur if the</span> +<span class="sd"> connection wasnt ever opened.</span> +<span class="sd"> """</span> + <span class="n">conn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">conn</span> + <span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> + <span class="bp">self</span><span class="o">.</span><span class="n">conn</span> <span class="o">=</span> <span class="kc">None</span></div> + +<div class="viewcode-block" id="SFTPHook.describe_directory"><a class="viewcode-back" href="../../../../code.html#airflow.contrib.hooks.sftp_hook.SFTPHook.describe_directory">[docs]</a> <span class="k">def</span> <span class="nf">describe_directory</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Returns a dictionary of {filename: {attributes}} for all files</span> +<span class="sd"> on the remote system (where the MLSD command is supported).</span> +<span class="sd"> :param path: full path to the remote directory</span> +<span class="sd"> :type path: str</span> +<span class="sd"> """</span> + <span class="n">conn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span> + <span class="n">flist</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">listdir_attr</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> + <span class="n">files</span> <span class="o">=</span> <span class="p">{}</span> + <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">flist</span><span class="p">:</span> + <span class="n">modify</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span> + <span class="n">f</span><span class="o">.</span><span class="n">st_mtime</span><span class="p">)</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">'%Y%m</span><span class="si">%d</span><span class="s1">%H%M%S'</span><span class="p">)</span> + <span class="n">files</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">filename</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> + <span class="s1">'size'</span><span class="p">:</span> <span class="n">f</span><span class="o">.</span><span class="n">st_size</span><span class="p">,</span> + <span class="s1">'type'</span><span class="p">:</span> <span class="s1">'dir'</span> <span class="k">if</span> <span class="n">stat</span><span class="o">.</span><span class="n">S_ISDIR</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">st_mode</span><span class="p">)</span> <span class="k">else</span> <span class="s1">'file'</span><span class="p">,</span> + <span class="s1">'modify'</span><span class="p">:</span> <span class="n">modify</span><span class="p">}</span> + <span class="k">return</span> <span class="n">files</span></div> + +<div class="viewcode-block" id="SFTPHook.list_directory"><a class="viewcode-back" href="../../../../code.html#airflow.contrib.hooks.sftp_hook.SFTPHook.list_directory">[docs]</a> <span class="k">def</span> <span class="nf">list_directory</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Returns a list of files on the remote system.</span> +<span class="sd"> :param path: full path to the remote directory to list</span> +<span class="sd"> :type path: str</span> +<span class="sd"> """</span> + <span class="n">conn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span> + <span class="n">files</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> + <span class="k">return</span> <span class="n">files</span></div> + +<div class="viewcode-block" id="SFTPHook.create_directory"><a class="viewcode-back" href="../../../../code.html#airflow.contrib.hooks.sftp_hook.SFTPHook.create_directory">[docs]</a> <span class="k">def</span> <span class="nf">create_directory</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="mi">777</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Creates a directory on the remote system.</span> +<span class="sd"> :param path: full path to the remote directory to create</span> +<span class="sd"> :type path: str</span> +<span class="sd"> :param mode: int representation of octal mode for directory</span> +<span class="sd"> """</span> + <span class="n">conn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span> + <span class="n">conn</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">mode</span><span class="p">)</span></div> + +<div class="viewcode-block" id="SFTPHook.delete_directory"><a class="viewcode-back" href="../../../../code.html#airflow.contrib.hooks.sftp_hook.SFTPHook.delete_directory">[docs]</a> <span class="k">def</span> <span class="nf">delete_directory</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Deletes a directory on the remote system.</span> +<span class="sd"> :param path: full path to the remote directory to delete</span> +<span class="sd"> :type path: str</span> +<span class="sd"> """</span> + <span class="n">conn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span> + <span class="n">conn</span><span class="o">.</span><span class="n">rmdir</span><span class="p">(</span><span class="n">path</span><span class="p">)</span></div> + +<div class="viewcode-block" id="SFTPHook.retrieve_file"><a class="viewcode-back" href="../../../../code.html#airflow.contrib.hooks.sftp_hook.SFTPHook.retrieve_file">[docs]</a> <span class="k">def</span> <span class="nf">retrieve_file</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">remote_full_path</span><span class="p">,</span> <span class="n">local_full_path</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Transfers the remote file to a local location.</span> +<span class="sd"> If local_full_path is a string path, the file will be put</span> +<span class="sd"> at that location</span> +<span class="sd"> :param remote_full_path: full path to the remote file</span> +<span class="sd"> :type remote_full_path: str</span> +<span class="sd"> :param local_full_path: full path to the local file</span> +<span class="sd"> :type local_full_path: str</span> +<span class="sd"> """</span> + <span class="n">conn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span> + <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">'Retrieving file from FTP: </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">remote_full_path</span><span class="p">))</span> + <span class="n">conn</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">remote_full_path</span><span class="p">,</span> <span class="n">local_full_path</span><span class="p">)</span> + <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">'Finished retrieving file from FTP: </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> + <span class="n">remote_full_path</span><span class="p">))</span></div> + +<div class="viewcode-block" id="SFTPHook.store_file"><a class="viewcode-back" href="../../../../code.html#airflow.contrib.hooks.sftp_hook.SFTPHook.store_file">[docs]</a> <span class="k">def</span> <span class="nf">store_file</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">remote_full_path</span><span class="p">,</span> <span class="n">local_full_path</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Transfers a local file to the remote location.</span> +<span class="sd"> If local_full_path_or_buffer is a string path, the file will be read</span> +<span class="sd"> from that location</span> +<span class="sd"> :param remote_full_path: full path to the remote file</span> +<span class="sd"> :type remote_full_path: str</span> +<span class="sd"> :param local_full_path: full path to the local file</span> +<span class="sd"> :type local_full_path: str</span> +<span class="sd"> """</span> + <span class="n">conn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span> + <span class="n">conn</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">local_full_path</span><span class="p">,</span> <span class="n">remote_full_path</span><span class="p">)</span></div> + +<div class="viewcode-block" id="SFTPHook.delete_file"><a class="viewcode-back" href="../../../../code.html#airflow.contrib.hooks.sftp_hook.SFTPHook.delete_file">[docs]</a> <span class="k">def</span> <span class="nf">delete_file</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Removes a file on the FTP Server</span> +<span class="sd"> :param path: full path to the remote file</span> +<span class="sd"> :type path: str</span> +<span class="sd"> """</span> + <span class="n">conn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span> + <span class="n">conn</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">path</span><span class="p">)</span></div> + + <span class="k">def</span> <span class="nf">get_mod_time</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span> + <span class="n">conn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_conn</span><span class="p">()</span> + <span class="n">ftp_mdtm</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="n">path</span><span class="p">)</span><span class="o">.</span><span class="n">st_mtime</span> + <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="n">ftp_mdtm</span><span class="p">)</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">'%Y%m</span><span class="si">%d</span><span class="s1">%H%M%S'</span><span class="p">)</span></div> +</pre></div> + + </div> + <div class="articleComments"> + + </div> + </div> + <footer> + + + <hr/> + + <div role="contentinfo"> + <p> + + </p> + </div> + Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. + +</footer> + + </div> + </div> + + </section> + + </div> + + + + + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT:'../../../../', + VERSION:'', + COLLAPSE_INDEX:false, + FILE_SUFFIX:'.html', + HAS_SOURCE: true, + SOURCELINK_SUFFIX: '.txt' + }; + </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> + + + + + + <script type="text/javascript" src="../../../../_static/js/theme.js"></script> + + + + + <script type="text/javascript"> + jQuery(function () { + SphinxRtdTheme.StickyNav.enable(); + }); + </script> + + +</body> +</html> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-airflow-site/blob/11437c14/_modules/airflow/contrib/hooks/slack_webhook_hook.html ---------------------------------------------------------------------- diff --git a/_modules/airflow/contrib/hooks/slack_webhook_hook.html b/_modules/airflow/contrib/hooks/slack_webhook_hook.html new file mode 100644 index 0000000..e3e9dfc --- /dev/null +++ b/_modules/airflow/contrib/hooks/slack_webhook_hook.html @@ -0,0 +1,364 @@ + + +<!DOCTYPE html> +<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]--> +<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]--> +<head> + <meta charset="utf-8"> + + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + + <title>airflow.contrib.hooks.slack_webhook_hook — Airflow Documentation</title> + + + + + + + + + + + + + + + + + + <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" /> + + + + + + <link rel="index" title="Index" + href="../../../../genindex.html"/> + <link rel="search" title="Search" href="../../../../search.html"/> + <link rel="top" title="Airflow Documentation" href="../../../../index.html"/> + <link rel="up" title="Module code" href="../../../index.html"/> + + + <script src="../../../../_static/js/modernizr.min.js"></script> + +</head> + +<body class="wy-body-for-nav" role="document"> + + + <div class="wy-grid-for-nav"> + + + <nav data-toggle="wy-nav-shift" class="wy-nav-side"> + <div class="wy-side-scroll"> + <div class="wy-side-nav-search"> + + + + <a href="../../../../index.html" class="icon icon-home"> Airflow + + + + </a> + + + + + + + +<div role="search"> + <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get"> + <input type="text" name="q" placeholder="Search docs" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> +</div> + + + </div> + + <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation"> + + + + + + + <ul> +<li class="toctree-l1"><a class="reference internal" href="../../../../project.html">Project</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../license.html">License</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../start.html">Quick Start</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../installation.html">Installation</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../tutorial.html">Tutorial</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../howto/index.html">How-to Guides</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../ui.html">UI / Screenshots</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../concepts.html">Concepts</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../profiling.html">Data Profiling</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../cli.html">Command Line Interface</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../scheduler.html">Scheduling & Triggers</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../plugins.html">Plugins</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../security.html">Security</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../timezone.html">Time zones</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../api.html">Experimental Rest API</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../integration.html">Integration</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../lineage.html">Lineage</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../faq.html">FAQ</a></li> +<li class="toctree-l1"><a class="reference internal" href="../../../../code.html">API Reference</a></li> +</ul> + + + + </div> + </div> + </nav> + + <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> + + + <nav class="wy-nav-top" role="navigation" aria-label="top navigation"> + + <i data-toggle="wy-nav-top" class="fa fa-bars"></i> + <a href="../../../../index.html">Airflow</a> + + </nav> + + + + <div class="wy-nav-content"> + <div class="rst-content"> + + + + + + + + + + + + + + + + +<div role="navigation" aria-label="breadcrumbs navigation"> + + <ul class="wy-breadcrumbs"> + + <li><a href="../../../../index.html">Docs</a> »</li> + + <li><a href="../../../index.html">Module code</a> »</li> + + <li>airflow.contrib.hooks.slack_webhook_hook</li> + + + <li class="wy-breadcrumbs-aside"> + + + + </li> + + </ul> + + + <hr/> +</div> + <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article"> + <div itemprop="articleBody"> + + <h1>Source code for airflow.contrib.hooks.slack_webhook_hook</h1><div class="highlight"><pre> +<span></span><span class="c1"># -*- coding: utf-8 -*-</span> +<span class="c1">#</span> +<span class="c1"># Licensed to the Apache Software Foundation (ASF) under one</span> +<span class="c1"># or more contributor license agreements. See the NOTICE file</span> +<span class="c1"># distributed with this work for additional information</span> +<span class="c1"># regarding copyright ownership. The ASF licenses this file</span> +<span class="c1"># to you under the Apache License, Version 2.0 (the</span> +<span class="c1"># "License"); you may not use this file except in compliance</span> +<span class="c1"># with the License. You may obtain a copy of the License at</span> +<span class="c1"># </span> +<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span> +<span class="c1"># </span> +<span class="c1"># Unless required by applicable law or agreed to in writing,</span> +<span class="c1"># software distributed under the License is distributed on an</span> +<span class="c1"># "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY</span> +<span class="c1"># KIND, either express or implied. See the License for the</span> +<span class="c1"># specific language governing permissions and limitations</span> +<span class="c1"># under the License.</span> +<span class="c1">#</span> +<span class="kn">import</span> <span class="nn">json</span> + +<span class="kn">from</span> <span class="nn">airflow.hooks.http_hook</span> <span class="k">import</span> <span class="n">HttpHook</span> +<span class="kn">from</span> <span class="nn">airflow.exceptions</span> <span class="k">import</span> <span class="n">AirflowException</span> + + +<div class="viewcode-block" id="SlackWebhookHook"><a class="viewcode-back" href="../../../../code.html#airflow.contrib.hooks.slack_webhook_hook.SlackWebhookHook">[docs]</a><span class="k">class</span> <span class="nc">SlackWebhookHook</span><span class="p">(</span><span class="n">HttpHook</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> This hook allows you to post messages to Slack using incoming webhooks.</span> +<span class="sd"> Takes both Slack webhook token directly and connection that has Slack webhook token.</span> +<span class="sd"> If both supplied, Slack webhook token will be used.</span> + +<span class="sd"> Each Slack webhook token can be pre-configured to use a specific channel, username and</span> +<span class="sd"> icon. You can override these defaults in this hook.</span> + +<span class="sd"> :param http_conn_id: connection that has Slack webhook token in the extra field</span> +<span class="sd"> :type http_conn_id: str</span> +<span class="sd"> :param webhook_token: Slack webhook token</span> +<span class="sd"> :type webhook_token: str</span> +<span class="sd"> :param message: The message you want to send on Slack</span> +<span class="sd"> :type message: str</span> +<span class="sd"> :param channel: The channel the message should be posted to</span> +<span class="sd"> :type channel: str</span> +<span class="sd"> :param username: The username to post to slack with</span> +<span class="sd"> :type username: str</span> +<span class="sd"> :param icon_emoji: The emoji to use as icon for the user posting to Slack</span> +<span class="sd"> :type icon_emoji: str</span> +<span class="sd"> :param link_names: Whether or not to find and link channel and usernames in your</span> +<span class="sd"> message</span> +<span class="sd"> :type link_names: bool</span> +<span class="sd"> :param proxy: Proxy to use to make the Slack webhook call</span> +<span class="sd"> :type proxy: str</span> +<span class="sd"> """</span> + <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> + <span class="n">http_conn_id</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> + <span class="n">webhook_token</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> + <span class="n">message</span><span class="o">=</span><span class="s2">""</span><span class="p">,</span> + <span class="n">channel</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> + <span class="n">username</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> + <span class="n">icon_emoji</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> + <span class="n">link_names</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> + <span class="n">proxy</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> + <span class="o">*</span><span class="n">args</span><span class="p">,</span> + <span class="o">**</span><span class="n">kwargs</span> + <span class="p">):</span> + <span class="nb">super</span><span class="p">(</span><span class="n">SlackWebhookHook</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> + <span class="bp">self</span><span class="o">.</span><span class="n">http_conn_id</span> <span class="o">=</span> <span class="n">http_conn_id</span> + <span class="bp">self</span><span class="o">.</span><span class="n">webhook_token</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_token</span><span class="p">(</span><span class="n">webhook_token</span><span class="p">,</span> <span class="n">http_conn_id</span><span class="p">)</span> + <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">message</span> + <span class="bp">self</span><span class="o">.</span><span class="n">channel</span> <span class="o">=</span> <span class="n">channel</span> + <span class="bp">self</span><span class="o">.</span><span class="n">username</span> <span class="o">=</span> <span class="n">username</span> + <span class="bp">self</span><span class="o">.</span><span class="n">icon_emoji</span> <span class="o">=</span> <span class="n">icon_emoji</span> + <span class="bp">self</span><span class="o">.</span><span class="n">link_names</span> <span class="o">=</span> <span class="n">link_names</span> + <span class="bp">self</span><span class="o">.</span><span class="n">proxy</span> <span class="o">=</span> <span class="n">proxy</span> + + <span class="k">def</span> <span class="nf">_get_token</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">token</span><span class="p">,</span> <span class="n">http_conn_id</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Given either a manually set token or a conn_id, return the webhook_token to use</span> +<span class="sd"> :param token: The manually provided token</span> +<span class="sd"> :param conn_id: The conn_id provided</span> +<span class="sd"> :return: webhook_token (str) to use</span> +<span class="sd"> """</span> + <span class="k">if</span> <span class="n">token</span><span class="p">:</span> + <span class="k">return</span> <span class="n">token</span> + <span class="k">elif</span> <span class="n">http_conn_id</span><span class="p">:</span> + <span class="n">conn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_connection</span><span class="p">(</span><span class="n">http_conn_id</span><span class="p">)</span> + <span class="n">extra</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">extra_dejson</span> + <span class="k">return</span> <span class="n">extra</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'webhook_token'</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span> + <span class="k">else</span><span class="p">:</span> + <span class="k">raise</span> <span class="n">AirflowException</span><span class="p">(</span><span class="s1">'Cannot get token: No valid Slack '</span> + <span class="s1">'webhook token nor conn_id supplied'</span><span class="p">)</span> + + <span class="k">def</span> <span class="nf">_build_slack_message</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Construct the Slack message. All relevant parameters are combined here to a valid</span> +<span class="sd"> Slack json message</span> +<span class="sd"> :return: Slack message (str) to send</span> +<span class="sd"> """</span> + <span class="n">cmd</span> <span class="o">=</span> <span class="p">{}</span> + + <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">channel</span><span class="p">:</span> + <span class="n">cmd</span><span class="p">[</span><span class="s1">'channel'</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">channel</span> + <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">username</span><span class="p">:</span> + <span class="n">cmd</span><span class="p">[</span><span class="s1">'username'</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">username</span> + <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">icon_emoji</span><span class="p">:</span> + <span class="n">cmd</span><span class="p">[</span><span class="s1">'icon_emoji'</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">icon_emoji</span> + <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">link_names</span><span class="p">:</span> + <span class="n">cmd</span><span class="p">[</span><span class="s1">'link_names'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span> + + <span class="c1"># there should always be a message to post ;-)</span> + <span class="n">cmd</span><span class="p">[</span><span class="s1">'text'</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">message</span> + <span class="k">return</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span> + +<div class="viewcode-block" id="SlackWebhookHook.execute"><a class="viewcode-back" href="../../../../code.html#airflow.contrib.hooks.slack_webhook_hook.SlackWebhookHook.execute">[docs]</a> <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> + <span class="sd">"""</span> +<span class="sd"> Remote Popen (actually execute the slack webhook call)</span> + +<span class="sd"> :param cmd: command to remotely execute</span> +<span class="sd"> :param kwargs: extra arguments to Popen (see subprocess.Popen)</span> +<span class="sd"> """</span> + <span class="n">proxies</span> <span class="o">=</span> <span class="p">{}</span> + <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">proxy</span><span class="p">:</span> + <span class="c1"># we only need https proxy for Slack, as the endpoint is https</span> + <span class="n">proxies</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'https'</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">proxy</span><span class="p">}</span> + + <span class="n">slack_message</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_build_slack_message</span><span class="p">()</span> + <span class="bp">self</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">endpoint</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">webhook_token</span><span class="p">,</span> + <span class="n">data</span><span class="o">=</span><span class="n">slack_message</span><span class="p">,</span> + <span class="n">headers</span><span class="o">=</span><span class="p">{</span><span class="s1">'Content-type'</span><span class="p">:</span> <span class="s1">'application/json'</span><span class="p">},</span> + <span class="n">extra_options</span><span class="o">=</span><span class="p">{</span><span class="s1">'proxies'</span><span class="p">:</span> <span class="n">proxies</span><span class="p">})</span></div></div> +</pre></div> + + </div> + <div class="articleComments"> + + </div> + </div> + <footer> + + + <hr/> + + <div role="contentinfo"> + <p> + + </p> + </div> + Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. + +</footer> + + </div> + </div> + + </section> + + </div> + + + + + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT:'../../../../', + VERSION:'', + COLLAPSE_INDEX:false, + FILE_SUFFIX:'.html', + HAS_SOURCE: true, + SOURCELINK_SUFFIX: '.txt' + }; + </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> + + + + + + <script type="text/javascript" src="../../../../_static/js/theme.js"></script> + + + + + <script type="text/javascript"> + jQuery(function () { + SphinxRtdTheme.StickyNav.enable(); + }); + </script> + + +</body> +</html> \ No newline at end of file