1# -*- coding: utf-8 -*- #
2# Copyright 2014 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"""Simplify fully-qualified paths for compute."""
16
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import unicode_literals
21
22
23def Name(uri):
24  """Get just the name of the object the uri refers to."""
25
26  # Since the path is assumed valid, we can just take the last piece.
27  return uri.split('/')[-1]
28
29
30def ScopedSuffix(uri):
31  """Get just the scoped part of the object the uri refers to."""
32
33  # The path is assumed valid.
34  if '/zones/' in uri:
35    # This is zonally scoped. Return the part after zone/.
36    return uri.split('/zones/')[-1]
37  elif '/regions/' in uri:
38    # This is regionally scoped. Return the part after regions/.
39    return uri.split('/regions/')[-1]
40  else:
41    # This is globally scoped. Return the name.
42    return Name(uri)
43
44
45def ProjectSuffix(uri):
46  """Get the entire relative path of the object the uri refers to."""
47
48  # Get the part after projects. The argument is assumed valid.
49  return uri.split('/projects/')[-1]
50