Exemplo n.º 1
0
def ResolveVersion(api_name, api_version=None):
  """Resolves the version for an API based on the APIs map and API overrides.

  Args:
    api_name: str, The API name (or the command surface name, if different).
    api_version: str, The API version.

  Raises:
    apis_internal.UnknownAPIError: If api_name does not exist in the APIs map.

  Returns:
    str, The resolved version.
  """
  # pylint:disable=protected-access
  api_name, api_name_alias = apis_internal._GetApiNameAndAlias(api_name)
  if api_name not in apis_map.MAP:
    raise apis_util.UnknownAPIError(api_name)

  version_overrides = properties.VALUES.api_client_overrides.AllValues()

  # First try to get api specific override, then try full surface override.
  api_version_override = None
  if api_version:
    api_version_override = version_overrides.get(
        '{}/{}'.format(api_name_alias, api_version), None)
  if not api_version_override:
    api_version_override = version_overrides.get(api_name_alias, api_version)

  return (api_version_override or
          # pylint:disable=protected-access
          apis_internal._GetDefaultVersion(api_name))
Exemplo n.º 2
0
  def RegisterApiByName(self, api_name, api_version=None):
    """Register the given API if it has not been registered already.

    Args:
      api_name: str, The API name.
      api_version: if available, the version of the API being registered.
    Returns:
      api version which was registered.
    """
    registered_versions = self.registered_apis.get(api_name, [])
    if api_version in registered_versions:
      # This API version has been registered.
      registered_versions.remove(api_version)
      registered_versions.append(api_version)
      return api_version
    if api_version is None:
      if registered_versions:
        # Use last registered api version as default.
        return registered_versions[-1]
      # pylint:disable=protected-access
      api_version = apis_internal._GetDefaultVersion(api_name)

    # pylint:disable=protected-access
    for collection in apis_internal._GetApiCollections(api_name, api_version):
      self._RegisterCollection(collection)

    self.registered_apis[api_name].append(api_version)
    return api_version
Exemplo n.º 3
0
  def RegisterApiByName(self, api_name, api_version=None):
    """Register the given API if it has not been registered already.

    Args:
      api_name: str, The API name.
      api_version: str, The API version, None for the default version.
    Returns:
      api version which was registered.
    """
    registered_versions = self.registered_apis.get(api_name, [])
    if api_version in registered_versions:
      # This API version has been registered.
      registered_versions.remove(api_version)
      registered_versions.append(api_version)
      return api_version
    if api_version is None:
      if registered_versions:
        # Use last registered api version as default.
        return registered_versions[-1]
      # pylint:disable=protected-access
      api_version = apis_internal._GetDefaultVersion(api_name)

    # pylint:disable=protected-access
    for collection in apis_internal._GetApiCollections(api_name, api_version):
      self._RegisterCollection(collection)

    self.registered_apis[api_name].append(api_version)
    return api_version
Exemplo n.º 4
0
def _GetDefaultVersion(api_name):
  """Gets the default version for the given api."""
  # pylint:disable=protected-access
  api_version = apis_internal._GetDefaultVersion(api_name)
  if not api_version:
    raise NoDefaultVersionError(api_name)
  log.warning('Using default version [{}] for api [{}].'
              .format(api_version, api_name))
  return api_version
Exemplo n.º 5
0
    def testGetDefaultVersion(self):
        api_name = 'dns'
        default_api_def = apis_internal._GetApiDef(api_name, 'v1')

        default_version = apis_internal._GetDefaultVersion(api_name)
        also_default_api_def = apis_internal._GetApiDef(
            api_name, default_version)

        self.assertEqual(default_api_def, also_default_api_def)
Exemplo n.º 6
0
def SetDefaultVersion(api_name, api_version):
    """Resets default version for given api."""
    # pylint:disable=protected-access
    api_def = apis_internal._GetApiDef(api_name, api_version)
    # pylint:disable=protected-access
    default_version = apis_internal._GetDefaultVersion(api_name)
    # pylint:disable=protected-access
    default_api_def = apis_internal._GetApiDef(api_name, default_version)
    default_api_def.default_version = False
    api_def.default_version = True
Exemplo n.º 7
0
def SetDefaultVersion(api_name, api_version):
  """Resets default version for given api."""
  # pylint:disable=protected-access
  api_def = apis_internal._GetApiDef(api_name, api_version)
  # pylint:disable=protected-access
  default_version = apis_internal._GetDefaultVersion(api_name)
  # pylint:disable=protected-access
  default_api_def = apis_internal._GetApiDef(api_name, default_version)
  default_api_def.default_version = False
  api_def.default_version = True
Exemplo n.º 8
0
    def testAddToApisMap(self):
        with self.assertRaisesRegex(
                apis_util.UnknownAPIError,
                r'API named \[hello\] does not exist in the APIs map'):
            apis_internal._GetApiDef('hello', 'v1')

        expected_api_def = apis.ConstructApiDef('hello', 'v1', True)
        apis.AddToApisMap('hello', 'v1', True)
        self.assertEqual('v1', apis_internal._GetDefaultVersion('hello'))
        self.assertEqual(expected_api_def,
                         apis_internal._GetApiDef('hello', 'v1'))
Exemplo n.º 9
0
def ResolveVersion(api_name, default_override=None):
    """Resolves the version for an API based on the APIs map and API overrides.

  Args:
    api_name: str, The API name (or the command surface name, if different).
    default_override: str, The override for the default version.

  Raises:
    apis_internal.UnknownAPIError: If api_name does not exist in the APIs map.

  Returns:
    str, The resolved version.
  """
    # pylint:disable=protected-access
    api_name, api_name_alias = apis_internal._GetApiNameAndAlias(api_name)
    if api_name not in apis_map.MAP:
        raise apis_util.UnknownAPIError(api_name)

    version_overrides = properties.VALUES.api_client_overrides.AllValues()
    version_override = version_overrides.get(api_name_alias, None)
    return (version_override or default_override or
            # pylint:disable=protected-access
            apis_internal._GetDefaultVersion(api_name))
Exemplo n.º 10
0
def ResolveVersion(api_name, default_override=None):
  """Resolves the version for an API based on the APIs map and API overrides.

  Args:
    api_name: str, The API name (or the command surface name, if different).
    default_override: str, The override for the default version.

  Raises:
    apis_internal.UnknownAPIError: If api_name does not exist in the APIs map.

  Returns:
    str, The resolved version.
  """
  # pylint:disable=protected-access
  api_name, api_name_alias = apis_internal._GetApiNameAndAlias(api_name)
  if api_name not in apis_map.MAP:
    raise apis_util.UnknownAPIError(api_name)

  version_overrides = properties.VALUES.api_client_overrides.AllValues()
  version_override = version_overrides.get(api_name_alias, None)
  return (version_override or default_override or
          # pylint:disable=protected-access
          apis_internal._GetDefaultVersion(api_name))
Exemplo n.º 11
0
def LoadApis():
  """Populate the global module namespace with API clients."""
  for api_name in apis_map.MAP:
    # pylint:disable=protected-access
    globals()[api_name] = apis.GetClientInstance(
        api_name, apis_internal._GetDefaultVersion(api_name))
Exemplo n.º 12
0
def LoadApis():
  """Populate the global module namespace with API clients."""
  for api_name in apis_map.MAP:
    # pylint:disable=protected-access
    globals()[api_name] = apis.GetClientInstance(
        api_name, apis_internal._GetDefaultVersion(api_name))
Exemplo n.º 13
0
 def testSetDefaultVersion(self):
     self.assertEqual('v1', apis_internal._GetDefaultVersion('dns'))
     apis.SetDefaultVersion('dns', 'v1beta1')
     self.assertEqual('v1beta1', apis_internal._GetDefaultVersion('dns'))
Exemplo n.º 14
0
 def testGetDefaultVersionUnknownAPI(self):
     self.assertEqual(None, apis_internal._GetDefaultVersion('something'))