vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / tests / helpers.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 import os
17 import json
18
19 from . import ROOT_DIR
20 from .resources import DIR as RESOURCES_DIR
21
22
23 def get_example_uri(*args):
24     return os.path.join(ROOT_DIR, 'examples', *args)
25
26
27 def get_resource_uri(*args):
28     return os.path.join(RESOURCES_DIR, *args)
29
30
31 def get_service_template_uri(*args):
32     return os.path.join(RESOURCES_DIR, 'service-templates', *args)
33
34
35 class FilesystemDataHolder(object):
36
37     def __init__(self, path, reset=False):
38         self._path = path
39         if reset or not os.path.exists(self._path) or open(self._path).read() == '':
40             self._dump({})
41
42     def _load(self):
43         with open(self._path) as f:
44             return json.load(f)
45
46     def _dump(self, value):
47         with open(self._path, 'w') as f:
48             return json.dump(value, f)
49
50     def __contains__(self, item):
51         return item in self._load()
52
53     def __setitem__(self, key, value):
54         dict_ = self._load()
55         dict_[key] = value
56         self._dump(dict_)
57
58     def __getitem__(self, item):
59         return self._load()[item]
60
61     def __iter__(self):
62         return iter(self._load())
63
64     def get(self, item, default=None):
65         return self._load().get(item, default)
66
67     def setdefault(self, key, value):
68         dict_ = self._load()
69         return_value = dict_.setdefault(key, value)
70         self._dump(dict_)
71         return return_value
72
73     def update(self, dict_=None, **kwargs):
74         current_dict = self._load()
75         if dict_:
76             current_dict.update(dict_)
77         current_dict.update(**kwargs)
78         self._dump(current_dict)
79
80     @property
81     def path(self):
82         return self._path