1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 Helpers for validating and coercing service template inputs.
22 from ruamel import yaml
24 from .env import logger
25 from .exceptions import AriaCliError
28 def inputs_to_dict(resources):
30 Returns a dictionary of inputs
32 :param resources: can be:
36 * directory containing multiple input files
37 * ``key1=value1;key2=value2`` pairs string.
38 * string formatted as JSON/YAML
39 * wildcard based string (e.g. ``*-inputs.yaml``)
46 for resource in resources:
47 logger.debug('Processing inputs source: {0}'.format(resource))
48 # Workflow parameters always pass an empty dictionary. We ignore it
49 if isinstance(resource, basestring):
51 parsed_dict.update(_parse_single_input(resource))
54 "Invalid input: {0}. It must represent a dictionary. "
55 "Valid values can be one of:{1} "
56 "- A path to a YAML file{1} "
57 "- A path to a directory containing YAML files{1} "
58 "- A single quoted wildcard based path "
59 "(e.g. '*-inputs.yaml'){1} "
60 "- A string formatted as JSON/YAML{1} "
61 "- A string formatted as key1=value1;key2=value2".format(
62 resource, os.linesep))
66 def _parse_single_input(resource):
68 # parse resource as string representation of a dictionary
69 return _plain_string_to_dict(resource)
71 input_files = glob.glob(resource)
73 if os.path.isdir(resource):
74 for input_file in os.listdir(resource):
76 _parse_yaml_path(os.path.join(resource, input_file)))
78 for input_file in input_files:
79 parsed_dict.update(_parse_yaml_path(input_file))
81 parsed_dict.update(_parse_yaml_path(resource))
85 def _parse_yaml_path(resource):
88 # if resource is a path - parse as a yaml file
89 if os.path.isfile(resource):
90 with open(resource) as f:
91 content = yaml.load(f.read())
93 # parse resource content as yaml
94 content = yaml.load(resource)
95 except yaml.error.YAMLError as e:
96 raise AriaCliError("'{0}' is not a valid YAML. {1}".format(
99 # Emtpy files return None
100 content = content or dict()
101 if not isinstance(content, dict):
107 def _plain_string_to_dict(input_string):
108 input_string = input_string.strip()
110 mapped_inputs = input_string.split(';')
111 for mapped_input in mapped_inputs:
112 mapped_input = mapped_input.strip()
115 split_mapping = mapped_input.split('=')
117 key = split_mapping[0].strip()
118 value = split_mapping[1].strip()
121 "Invalid input format: {0}, the expected format is: "
122 "key1=value1;key2=value2".format(input_string))
123 input_dict[key] = value