vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / cli / config / config.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 CLI configuration mechanism.
18 """
19
20 import os
21 import pkg_resources
22 from ruamel import yaml
23
24 from jinja2.environment import Template
25
26
27 CONFIG_FILE_NAME = 'config.yaml'
28
29
30 class CliConfig(object):
31
32     def __init__(self, config_path):
33         with open(config_path) as f:
34             self._config = yaml.safe_load(f.read())
35
36     @classmethod
37     def create_config(cls, workdir):
38         config_path = os.path.join(workdir, CONFIG_FILE_NAME)
39         if not os.path.isfile(config_path):
40             config_template = pkg_resources.resource_string(
41                 __package__,
42                 'config_template.yaml')
43
44             default_values = {
45                 'log_path': os.path.join(workdir, 'cli.log'),
46                 'enable_colors': True
47             }
48
49             template = Template(config_template)
50             rendered = template.render(**default_values)
51             with open(config_path, 'w') as f:
52                 f.write(rendered)
53                 f.write(os.linesep)
54
55         return cls(config_path)
56
57     @property
58     def logging(self):
59         return self.Logging(self._config.get('logging'))
60
61     class Logging(object):
62
63         def __init__(self, logging):
64             self._logging = logging or {}
65
66         @property
67         def filename(self):
68             return self._logging.get('filename')
69
70         @property
71         def loggers(self):
72             return self._logging.get('loggers', {})
73
74         @property
75         def execution(self):
76             return self.Execution(self._logging.get('execution'))
77
78         class Execution(object):
79
80             def __init__(self, execution_logging):
81                 self._execution_logging = execution_logging
82
83             @property
84             def colors_enabled(self):
85                 return self.colors.get('enabled', False)
86
87             @property
88             def colors(self):
89                 return self._execution_logging.get('colors', {})
90
91             @property
92             def formats(self):
93                 return self._execution_logging.get('formats', {})