Initial commit
[ta/config-manager.git] / cmframework / src / cmframework / server / cmsnapshot.py
1 # Copyright 2019 Nokia
2
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
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14 import logging
15 import re
16 import datetime
17
18 from cmframework.apis import cmerror
19
20
21 class CMSnapshot(object):
22     def __init__(self, handler):
23         logging.debug('CMSnapshot constructed')
24
25         self._handler = handler
26         self._metadata = {}
27         self._data = {}
28
29     def get_property(self, prop_name):
30         if not self._metadata:
31             raise cmerror.CMError('No data: create or load first')
32
33         properties = self.get_properties(prop_name)
34
35         return properties.get(prop_name)
36
37     def get_properties(self, prop_filter='.*'):
38         if not self._data:
39             raise cmerror.CMError('No data: create or load first')
40
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
46
47         return matched_properties
48
49     def create(self, snapshot_name, source_backend, custom_metadata=None):
50         logging.debug('create_snapshot called, snapshot name is %s', snapshot_name)
51
52         if self._handler.snapshot_exists(snapshot_name):
53             raise cmerror.CMError('Snapshot already exist')
54
55         self._metadata = {}
56         self._metadata['name'] = snapshot_name
57         self._metadata['creation_date'] = datetime.datetime.now().isoformat()
58         self._metadata['custom'] = custom_metadata
59
60         self._data = source_backend.get_properties('.*')
61
62         snapshot_data = {'snapshot_properties': self._data, 'snapshot_metadata': self._metadata}
63         self._handler.set_data(snapshot_name, snapshot_data)
64
65     def load(self, snapshot_name):
66         logging.debug('load_snapshot called, snapshot name is %s', snapshot_name)
67
68         if not self._handler.snapshot_exists(snapshot_name):
69             raise cmerror.CMError('Snapshot does not exist')
70
71         snapshot_data = self._handler.get_data(snapshot_name)
72
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))
76
77         self._data = snapshot_data.get('snapshot_properties')
78
79     def restore(self, target_backend):
80         logging.debug('restore_snapshot called')
81
82         if not self._data:
83             raise cmerror.CMError('No data: create or load first')
84
85         current_properties = target_backend.get_properties('.*')
86         current_keys = current_properties.keys()
87
88         if len(current_keys) == 1:
89             target_backend.delete_property(current_keys[0])
90         else:
91             target_backend.delete_properties(current_keys)
92
93         target_backend.set_properties(self._data)
94
95     def list(self):
96         logging.debug('list_snapshots called')
97
98         snapshots = []
99
100         snapshot_names = self._handler.list_snapshots()
101
102         for snapshot_name in snapshot_names:
103             snapshot_data = self._handler.get_data(snapshot_name)
104             metadata = snapshot_data.get('snapshot_metadata')
105             if not metadata:
106                 logging.warning('Could not load snapshot metadata for %s', snapshot_name)
107                 continue
108
109             snapshots.append(metadata)
110
111         return snapshots
112
113     def delete(self, snapshot_name):
114         logging.debug('delete_snapshot called, snapshot name is %s', snapshot_name)
115
116         if not self._handler.snapshot_exists(snapshot_name):
117             raise cmerror.CMError('Snapshot does not exist')
118
119         self._handler.delete_snapshot(snapshot_name)