def is_valid(self, bundle, request=None): from chroma_core.lib.storage_plugin.manager import storage_plugin_manager from chroma_core.lib.storage_plugin.manager import PluginNotFound errors = defaultdict(list) if 'alias' in bundle.data and bundle.data['alias'] is not None: alias = bundle.data['alias'] if alias.strip() == "": errors['alias'].append("May not be blank") elif alias != alias.strip(): errors['alias'].append("No trailing whitespace allowed") if 'plugin_name' in bundle.data: try: storage_plugin_manager.get_plugin_class( bundle.data['plugin_name']) except PluginNotFound, e: errors['plugin_name'].append(e.__str__()) else: if 'class_name' in bundle.data: try: storage_plugin_manager.get_plugin_resource_class( bundle.data['plugin_name'], bundle.data['class_name']) except PluginNotFound, e: errors['class_name'].append(e.__str__())
def obj_create(self, bundle, request=None, **kwargs): # Note: not importing this at module scope so that this module can # be imported without loading plugins (useful at installation) from chroma_core.lib.storage_plugin.manager import storage_plugin_manager resource_class, resource_class_id = storage_plugin_manager.get_plugin_resource_class( bundle.data['plugin_name'], bundle.data['class_name']) attrs = {} input_attrs = bundle.data['attrs'] for name, property in resource_class.get_all_attribute_properties(): if name in input_attrs: attrs[name] = property.encrypt(property.cast( input_attrs[name])) elif property.default: attrs[name] = property.default elif not property.optional: # TODO: proper validation raise RuntimeError("%s not optional" % name) # Construct a record record, created = StorageResourceRecord.get_or_create_root( resource_class, resource_class_id, attrs) #record_dict = self.full_dehydrate(self.build_bundle(obj = record)).data bundle.obj = record return bundle
def _sort_by_attr(self, obj_list, options=None, **kwargs): options = options or {} order_by = options.get('order_by', None) if not order_by: return obj_list if order_by.find('attr_') == 0: attr_name = order_by[5:] invert = False elif order_by.find('attr_') == 1: attr_name = order_by[6:] invert = True else: raise RuntimeError("Can't sort on %s" % order_by) try: class_name = kwargs['class_name'] plugin_name = kwargs['plugin_name'] except KeyError: return obj_list else: from chroma_core.lib.storage_plugin.manager import storage_plugin_manager klass, klass_id = storage_plugin_manager.get_plugin_resource_class( plugin_name, class_name) model_klass = klass.attr_model_class(attr_name) filter_args = {model_klass.__name__.lower() + "__key": attr_name} order_attr = model_klass.__name__.lower() + "__value" return obj_list.filter( **filter_args).order_by(("-" if invert else "") + order_attr)
def get_statistic_properties(self, stat_name): from chroma_core.lib.storage_plugin.manager import storage_plugin_manager klass, klass_id = storage_plugin_manager.get_plugin_resource_class( self.resource_class.storage_plugin.module_name, self.resource_class.class_name) return klass._meta.storage_statistics[stat_name]
def synthetic_volume(serial=None, with_storage=True, usable_for_lustre=True): """ Create a Volume and an underlying StorageResourceRecord """ volume = Volume.objects.create() if not serial: serial = "foobar%d" % volume.id attrs = {'serial': serial, 'size': 8192000} if with_storage: from chroma_core.lib.storage_plugin.manager import storage_plugin_manager resource_class, resource_class_id = storage_plugin_manager.get_plugin_resource_class( 'linux', 'ScsiDevice') storage_resource, created = StorageResourceRecord.get_or_create_root( resource_class, resource_class_id, attrs) volume.storage_resource = storage_resource volume.usable_for_lustre = usable_for_lustre volume.save() return volume
def to_resource_class(self): from chroma_core.lib.storage_plugin.manager import storage_plugin_manager klass, klass_id = storage_plugin_manager.get_plugin_resource_class( self.resource_class.storage_plugin.module_name, self.resource_class.class_name ) return klass
def setUp(self): super(TestCallbacks, self).setUp() import chroma_core.lib.storage_plugin.manager self.orig_manager = chroma_core.lib.storage_plugin.manager.storage_plugin_manager chroma_core.lib.storage_plugin.manager.storage_plugin_manager = ( chroma_core.lib.storage_plugin.manager.StoragePluginManager() ) from chroma_core.lib.storage_plugin.manager import storage_plugin_manager storage_plugin_manager._load_plugin(sys.modules[__name__], "test_mod", TestPlugin) from chroma_core.models import StorageResourceRecord resource_class, resource_class_id = storage_plugin_manager.get_plugin_resource_class( "test_mod", "TestGlobalResource" ) record, created = StorageResourceRecord.get_or_create_root(resource_class, resource_class_id, {"name": "test1"}) from chroma_core.lib.storage_plugin.query import ResourceQuery scannable_record = StorageResourceRecord.objects.get() self.scannable_resource = ResourceQuery().get_resource(scannable_record) self.scannable_global_id = scannable_record.pk self.resource_manager = mock.Mock(_sessions={}) self.plugin = TestPlugin(self.resource_manager, self.scannable_global_id) self.resource_manager._sessions[self.scannable_global_id] = PluginSession( self.plugin, self.scannable_global_id, 0 )
def _record_by_attributes(self, fn, plugin, klass, **attrs): from chroma_core.lib.storage_plugin.manager import storage_plugin_manager import json klass, klass_id = storage_plugin_manager.get_plugin_resource_class( plugin, klass) # FIXME: validate that attrs.keys() are all part of the resource's GlobalId resource = klass(**attrs) return fn(resource_class__id=klass_id, storage_id_str=json.dumps(resource.id_tuple()), storage_id_scope=None)
def _make_global_resource(self, plugin_name, class_name, attrs): from chroma_core.lib.storage_plugin.manager import storage_plugin_manager resource_class, resource_class_id = storage_plugin_manager.get_plugin_resource_class( plugin_name, class_name) resource_record, created = StorageResourceRecord.get_or_create_root( resource_class, resource_class_id, attrs) resource = resource_record.to_resource() resource._handle = self._get_handle() resource._handle_global = False return resource_record, resource
def synthetic_host(address=None, nids=list([]), storage_resource=False, fqdn=None, nodename=None, server_profile='test_profile'): """ Create a ManagedHost + paraphernalia, with states set as if configuration happened successfully :param storage_resource: If true, create a PluginAgentResources (additional overhead, only sometimes required) """ server_profile = ServerProfile.objects.get(name=server_profile) if address is None: address = random_str(postfix=".tld") if fqdn is None: fqdn = address if nodename is None: nodename = address host = ManagedHost.objects.create( address=address, fqdn=fqdn, nodename=nodename, state='managed', server_profile=server_profile, immutable_state=not server_profile.managed if server_profile else False) ObjectCache.add(ManagedHost, host) lnet_configuration = synthetic_lnet_configuration(host, nids) if server_profile.managed: synthetic_rsyslog_configuration(host) synthetic_ntp_configuration(host) synthetic_corosync_configuration(host) synthetic_pacemaker_configuration(host) log.debug("synthetic_host: %s %s" % (address, lnet_configuration.get_nids())) if storage_resource: from chroma_core.lib.storage_plugin.manager import storage_plugin_manager resource_class, resource_class_id = storage_plugin_manager.get_plugin_resource_class( 'linux', 'PluginAgentResources') StorageResourceRecord.get_or_create_root(resource_class, resource_class_id, { 'plugin_name': 'linux', 'host_id': host.id }) return host
def _make_local_resource(self, plugin_name, class_name, **kwargs): from chroma_core.lib.storage_plugin.manager import storage_plugin_manager klass, klass_id = storage_plugin_manager.get_plugin_resource_class( plugin_name, class_name) resource = klass(**kwargs) resource.validate() resource._handle = self._get_handle() resource._handle_global = False return resource
def get_scannable_id_record_by_attributes(self, scope, plugin, klass, **attrs): # FIXME: horrendous function name indicating overcomplication # this is for getting a resource which uses a ScopedId identifier attribute from chroma_core.lib.storage_plugin.manager import storage_plugin_manager from chroma_core.models import StorageResourceRecord import json klass, klass_id = storage_plugin_manager.get_plugin_resource_class(plugin, klass) resource = klass(**attrs) return StorageResourceRecord.objects.get( resource_class__id = klass_id, storage_id_str = json.dumps(resource.id_tuple()), storage_id_scope = scope)
def _create_plugin_instance(self, host): from chroma_core.lib.storage_plugin.manager import storage_plugin_manager resource_class, resource_class_id = storage_plugin_manager.get_plugin_resource_class( "linux", "PluginAgentResources") # FIXME: it is weird that the PluginAgentResources class lives in the linux plugin but is used by all of them record, created = StorageResourceRecord.get_or_create_root( resource_class, resource_class_id, { "plugin_name": self._plugin_name, "host_id": host.id }) return self._plugin_klass(self._resource_manager, record.id)
def setUp(self): super(TestAddRemove, self).setUp() import chroma_core.lib.storage_plugin.manager self.orig_manager = chroma_core.lib.storage_plugin.manager.storage_plugin_manager chroma_core.lib.storage_plugin.manager.storage_plugin_manager = chroma_core.lib.storage_plugin.manager.StoragePluginManager( ) from chroma_core.lib.storage_plugin.manager import storage_plugin_manager storage_plugin_manager._load_plugin(sys.modules[__name__], 'test_mod', TestPlugin) from chroma_core.models import StorageResourceRecord resource_class, resource_class_id = storage_plugin_manager.get_plugin_resource_class( 'test_mod', 'TestGlobalResource') record, created = StorageResourceRecord.get_or_create_root( resource_class, resource_class_id, {'name': 'test1'}) from chroma_core.lib.storage_plugin.query import ResourceQuery scannable_record = StorageResourceRecord.objects.get() self.scannable_resource = ResourceQuery().get_resource( scannable_record) self.scannable_global_id = scannable_record.pk