blob: 0613bfef015a4719f616111bce42de2b63f41729 [file] [log] [blame]
martin f. krafftf37f0682013-06-14 16:36:20 +02001#
2# -*- coding: utf-8 -*-
3#
4# This file is part of reclass (http://github.com/madduck/reclass)
5#
6# Copyright © 2007–13 martin f. krafft <madduck@madduck.net>
7# Released under the terms of the Artistic Licence 2.0
8#
9class NodeStorageBase(object):
10
11 def __init__(self, nodes_uri, classes_uri):
12 self._nodes_uri = nodes_uri
13 self._classes_uri = classes_uri
14
15 nodes_uri = property(lambda self: self._nodes_uri)
16 classes_uri = property(lambda self: self._classes_uri)
17
18 def _read_entity(self, node, base_uri, seen={}):
19 raise NotImplementedError, "Storage class not implement node info retrieval"
20
21 def nodeinfo(self, node):
22 entity, uri = self._read_nodeinfo(node, self.nodes_uri, {})
23 return {'RECLASS' : {'node': node, 'node_uri': uri},
24 'classes': list(entity.classes),
25 'applications': list(entity.applications),
26 'parameters': dict(entity.parameters)
27 }
28
29 def _list_inventory(self):
30 raise NotImplementedError, "Storage class does not implement inventory listing"
31
32 def inventory(self):
33 entity, applications, classes = self._list_inventory()
34 ret = classes
35 ret.update([(k + '_hosts',v) for k,v in applications.iteritems()])
36 return ret
37
38class StorageBackendLoader(object):
39
40 def __init__(self, storage_type):
41 self._name = storage_type
42 try:
43 self._module = __import__(storage_type, globals(), locals(), storage_type)
44 except ImportError:
45 raise NotImplementedError
46
47 def load(self, attr='ExternalNodeStorage'):
48 klass = getattr(self._module, attr, None)
49 if klass is None:
50 raise AttributeError, \
51 'Storage backend class {0} does not export "{1}"'.format(self._name, klass)
52 return klass