1# -*- coding: utf-8 -*-
2# Copyright 2013 Google Inc. 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"""API map classes used with the CloudApiDelegator class."""
16
17from __future__ import absolute_import
18
19from gslib.boto_translation import BotoTranslation
20from gslib.gcs_json_api import GcsJsonApi
21
22
23class ApiSelector(object):
24  """Enum class for API."""
25  XML = 'XML'
26  JSON = 'JSON'
27
28
29class ApiMapConstants(object):
30  """Enum class for API map entries."""
31  API_MAP = 'apiclass'
32  SUPPORT_MAP = 'supported'
33  DEFAULT_MAP = 'default'
34
35
36class GsutilApiClassMapFactory(object):
37  """Factory for generating gsutil API class maps.
38
39  A valid class map is defined as:
40    {
41      (key) Provider prefix used in URI strings.
42      (value) {
43        (key) ApiSelector describing the API format.
44        (value) CloudApi child class that implements this API.
45      }
46    }
47  """
48
49  @classmethod
50  def GetClassMap(cls):
51    """Returns the default gsutil class map."""
52    gs_class_map = {
53        ApiSelector.XML: BotoTranslation,
54        ApiSelector.JSON: GcsJsonApi
55    }
56    s3_class_map = {
57        ApiSelector.XML: BotoTranslation
58    }
59    class_map = {
60        'gs': gs_class_map,
61        's3': s3_class_map
62    }
63    return class_map
64
65
66class GsutilApiMapFactory(object):
67  """Factory the generates the default gsutil API map.
68
69    The API map determines which Cloud API implementation is used for a given
70    command.  A valid API map is defined as:
71    {
72      (key) ApiMapConstants.API_MAP : (value) Gsutil API class map (as
73          described in GsutilApiClassMapFactory comments).
74      (key) ApiMapConstants.SUPPORT_MAP : (value) {
75        (key) Provider prefix used in URI strings.
76        (value) list of ApiSelectors supported by the command for this provider.
77      }
78      (key) ApiMapConstants.DEFAULT_MAP : (value) {
79        (key) Provider prefix used in URI strings.
80        (value) Default ApiSelector for this command and provider.
81      }
82    }
83  """
84
85  @classmethod
86  def GetApiMap(cls, gsutil_api_class_map_factory, support_map, default_map):
87    """Creates a GsutilApiMap for use by the command from the inputs.
88
89    Args:
90      gsutil_api_class_map_factory: Factory defining a GetClassMap() function
91                                    adhering to GsutilApiClassMapFactory
92                                    semantics.
93      support_map: Entries for ApiMapConstants.SUPPORT_MAP as described above.
94      default_map: Entries for ApiMapConstants.DEFAULT_MAP as described above.
95
96    Returns:
97      GsutilApiMap generated from the inputs.
98    """
99    return {
100        ApiMapConstants.API_MAP: gsutil_api_class_map_factory.GetClassMap(),
101        ApiMapConstants.SUPPORT_MAP: support_map,
102        ApiMapConstants.DEFAULT_MAP: default_map
103    }
104