1# -*- coding: utf-8 -*- #
2# Copyright 2020 Google LLC. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#    http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""Preferred method of generating a copy task."""
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import unicode_literals
21
22from googlecloudsdk.command_lib.storage import storage_url
23from googlecloudsdk.command_lib.storage.tasks.cp import daisy_chain_copy_task
24from googlecloudsdk.command_lib.storage.tasks.cp import file_download_task
25from googlecloudsdk.command_lib.storage.tasks.cp import file_upload_task
26from googlecloudsdk.command_lib.storage.tasks.cp import intra_cloud_copy_task
27
28
29def get_copy_task(source_resource, destination_resource):
30  """Factory method that returns the correct copy task for the arguments.
31
32  Args:
33    source_resource (resource_reference.Resource): Reference to file to copy.
34    destination_resource (resource_reference.Resource): Reference to
35        destination to copy file to.
36
37  Returns:
38    Task object that can be executed to perform a copy.
39
40  Raises:
41    NotImplementedError: Cross-cloud copy.
42    ValueError: Local filesystem copy.
43  """
44  source_url = source_resource.storage_url
45  destination_url = destination_resource.storage_url
46
47  if (isinstance(source_url, storage_url.FileUrl)
48      and isinstance(destination_url, storage_url.FileUrl)):
49    raise ValueError('Local copies not supported. Gcloud command-line tool is'
50                     ' meant for cloud operations.')
51
52  if (isinstance(source_url, storage_url.CloudUrl)
53      and isinstance(destination_url, storage_url.FileUrl)):
54    return file_download_task.FileDownloadTask(source_resource,
55                                               destination_resource)
56
57  if (isinstance(source_url, storage_url.FileUrl)
58      and isinstance(destination_url, storage_url.CloudUrl)):
59    return file_upload_task.FileUploadTask(source_resource,
60                                           destination_resource)
61
62  if (isinstance(source_url, storage_url.CloudUrl)
63      and isinstance(destination_url, storage_url.CloudUrl)):
64    if source_url.scheme != destination_url.scheme:
65      return daisy_chain_copy_task.DaisyChainCopyTask(source_resource,
66                                                      destination_resource)
67    return intra_cloud_copy_task.IntraCloudCopyTask(source_resource,
68                                                    destination_resource)
69