|
| 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: str |
| 25 | + :param full_name: |
| 26 | + The full resource name of a config. The full resource name looks like |
| 27 | + ``projects/project-name/configs/config-name`` and is returned as the |
| 28 | + ``name`` field of a config resource. See: |
| 29 | + https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs |
| 30 | +
|
| 31 | + :rtype: str |
| 32 | + :returns: The config's short name, given its full resource name. |
| 33 | + :raises: :class:`ValueError` if ``full_name`` is not the expected format |
| 34 | + """ |
| 35 | + projects, _, configs, result = full_name.split('/') |
| 36 | + if projects != 'projects' or configs != 'configs': |
| 37 | + raise ValueError( |
| 38 | + 'Unexpected format of resource', full_name, |
| 39 | + 'Expected "projects/{proj}/configs/{cfg}"') |
| 40 | + return result |
| 41 | + |
| 42 | + |
| 43 | +def variable_name_from_full_name(full_name): |
| 44 | + """Extract the variable name from a full resource name. |
| 45 | +
|
| 46 | + >>> variable_name_from_full_name( |
| 47 | + 'projects/my-proj/configs/my-config/variables/var-name') |
| 48 | + "var-name" |
| 49 | + >>> variable_name_from_full_name( |
| 50 | + 'projects/my-proj/configs/my-config/variables/another/var/name') |
| 51 | + "another/var/name" |
| 52 | +
|
| 53 | + :type full_name: str |
| 54 | + :param full_name: |
| 55 | + The full resource name of a variable. The full resource name looks like |
| 56 | + ``projects/prj-name/configs/cfg-name/variables/var-name`` and is |
| 57 | + returned as the ``name`` field of a variable resource. See: |
| 58 | + https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.variables |
| 59 | +
|
| 60 | + :rtype: str |
| 61 | + :returns: The variable's short name, given its full resource name. |
| 62 | + :raises: :class:`ValueError` if ``full_name`` is not the expected format |
| 63 | + """ |
| 64 | + projects, _, configs, _, variables, result = full_name.split('/', 5) |
| 65 | + if (projects != 'projects' or configs != 'configs' or |
| 66 | + variables != 'variables'): |
| 67 | + raise ValueError( |
| 68 | + 'Unexpected format of resource', full_name, |
| 69 | + 'Expected "projects/{proj}/configs/{cfg}/variables/..."') |
| 70 | + return result |
0 commit comments