|
| 1 | +# Copyright 2016 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Shared helper functions for RuntimeConfig API classes.""" |
| 16 | + |
| 17 | + |
| 18 | +def config_name_from_full_name(full_name): |
| 19 | + """Extract the config name from a full resource name. |
| 20 | +
|
| 21 | + >>> config_name_from_full_name('projects/my-proj/configs/my-config') |
| 22 | + "my-config" |
| 23 | +
|
| 24 | + :type full_name: string |
| 25 | + :param full_name: the full resource name of a config. The full resource |
| 26 | + name looks like |
| 27 | + ``projects/project-name/configs/config-name`` and is |
| 28 | + returned as the ``name`` field of a config resource. |
| 29 | + See: |
| 30 | + https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs |
| 31 | +
|
| 32 | + :rtype: string |
| 33 | + :returns: the config's short name, given its full resource name |
| 34 | + """ |
| 35 | + _, _, _, result = full_name.split('/') |
| 36 | + return result |
| 37 | + |
| 38 | + |
| 39 | +def variable_name_from_full_name(full_name): |
| 40 | + """Extract the variable name from a full resource name. |
| 41 | +
|
| 42 | + >>> variable_name_from_full_name( |
| 43 | + 'projects/my-proj/configs/my-config/variables/var-name') |
| 44 | + "var-name" |
| 45 | + >>> variable_name_from_full_name( |
| 46 | + 'projects/my-proj/configs/my-config/variables/another/var/name') |
| 47 | + "another/var/name" |
| 48 | +
|
| 49 | + :type full_name: string |
| 50 | + :param full_name: the full resource name of a variable. The full resource |
| 51 | + name looks like |
| 52 | + ``projects/prj-name/configs/cfg-name/variables/var-name`` |
| 53 | + and is returned as the ``name`` field of a variable |
| 54 | + resource. See: |
| 55 | + https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.variables |
| 56 | +
|
| 57 | + :rtype: string |
| 58 | + :returns: the variable's short name, given its full resource name |
| 59 | + """ |
| 60 | + parts = full_name.split('/') |
| 61 | + assert parts[0] == 'projects' |
| 62 | + assert parts[2] == 'configs' |
| 63 | + assert parts[4] == 'variables' |
| 64 | + |
| 65 | + # Variables are hierarchical. Each node in the hierarchy is separated by a |
| 66 | + # / character. |
| 67 | + return '/'.join(parts[5:]) |
0 commit comments