Debian Codegraph @

heatclient/common/environment_format.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import yaml
14
15 from heatclient._i18n import _
16 from heatclient.common import template_format
17
18
19 SECTIONS = (
20 PARAMETER_DEFAULTS, PARAMETERS, RESOURCE_REGISTRY,
21 ENCRYPTED_PARAM_NAMES, EVENT_SINKS,
22 PARAMETER_MERGE_STRATEGIES
23 ) = (
24 'parameter_defaults', 'parameters', 'resource_registry',
25 'encrypted_param_names', 'event_sinks',
26 'parameter_merge_strategies'
27 )
28
29
30 def parse(env_str):
31 """Takes a string and returns a dict containing the parsed structure.
32
33 This includes determination of whether the string is using the
34 YAML format.
35 """
36 try:
37 env = yaml.load(env_str, Loader=template_format.yaml_loader)
38 except yaml.YAMLError:
39 # NOTE(prazumovsky): we need to return more informative error for
40 # user, so use SafeLoader, which return error message with template
41 # snippet where error has been occurred.
42 try:
43 env = yaml.load(env_str, Loader=yaml.SafeLoader)
44 except yaml.YAMLError as yea:
45 raise ValueError(yea)
46 else:
47 if env is None:
48 env = {}
49 elif not isinstance(env, dict):
50 raise ValueError(_('The environment is not a valid '
51 'YAML mapping data type.'))
52
53 for param in env:
54 if param not in SECTIONS:
55 raise ValueError(_('environment has wrong section "%s"') % param)
56
57 return env
58
59
60 def default_for_missing(env):
61 """Checks a parsed environment for missing sections."""
62 for param in SECTIONS:
63 if param not in env and param != PARAMETER_MERGE_STRATEGIES:
64 if param in (ENCRYPTED_PARAM_NAMES, EVENT_SINKS):
65 env[param] = []
66 else:
67 env[param] = {}