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

[AIRFLOW-4255] Make GCS Hook Backwards compatible #5089

Merged
merged 5 commits into from
Apr 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 24 additions & 4 deletions airflow/contrib/hooks/gcs_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import gzip as gz
import os
import shutil
import warnings

from google.cloud import storage

Expand Down Expand Up @@ -173,7 +174,8 @@ def download(self, bucket, object, filename=None):

# pylint:disable=redefined-builtin
def upload(self, bucket, object, filename,
mime_type='application/octet-stream', gzip=False):
mime_type='application/octet-stream', gzip=False,
multipart=False, num_retries=0):
"""
Uploads a local file to Google Cloud Storage.

Expand All @@ -187,8 +189,15 @@ def upload(self, bucket, object, filename,
:type mime_type: str
:param gzip: Option to compress file for upload
:type gzip: bool
:param multipart: Deprecated parameter. Multipart would be handled automatically
:type multipart: bool or int
:param num_retries: Deprecated parameter. Retries would be handled automatically
:type num_retries: int
"""

warnings.warn("'multipart' and 'num_retries' parameters have been deprecated."
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest passing multipart=None and then an if multipart is not None -- otherwise there is no way for the user to "fix" this warning.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done. Updated

" They are handled automatically by the Storage client", DeprecationWarning)

if gzip:
filename_gz = filename + '.gz'

Expand Down Expand Up @@ -256,15 +265,20 @@ def is_updated_after(self, bucket, object, ts):

return False

def delete(self, bucket, object):
def delete(self, bucket, object, generation=None):
"""
Deletes an object from the bucket.

:param bucket: name of the bucket, where the object resides
:type bucket: str
:param object: name of the object to delete
:type object: str
:param generation: Deprecated parameter
:type generation: str
"""

warnings.warn("'generation' parameter is no longer supported", DeprecationWarning)

client = self.get_conn()
bucket = client.get_bucket(bucket_name=bucket)
blob = bucket.blob(blob_name=object)
Expand Down Expand Up @@ -477,7 +491,8 @@ def insert_bucket_acl(self, bucket, entity, role, user_project=None):

self.log.info('A new ACL entry created in bucket: %s', bucket)

def insert_object_acl(self, bucket, object_name, entity, role, user_project=None):
def insert_object_acl(self, bucket, object_name, entity, role, generation=None,
user_project=None):
"""
Creates a new ACL entry on the specified object.
See: https://cloud.google.com/storage/docs/json_api/v1/objectAccessControls/insert
Expand All @@ -496,10 +511,13 @@ def insert_object_acl(self, bucket, object_name, entity, role, user_project=None
:param role: The access permission for the entity.
Acceptable values are: "OWNER", "READER".
:type role: str
:param generation: (Deprecated) Parameter is no longer supported.
:type generation: str
:param user_project: (Optional) The project to be billed for this request.
Required for Requester Pays buckets.
:type user_project: str
"""
warnings.warn("'generation' parameter is no longer supported", DeprecationWarning)
self.log.info('Creating a new ACL entry for object: %s in bucket: %s',
object_name, bucket)
client = self.get_conn()
Expand All @@ -515,7 +533,7 @@ def insert_object_acl(self, bucket, object_name, entity, role, user_project=None
self.log.info('A new ACL entry created for object: %s in bucket: %s',
object_name, bucket)

def compose(self, bucket, source_objects, destination_object):
def compose(self, bucket, source_objects, destination_object, num_retries=5):
"""
Composes a list of existing object into a new object in the same storage bucket

Expand All @@ -533,6 +551,8 @@ def compose(self, bucket, source_objects, destination_object):
:param destination_object: The path of the object if given.
:type destination_object: str
"""
warnings.warn("'num_retries' parameter is Deprecated. Retries are "
"now handled automatically", DeprecationWarning)

if not source_objects or not len(source_objects):
raise ValueError('source_objects cannot be empty.')
Expand Down
29 changes: 19 additions & 10 deletions tests/contrib/hooks/test_gcs_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
# specific language governing permissions and limitations
# under the License.

import unittest
import six

import tempfile
import os

Expand All @@ -27,6 +28,11 @@
from tests.contrib.utils.base_gcp_mock import mock_base_gcp_hook_default_project_id
from google.cloud import storage
from google.cloud import exceptions
if six.PY2:
# Need `assertWarns` back-ported from unittest2
import unittest2 as unittest
else:
import unittest

BASE_STRING = 'airflow.contrib.hooks.gcp_api_base_hook.{}'
GCS_STRING = 'airflow.contrib.hooks.gcs_hook.{}'
Expand Down Expand Up @@ -235,7 +241,8 @@ def test_delete(self, mock_service, mock_bucket):
delete_method = get_blob_method.return_value.delete
delete_method.return_value = blob_to_be_deleted

response = self.gcs_hook.delete(bucket=test_bucket, object=test_object)
with self.assertWarns(DeprecationWarning):
response = self.gcs_hook.delete(bucket=test_bucket, object=test_object)
self.assertIsNone(response)

@mock.patch(GCS_STRING.format('GoogleCloudStorageHook.get_conn'))
Expand Down Expand Up @@ -386,11 +393,12 @@ def test_compose_without_destination_object(self, mock_service):
test_destination_object = None

with self.assertRaises(ValueError) as e:
self.gcs_hook.compose(
bucket=test_bucket,
source_objects=test_source_objects,
destination_object=test_destination_object
)
with self.assertWarns(DeprecationWarning):
self.gcs_hook.compose(
bucket=test_bucket,
source_objects=test_source_objects,
destination_object=test_destination_object
)

self.assertEqual(
str(e.exception),
Expand Down Expand Up @@ -422,9 +430,10 @@ def test_upload(self, mock_service):
.blob.return_value.upload_from_filename
upload_method.return_value = None

response = self.gcs_hook.upload(test_bucket,
test_object,
self.testfile.name)
with self.assertWarns(DeprecationWarning):
response = self.gcs_hook.upload(test_bucket,
test_object,
self.testfile.name)

self.assertIsNone(response)
upload_method.assert_called_once_with(
Expand Down