Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion UPDATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ The following table shows changes in import paths.
|airflow.contrib.hooks.gcp_dataflow_hook.DataFlowHook |airflow.gcp.hooks.dataflow.DataFlowHook |
|airflow.contrib.hooks.gcp_dataproc_hook.DataProcHook |airflow.gcp.hooks.dataproc.DataProcHook |
|airflow.contrib.hooks.gcp_dlp_hook.CloudDLPHook |airflow.gcp.hooks.dlp.CloudDLPHook |
|airflow.contrib.hooks.gcp_function_hook.GcfHook |airflow.gcp.hooks.functions.GcfHook |
|airflow.contrib.hooks.gcp_function_hook.GcfHook |airflow.gcp.hooks.functions.CloudFunctionsHook |
|airflow.contrib.hooks.gcp_kms_hook.GoogleCloudKMSHook |airflow.gcp.hooks.kms.GoogleCloudKMSHook |
|airflow.contrib.hooks.gcp_mlengine_hook.MLEngineHook |airflow.gcp.hooks.mlengine.MLEngineHook |
|airflow.contrib.hooks.gcp_natural_language_hook.CloudNaturalLanguageHook |airflow.gcp.hooks.natural_language.CloudNaturalLanguageHook |
Expand Down
16 changes: 15 additions & 1 deletion airflow/contrib/hooks/gcp_function_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,23 @@
import warnings

# pylint: disable=unused-import
from airflow.gcp.hooks.functions import GcfHook # noqa
from airflow.gcp.hooks.functions import CloudFunctionsHook # noqa

warnings.warn(
"This module is deprecated. Please use `airflow.gcp.hooks.functions`.",
DeprecationWarning, stacklevel=2
)


class GcfHook(CloudFunctionsHook):
"""
This class is deprecated. Please use `airflow.gcp.hooks.functions.CloudFunctionsHook`.
"""

def __init__(self, *args, **kwargs):
warnings.warn(
"This class is deprecated. Please use `airflow.gcp.hooks.functions.CloudFunctionsHook`.",
DeprecationWarning, stacklevel=2
)

super().__init__(*args, **kwargs)
2 changes: 1 addition & 1 deletion airflow/gcp/hooks/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@


# noinspection PyAbstractClass
class GcfHook(GoogleCloudBaseHook):
class CloudFunctionsHook(GoogleCloudBaseHook):
"""
Hook for the Google Cloud Functions APIs.

Expand Down
8 changes: 4 additions & 4 deletions airflow/gcp/operators/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from googleapiclient.errors import HttpError

from airflow import AirflowException
from airflow.gcp.hooks.functions import GcfHook
from airflow.gcp.hooks.functions import CloudFunctionsHook
from airflow.gcp.utils.field_validator import GcpBodyFieldValidator, GcpFieldValidationException
from airflow.models import BaseOperator
from airflow.utils.decorators import apply_defaults
Expand Down Expand Up @@ -190,7 +190,7 @@ def _set_airflow_version_label(self):
{'airflow-version': 'v' + version.replace('.', '-').replace('+', '-')})

def execute(self, context):
hook = GcfHook(gcp_conn_id=self.gcp_conn_id, api_version=self.api_version)
hook = CloudFunctionsHook(gcp_conn_id=self.gcp_conn_id, api_version=self.api_version)
if self.zip_path_preprocessor.should_upload_function():
self.body[GCF_SOURCE_UPLOAD_URL] = self._upload_source_code(hook)
self._validate_all_body_fields()
Expand Down Expand Up @@ -334,7 +334,7 @@ def _validate_inputs(self):
'Parameter name must match pattern: {}'.format(FUNCTION_NAME_PATTERN))

def execute(self, context):
hook = GcfHook(gcp_conn_id=self.gcp_conn_id, api_version=self.api_version)
hook = CloudFunctionsHook(gcp_conn_id=self.gcp_conn_id, api_version=self.api_version)
try:
return hook.delete_function(self.name)
except HttpError as e:
Expand Down Expand Up @@ -389,7 +389,7 @@ def __init__(
self.api_version = api_version

def execute(self, context: Dict):
hook = GcfHook(api_version=self.api_version, gcp_conn_id=self.gcp_conn_id)
hook = CloudFunctionsHook(api_version=self.api_version, gcp_conn_id=self.gcp_conn_id)
self.log.info('Calling function %s.', self.function_id)
result = hook.call_function(
function_id=self.function_id,
Expand Down
48 changes: 24 additions & 24 deletions tests/gcp/hooks/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import unittest

from airflow import AirflowException
from airflow.gcp.hooks.functions import GcfHook
from airflow.gcp.hooks.functions import CloudFunctionsHook
from tests.compat import PropertyMock, mock
from tests.gcp.utils.base_gcp_mock import (
GCP_PROJECT_ID_HOOK_UNIT_TEST, get_open_mock, mock_base_gcp_hook_default_project_id,
Expand All @@ -36,9 +36,9 @@ class TestFunctionHookNoDefaultProjectId(unittest.TestCase):
def setUp(self):
with mock.patch('airflow.gcp.hooks.base.GoogleCloudBaseHook.__init__',
new=mock_base_gcp_hook_no_default_project_id):
self.gcf_function_hook_no_project_id = GcfHook(gcp_conn_id='test', api_version='v1')
self.gcf_function_hook_no_project_id = CloudFunctionsHook(gcp_conn_id='test', api_version='v1')

@mock.patch("airflow.gcp.hooks.functions.GcfHook._authorize")
@mock.patch("airflow.gcp.hooks.functions.CloudFunctionsHook._authorize")
@mock.patch("airflow.gcp.hooks.functions.build")
def test_gcf_client_creation(self, mock_build, mock_authorize):
result = self.gcf_function_hook_no_project_id.get_conn()
Expand All @@ -53,8 +53,8 @@ def test_gcf_client_creation(self, mock_build, mock_authorize):
new_callable=PropertyMock,
return_value=None
)
@mock.patch('airflow.gcp.hooks.functions.GcfHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.GcfHook._wait_for_operation_to_complete')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook._wait_for_operation_to_complete')
def test_create_new_function_missing_project_id(
self, wait_for_operation_to_complete, get_conn, mock_project_id
):
Expand All @@ -74,8 +74,8 @@ def test_create_new_function_missing_project_id(
self.assertIn("The project id must be passed", str(err))
wait_for_operation_to_complete.assert_not_called()

@mock.patch('airflow.gcp.hooks.functions.GcfHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.GcfHook._wait_for_operation_to_complete')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook._wait_for_operation_to_complete')
def test_create_new_function_overridden_project_id(self, wait_for_operation_to_complete, get_conn):
create_method = get_conn.return_value.projects.return_value.locations. \
return_value.functions.return_value.create
Expand All @@ -99,7 +99,7 @@ def test_create_new_function_overridden_project_id(self, wait_for_operation_to_c
return_value=None
)
@mock.patch('requests.put')
@mock.patch('airflow.gcp.hooks.functions.GcfHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook.get_conn')
def test_upload_function_zip_missing_project_id(
self, get_conn, requests_put, mock_project_id
):
Expand All @@ -122,7 +122,7 @@ def test_upload_function_zip_missing_project_id(
self.assertIn("The project id must be passed", str(err))

@mock.patch('requests.put')
@mock.patch('airflow.gcp.hooks.functions.GcfHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook.get_conn')
def test_upload_function_zip_overridden_project_id(self, get_conn, requests_put):
mck, open_module = get_open_mock()
with mock.patch('{}.open'.format(open_module), mck):
Expand Down Expand Up @@ -152,9 +152,9 @@ class TestFunctionHookDefaultProjectId(unittest.TestCase):
def setUp(self):
with mock.patch('airflow.gcp.hooks.base.GoogleCloudBaseHook.__init__',
new=mock_base_gcp_hook_default_project_id):
self.gcf_function_hook = GcfHook(gcp_conn_id='test', api_version='v1')
self.gcf_function_hook = CloudFunctionsHook(gcp_conn_id='test', api_version='v1')

@mock.patch("airflow.gcp.hooks.functions.GcfHook._authorize")
@mock.patch("airflow.gcp.hooks.functions.CloudFunctionsHook._authorize")
@mock.patch("airflow.gcp.hooks.functions.build")
def test_gcf_client_creation(self, mock_build, mock_authorize):
result = self.gcf_function_hook.get_conn()
Expand All @@ -169,8 +169,8 @@ def test_gcf_client_creation(self, mock_build, mock_authorize):
new_callable=PropertyMock,
return_value=GCP_PROJECT_ID_HOOK_UNIT_TEST
)
@mock.patch('airflow.gcp.hooks.functions.GcfHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.GcfHook._wait_for_operation_to_complete')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook._wait_for_operation_to_complete')
def test_create_new_function(self, wait_for_operation_to_complete, get_conn, mock_project_id):
create_method = get_conn.return_value.projects.return_value.locations.\
return_value.functions.return_value.create
Expand All @@ -187,8 +187,8 @@ def test_create_new_function(self, wait_for_operation_to_complete, get_conn, moc
execute_method.assert_called_once_with(num_retries=5)
wait_for_operation_to_complete.assert_called_once_with(operation_name='operation_id')

@mock.patch('airflow.gcp.hooks.functions.GcfHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.GcfHook._wait_for_operation_to_complete')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook._wait_for_operation_to_complete')
def test_create_new_function_override_project_id(self, wait_for_operation_to_complete, get_conn):
create_method = get_conn.return_value.projects.return_value.locations. \
return_value.functions.return_value.create
Expand All @@ -206,7 +206,7 @@ def test_create_new_function_override_project_id(self, wait_for_operation_to_com
execute_method.assert_called_once_with(num_retries=5)
wait_for_operation_to_complete.assert_called_once_with(operation_name='operation_id')

@mock.patch('airflow.gcp.hooks.functions.GcfHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook.get_conn')
def test_get_function(self, get_conn):
get_method = get_conn.return_value.projects.return_value.locations. \
return_value.functions.return_value.get
Expand All @@ -220,8 +220,8 @@ def test_get_function(self, get_conn):
get_method.assert_called_once_with(name='function')
execute_method.assert_called_once_with(num_retries=5)

@mock.patch('airflow.gcp.hooks.functions.GcfHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.GcfHook._wait_for_operation_to_complete')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook._wait_for_operation_to_complete')
def test_delete_function(self, wait_for_operation_to_complete, get_conn):
delete_method = get_conn.return_value.projects.return_value.locations. \
return_value.functions.return_value.delete
Expand All @@ -235,8 +235,8 @@ def test_delete_function(self, wait_for_operation_to_complete, get_conn):
delete_method.assert_called_once_with(name='function')
execute_method.assert_called_once_with(num_retries=5)

@mock.patch('airflow.gcp.hooks.functions.GcfHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.GcfHook._wait_for_operation_to_complete')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook._wait_for_operation_to_complete')
def test_update_function(self, wait_for_operation_to_complete, get_conn):
patch_method = get_conn.return_value.projects.return_value.locations. \
return_value.functions.return_value.patch
Expand All @@ -263,7 +263,7 @@ def test_update_function(self, wait_for_operation_to_complete, get_conn):
return_value=GCP_PROJECT_ID_HOOK_UNIT_TEST
)
@mock.patch('requests.put')
@mock.patch('airflow.gcp.hooks.functions.GcfHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook.get_conn')
def test_upload_function_zip(self, get_conn, requests_put, mock_project_id):
mck, open_module = get_open_mock()
with mock.patch('{}.open'.format(open_module), mck):
Expand All @@ -288,7 +288,7 @@ def test_upload_function_zip(self, get_conn, requests_put, mock_project_id):
)

@mock.patch('requests.put')
@mock.patch('airflow.gcp.hooks.functions.GcfHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook.get_conn')
def test_upload_function_zip_overridden_project_id(self, get_conn, requests_put):
mck, open_module = get_open_mock()
with mock.patch('{}.open'.format(open_module), mck):
Expand All @@ -313,7 +313,7 @@ def test_upload_function_zip_overridden_project_id(self, get_conn, requests_put)
url='http://uploadHere'
)

@mock.patch('airflow.gcp.hooks.functions.GcfHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook.get_conn')
def test_call_function(self, mock_get_conn):
payload = {'executionId': 'wh41ppcyoa6l', 'result': 'Hello World!'}
call = mock_get_conn.return_value.projects.return_value.\
Expand All @@ -338,7 +338,7 @@ def test_call_function(self, mock_get_conn):
call.assert_called_once_with(body=input_data, name=name)
self.assertDictEqual(result, payload)

@mock.patch('airflow.gcp.hooks.functions.GcfHook.get_conn')
@mock.patch('airflow.gcp.hooks.functions.CloudFunctionsHook.get_conn')
def test_call_function_error(self, mock_get_conn):
payload = {'error': 'Something very bad'}
call = mock_get_conn.return_value.projects.return_value. \
Expand Down
Loading