blob: 95443af2d437cbf3f26777e70e9a39908675714a [file] [log] [blame]
<!--
Javascript to render AIRFLOW-XXX and PR references in text
as HTML links.
Overrides extrahead block from sphinx_rtd_theme
https://www.sphinx-doc.org/en/master/templating.html
-->
<!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>Google Cloud Functions Operators &mdash; Airflow Documentation</title>
<link rel="shortcut icon" href="../../../_static/pin_32.png"/>
<script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
<script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></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/language_data.js"></script>
<script type="text/javascript" src="../../../_static/js/theme.js"></script>
<link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../../../_static/graphviz.css" type="text/css" />
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<link rel="next" title="Google Cloud Storage Operators" href="gcs.html" />
<link rel="prev" title="Google Compute Engine Operators" href="compute.html" />
<script>
document.addEventListener('DOMContentLoaded', function() {
var el = document.getElementById('changelog');
if (el !== null ) {
// [AIRFLOW-...]
el.innerHTML = el.innerHTML.replace(
/\[(AIRFLOW-[\d]+)\]/g,
`<a href="https://issues.apache.org/jira/browse/$1">[$1]</a>`
);
// (#...)
el.innerHTML = el.innerHTML.replace(
/\(#([\d]+)\)/g,
`<a href="https://github.com/apache/airflow/pull/$1">(#$1)</a>`
);
};
})
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-140539454-1']);
_gaq.push(['_trackPageview']);
</script>
<style>
.example-header {
position: relative;
background: #9AAA7A;
padding: 8px 16px;
margin-bottom: 0;
}
.example-header--with-button {
padding-right: 166px;
}
.example-header:after{
content: '';
display: table;
clear: both;
}
.example-title {
display:block;
padding: 4px;
margin-right: 16px;
color: white;
overflow-x: auto;
}
.example-header-button {
top: 8px;
right: 16px;
position: absolute;
}
.example-header + .highlight-python {
margin-top: 0 !important;
}
.viewcode-button {
display: inline-block;
padding: 8px 16px;
border: 0;
margin: 0;
outline: 0;
border-radius: 2px;
-webkit-box-shadow: 0 3px 5px 0 rgba(0,0,0,.3);
box-shadow: 0 3px 6px 0 rgba(0,0,0,.3);
color: #404040;
background-color: #e7e7e7;
cursor: pointer;
font-size: 16px;
font-weight: 500;
line-height: 1;
text-decoration: none;
text-overflow: ellipsis;
overflow: hidden;
text-transform: uppercase;
-webkit-transition: background-color .2s;
transition: background-color .2s;
vertical-align: middle;
white-space: nowrap;
}
.viewcode-button:visited {
color: #404040;
}
.viewcode-button:hover, .viewcode-button:focus {
color: #404040;
background-color: #d6d6d6;
}
</style>
</head>
<body class="wy-body-for-nav">
<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 class="version">
1.10.6
</div>
<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 class="current">
<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 current"><a class="reference internal" href="../../index.html">How-to Guides</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../../set-config.html">Setting Configuration Options</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../initialize-database.html">Initializing a Database Backend</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="../index.html">Using Operators</a><ul class="current">
<li class="toctree-l3"><a class="reference internal" href="../bash.html">BashOperator</a></li>
<li class="toctree-l3"><a class="reference internal" href="../dingding.html">Dingding Operators</a></li>
<li class="toctree-l3 current"><a class="reference internal" href="index.html">Google Cloud Operators</a><ul class="current">
<li class="toctree-l4"><a class="reference internal" href="bigtable.html">Google Cloud Bigtable Operators</a></li>
<li class="toctree-l4"><a class="reference internal" href="cloud_build.html">Google Cloud Build Operators</a></li>
<li class="toctree-l4"><a class="reference internal" href="compute.html">Google Compute Engine Operators</a></li>
<li class="toctree-l4 current"><a class="current reference internal" href="#">Google Cloud Functions Operators</a></li>
<li class="toctree-l4"><a class="reference internal" href="gcs.html">Google Cloud Storage Operators</a></li>
<li class="toctree-l4"><a class="reference internal" href="natural_language.html">Google Cloud Natural Language Operators</a></li>
<li class="toctree-l4"><a class="reference internal" href="spanner.html">Google Cloud Spanner Operators</a></li>
<li class="toctree-l4"><a class="reference internal" href="speech.html">Google Cloud Text to Speech Operators</a></li>
<li class="toctree-l4"><a class="reference internal" href="speech.html#google-cloud-speech-to-text-operators">Google Cloud Speech to Text Operators</a></li>
<li class="toctree-l4"><a class="reference internal" href="sql.html">Google Cloud Sql Operators</a></li>
<li class="toctree-l4"><a class="reference internal" href="transfer.html">Google Cloud Transfer Service Operators</a></li>
<li class="toctree-l4"><a class="reference internal" href="translate.html">Google Cloud Translate Operators</a></li>
<li class="toctree-l4"><a class="reference internal" href="translate-speech.html">Google Cloud Speech Translate Operators</a></li>
<li class="toctree-l4"><a class="reference internal" href="video.html">Google Cloud Video Intelligence Operators</a></li>
<li class="toctree-l4"><a class="reference internal" href="vision.html">Google Cloud Vision Operators</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="../papermill.html">Papermill</a></li>
<li class="toctree-l3"><a class="reference internal" href="../python.html">PythonOperator</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../connection/index.html">Managing Connections</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../secure-connections.html">Securing Connections</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../secure-connections.html#rotating-encryption-keys">Rotating encryption keys</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../write-logs.html">Writing Logs</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../executor/use-celery.html">Celery Executor</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../executor/use-dask.html">Dask Executor</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../executor/use-mesos.html">Scaling Out with Mesos (community contributed)</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../run-behind-proxy.html">Running Airflow behind a reverse proxy</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../run-with-systemd.html">Running Airflow with systemd</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../run-with-upstart.html">Running Airflow with upstart</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../use-test-config.html">Using the Test Mode Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../check-health.html">Checking Airflow Health Status</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../define_extra_link.html">Define an operator extra link</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../tracking-user-activity.html">Tracking User Activity</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../cli-completion.html">Set Up Bash/Zsh Completion</a></li>
</ul>
</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 Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../scheduler.html">Scheduling &amp; 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">REST API Reference</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="../../../metrics.html">Metrics</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../errors.html">Error Tracking</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../kubernetes.html">Kubernetes</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="../../../changelog.html">Changelog</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="../../../macros.html">Macros reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../_api/index.html">Python API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../privacy_notice.html">Privacy Notice</a></li>
</ul>
<p class="caption"><span class="caption-text">References</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../_api/index.html">Python API</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" 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> &raquo;</li>
<li><a href="../../index.html">How-to Guides</a> &raquo;</li>
<li><a href="../index.html">Using Operators</a> &raquo;</li>
<li><a href="index.html">Google Cloud Operators</a> &raquo;</li>
<li>Google Cloud Functions Operators</li>
<li class="wy-breadcrumbs-aside">
<a href="../../../_sources/howto/operator/gcp/function.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<blockquote>
<div></div></blockquote>
<div class="section" id="google-cloud-functions-operators">
<h1>Google Cloud Functions Operators<a class="headerlink" href="#google-cloud-functions-operators" title="Permalink to this headline"></a></h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#gcffunctiondeleteoperator" id="id5">GcfFunctionDeleteOperator</a></p></li>
<li><p><a class="reference internal" href="#gcffunctiondeployoperator" id="id6">GcfFunctionDeployOperator</a></p></li>
</ul>
</div>
<div class="section" id="gcffunctiondeleteoperator">
<span id="howto-operator-gcffunctiondeleteoperator"></span><h2><a class="toc-backref" href="#id5">GcfFunctionDeleteOperator</a><a class="headerlink" href="#gcffunctiondeleteoperator" title="Permalink to this headline"></a></h2>
<p>Use the operator to delete a function from Google Cloud Functions.</p>
<p>For parameter definition, take a look at
<a class="reference internal" href="../../../_api/airflow/contrib/operators/gcp_function_operator/index.html#airflow.contrib.operators.gcp_function_operator.GcfFunctionDeleteOperator" title="airflow.contrib.operators.gcp_function_operator.GcfFunctionDeleteOperator"><code class="xref py py-class docutils literal notranslate"><span class="pre">GcfFunctionDeleteOperator</span></code></a>.</p>
<div class="section" id="arguments">
<h3>Arguments<a class="headerlink" href="#arguments" title="Permalink to this headline"></a></h3>
<p>The following examples of OS environment variables show how you can build function name
to use in the operator:</p>
<div class="example-block-wrapper docutils container">
<p class="example-header example-header--with-button"><span class="example-title">airflow/contrib/example_dags/example_gcp_function.py</span><a class="example-header-button viewcode-button reference internal" href="../../../_modules/airflow/contrib/example_dags/example_gcp_function.html"><span>View Source</span></a></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">GCP_PROJECT_ID</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;GCP_PROJECT_ID&#39;</span><span class="p">,</span> <span class="s1">&#39;example-project&#39;</span><span class="p">)</span>
<span class="n">GCP_LOCATION</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;GCP_LOCATION&#39;</span><span class="p">,</span> <span class="s1">&#39;europe-west1&#39;</span><span class="p">)</span>
<span class="n">GCF_SHORT_FUNCTION_NAME</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;GCF_SHORT_FUNCTION_NAME&#39;</span><span class="p">,</span> <span class="s1">&#39;hello&#39;</span><span class="p">)</span><span class="o">.</span>\
<span class="n">replace</span><span class="p">(</span><span class="s2">&quot;-&quot;</span><span class="p">,</span> <span class="s2">&quot;_&quot;</span><span class="p">)</span> <span class="c1"># make sure there are no dashes in function name (!)</span>
<span class="n">FUNCTION_NAME</span> <span class="o">=</span> <span class="s1">&#39;projects/{}/locations/{}/functions/{}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">GCP_PROJECT_ID</span><span class="p">,</span>
<span class="n">GCP_LOCATION</span><span class="p">,</span>
<span class="n">GCF_SHORT_FUNCTION_NAME</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="using-the-operator">
<h3>Using the operator<a class="headerlink" href="#using-the-operator" title="Permalink to this headline"></a></h3>
<div class="example-block-wrapper docutils container">
<p class="example-header example-header--with-button"><span class="example-title">airflow/contrib/example_dags/example_gcp_function.py</span><a class="example-header-button viewcode-button reference internal" href="../../../_modules/airflow/contrib/example_dags/example_gcp_function.html"><span>View Source</span></a></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">delete_task</span> <span class="o">=</span> <span class="n">GcfFunctionDeleteOperator</span><span class="p">(</span>
<span class="n">task_id</span><span class="o">=</span><span class="s2">&quot;gcf_delete_task&quot;</span><span class="p">,</span>
<span class="n">name</span><span class="o">=</span><span class="n">FUNCTION_NAME</span>
<span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="templating">
<h3>Templating<a class="headerlink" href="#templating" title="Permalink to this headline"></a></h3>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">template_fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;gcp_conn_id&#39;</span><span class="p">,</span> <span class="s1">&#39;api_version&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="more-information">
<h3>More information<a class="headerlink" href="#more-information" title="Permalink to this headline"></a></h3>
<p>See Google Cloud Functions API documentation to <a class="reference external" href="https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions/delete">delete a function</a>.</p>
</div>
</div>
<div class="section" id="gcffunctiondeployoperator">
<span id="howto-operator-gcffunctiondeployoperator"></span><h2><a class="toc-backref" href="#id6">GcfFunctionDeployOperator</a><a class="headerlink" href="#gcffunctiondeployoperator" title="Permalink to this headline"></a></h2>
<p>Use the operator to deploy a function to Google Cloud Functions.
If a function with this name already exists, it will be updated.</p>
<p>For parameter definition, take a look at
<a class="reference internal" href="../../../_api/airflow/contrib/operators/gcp_function_operator/index.html#airflow.contrib.operators.gcp_function_operator.GcfFunctionDeployOperator" title="airflow.contrib.operators.gcp_function_operator.GcfFunctionDeployOperator"><code class="xref py py-class docutils literal notranslate"><span class="pre">GcfFunctionDeployOperator</span></code></a>.</p>
<div class="section" id="id1">
<h3>Arguments<a class="headerlink" href="#id1" title="Permalink to this headline"></a></h3>
<p>In the example DAG the following environment variables are used to parameterize the
operator’s definition:</p>
<div class="example-block-wrapper docutils container">
<p class="example-header example-header--with-button"><span class="example-title">airflow/contrib/example_dags/example_gcp_function.py</span><a class="example-header-button viewcode-button reference internal" href="../../../_modules/airflow/contrib/example_dags/example_gcp_function.html"><span>View Source</span></a></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">GCP_PROJECT_ID</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;GCP_PROJECT_ID&#39;</span><span class="p">,</span> <span class="s1">&#39;example-project&#39;</span><span class="p">)</span>
<span class="n">GCP_LOCATION</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;GCP_LOCATION&#39;</span><span class="p">,</span> <span class="s1">&#39;europe-west1&#39;</span><span class="p">)</span>
<span class="n">GCF_SHORT_FUNCTION_NAME</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;GCF_SHORT_FUNCTION_NAME&#39;</span><span class="p">,</span> <span class="s1">&#39;hello&#39;</span><span class="p">)</span><span class="o">.</span>\
<span class="n">replace</span><span class="p">(</span><span class="s2">&quot;-&quot;</span><span class="p">,</span> <span class="s2">&quot;_&quot;</span><span class="p">)</span> <span class="c1"># make sure there are no dashes in function name (!)</span>
<span class="n">FUNCTION_NAME</span> <span class="o">=</span> <span class="s1">&#39;projects/{}/locations/{}/functions/{}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">GCP_PROJECT_ID</span><span class="p">,</span>
<span class="n">GCP_LOCATION</span><span class="p">,</span>
<span class="n">GCF_SHORT_FUNCTION_NAME</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="example-block-wrapper docutils container">
<p class="example-header example-header--with-button"><span class="example-title">airflow/contrib/example_dags/example_gcp_function.py</span><a class="example-header-button viewcode-button reference internal" href="../../../_modules/airflow/contrib/example_dags/example_gcp_function.html"><span>View Source</span></a></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">GCF_SOURCE_ARCHIVE_URL</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;GCF_SOURCE_ARCHIVE_URL&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
<span class="n">GCF_SOURCE_UPLOAD_URL</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;GCF_SOURCE_UPLOAD_URL&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
<span class="n">GCF_SOURCE_REPOSITORY</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span>
<span class="s1">&#39;GCF_SOURCE_REPOSITORY&#39;</span><span class="p">,</span>
<span class="s1">&#39;https://source.developers.google.com/&#39;</span>
<span class="s1">&#39;projects/{}/repos/hello-world/moveable-aliases/master&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">GCP_PROJECT_ID</span><span class="p">))</span>
<span class="n">GCF_ZIP_PATH</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;GCF_ZIP_PATH&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
<span class="n">GCF_ENTRYPOINT</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;GCF_ENTRYPOINT&#39;</span><span class="p">,</span> <span class="s1">&#39;helloWorld&#39;</span><span class="p">)</span>
<span class="n">GCF_RUNTIME</span> <span class="o">=</span> <span class="s1">&#39;nodejs6&#39;</span>
<span class="n">GCP_VALIDATE_BODY</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;GCP_VALIDATE_BODY&#39;</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
<p>Some of those variables are used to create the request’s body:</p>
<div class="example-block-wrapper docutils container">
<p class="example-header example-header--with-button"><span class="example-title">airflow/contrib/example_dags/example_gcp_function.py</span><a class="example-header-button viewcode-button reference internal" href="../../../_modules/airflow/contrib/example_dags/example_gcp_function.html"><span>View Source</span></a></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">body</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="n">FUNCTION_NAME</span><span class="p">,</span>
<span class="s2">&quot;entryPoint&quot;</span><span class="p">:</span> <span class="n">GCF_ENTRYPOINT</span><span class="p">,</span>
<span class="s2">&quot;runtime&quot;</span><span class="p">:</span> <span class="n">GCF_RUNTIME</span><span class="p">,</span>
<span class="s2">&quot;httpsTrigger&quot;</span><span class="p">:</span> <span class="p">{}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<p>When a DAG is created, the default_args dictionary can be used to pass
arguments common with other tasks:</p>
<div class="example-block-wrapper docutils container">
<p class="example-header example-header--with-button"><span class="example-title">airflow/contrib/example_dags/example_gcp_function.py</span><a class="example-header-button viewcode-button reference internal" href="../../../_modules/airflow/contrib/example_dags/example_gcp_function.html"><span>View Source</span></a></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">default_args</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">&#39;start_date&#39;</span><span class="p">:</span> <span class="n">dates</span><span class="o">.</span><span class="n">days_ago</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<p>Note that the neither the body nor the default args are complete in the above examples.
Depending on the variables set, there might be different variants on how to pass source
code related fields. Currently, you can pass either <code class="docutils literal notranslate"><span class="pre">sourceArchiveUrl</span></code>,
<code class="docutils literal notranslate"><span class="pre">sourceRepository</span></code> or <code class="docutils literal notranslate"><span class="pre">sourceUploadUrl</span></code> as described in the
<a class="reference external" href="https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions#CloudFunction">Cloud Functions API specification</a>.</p>
<p>Additionally, <code class="docutils literal notranslate"><span class="pre">default_args</span></code> or direct operator args might contain <code class="docutils literal notranslate"><span class="pre">zip_path</span></code>
parameter
to run the extra step of uploading the source code before deploying it.
In this case, you also need to provide an empty <code class="docutils literal notranslate"><span class="pre">sourceUploadUrl</span></code>
parameter in the body.</p>
</div>
<div class="section" id="id2">
<h3>Using the operator<a class="headerlink" href="#id2" title="Permalink to this headline"></a></h3>
<p>Depending on the combination of parameters, the Function’s source code can be obtained
from different sources:</p>
<div class="example-block-wrapper docutils container">
<p class="example-header example-header--with-button"><span class="example-title">airflow/contrib/example_dags/example_gcp_function.py</span><a class="example-header-button viewcode-button reference internal" href="../../../_modules/airflow/contrib/example_dags/example_gcp_function.html"><span>View Source</span></a></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">GCF_SOURCE_ARCHIVE_URL</span><span class="p">:</span>
<span class="n">body</span><span class="p">[</span><span class="s1">&#39;sourceArchiveUrl&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">GCF_SOURCE_ARCHIVE_URL</span>
<span class="k">elif</span> <span class="n">GCF_SOURCE_REPOSITORY</span><span class="p">:</span>
<span class="n">body</span><span class="p">[</span><span class="s1">&#39;sourceRepository&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">&#39;url&#39;</span><span class="p">:</span> <span class="n">GCF_SOURCE_REPOSITORY</span>
<span class="p">}</span>
<span class="k">elif</span> <span class="n">GCF_ZIP_PATH</span><span class="p">:</span>
<span class="n">body</span><span class="p">[</span><span class="s1">&#39;sourceUploadUrl&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
<span class="n">default_args</span><span class="p">[</span><span class="s1">&#39;zip_path&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">GCF_ZIP_PATH</span>
<span class="k">elif</span> <span class="n">GCF_SOURCE_UPLOAD_URL</span><span class="p">:</span>
<span class="n">body</span><span class="p">[</span><span class="s1">&#39;sourceUploadUrl&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">GCF_SOURCE_UPLOAD_URL</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="s2">&quot;Please provide one of the source_code parameters&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<p>The code to create the operator:</p>
<div class="example-block-wrapper docutils container">
<p class="example-header example-header--with-button"><span class="example-title">airflow/contrib/example_dags/example_gcp_function.py</span><a class="example-header-button viewcode-button reference internal" href="../../../_modules/airflow/contrib/example_dags/example_gcp_function.html"><span>View Source</span></a></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">deploy_task</span> <span class="o">=</span> <span class="n">GcfFunctionDeployOperator</span><span class="p">(</span>
<span class="n">task_id</span><span class="o">=</span><span class="s2">&quot;gcf_deploy_task&quot;</span><span class="p">,</span>
<span class="n">project_id</span><span class="o">=</span><span class="n">GCP_PROJECT_ID</span><span class="p">,</span>
<span class="n">location</span><span class="o">=</span><span class="n">GCP_LOCATION</span><span class="p">,</span>
<span class="n">body</span><span class="o">=</span><span class="n">body</span><span class="p">,</span>
<span class="n">validate_body</span><span class="o">=</span><span class="n">GCP_VALIDATE_BODY</span>
<span class="p">)</span>
</pre></div>
</div>
</div>
<p>You can also create the operator without project id - project id will be retrieved
from the GCP connection used:</p>
<div class="example-block-wrapper docutils container">
<p class="example-header example-header--with-button"><span class="example-title">airflow/contrib/example_dags/example_gcp_function.py</span><a class="example-header-button viewcode-button reference internal" href="../../../_modules/airflow/contrib/example_dags/example_gcp_function.html"><span>View Source</span></a></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">deploy2_task</span> <span class="o">=</span> <span class="n">GcfFunctionDeployOperator</span><span class="p">(</span>
<span class="n">task_id</span><span class="o">=</span><span class="s2">&quot;gcf_deploy2_task&quot;</span><span class="p">,</span>
<span class="n">location</span><span class="o">=</span><span class="n">GCP_LOCATION</span><span class="p">,</span>
<span class="n">body</span><span class="o">=</span><span class="n">body</span><span class="p">,</span>
<span class="n">validate_body</span><span class="o">=</span><span class="n">GCP_VALIDATE_BODY</span>
<span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="id3">
<h3>Templating<a class="headerlink" href="#id3" title="Permalink to this headline"></a></h3>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">template_fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;project_id&#39;</span><span class="p">,</span> <span class="s1">&#39;location&#39;</span><span class="p">,</span> <span class="s1">&#39;gcp_conn_id&#39;</span><span class="p">,</span> <span class="s1">&#39;api_version&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="troubleshooting">
<h3>Troubleshooting<a class="headerlink" href="#troubleshooting" title="Permalink to this headline"></a></h3>
<p>If during the deploy you see an error similar to:</p>
<p><cite>“HttpError 403: Missing necessary permission iam.serviceAccounts.actAs for on resource
project-name&#64;appspot.gserviceaccount.com. Please grant the
roles/iam.serviceAccountUser role.”</cite></p>
<p>it means that your service account does not have the correct Cloud IAM permissions.</p>
<ol class="arabic simple">
<li><p>Assign your Service Account the Cloud Functions Developer role.</p></li>
<li><p>Grant the user the Cloud IAM Service Account User role on the Cloud Functions runtime
service account.</p></li>
</ol>
<p>The typical way of assigning Cloud IAM permissions with <code class="docutils literal notranslate"><span class="pre">gcloud</span></code> is
shown below. Just replace PROJECT_ID with ID of your Google Cloud Platform project
and SERVICE_ACCOUNT_EMAIL with the email ID of your service account.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>gcloud iam service-accounts add-iam-policy-binding <span class="se">\</span>
PROJECT_ID@appspot.gserviceaccount.com <span class="se">\</span>
--member<span class="o">=</span><span class="s2">&quot;serviceAccount:[SERVICE_ACCOUNT_EMAIL]&quot;</span> <span class="se">\</span>
--role<span class="o">=</span><span class="s2">&quot;roles/iam.serviceAccountUser&quot;</span>
</pre></div>
</div>
<p>You can also do that via the GCP Web console.</p>
<p>See <a class="reference external" href="https://cloud.google.com/functions/docs/reference/iam/roles#adding_the_iam_service_agent_user_role_to_the_runtime_service_account">Adding the IAM service agent user role to the runtime service</a> for details.</p>
<p>If the source code for your function is in Google Source Repository, make sure that
your service account has the Source Repository Viewer role so that the source code
can be downloaded if necessary.</p>
</div>
<div class="section" id="id4">
<h3>More information<a class="headerlink" href="#id4" title="Permalink to this headline"></a></h3>
<p>See Google Cloud API documentation <a class="reference external" href="https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions/create">to create a function</a>.</p>
</div>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="gcs.html" class="btn btn-neutral float-right" title="Google Cloud Storage Operators" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="compute.html" class="btn btn-neutral float-left" title="Google Compute Engine Operators" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
<div class="footer">This page uses <a href="https://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com. Check our
<a href="../../../privacy_notice.html">Privacy Policy</a>
for more details.
<script type="text/javascript">
(function() {
var ga = document.createElement('script');
ga.src = ('https:' == document.location.protocol ?
'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
ga.setAttribute('async', 'true');
var nodes = document.documentElement.childNodes;
var i = -1;
var node;
do {
i++;
node = nodes[i]
} while(node.nodeType !== Node.ELEMENT_NODE);
node.appendChild(ga);
})();
</script>
</div>
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>