def _create_or_update_type(type_def): """ This method creates or updates a type definition in MongoDB. :param type_def: the type definition to update or create. If a type definition with the same as an existing type, the type is updated, otherwise it is created. :type type_def: ContentType :return: This method will always return None :rtype: None """ # Make sure a collection exists for the type database = pulp_db.get_database() collection_name = unit_collection_name(type_def.id) if collection_name not in database.collection_names(): pulp_db.get_collection(collection_name, create=True) # Add or update an entry in the types list content_type_collection = ContentType.get_collection() content_type = ContentType( type_def.id, type_def.display_name, type_def.description, type_def.unit_key, type_def.search_indexes, type_def.referenced_types) # no longer rely on _id = id existing_type = content_type_collection.find_one({'id': type_def.id}, fields=[]) if existing_type is not None: content_type._id = existing_type['_id'] # XXX this still causes a potential race condition when 2 users are updating the same type content_type_collection.save(content_type, safe=True)
def _create_or_update_type(type_def): """ This method creates or updates a type definition in MongoDB. :param type_def: the type definition to update or create. If a type definition with the same as an existing type, the type is updated, otherwise it is created. :type type_def: ContentType :return: This method will always return None :rtype: None """ # Make sure a collection exists for the type database = connection.get_database() collection_name = unit_collection_name(type_def.id) if collection_name not in database.collection_names(): connection.get_collection(collection_name, create=True) # Add or update an entry in the types list content_type_collection = ContentType.get_collection() content_type = ContentType(type_def.id, type_def.display_name, type_def.description, type_def.unit_key, type_def.search_indexes, type_def.referenced_types) # no longer rely on _id = id existing_type = content_type_collection.find_one({'id': type_def.id}, projection=[]) if existing_type is not None: content_type._id = existing_type['_id'] # XXX this still causes a potential race condition when 2 users are updating the same type content_type_collection.save(content_type)
def test_create_or_update_existing_type_collection(self): """ Tests calling create_or_update with a change to an existing type collection is successful. """ # Setup type_def = TypeDefinition("rpm", "RPM", "RPM Packages", ["name"], ["name"], []) types_db._create_or_update_type(type_def) # Test type_def.display_name = "new-name" type_def.description = "new-description" type_def.unit_key = "new-key" type_def.search_indexes = None types_db._create_or_update_type(type_def) # Verify # Present in types collection all_types = list(ContentType.get_collection().find()) self.assertEqual(1, len(all_types)) found = all_types[0] self.assertEqual(type_def.id, found["id"]) self.assertEqual(type_def.display_name, found["display_name"]) self.assertEqual(type_def.description, found["description"]) self.assertEqual(type_def.unit_key, found["unit_key"]) self.assertEqual(type_def.search_indexes, found["search_indexes"]) # Type collection exists collection_name = types_db.unit_collection_name(type_def.id) self.assertTrue(collection_name in pulp_db.get_database().collection_names())
def test_create_or_update_type_collection(self): """ Tests the call to create a new type collection works. """ # Setup type_def = TypeDefinition('rpm', 'RPM', 'RPM Packages', ['name'], ['name'], []) # Test types_db._create_or_update_type(type_def) # Verify # Present in types collection all_types = list(ContentType.get_collection().find()) self.assertEqual(1, len(all_types)) found = all_types[0] self.assertEqual(type_def.id, found['id']) self.assertEqual(type_def.display_name, found['display_name']) self.assertEqual(type_def.description, found['description']) self.assertEqual(type_def.unit_key, found['unit_key']) self.assertEqual(type_def.search_indexes, found['search_indexes']) # Type collection exists collection_name = types_db.unit_collection_name(type_def.id) self.assertTrue(collection_name in pulp_db.get_database().collection_names())
def test_create_or_update_type_collection(self): """ Tests the call to create a new type collection works. """ # Setup type_def = TypeDefinition('rpm', 'RPM', 'RPM Packages', ['name'], ['name'], []) # Test types_db._create_or_update_type(type_def) # Verify # Present in types collection all_types = list(ContentType.get_collection().find()) self.assertEqual(1, len(all_types)) found = all_types[0] self.assertEqual(type_def.id, found['id']) self.assertEqual(type_def.display_name, found['display_name']) self.assertEqual(type_def.description, found['description']) self.assertEqual(type_def.unit_key, found['unit_key']) self.assertEqual(type_def.search_indexes, found['search_indexes']) # Type collection exists collection_name = types_db.unit_collection_name(type_def.id) self.assertTrue( collection_name in pulp_db.database().collection_names())
def test_create_or_update_existing_type_collection(self): """ Tests calling create_or_update with a change to an existing type collection is successful. """ # Setup type_def = TypeDefinition('rpm', 'RPM', 'RPM Packages', ['name'], ['name'], []) types_db._create_or_update_type(type_def) # Test type_def.display_name = 'new-name' type_def.description = 'new-description' type_def.unit_key = 'new-key' type_def.search_indexes = None types_db._create_or_update_type(type_def) # Verify # Present in types collection all_types = list(ContentType.get_collection().find()) self.assertEqual(1, len(all_types)) found = all_types[0] self.assertEqual(type_def.id, found['id']) self.assertEqual(type_def.display_name, found['display_name']) self.assertEqual(type_def.description, found['description']) self.assertEqual(type_def.unit_key, found['unit_key']) self.assertEqual(type_def.search_indexes, found['search_indexes']) # Type collection exists collection_name = types_db.unit_collection_name(type_def.id) self.assertTrue( collection_name in pulp_db.database().collection_names())
def test_create_or_update_existing_type_collection(self): """ Tests calling create_or_update with a change to an existing type collection is successful. """ # Setup type_def = TypeDefinition('rpm', 'RPM', 'RPM Packages', ['name'], ['name'], []) types_db._create_or_update_type(type_def) # Test type_def.display_name = 'new-name' type_def.description = 'new-description' type_def.unit_key = 'new-key' type_def.search_indexes = None types_db._create_or_update_type(type_def) # Verify # Present in types collection all_types = list(ContentType.get_collection().find()) self.assertEqual(1, len(all_types)) found = all_types[0] self.assertEqual(type_def.id, found['id']) self.assertEqual(type_def.display_name, found['display_name']) self.assertEqual(type_def.description, found['description']) self.assertEqual(type_def.unit_key, found['unit_key']) self.assertEqual(type_def.search_indexes, found['search_indexes']) # Type collection exists collection_name = types_db.unit_collection_name(type_def.id) self.assertTrue(collection_name in pulp_db.get_database().collection_names())
def all_type_definitions(): """ @return: list of all type definitions in the database (mongo SON objects) @rtype: list of dict """ coll = ContentType.get_collection() types = list(coll.find()) return types
def _create_or_update_type(type_def): # Make sure a collection exists for the type database = pulp_db.get_database() collection_name = unit_collection_name(type_def.id) if collection_name not in database.collection_names(): pulp_db.get_collection(collection_name, create=True) # Add or update an entry in the types list content_type_collection = ContentType.get_collection() content_type = ContentType(type_def.id, type_def.display_name, type_def.description, type_def.unit_key, type_def.search_indexes, type_def.referenced_types) # no longer rely on _id = id existing_type = content_type_collection.find_one({'id': type_def.id}, fields=[]) if existing_type is not None: content_type._id = existing_type['_id'] # XXX this still causes a potential race condition when 2 users are updating the same type content_type_collection.save(content_type, safe=True)
def type_definition(type_id): """ Return a type definition @param type_id: unique type id @type type_id: str @return: corresponding type definition, None if not found @rtype: SON or None """ collection = ContentType.get_collection() type_ = collection.find_one({'id': type_id}) return type_
def all_type_ids(): """ @return: list of IDs for all types currently in the database; empty list if there are no IDs in the database @rtype: list of str """ collection = ContentType.get_collection() type_id_son = list(collection.find(fields={'id' : 1})) type_ids = [t['id'] for t in type_id_son] return type_ids
def all_type_ids(): """ @return: list of IDs for all types currently in the database; empty list if there are no IDs in the database @rtype: list of str """ collection = ContentType.get_collection() type_id_son = list(collection.find(projection={'id': 1})) type_ids = [t['id'] for t in type_id_son] return type_ids
def all_type_collection_names(): """ @return: list of collection names for all types currently in the database @rtype: list of str """ collection = ContentType.get_collection() type_ids = list(collection.find(fields={'id' : 1})) type_collection_names = [] for id in type_ids: type_collection_names.append(unit_collection_name(id['id'])) return type_collection_names
def all_type_collection_names(): """ @return: list of collection names for all types currently in the database @rtype: list of str """ collection = ContentType.get_collection() type_ids = list(collection.find(projection={'id': 1})) type_collection_names = [] for id in type_ids: type_collection_names.append(unit_collection_name(id['id'])) return type_collection_names
def type_units_unit_key(type_id): """ Get the unit key for a given content type collection. If no type definition is found for the given ID, None is returned @param type_id: unique content type identifier @type type_id: str @return: list of indices that can uniquely identify a document in the content type collection @rtype: list of str or None """ collection = ContentType.get_collection() type_def = collection.find_one({'id': type_id}) if type_def is None: return None return type_def['unit_key']
def clean(): """ Purges the database of all types and their associated collections. This isn't really meant to be run from Pulp server code but rather as a utility for test cases. """ LOG.info('Purging the database of all content type definitions and collections') # Search the database instead of just going on what's in the type listing # just in case they got out of sync database = pulp_db.get_database() all_collection_names = database.collection_names() type_collection_names = [n for n in all_collection_names if n.startswith(TYPE_COLLECTION_PREFIX)] for drop_me in type_collection_names: database[drop_me].drop() # Purge the types collection of all entries type_collection = ContentType.get_collection() type_collection.remove(safe=True)
def define_plugins(self): collection = ContentType.get_collection() for type_id in ALL_TYPES: collection.save(dict(id=type_id, unit_key=UNIT_METADATA.keys()), safe=True)
def define_plugins(self): collection = ContentType.get_collection() collection.save(dict(id=self.TYPEDEF_ID, unit_key=self.UNIT_KEY.keys()))
def _validate_content_type(): objectdb = ContentType.get_collection() reference = ContentType('', '', '', [], [], []) return _validate_model(ConsumerHistoryEvent.__name__, objectdb, reference)