def validate(self, data): """ Validate that the Serializer contains valid data. Validates JSON structure of migration_plan. Checks pulp2 and pulp3 plugins are installed. """ schema = json.loads(SCHEMA) validator = Draft7Validator(schema) if isinstance(data['plan'], str): loaded_plan = json.loads(data['plan']) elif isinstance(data['plan'], dict): loaded_plan = data['plan'] else: raise serializers.ValidationError( _("Must provide a (JSON-encoded) string or dict for 'plan', not list") ) err = [] for error in sorted(validator.iter_errors(loaded_plan), key=str): err.append(error.message) if err: raise serializers.ValidationError( _("Provided Migration Plan format is invalid:'{}'".format(err)) ) plugins_to_migrate = set() for plugin_type in loaded_plan['plugins']: plugins_to_migrate.add(plugin_type['type']) if len(loaded_plan['plugins']) != len(plugins_to_migrate): raise serializers.ValidationError( _("Provided Migration Plan contains same plugin type specified more that once.") ) # MongoDB connection initialization connection.initialize() db = connection.get_database() for plugin in plugins_to_migrate: plugin_migrator = PLUGIN_MIGRATORS.get(plugin) if not plugin_migrator: raise serializers.ValidationError( _("Migration of {} plugin is not supported.".format(plugin)) ) if plugin_migrator.pulp3_plugin not in INSTALLED_PULP_PLUGINS: raise serializers.ValidationError( _("Plugin {} is not installed in pulp3.".format(plugin)) ) try: db.command("collstats", plugin_migrator.pulp2_collection) except OperationFailure: raise serializers.ValidationError( _("Plugin {} is not installed in pulp2.".format(plugin)) ) data['plan'] = loaded_plan return data
def validate(self, data): """ Validate that the Serializer contains valid data. Validates JSON structure of migration_plan. Checks pulp2 and pulp3 plugins are installed. """ schema = json.loads(SCHEMA) validator = Draft7Validator(schema) loaded_plan = json.loads(data['plan']) err = [] for error in sorted(validator.iter_errors(loaded_plan), key=str): err.append(error.message) if err: raise serializers.ValidationError( _("Provided Migration Plan format is invalid:'{}'".format(err)) ) plugins_to_migrate = set() for plugin_type in loaded_plan['plugins']: plugins_to_migrate.add(plugin_type['type']) if len(loaded_plan['plugins']) != len(plugins_to_migrate): raise serializers.ValidationError( _("Provided Migration Plan contains same plugin type specified more that once.") ) # MongoDB connection initialization connection.initialize() db = connection.get_database() for plugin in plugins_to_migrate: if PULP_2TO3_PLUGIN_MAP.get(plugin) not in INSTALLED_PULP_PLUGINS: raise serializers.ValidationError( _("Plugin {} is not installed in pulp3.".format(plugin)) ) try: collection = PULP2_COLLECTION_MAP.get(plugin) db.command("collstats", collection) except OperationFailure: raise serializers.ValidationError( _("Plugin {} is not installed in pulp2.".format(plugin)) ) data['plan'] = loaded_plan return data