Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GCP Financial Services API #43446

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
286e38c
add financial services hook
claw89 Oct 21, 2024
8673f7f
add create instance airflow operator
claw89 Oct 21, 2024
e2a0196
add delete and get instance operators
claw89 Oct 21, 2024
44cd7a6
add sensor for long running operations
claw89 Oct 22, 2024
3477b25
allow templated fields in financial services operators
claw89 Oct 22, 2024
3f5abef
add example DAG for financial services
claw89 Oct 22, 2024
ed45f03
add hook unit tests for financial services
claw89 Oct 23, 2024
2994681
add operator unit tests for financial services
claw89 Oct 23, 2024
3ea9891
add sensor unit tests for financial services
claw89 Oct 23, 2024
8eb9a8a
update docstrings by removing args related to googleapi discovery
claw89 Oct 23, 2024
18215a6
add system test for financial services
claw89 Oct 24, 2024
fb6adda
add docs for FinancialServicesCreateInstanceOperator
claw89 Oct 24, 2024
bdf7140
add remaining instance operator docs
claw89 Oct 24, 2024
5027afe
add operation sensor docs
claw89 Oct 24, 2024
6b9fe2e
add discovery doc arg to financial services operators
claw89 Oct 24, 2024
a874878
remove example DAG
claw89 Oct 24, 2024
9b62db0
fix docstrings and refactor
claw89 Oct 25, 2024
9905ecf
Merge branch 'apache:main' into financial-services
claw89 Oct 28, 2024
223eaf2
Merge branch 'apache:main' into financial-services
claw89 Oct 30, 2024
188de90
fix nullable KMS_KEY arg
claw89 Oct 30, 2024
e8f5339
fix documentation
claw89 Oct 30, 2024
02ce717
Merge branch 'apache:main' into financial-services
claw89 Nov 1, 2024
89fa2e7
remove delegate_to deprecation check
claw89 Oct 30, 2024
48ee457
divide resource names into parts in args
claw89 Oct 30, 2024
628b1a5
divide resource names in operators
claw89 Oct 30, 2024
1395315
use google api_core Operation type
claw89 Oct 31, 2024
892db75
replace operation sensor with wait_for_operation method in hook
claw89 Nov 1, 2024
1058c09
update test args
claw89 Nov 1, 2024
fb67fd9
add developer key option for connecting to financial serivces
claw89 Nov 1, 2024
e43f6e5
update unit tests for new connection options
claw89 Nov 1, 2024
cc783fe
update documentation
claw89 Nov 1, 2024
984fa74
Merge branch 'apache:main' into financial-services
claw89 Nov 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
.. Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

.. http://www.apache.org/licenses/LICENSE-2.0

.. Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.



Google Cloud Financial Services Operators
=========================================

Prerequisite Tasks
^^^^^^^^^^^^^^^^^^

.. include:: /operators/_partials/prerequisite_tasks.rst

Manage instances
^^^^^^^^^^^^^^^^

.. _howto/operator:FinancialServicesCreateInstanceOperator:

Create an AML AI instance
-------------------------

Use the :class:`~airflow.providers.google.cloud.operators.financial_services.FinancialServicesCreateInstanceOperator`
operator to create an AML AI instance in the Google Cloud Financial Services API.

Using the operator
""""""""""""""""""

.. exampleinclude:: /../../providers/tests/system/google/cloud/financial_services/example_financial_services.py
:language: python
:dedent: 4
:start-after: [START howto_operator_financial_services_create_instance]
:end-before: [END howto_operator_financial_services_create_instance]

Templating
""""""""""

.. literalinclude:: /../../providers/src/airflow/providers/google/cloud/operators/financial_services.py
:language: python
:dedent: 4
:start-after: [START howto_operator_financial_services_create_instance_template_fields]
:end-before: [END howto_operator_financial_services_create_instance_template_fields]

More information
""""""""""""""""

See Google Cloud Financial Services API documentation to `create an instance
<https://cloud.google.com/financial-services/anti-money-laundering/docs/reference/rest/v1/projects.locations.instances/create>`_.

.. _howto/operator:FinancialServicesCreateInstanceOperator:


.. _howto/operator:FinancialServicesGetInstanceOperator:

Get an AML AI instance
-------------------------

Use the :class:`~airflow.providers.google.cloud.operators.financial_services.FinancialServicesGetInstanceOperator`
operator to get an AML AI instance in the Google Cloud Financial Services API.

Using the operator
""""""""""""""""""

.. exampleinclude:: /../../providers/tests/system/google/cloud/financial_services/example_financial_services.py
:language: python
:dedent: 4
:start-after: [START howto_operator_financial_services_get_instance]
:end-before: [END howto_operator_financial_services_get_instance]

Templating
""""""""""

.. literalinclude:: /../../providers/src/airflow/providers/google/cloud/operators/financial_services.py
:language: python
:dedent: 4
:start-after: [START howto_operator_financial_services_get_instance_template_fields]
:end-before: [END howto_operator_financial_services_get_instance_template_fields]

More information
""""""""""""""""

See Google Cloud Financial Services API documentation to `get an instance
<https://cloud.google.com/financial-services/anti-money-laundering/docs/reference/rest/v1/projects.locations.instances/get>`_.

.. _howto/operator:FinancialServicesGetInstanceOperator:


.. _howto/operator:FinancialServicesDeleteInstanceOperator:

Delete an AML AI instance
-------------------------

Use the :class:`~airflow.providers.google.cloud.operators.financial_services.FinancialServicesDeleteInstanceOperator`
operator to delete an AML AI instance in the Google Cloud Financial Services API.

Using the operator
""""""""""""""""""

.. exampleinclude:: /../../providers/tests/system/google/cloud/financial_services/example_financial_services.py
:language: python
:dedent: 4
:start-after: [START howto_operator_financial_services_delete_instance]
:end-before: [END howto_operator_financial_services_delete_instance]

Templating
""""""""""

.. literalinclude:: /../../providers/src/airflow/providers/google/cloud/operators/financial_services.py
:language: python
:dedent: 4
:start-after: [START howto_operator_financial_services_delete_instance_template_fields]
:end-before: [END howto_operator_financial_services_delete_instance_template_fields]

More information
""""""""""""""""

See Google Cloud Financial Services API documentation to `delete an instance
<https://cloud.google.com/financial-services/anti-money-laundering/docs/reference/rest/v1/projects.locations.instances/delete>`_.

.. _howto/operator:FinancialServicesDeleteInstanceOperator:


Sensors
^^^^^^^

.. _howto/sensor:FinancialServicesOperationSensor:

Check operation status
----------------------

Use the :class:`~airflow.providers.google.cloud.sensors.financial_services.FinancialServicesOperationSensor`
sensor to check the status of an operation in the Google Cloud Financial Services API.

Using the sensor
""""""""""""""""""

.. exampleinclude:: /../../providers/tests/system/google/cloud/financial_services/example_financial_services.py
:language: python
:dedent: 4
:start-after: [START howto_sensor_financial_services_operation]
:end-before: [END howto_sensor_financial_services_operation]

Templating
""""""""""

.. literalinclude:: /../../providers/src/airflow/providers/google/cloud/operators/financial_services.py
:language: python
:dedent: 4
:start-after: [START howto_sensor_financial_services_operation_template_fields]
:end-before: [END howto_sensor_financial_services_operation_template_fields]

More information
""""""""""""""""

See Google Cloud Financial Services API documentation to `get an operation
<https://cloud.google.com/financial-services/anti-money-laundering/docs/reference/rest/v1/projects.locations.operations/get>`_.

.. _howto/sensor:FinancialServicesOperationSensor:
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations

from googleapiclient.discovery import Resource, build_from_document

from airflow.providers.google.common.hooks.base_google import GoogleBaseHook


class FinancialServicesHook(GoogleBaseHook):
"""
Hook for interacting with the Google Financial Services API.

:param discovery_doc: Discovery document for building the Financial Services API
as described `here <https://cloud.google.com/financial-services/anti-money-laundering/docs/reference/rest#discovery-document>`__
:param gcp_conn_id: Identifier of connection to Google Cloud Platform.
Defaults to "google_cloud_default".
"""

connection: Resource | None = None

def __init__(
self,
discovery_doc: dict,
gcp_conn_id: str = "google_cloud_default",
**kwargs,
) -> None:
if kwargs.get("delegate_to") is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is not needed, because you created a new Hook. And this if-clause was added for deprecate delegate_to parameter in old hooks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment. I've removed this part from the code.

raise RuntimeError(
"The `delegate_to` parameter has been deprecated before and finally"
" removed in this version"
" of Google Provider. You MUST convert it to `impersonate_chain`"
)
super().__init__(
gcp_conn_id=gcp_conn_id,
impersonation_chain=None,
)
self.discovery_doc = discovery_doc

def get_conn(self) -> Resource:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we try to download a discovery document for an authorized user? Firstly authorize the user, then download a discovery doc and after that build a connection. Similar to this code for firebase

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The credentials retrieved using GoogleBaseHook don't authenticate the Financial Services; for this service a developer key should be provided due to the API is not currently being publicly General Available. For some use cases, we expect that providing the discovery document may be more feasible than creating and sharing an API key in GCP. However, I agree that having the option to use the developer key could also be useful.

I have made updates to enable users to supply either a developer API key OR a discovery document as a local .json file by setting Airflow variables. An exception will be raised if neither of these variables are set.

"""
Establish a connection to the Google Financial Services API.

:return: A Google Cloud Financial Services API service resource.
"""
if not self.connection:
self.connection = build_from_document(self.discovery_doc)

return self.connection

def get_instance(self, instance_resource_uri: str) -> dict:
"""
Get a Financial Services Anti-Money Laundering AI instance.

:param instance_resource_uri: URI of the instance to get (format:
'projects/<Project ID>/locations/<Location>/instances/<Instance ID>)

:returns: A dictionary containing the instance metadata
"""
conn = self.get_conn()
response = conn.projects().locations().instances().get(name=instance_resource_uri).execute()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all our methods in hooks we divide name for parameters: project_id, location_id and instance_id and then unite these parameters in the name inside the method. For example https://github.com/apache/airflow/blob/main/providers/src/airflow/providers/google/cloud/hooks/dataproc_metastore.py#L117

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment. I have made the updates to hook and operator code accordingly.

return response

def create_instance(self, instance_id: str, kms_key_uri: str, location_resource_uri: str) -> dict:
"""
Create a Financial Services Anti-Money Laundering AI instance.

:param instance_id: Identifier for the instance to create
:param kms_key_uri: URI of the KMS key to that will be used for instance encryption
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as mentioned above for location_resource_uri and kms_key_uri. These URIs should be divided on parameters.

(format: 'projects/<Project ID>/locations/<Location>/keyRings/<Key Ring>/
cryptoKeys/<Key>')

:returns: A dictionary containing metadata for the create instance operation
"""
conn = self.get_conn()
response = (
conn.projects()
.locations()
.instances()
.create(
parent=location_resource_uri,
instanceId=instance_id,
body={"kmsKey": kms_key_uri},
)
.execute()
)
return response

def delete_instance(self, instance_resource_uri: str) -> dict:
"""
Delete a Financial Services Anti-Money Laundering AI instance.

:param instance_resource_uri: URI of the instance to delete (format:
'projects/<Project ID>/locations/<Location>/instances/<Instance ID>)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same instance_resource_uri should be divided on parameters.


:returns: A dictionary containing metadata for the delete instance
operation
"""
conn = self.get_conn()
response = conn.projects().locations().instances().delete(name=instance_resource_uri).execute()
return response

def get_operation(self, operation_resource_uri: str) -> dict:
"""
Get a Financial Services Anti-Money Laundering AI operation.

:param operation_resource_uri: URI of the operation to get (format:
'projects/<Project ID>/locations/<Location>/operations/<Operation ID>)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same operation_resource_uri should be divided on parameters.


:return: A dictionary containing metadata for the operation
"""
conn = self.get_conn()
response = conn.projects().locations().operations().get(name=operation_resource_uri).execute()
return response
Loading
Loading