def AddToApisMap(api_name, api_version, default=None, base_pkg='googlecloudsdk.third_party.apis'): """Adds the APIDef specified by the given arguments to the APIs map. This method should only be used for runtime patcing of the APIs map. Additions to the map should ensure that there is only one and only one default version for each API. Args: api_name: str, The API name (or the command surface name, if different). api_version: str, The version of the API. default: bool, Whether this API version is the default. If set to None will be set to True if this is first version of api, otherwise false. base_pkg: str, Base package from which generated API files are accessed. """ # pylint:disable=protected-access api_name, _ = apis_internal._GetApiNameAndAlias(api_name) api_def = ConstructApiDef(api_name, api_version, default, base_pkg) api_versions = apis_map.MAP.get(api_name, {}) if default is None: api_def.default_version = not api_versions api_versions[api_version] = api_def apis_map.MAP[api_name] = api_versions
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))
def ConstructApiDef(api_name, api_version, is_default, base_pkg='googlecloudsdk.third_party.apis'): """Creates and returns the APIDef specified by the given arguments. Args: api_name: str, The API name (or the command surface name, if different). api_version: str, The version of the API. is_default: bool, Whether this API version is the default. base_pkg: str, Base package from which generated API files are accessed. Returns: APIDef, The APIDef created using the given args. """ # pylint:disable=protected-access api_name, _ = apis_internal._GetApiNameAndAlias(api_name) client_cls_name = _CamelCase(api_name) + _CamelCase(api_version) class_path = '{base}.{api_name}.{api_version}'.format( base=base_pkg, api_name=api_name, api_version=api_version,) common_fmt = '{api_name}_{api_version}_' client_cls_path_fmt = common_fmt + 'client.{api_client_class}' client_cls_path = client_cls_path_fmt.format(api_name=api_name, api_version=api_version, api_client_class=client_cls_name) messages_mod_path_fmt = common_fmt + 'messages' messages_mod_path = messages_mod_path_fmt.format(api_name=api_name, api_version=api_version) return apis_map.APIDef(class_path, client_cls_path, messages_mod_path, is_default)
def _ValidateAndGetDefaultVersion(api_name, api_version): """Validates the API exists and gets the default version if not given.""" # pylint:disable=protected-access api_name, _ = apis_internal._GetApiNameAndAlias(api_name) api_vers = apis_map.MAP.get(api_name, {}) if not api_vers: # No versions, this API is not registered. raise UnknownAPIError(api_name) if api_version: if api_version not in api_vers: raise UnknownAPIVersionError(api_name, api_version) return api_version for version, api_def in api_vers.iteritems(): if api_def.default_version: return version raise NoDefaultVersionError(api_name)
def _ValidateAndGetDefaultVersion(api_name, api_version): """Validates the API exists and gets the default version if not given.""" # pylint:disable=protected-access api_name, _ = apis_internal._GetApiNameAndAlias(api_name) api_vers = apis_map.MAP.get(api_name, {}) if not api_vers: # No versions, this API is not registered. raise UnknownAPIError(api_name) if api_version: if api_version not in api_vers: raise UnknownAPIVersionError(api_name, api_version) return api_version for version, api_def in six.iteritems(api_vers): if api_def.default_version: return version raise NoDefaultVersionError(api_name)
def _AddToApisMap(api_name, api_version, api_def): """Adds the APIDef specified by the given arguments to the APIs map. This method should only be used for runtime patching of the APIs map. Additions to the map should ensure that there is only one and only one default version for each API. Args: api_name: str, The API name (or the command surface name, if different). api_version: str, The version of the API. api_def: APIDef for the API version. """ # pylint:disable=protected-access api_name, _ = apis_internal._GetApiNameAndAlias(api_name) # Register API version as default if this API did not exist, # otherwise we'll set the first APIs map api_versions = apis_map.MAP.get(api_name, {}) api_def.default_version = not api_versions api_versions[api_version] = api_def apis_map.MAP[api_name] = api_versions
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))