vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / cli / inputs.py
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
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15
16 """
17 Helpers for validating and coercing service template inputs.
18 """
19
20 import os
21 import glob
22 from ruamel import yaml
23
24 from .env import logger
25 from .exceptions import AriaCliError
26
27
28 def inputs_to_dict(resources):
29     """
30     Returns a dictionary of inputs
31
32     :param resources: can be:
33
34      * list of files
35      * single file
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``)
40     """
41     if not resources:
42         return dict()
43
44     parsed_dict = {}
45
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):
50             try:
51                 parsed_dict.update(_parse_single_input(resource))
52             except AriaCliError:
53                 raise AriaCliError(
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))
63     return parsed_dict
64
65
66 def _parse_single_input(resource):
67     try:
68         # parse resource as string representation of a dictionary
69         return _plain_string_to_dict(resource)
70     except AriaCliError:
71         input_files = glob.glob(resource)
72         parsed_dict = dict()
73         if os.path.isdir(resource):
74             for input_file in os.listdir(resource):
75                 parsed_dict.update(
76                     _parse_yaml_path(os.path.join(resource, input_file)))
77         elif input_files:
78             for input_file in input_files:
79                 parsed_dict.update(_parse_yaml_path(input_file))
80         else:
81             parsed_dict.update(_parse_yaml_path(resource))
82     return parsed_dict
83
84
85 def _parse_yaml_path(resource):
86
87     try:
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())
92         else:
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(
97             resource, str(e)))
98
99     # Emtpy files return None
100     content = content or dict()
101     if not isinstance(content, dict):
102         raise AriaCliError()
103
104     return content
105
106
107 def _plain_string_to_dict(input_string):
108     input_string = input_string.strip()
109     input_dict = {}
110     mapped_inputs = input_string.split(';')
111     for mapped_input in mapped_inputs:
112         mapped_input = mapped_input.strip()
113         if not mapped_input:
114             continue
115         split_mapping = mapped_input.split('=')
116         try:
117             key = split_mapping[0].strip()
118             value = split_mapping[1].strip()
119         except IndexError:
120             raise AriaCliError(
121                 "Invalid input format: {0}, the expected format is: "
122                 "key1=value1;key2=value2".format(input_string))
123         input_dict[key] = value
124     return input_dict