vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / storage / api.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 Storage APIs.
18 """
19
20 import threading
21
22
23 class StorageAPI(object):
24     """
25     Base class for storage APIs.
26     """
27     def create(self, **kwargs):
28         """
29         Create a storage API.
30         :param kwargs:
31         :return:
32         """
33         raise NotImplementedError('Subclass must implement abstract create method')
34
35
36 class ModelAPI(StorageAPI):
37     """
38     Base class for model APIs ("MAPI").
39     """
40     def __init__(self, model_cls, name=None, **kwargs):
41         """
42         :param model_cls: representing class of the model
43         :param name: name of the model
44         """
45         super(ModelAPI, self).__init__(**kwargs)
46         self._model_cls = model_cls
47         self._name = name or model_cls.__modelname__
48         self._thread_local = threading.local()
49         self._thread_local._instrumentation = []
50
51     @property
52     def _instrumentation(self):
53         if not hasattr(self._thread_local, '_instrumentation'):
54             self._thread_local._instrumentation = []
55         return self._thread_local._instrumentation
56
57
58     @property
59     def name(self):
60         """
61         Name of the class.
62
63         :type: :obj:`basestring`
64         """
65         return self._name
66
67     @property
68     def model_cls(self):
69         """
70         Class representing the model
71
72         :type: :obj:`Type`
73         """
74         return self._model_cls
75
76     def get(self, entry_id, filters=None, **kwargs):
77         """
78         Gets a model from storage.
79
80         :param entry_id:
81         """
82         raise NotImplementedError('Subclass must implement abstract get method')
83
84     def put(self, entry, **kwargs):
85         """
86         Puts a model in storage.
87
88         :param entry:
89         """
90         raise NotImplementedError('Subclass must implement abstract store method')
91
92     def delete(self, entry_id, **kwargs):
93         """
94         Deletes a model from storage.
95
96         :param entry_id:
97         """
98         raise NotImplementedError('Subclass must implement abstract delete method')
99
100     def __iter__(self):
101         return self.iter()
102
103     def iter(self, **kwargs):
104         """
105         Iterate over all models in storage.
106         """
107         raise NotImplementedError('Subclass must implement abstract iter method')
108
109     def update(self, entry, **kwargs):
110         """
111         Update a model in storage.
112
113         :param entry:
114         :param kwargs:
115         """
116         raise NotImplementedError('Subclass must implement abstract update method')
117
118
119 class ResourceAPI(StorageAPI):
120     """
121     Base class for resource APIs ("RAPI").
122     """
123     def __init__(self, name, **kwargs):
124         """
125         :param name: resource type
126         """
127         super(ResourceAPI, self).__init__(**kwargs)
128         self._name = name
129
130     @property
131     def name(self):
132         """
133         Name of resource.
134
135         :type: :obj:`basestring`
136         """
137         return self._name
138
139     def read(self, entry_id, path, **kwargs):
140         """
141         Get a bytesteam for a resource from storage.
142
143         :param entry_id:
144         :param path:
145         """
146         raise NotImplementedError('Subclass must implement abstract read method')
147
148     def delete(self, entry_id, path, **kwargs):
149         """
150         Delete a resource from storage.
151
152         :param entry_id:
153         :param path:
154         """
155         raise NotImplementedError('Subclass must implement abstract delete method')
156
157     def download(self, entry_id, destination, path=None, **kwargs):
158         """
159         Download a resource from storage.
160
161         :param entry_id:
162         :param destination:
163         :param path:
164         """
165         raise NotImplementedError('Subclass must implement abstract download method')
166
167     def upload(self, entry_id, source, path=None, **kwargs):
168         """
169         Upload a resource to storage.
170
171         :param entry_id:
172         :param source:
173         :param path:
174         """
175         raise NotImplementedError('Subclass must implement abstract upload method')
176
177
178 def generate_lower_name(model_cls):
179     """
180     Generates the name of the class from the class object, e.g. ``SomeClass`` -> ``some_class``
181
182     :param model_cls: class to evaluate
183     :return: lowercase name
184     :rtype: basestring
185     """
186     return getattr(model_cls, '__mapiname__', model_cls.__tablename__)