| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 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 |
|
| 40 |
|
| 41 |
|
| 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] = {} |