Skip to content

Commit f5e3b03

Browse files
potiukashb
authored andcommitted
[AIRFLOW-3232] More readable GCF operator documentation (#4067)
1 parent d176fa5 commit f5e3b03

File tree

4 files changed

+69
-62
lines changed

4 files changed

+69
-62
lines changed

airflow/contrib/hooks/gcp_function_hook.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
# noinspection PyAbstractClass
3636
class GcfHook(GoogleCloudBaseHook):
3737
"""
38-
Hook for Google Cloud Functions APIs.
38+
Hook for the Google Cloud Functions APIs.
3939
"""
4040
_conn = None
4141

@@ -48,7 +48,7 @@ def __init__(self,
4848

4949
def get_conn(self):
5050
"""
51-
Retrieves connection to cloud functions.
51+
Retrieves the connection to Cloud Functions.
5252
5353
:return: Google Cloud Function services object
5454
:rtype: dict
@@ -61,7 +61,7 @@ def get_conn(self):
6161

6262
def get_function(self, name):
6363
"""
64-
Returns the function with a given name.
64+
Returns the Cloud Function with the given name.
6565
6666
:param name: name of the function
6767
:type name: str
@@ -73,9 +73,9 @@ def get_function(self, name):
7373

7474
def list_functions(self, full_location):
7575
"""
76-
Lists all functions created in the location.
76+
Lists all Cloud Functions created in the location.
7777
78-
:param full_location: full location including project. On the form
78+
:param full_location: full location including the project in the form of
7979
of /projects/<PROJECT>/location/<LOCATION>
8080
:type full_location: str
8181
:return: array of CloudFunction objects - representing functions in the location
@@ -87,12 +87,12 @@ def list_functions(self, full_location):
8787

8888
def create_new_function(self, full_location, body):
8989
"""
90-
Creates new cloud function in location given with body specified.
90+
Creates a new function in Cloud Function in the location specified in the body.
9191
92-
:param full_location: full location including project. On the form
92+
:param full_location: full location including the project in the form of
9393
of /projects/<PROJECT>/location/<LOCATION>
9494
:type full_location: str
95-
:param body: body required by the cloud function insert API
95+
:param body: body required by the Cloud Functions insert API
9696
:type body: dict
9797
:return: response returned by the operation
9898
:rtype: dict
@@ -106,7 +106,7 @@ def create_new_function(self, full_location, body):
106106

107107
def update_function(self, name, body, update_mask):
108108
"""
109-
Updates cloud function according to the update mask specified.
109+
Updates Cloud Functions according to the specified update mask.
110110
111111
:param name: name of the function
112112
:type name: str
@@ -129,10 +129,10 @@ def upload_function_zip(self, parent, zip_path):
129129
"""
130130
Uploads zip file with sources.
131131
132-
:param parent: project and location in which signed upload URL should be generated
133-
in the form of /projects/<PROJECT>/location/<LOCATION>
132+
:param parent: Google Cloud Platform project id and region where zip file should
133+
be uploaded in the form of /projects/<PROJECT>/location/<LOCATION>
134134
:type parent: str
135-
:param zip_path: path of the file to upload (should point to valid .zip file)
135+
:param zip_path: path of the valid .zip file to upload
136136
:type zip_path: str
137137
:return: Upload URL that was returned by generateUploadUrl method
138138
"""
@@ -156,7 +156,7 @@ def upload_function_zip(self, parent, zip_path):
156156

157157
def delete_function(self, name):
158158
"""
159-
Deletes cloud function specified by name.
159+
Deletes the specified Cloud Function.
160160
161161
:param name: name of the function
162162
:type name: str

airflow/contrib/operators/gcp_function_operator.py

+18-17
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,29 @@ def _validate_max_instances(value):
8080

8181
class GcfFunctionDeployOperator(BaseOperator):
8282
"""
83-
Create a function in Google Cloud Functions.
83+
Creates a function in Google Cloud Functions.
8484
85-
:param project_id: Project ID that the operator works on
85+
:param project_id: Google Cloud Platform Project ID where the function should
86+
be created.
8687
:type project_id: str
87-
:param location: Region where the operator operates on
88+
:param location: Google Cloud Platform region where the function should be created.
8889
:type location: str
89-
:param body: Body of the cloud function definition. The body must be a CloudFunction
90-
dictionary as described in:
90+
:param body: Body of the Cloud Functions definition. The body must be a
91+
Cloud Functions dictionary as described in:
9192
https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions
92-
(note that different API versions require different
93-
variants of the CloudFunction dictionary)
93+
. Different API versions require different variants of the Cloud Functions
94+
dictionary.
9495
:type body: dict or google.cloud.functions.v1.CloudFunction
95-
:param gcp_conn_id: The connection ID to use connecting to Google Cloud Platform.
96+
:param gcp_conn_id: The connection ID to use to connect to Google Cloud Platform.
9697
:type gcp_conn_id: str
97-
:param api_version: Version of the API used (for example v1).
98+
:param api_version: API version used (for example v1 or v1beta1).
9899
:type api_version: str
99-
:param zip_path: Path to zip file containing source code of the function. If it is
100-
set, then sourceUploadUrl should not be specified in the body (or it should
101-
be empty), then the zip file will be uploaded using upload URL generated
102-
via generateUploadUrl from cloud functions API
100+
:param zip_path: Path to zip file containing source code of the function. If the path
101+
is set, the sourceUploadUrl should not be specified in the body or it should
102+
be empty. Then the zip file will be uploaded using the upload URL generated
103+
via generateUploadUrl from the Cloud Functions API.
103104
:type zip_path: str
104-
:param validate_body: If set to False, no body validation is performed.
105+
:param validate_body: If set to False, body validation is not performed.
105106
:type validate_body: bool
106107
"""
107108

@@ -265,14 +266,14 @@ def preprocess_body(self):
265266

266267
class GcfFunctionDeleteOperator(BaseOperator):
267268
"""
268-
Delete a function with specified name from Google Cloud Functions.
269+
Deletes the specified function from Google Cloud Functions.
269270
270271
:param name: A fully-qualified function name, matching
271272
the pattern: `^projects/[^/]+/locations/[^/]+/functions/[^/]+$`
272273
:type name: str
273-
:param gcp_conn_id: The connection ID to use connecting to Google Cloud Platform.
274+
:param gcp_conn_id: The connection ID to use to connect to Google Cloud Platform.
274275
:type gcp_conn_id: str
275-
:param api_version: Version of the API used (for example v1).
276+
:param api_version: API version used (for example v1 or v1beta1).
276277
:type api_version: str
277278
"""
278279

docs/howto/operator.rst

+36-30
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,18 @@ to delete a function from Google Cloud Functions.
179179

180180
Troubleshooting
181181
"""""""""""""""
182+
If you want to run or deploy an operator using a service account and get “forbidden 403”
183+
errors, it means that your service account does not have the correct
184+
Cloud IAM permissions.
182185

183-
In case you want to run deploy operator using a service account and get "forbidden 403"
184-
errors, it means that your service account has not enough permissions set via IAM.
186+
1. Assign your Service Account the Cloud Functions Developer role.
187+
2. Grant the user the Cloud IAM Service Account User role on the Cloud Functions runtime
188+
service account.
189+
190+
The typical way of assigning Cloud IAM permissions with `gcloud` is
191+
shown below. Just replace PROJECT_ID with ID of your Google Cloud Platform project
192+
and SERVICE_ACCOUNT_EMAIL with the email ID of your service account.
185193

186-
* First you need to Assign your Service Account "Cloud Functions Developer" role
187-
* Make sure you grant the user the IAM Service Account User role on the Cloud Functions
188-
Runtime service account. Typical way of doing it with gcloud is shown below - just
189-
replace PROJECT_ID with ID of your project and SERVICE_ACCOUNT_EMAIL with the email id
190-
of your service account.
191194

192195
.. code-block:: bash
193196
@@ -205,40 +208,40 @@ GcfFunctionDeployOperator
205208
Use the :class:`~airflow.contrib.operators.gcp_function_operator.GcfFunctionDeployOperator`
206209
to deploy a function from Google Cloud Functions.
207210

208-
The examples below use Airflow variables defined in order to show various variants and
209-
combinations of default_args you can use. The variables are defined as follows:
211+
The following examples of Airflow variables show various variants and combinations
212+
of default_args that you can use. The variables are defined as follows:
210213

211214
.. literalinclude:: ../../airflow/contrib/example_dags/example_gcp_function_deploy_delete.py
212215
:language: python
213216
:start-after: [START howto_operator_gcf_deploy_variables]
214217
:end-before: [END howto_operator_gcf_deploy_variables]
215218

216-
With those variables one can define body of the request:
219+
With those variables you can define the body of the request:
217220

218221
.. literalinclude:: ../../airflow/contrib/example_dags/example_gcp_function_deploy_delete.py
219222
:language: python
220223
:start-after: [START howto_operator_gcf_deploy_body]
221224
:end-before: [END howto_operator_gcf_deploy_body]
222225

223-
The default_args dictionary when you create DAG can be used to pass body and other
224-
arguments:
226+
When you create a DAG, the default_args dictionary can be used to pass the body and
227+
other arguments:
225228

226229
.. literalinclude:: ../../airflow/contrib/example_dags/example_gcp_function_deploy_delete.py
227230
:language: python
228231
:start-after: [START howto_operator_gcf_deploy_args]
229232
:end-before: [END howto_operator_gcf_deploy_args]
230233

231-
Note that the neither the body nor default args are complete in the above examples.
232-
Depending on the variables set there might be different variants on how to pass
233-
source code related fields. Currently you can pass either
234-
`sourceArchiveUrl`, `sourceRepository` or `sourceUploadUrl` as described in
234+
Note that the neither the body nor the default args are complete in the above examples.
235+
Depending on the set variables, there might be different variants on how to pass source
236+
code related fields. Currently, you can pass either sourceArchiveUrl, sourceRepository
237+
or sourceUploadUrl as described in the
235238
`CloudFunction API specification <https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions#CloudFunction>`_.
236-
Additionally default_args might contain `zip_path` parameter to run extra step
237-
of uploading the source code before deploying it. In the last case you also need to
239+
Additionally, default_args might contain zip_path parameter to run the extra step of
240+
uploading the source code before deploying it. In the last case, you also need to
238241
provide an empty `sourceUploadUrl` parameter in the body.
239242

240-
Example logic of setting the source code related fields based on variables defined above
241-
is shown here:
243+
Based on the variables defined above, example logic of setting the source code
244+
related fields is shown here:
242245

243246
.. literalinclude:: ../../airflow/contrib/example_dags/example_gcp_function_deploy_delete.py
244247
:language: python
@@ -255,14 +258,17 @@ The code to create the operator:
255258
Troubleshooting
256259
"""""""""""""""
257260

258-
In case you want to run deploy operator using a service account and get "forbidden 403"
259-
errors, it means that your service account has not enough permissions set via IAM.
261+
If you want to run or deploy an operator using a service account and get “forbidden 403”
262+
errors, it means that your service account does not have the correct
263+
Cloud IAM permissions.
264+
265+
1. Assign your Service Account the Cloud Functions Developer role.
266+
2. Grant the user the Cloud IAM Service Account User role on the Cloud Functions runtime
267+
service account.
260268

261-
* First you need to Assign your Service Account "Cloud Functions Developer" role
262-
* Make sure you grant the user the IAM Service Account User role on the Cloud Functions
263-
Runtime service account. Typical way of doing it with gcloud is shown below - just
264-
replace PROJECT_ID with ID of your project and SERVICE_ACCOUNT_EMAIL with the email id
265-
of your service account.
269+
The typical way of assigning Cloud IAM permissions with `gcloud` is
270+
shown below. Just replace PROJECT_ID with ID of your Google Cloud Platform project
271+
and SERVICE_ACCOUNT_EMAIL with the email ID of your service account.
266272

267273
.. code-block:: bash
268274
@@ -274,6 +280,6 @@ of your service account.
274280
275281
See `Adding the IAM service agent user role to the runtime service <https://cloud.google.com/functions/docs/reference/iam/roles#adding_the_iam_service_agent_user_role_to_the_runtime_service_account>`_ for details
276282

277-
Also make sure that your service account has access to the source code of function
278-
in case it should be downloaded. It might mean that you add Source Repository Viewer
279-
role to the service account in case the source code is in Google Source Repository.
283+
If the source code for your function is in Google Source Repository, make sure that
284+
your service account has the Source Repository Viewer role so that the source code
285+
can be downloaded if necessary.

docs/integration.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,8 @@ Cloud Functions
539539
Cloud Functions Operators
540540
"""""""""""""""""""""""""
541541

542-
- :ref:`GcfFunctionDeployOperator` : deploy Google Cloud Function to the cloud.
543-
- :ref:`GcfFunctionDeleteOperator` : delete Google Cloud Function in the cloud.
542+
- :ref:`GcfFunctionDeployOperator` : deploy Google Cloud Function to Google Cloud Platform
543+
- :ref:`GcfFunctionDeleteOperator` : delete Google Cloud Function in Google Cloud Platform
544544

545545
.. autoclass:: airflow.contrib.operators.gcp_operator.GCP
546546

0 commit comments

Comments
 (0)