3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 from cmframework.apis import cmerror
21 class CMSnapshot(object):
22 def __init__(self, handler):
23 logging.debug('CMSnapshot constructed')
25 self._handler = handler
29 def get_property(self, prop_name):
30 if not self._metadata:
31 raise cmerror.CMError('No data: create or load first')
33 properties = self.get_properties(prop_name)
35 return properties.get(prop_name)
37 def get_properties(self, prop_filter='.*'):
39 raise cmerror.CMError('No data: create or load first')
41 matched_properties = {}
42 pattern = re.compile(prop_filter)
43 for key, value in self._data:
44 if pattern.match(key):
45 matched_properties[key] = value
47 return matched_properties
49 def create(self, snapshot_name, source_backend, custom_metadata=None):
50 logging.debug('create_snapshot called, snapshot name is %s', snapshot_name)
52 if self._handler.snapshot_exists(snapshot_name):
53 raise cmerror.CMError('Snapshot already exist')
56 self._metadata['name'] = snapshot_name
57 self._metadata['creation_date'] = datetime.datetime.now().isoformat()
58 self._metadata['custom'] = custom_metadata
60 self._data = source_backend.get_properties('.*')
62 snapshot_data = {'snapshot_properties': self._data, 'snapshot_metadata': self._metadata}
63 self._handler.set_data(snapshot_name, snapshot_data)
65 def load(self, snapshot_name):
66 logging.debug('load_snapshot called, snapshot name is %s', snapshot_name)
68 if not self._handler.snapshot_exists(snapshot_name):
69 raise cmerror.CMError('Snapshot does not exist')
71 snapshot_data = self._handler.get_data(snapshot_name)
73 self._metadata = snapshot_data.get('snapshot_metadata')
74 if not self._metadata:
75 raise cmerror.CMError('Could not load snapshot metadata for {}'.format(snapshot_name))
77 self._data = snapshot_data.get('snapshot_properties')
79 def restore(self, target_backend):
80 logging.debug('restore_snapshot called')
83 raise cmerror.CMError('No data: create or load first')
85 current_properties = target_backend.get_properties('.*')
86 current_keys = current_properties.keys()
88 if len(current_keys) == 1:
89 target_backend.delete_property(current_keys[0])
91 target_backend.delete_properties(current_keys)
93 target_backend.set_properties(self._data)
96 logging.debug('list_snapshots called')
100 snapshot_names = self._handler.list_snapshots()
102 for snapshot_name in snapshot_names:
103 snapshot_data = self._handler.get_data(snapshot_name)
104 metadata = snapshot_data.get('snapshot_metadata')
106 logging.warning('Could not load snapshot metadata for %s', snapshot_name)
109 snapshots.append(metadata)
113 def delete(self, snapshot_name):
114 logging.debug('delete_snapshot called, snapshot name is %s', snapshot_name)
116 if not self._handler.snapshot_exists(snapshot_name):
117 raise cmerror.CMError('Snapshot does not exist')
119 self._handler.delete_snapshot(snapshot_name)