def populate(self): manager = factory.consumer_manager() for id in self.CONSUMER_IDS: manager.register(id) manager = factory.consumer_profile_manager() for id in self.CONSUMER_IDS: manager.create(id, 'rpm', self.PROFILE)
def units_applicable(self, criteria, units): """ Detemine and report which of the specified content units is applicable to consumers specified by the I{criteria}. @param criteria: The consumer selection criteria. @type criteria: list @param units: A list of content units to be installed. @type units: list of: { type_id:<str>, unit_key:<dict> } @return: A dict: {consumer_id:[<ApplicabilityReport>]} @rtype: list """ result = {} conduit = ProfilerConduit() manager = managers.consumer_query_manager() ids = [c['id'] for c in manager.find_by_criteria(criteria)] manager = managers.consumer_profile_manager() profiles = manager.find_profiles(ids) for id in ids: for unit in units: typeid = unit['type_id'] profiler, cfg = self.__profiler(typeid) pc = self.__profiled_consumer(id) report = profiler.unit_applicable(pc, unit, cfg, conduit) report.unit = unit ulist = result.setdefault(id, []) ulist.append(report) return result
def PUT(self, consumer_id, content_type): """ Update the association of a profile with a consumer by content type ID. @param consumer_id: A consumer ID. @type consumer_id: str @param content_type: A content unit type ID. @type content_type: str @return: The updated model object: {consumer_id:<str>, content_type:<str>, profile:<dict>} @rtype: dict """ body = self.params() profile = body.get('profile') manager = managers.consumer_profile_manager() tags = [resource_tag(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id), resource_tag(dispatch_constants.RESOURCE_CONTENT_UNIT_TYPE, content_type), action_tag('profile_update')] call_request = CallRequest(manager.update, [consumer_id, content_type], {'profile': profile}, tags=tags, weight=0, kwarg_blacklist=['profile']) call_request.reads_resource(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id) call_report = CallReport.from_call_request(call_request) call_report.serialize_result = False consumer = execution.execute_sync(call_request, call_report) link = serialization.link.child_link_obj(consumer_id, content_type) consumer.update(link) return self.ok(consumer)
def post(self, request, consumer_id): """ Associate a profile with a consumer by content type ID. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param consumer_id: A consumer ID. :type consumer_id: str :raises MissingValue: if some parameter were not provided :return: Response representing the created profile :rtype: django.http.HttpResponse """ body = request.body_as_json content_type = body.get('content_type') profile = body.get('profile') manager = factory.consumer_profile_manager() new_profile = manager.create(consumer_id, content_type, profile) if content_type is None: raise MissingValue('content_type') link = add_link_profile(new_profile) response = generate_json_response_with_pulp_encoder(new_profile) redirect_response = generate_redirect_response(response, link['_href']) return redirect_response
def DELETE(self, consumer_id, content_type): """ Delete an association between the specified consumer and profile. Designed to be idempotent. @param consumer_id: A consumer ID. @type consumer_id: str @param content_type: The content type ID. @type content_type: str @return: The deleted model object: {consumer_id:<str>, content_type:<str>, profile:<dict>} Or, None if bind does not exist. @rtype: dict """ manager = managers.consumer_profile_manager() resources = { dispatch_constants.RESOURCE_CONSUMER_TYPE: {consumer_id:dispatch_constants.RESOURCE_READ_OPERATION}, } args = [ consumer_id, content_type, ] tags = [ resource_tag(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id), ] call_request = CallRequest(manager.delete, args=args, resources=resources, tags=tags) return self.ok(execution.execute(call_request))
def PUT(self, consumer_id, content_type): """ Update the association of a profile with a consumer by content type ID. @param consumer_id: A consumer ID. @type consumer_id: str @param content_type: A content unit type ID. @type content_type: str @return: The updated model object: {consumer_id:<str>, content_type:<str>, profile:<dict>} @rtype: dict """ body = self.params() profile = body.get('profile') resources = { dispatch_constants.RESOURCE_CONSUMER_TYPE: {consumer_id:dispatch_constants.RESOURCE_READ_OPERATION}, } args = [ consumer_id, content_type, profile, ] manager = managers.consumer_profile_manager() call_request = CallRequest( manager.update, args, resources=resources, weight=0) link = serialization.link.child_link_obj(consumer_id, content_type) result = execution.execute_sync_created(self, call_request, link) return result
def test_missing_consumer(self): # Test manager = factory.consumer_profile_manager() # self.CONSUMER_ID is not an existing consumer, as it is not built during setUp(), so this # should raise MissingResource self.assertRaises(MissingResource, manager.update, self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1)
def test_delete_not_found(self): # Setup manager = factory.consumer_profile_manager() # Test path = '/v2/consumers/%s/profiles/unknown/' % self.CONSUMER_ID status, body = self.delete(path) self.assertEqual(status, 404)
def test_get_profiles_none(self): # Setup self.populate() # Test manager = factory.consumer_profile_manager() profiles = manager.get_profiles(self.CONSUMER_ID) self.assertEquals(len(profiles), 0)
def DELETE(self, consumer_id, content_type): """ Delete an association between the specified consumer and profile. Designed to be idempotent. @param consumer_id: A consumer ID. @type consumer_id: str @param content_type: The content type ID. @type content_type: str @return: The deleted model object: {consumer_id:<str>, content_type:<str>, profile:<dict>} Or, None if bind does not exist. @rtype: dict """ manager = managers.consumer_profile_manager() args = [ consumer_id, content_type, ] tags = [ resource_tag(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id), ] call_request = CallRequest(manager.delete, args=args, tags=tags) call_request.reads_resource(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id) return self.ok(execution.execute(call_request))
def unregister(self, id): """ Unregisters given consumer. @param id: identifies the consumer being unregistered @type id: str @raises MissingResource: if the given consumer does not exist @raises OperationFailed: if any part of the unregister process fails; the exception will contain information on which sections failed @raises PulpExecutionException: if error during updating database collection """ self.get_consumer(id) # Remove associate bind manager = factory.consumer_bind_manager() manager.consumer_deleted(id) # Remove associated profiles manager = factory.consumer_profile_manager() manager.consumer_deleted(id) # Notify agent agent_consumer = factory.consumer_agent_manager() agent_consumer.unregistered(id) # Database Updates try: Consumer.get_collection().remove({'id' : id}, safe=True) except Exception: _LOG.exception('Error updating database collection while removing consumer [%s]' % id) raise PulpExecutionException("database-error"), None, sys.exc_info()[2] factory.consumer_history_manager().record_event(id, 'consumer_unregistered')
def populate_profile(self, id, env): manager = managers.consumer_profile_manager() profile = [] for i in range(0, env.profile_units): p = PROFILE_TEMPLATE.copy() p['name'] = 'unit_%d' % i profile.append(p) manager.create(id, self.TYPE_ID, profile)
def test_get_profile_not_found(self): # Setup self.populate() # Test manager = factory.consumer_profile_manager() manager.create(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) # Verify self.assertRaises(MissingResource, manager.get_profile, self.CONSUMER_ID, self.TYPE_1)
def test_get_profile(self): # Setup self.populate() # Test manager = factory.consumer_profile_manager() manager.create(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.create(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) profile = manager.get_profile(self.CONSUMER_ID, self.TYPE_1) self.assertTrue(profile is not None)
def GET(self, consumer_id, content_type): """ @param consumer_id: The consumer ID. @type consumer_id: str """ manager = managers.consumer_profile_manager() profile = manager.get_profile(consumer_id, content_type) serialized = serialization.consumer.profile(profile) return self.ok(serialized)
def unregister(consumer_id): """ Unregisters given consumer. :param consumer_id: identifies the consumer being unregistered :type consumer_id: str :raises MissingResource: if the given consumer does not exist :raises OperationFailed: if any part of the unregister process fails; the exception will contain information on which sections failed :raises PulpExecutionException: if error during updating database collection """ ConsumerManager.get_consumer(consumer_id) # Remove associate bind manager = factory.consumer_bind_manager() manager.consumer_deleted(consumer_id) # Remove associated profiles manager = factory.consumer_profile_manager() manager.consumer_deleted(consumer_id) # Notify agent agent_consumer = factory.consumer_agent_manager() agent_consumer.unregistered(consumer_id) # remove from consumer groups group_manager = factory.consumer_group_manager() group_manager.remove_consumer_from_groups(consumer_id) # delete any scheduled unit installs schedule_manager = factory.consumer_schedule_manager() for schedule in schedule_manager.get(consumer_id): # using "delete" on utils skips validation that the consumer exists. schedule_utils.delete(schedule.id) # Database Updates try: Consumer.get_collection().remove({'id': consumer_id}, safe=True) except Exception: _logger.exception( 'Error updating database collection while removing consumer [%s]' % consumer_id) raise PulpExecutionException( "database-error"), None, sys.exc_info()[2] # remove the consumer from any groups it was a member of group_manager = factory.consumer_group_manager() group_manager.remove_consumer_from_groups(consumer_id) factory.consumer_history_manager().record_event( consumer_id, 'consumer_unregistered')
def GET(self, consumer_id): """ Get all profiles associated with a consumer. @param consumer_id: The consumer ID. @type consumer_id: str @return: A list of profiles: profile is: {consumer_id:<str>, content_type:<str>, profile:<dict>} @return: list """ manager = managers.consumer_profile_manager() profiles = manager.get_profiles(consumer_id) profiles = [serialization.consumer.profile(p) for p in profiles] return self.ok(profiles)
def test_get_by_type(self): # Setup self.populate() manager = factory.consumer_profile_manager() manager.create(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.create(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) # Test path = '/v2/consumers/%s/profiles/%s/' % (self.CONSUMER_ID, self.TYPE_1) status, body = self.get(path) self.assertEqual(status, 200) self.assertEqual(body['consumer_id'], self.CONSUMER_ID) self.assertEqual(body['content_type'], self.TYPE_1) self.assertEqual(body['profile'], self.PROFILE_1)
def test_create(self): # Setup self.populate() # Test manager = factory.consumer_profile_manager() manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) # Verify collection = UnitProfile.get_collection() cursor = collection.find({"consumer_id": self.CONSUMER_ID}) profiles = list(cursor) self.assertEquals(len(profiles), 1) self.assertEquals(profiles[0]["consumer_id"], self.CONSUMER_ID) self.assertEquals(profiles[0]["content_type"], self.TYPE_1) self.assertEquals(profiles[0]["profile"], self.PROFILE_1)
def unregister(self, consumer_id): """ Unregisters given consumer. @param consumer_id: identifies the consumer being unregistered @type consumer_id: str @raises MissingResource: if the given consumer does not exist @raises OperationFailed: if any part of the unregister process fails; the exception will contain information on which sections failed @raises PulpExecutionException: if error during updating database collection """ self.get_consumer(consumer_id) # Remove associate bind manager = factory.consumer_bind_manager() manager.consumer_deleted(consumer_id) # Remove associated profiles manager = factory.consumer_profile_manager() manager.consumer_deleted(consumer_id) # Notify agent agent_consumer = factory.consumer_agent_manager() agent_consumer.unregistered(consumer_id) # remove from consumer groups group_manager = factory.consumer_group_manager() group_manager.remove_consumer_from_groups(consumer_id) # delete any scheduled unit installs schedule_manager = factory.schedule_manager() schedule_manager.delete_all_unit_install_schedules(consumer_id) schedule_manager.delete_all_unit_update_schedules(consumer_id) schedule_manager.delete_all_unit_uninstall_schedules(consumer_id) # Database Updates try: Consumer.get_collection().remove({'id' : consumer_id}, safe=True) except Exception: _LOG.exception('Error updating database collection while removing ' 'consumer [%s]' % consumer_id) raise PulpExecutionException("database-error"), None, sys.exc_info()[2] # remove the consumer from any groups it was a member of group_manager = factory.consumer_group_manager() group_manager.remove_consumer_from_groups(consumer_id) factory.consumer_history_manager().record_event(consumer_id, 'consumer_unregistered')
def test_fetch_by_type2(self): # Setup self.populate() collection = UnitProfile.get_collection() manager = factory.consumer_profile_manager() manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.update(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) # Test profile = manager.get_profile(self.CONSUMER_ID, self.TYPE_2) # Verify self.assertTrue(profile is not None) self.assertEquals(profile["consumer_id"], self.CONSUMER_ID) self.assertEquals(profile["content_type"], self.TYPE_2) self.assertEquals(profile["profile"], self.PROFILE_2)
def test_fetch_by_type2(self): # Setup self.populate() collection = UnitProfile.get_collection() manager = factory.consumer_profile_manager() manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.update(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) # Test profile = manager.get_profile(self.CONSUMER_ID, self.TYPE_2) # Verify self.assertTrue(profile is not None) self.assertEquals(profile['consumer_id'], self.CONSUMER_ID) self.assertEquals(profile['content_type'], self.TYPE_2) self.assertEquals(profile['profile'], self.PROFILE_2)
def test_create(self): # Setup self.populate() # Test manager = factory.consumer_profile_manager() manager.create(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) # Verify collection = UnitProfile.get_collection() cursor = collection.find({'consumer_id': self.CONSUMER_ID}) profiles = list(cursor) self.assertEquals(len(profiles), 1) self.assertEquals(profiles[0]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[0]['content_type'], self.TYPE_1) self.assertEquals(profiles[0]['profile'], self.PROFILE_1)
def unregister(consumer_id): """ Unregisters given consumer. :param consumer_id: identifies the consumer being unregistered :type consumer_id: str :raises MissingResource: if the given consumer does not exist :raises OperationFailed: if any part of the unregister process fails; the exception will contain information on which sections failed :raises PulpExecutionException: if error during updating database collection """ ConsumerManager.get_consumer(consumer_id) # Remove associate bind manager = factory.consumer_bind_manager() manager.consumer_deleted(consumer_id) # Remove associated profiles manager = factory.consumer_profile_manager() manager.consumer_deleted(consumer_id) # Notify agent agent_consumer = factory.consumer_agent_manager() agent_consumer.unregister(consumer_id) # remove from consumer groups group_manager = factory.consumer_group_manager() group_manager.remove_consumer_from_groups(consumer_id) # delete any scheduled unit installs schedule_manager = factory.consumer_schedule_manager() for schedule in schedule_manager.get(consumer_id): # using "delete" on utils skips validation that the consumer exists. schedule_utils.delete(schedule.id) # Database Updates try: Consumer.get_collection().remove({'id': consumer_id}) except Exception: _logger.exception( 'Error updating database collection while removing consumer [%s]' % consumer_id) raise PulpExecutionException("database-error"), None, sys.exc_info()[2] # remove the consumer from any groups it was a member of group_manager = factory.consumer_group_manager() group_manager.remove_consumer_from_groups(consumer_id) factory.consumer_history_manager().record_event(consumer_id, 'consumer_unregistered')
def test_fetch_by_type2(self): # Setup self.populate() manager = factory.consumer_profile_manager() manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.update(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) # Test profile = manager.get_profile(self.CONSUMER_ID, self.TYPE_2) # Verify self.assertTrue(profile is not None) self.assertEquals(profile['consumer_id'], self.CONSUMER_ID) self.assertEquals(profile['content_type'], self.TYPE_2) self.assertEquals(profile['profile'], self.PROFILE_2) expected_hash = UnitProfile.calculate_hash(self.PROFILE_2) self.assertEqual(profile['profile_hash'], expected_hash)
def test_fetch_by_type1(self): # Setup self.populate() manager = factory.consumer_profile_manager() manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.update(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) # Test profile = manager.get_profile(self.CONSUMER_ID, self.TYPE_1) # Verify self.assertTrue(profile is not None) self.assertEquals(profile['consumer_id'], self.CONSUMER_ID) self.assertEquals(profile['content_type'], self.TYPE_1) self.assertEquals(profile['profile'], self.PROFILE_1) expected_hash = UnitProfile.calculate_hash(self.PROFILE_1) self.assertEqual(profile['profile_hash'], expected_hash)
def __profiled_consumer(self, consumer_id): """ Get a profiler consumer model object. :param id: A consumer ID. :type id: str :return: A populated profiler consumer model object. :rtype: L{ProfiledConsumer} """ profiles = {} manager = managers.consumer_profile_manager() for p in manager.get_profiles(consumer_id): typeid = p['content_type'] profile = p['profile'] profiles[typeid] = profile return ProfiledConsumer(consumer_id, profiles)
def test_delete(self): # Setup self.populate() manager = factory.consumer_profile_manager() manager.create(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.create(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) profiles = manager.get_profiles(self.CONSUMER_ID) self.assertEquals(len(profiles), 2) # Test path = '/v2/consumers/%s/profiles/%s/' % (self.CONSUMER_ID, self.TYPE_1) status, body = self.delete(path) profiles = manager.get_profiles(self.CONSUMER_ID) self.assertEquals(len(profiles), 1) profile = manager.get_profile(self.CONSUMER_ID, self.TYPE_2) self.assertTrue(profile is not None)
def DELETE(self, consumer_id, content_type): """ Delete an association between the specified consumer and profile. Designed to be idempotent. @param consumer_id: A consumer ID. @type consumer_id: str @param content_type: The content type ID. @type content_type: str @return: The deleted model object: {consumer_id:<str>, content_type:<str>, profile:<dict>} Or, None if bind does not exist. @rtype: dict """ manager = managers.consumer_profile_manager() return self.ok(manager.delete(consumer_id, content_type))
def __profiled_consumer(self, consumer_id): """ Get a profiler consumer model object. @param id: A consumer ID. @type id: str @return: A populated profiler consumer model object. @rtype: L{ProfiledConsumer} """ profiles = {} manager = managers.consumer_profile_manager() for p in manager.get_profiles(consumer_id): typeid = p['content_type'] profile = p['profile'] profiles[typeid] = profile return ProfiledConsumer(consumer_id, profiles)
def test_consumer_deleted(self): # Setup self.populate() collection = UnitProfile.get_collection() manager = factory.consumer_profile_manager() manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.update(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) collection = UnitProfile.get_collection() cursor = collection.find({'consumer_id': self.CONSUMER_ID}) profiles = list(cursor) self.assertEquals(len(profiles), 2) # Test manager.consumer_deleted(self.CONSUMER_ID) cursor = collection.find() profiles = list(cursor) self.assertEquals(len(profiles), 0)
def test_consumer_deleted(self): # Setup self.populate() collection = UnitProfile.get_collection() manager = factory.consumer_profile_manager() manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.update(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) collection = UnitProfile.get_collection() cursor = collection.find({"consumer_id": self.CONSUMER_ID}) profiles = list(cursor) self.assertEquals(len(profiles), 2) # Test manager.consumer_deleted(self.CONSUMER_ID) cursor = collection.find() profiles = list(cursor) self.assertEquals(len(profiles), 0)
def GET(self, consumer_id): """ Get all profiles associated with a consumer. @param consumer_id: The consumer ID. @type consumer_id: str @return: A list of profiles: profile is: {consumer_id:<str>, content_type:<str>, profile:<dict>} @return: list """ # Check that the consumer exists and raise a MissingResource exception, in case it doesn't. managers.consumer_manager().get_consumer(consumer_id) manager = managers.consumer_profile_manager() profiles = manager.get_profiles(consumer_id) profiles = [serialization.consumer.profile(p) for p in profiles] return self.ok(profiles)
def test_create(self): # Setup self.populate() # Test manager = factory.consumer_profile_manager() manager.create(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) # Verify collection = UnitProfile.get_collection() cursor = collection.find({'consumer_id': self.CONSUMER_ID}) profiles = list(cursor) self.assertEquals(len(profiles), 1) self.assertEquals(profiles[0]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[0]['content_type'], self.TYPE_1) self.assertEquals(profiles[0]['profile'], self.PROFILE_1) expected_hash = UnitProfile.calculate_hash(self.PROFILE_1) self.assertEqual(profiles[0]['profile_hash'], expected_hash)
def _profiled_consumer(consumer_id): """ Get a profiler consumer model object. :param consumer_id: A consumer ID. :type consumer_id: str :return: A populated profiler consumer model object. :rtype: pulp.plugins.model.Consumer """ profiles = {} manager = managers.consumer_profile_manager() for p in manager.get_profiles(consumer_id): typeid = p['content_type'] profile = p['profile'] profiles[typeid] = profile return ProfiledConsumer(consumer_id, profiles)
def test_post(self): # Setup self.populate() # Test path = '/v2/consumers/%s/profiles/' % self.CONSUMER_ID body = dict(content_type=self.TYPE_1, profile=self.PROFILE_1) status, body = self.post(path, body) # Verify self.assertEqual(status, 201) self.assertEqual(body['consumer_id'], self.CONSUMER_ID) self.assertEqual(body['content_type'], self.TYPE_1) self.assertEqual(body['profile'], self.PROFILE_1) manager = factory.consumer_profile_manager() profile = manager.get_profile(self.CONSUMER_ID, self.TYPE_1) for key in ('consumer_id', 'content_type', 'profile'): self.assertEqual(body[key], profile[key])
def _get_consumer_profile_map(consumer_ids): """ Create a consumer-profile map: {consumer id: {'profiles': list of tuples with profile details, 'all_profiles_hash': hash of all consumer's profile_hashes}} :param consumer_ids: consumers for which applicability needs to be regenerated :type consumer_ids: list :return: consumer-profile map described above :rtype: dict """ consumer_profile_manager = managers.consumer_profile_manager() consumer_ids = list(set(consumer_ids)) # Get all unit profiles associated with given consumers unit_profile_criteria = Criteria( filters={'consumer_id': { '$in': consumer_ids }}, fields=['consumer_id', 'profile_hash', 'content_type', 'id']) all_unit_profiles = consumer_profile_manager.find_by_criteria( unit_profile_criteria) consumer_profiles_map = {} for unit_profile in all_unit_profiles: profile_hash = unit_profile['profile_hash'] content_type = unit_profile['content_type'] consumer_id = unit_profile['consumer_id'] profile_id = unit_profile['id'] profile_tuple = (profile_hash, content_type, profile_id) # Add this tuple to the list of profile tuples for a consumer consumer_profiles_map.setdefault(consumer_id, {}) consumer_profiles_map[consumer_id].setdefault( 'profiles', []).append(profile_tuple) # Calculate and add all_profiles_hash to the map for consumer_id, pdata in consumer_profiles_map.items(): profile_hashes = [pr_hash for pr_hash, _, _ in pdata['profiles']] all_profiles_hash = _calculate_all_profiles_hash(profile_hashes) consumer_profiles_map[consumer_id][ 'all_profiles_hash'] = all_profiles_hash return consumer_profiles_map
def test_update_with_consumer_history(self): # Setup self.populate() manager = factory.consumer_profile_manager() manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) # Test manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_2) # Verify collection = ConsumerHistoryEvent.get_collection() history = collection.find_one({'consumer_id': self.CONSUMER_ID, 'type': 'unit_profile_changed', 'details': {'profile_content_type': self.TYPE_1}}) self.assertTrue(history is not None) self.assertEqual(history['consumer_id'], self.CONSUMER_ID) self.assertEqual(history['type'], 'unit_profile_changed') self.assertEqual(history['originator'], 'SYSTEM') self.assertEqual(history['details'], {'profile_content_type': self.TYPE_1})
def test_get_profiles(self): # Setup self.populate() # Test manager = factory.consumer_profile_manager() manager.create(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.create(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) profiles = manager.get_profiles(self.CONSUMER_ID) # Verify profiles = sorted(profiles) self.assertEquals(len(profiles), 2) self.assertEquals(profiles[0]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[0]['content_type'], self.TYPE_1) self.assertEquals(profiles[0]['profile'], self.PROFILE_1) self.assertEquals(profiles[1]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[1]['content_type'], self.TYPE_2) self.assertEquals(profiles[1]['profile'], self.PROFILE_2)
def POST(self, consumer_id): """ Associate a profile with a consumer by content type ID. @param consumer_id: A consumer ID. @type consumer_id: str @return: The created model object: {consumer_id:<str>, content_type:<str>, profile:<dict>} @rtype: dict """ body = self.params() content_type = body.get('content_type') profile = body.get('profile') manager = managers.consumer_profile_manager() new_profile = manager.create(consumer_id, content_type, profile) link = serialization.link.child_link_obj(consumer_id, content_type) new_profile.update(link) return self.created(link['_href'], new_profile)
def delete(self, request, consumer_id, content_type): """ Delete an association between the specified consumer and profile. Designed to be idempotent. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param consumer_id: A consumer ID. :type consumer_id: str :param content_type: The content type ID. :type content_type: str :return: An empty response :rtype: django.http.HttpResponse """ manager = factory.consumer_profile_manager() response = manager.delete(consumer_id, content_type) return generate_json_response(response)
def get(self, request, consumer_id, content_type): """ Get profile by content type associated with consumer. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param consumer_id: The consumer ID. :type consumer_id: str :param content_type: The content type :type consumer_id: str :return: Response representing consumer's profile :rtype: django.http.HttpResponse """ manager = factory.consumer_profile_manager() profile = manager.get_profile(consumer_id, content_type) add_link_profile(profile) return generate_json_response_with_pulp_encoder(profile)
def test_multiple_types(self): # Setup self.populate() collection = UnitProfile.get_collection() # Test manager = factory.consumer_profile_manager() manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.update(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) # Verify cursor = collection.find({'consumer_id': self.CONSUMER_ID}) cursor.sort('content_type', pymongo.ASCENDING) profiles = list(cursor) # Type_1 self.assertEquals(len(profiles), 2) self.assertEquals(profiles[0]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[0]['content_type'], self.TYPE_1) self.assertEquals(profiles[0]['profile'], self.PROFILE_1) self.assertEquals(profiles[1]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[1]['content_type'], self.TYPE_2) self.assertEquals(profiles[1]['profile'], self.PROFILE_2)
def populate(self, bindings=False, profiles=False): if bindings: manager = factory.repo_manager() manager.create_repo(self.REPO_ID) manager = factory.repo_distributor_manager() manager.add_distributor(self.REPO_ID, self.DISTRIBUTOR_TYPE_ID, {}, True, distributor_id=self.DISTRIBUTOR_ID) for consumer_id in self.CONSUMER_IDS: manager = factory.consumer_manager() manager.register(consumer_id) if bindings: manager = factory.consumer_bind_manager() manager.bind(consumer_id, self.REPO_ID, self.DISTRIBUTOR_ID, self.NOTIFY_AGENT, self.BINDING_CONFIG) if profiles: manager = factory.consumer_profile_manager() for consumer_id in self.CONSUMER_IDS: manager.create(consumer_id, 'rpm', self.PROFILE)
def get(self, request, consumer_id): """ Get all profiles associated with a consumer. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param consumer_id: A consumer ID. :type consumer_id: str :return: Response representing list of profiles :rtype: django.http.HttpResponse """ # Check that the consumer exists and raise a MissingResource exception, in case it doesn't. factory.consumer_manager().get_consumer(consumer_id) manager = factory.consumer_profile_manager() profiles = manager.get_profiles(consumer_id) for consumer_profile in profiles: add_link_profile(consumer_profile) return generate_json_response_with_pulp_encoder(profiles)
def PUT(self, consumer_id, content_type): """ Update the association of a profile with a consumer by content type ID. @param consumer_id: A consumer ID. @type consumer_id: str @param content_type: A content unit type ID. @type content_type: str @return: The updated model object: {consumer_id:<str>, content_type:<str>, profile:<dict>} @rtype: dict """ body = self.params() profile = body.get('profile') manager = managers.consumer_profile_manager() consumer = manager.update(consumer_id, content_type, profile) link = serialization.link.child_link_obj(consumer_id, content_type) consumer.update(link) return self.ok(consumer)
def test_delete(self): # Setup self.populate() collection = UnitProfile.get_collection() manager = factory.consumer_profile_manager() manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.update(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) collection = UnitProfile.get_collection() cursor = collection.find({'consumer_id': self.CONSUMER_ID}) profiles = list(cursor) self.assertEquals(len(profiles), 2) # Test manager.delete(self.CONSUMER_ID, self.TYPE_1) # Verify collection = UnitProfile.get_collection() cursor = collection.find({'consumer_id': self.CONSUMER_ID}) profiles = list(cursor) self.assertEquals(len(profiles), 1) self.assertEquals(profiles[0]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[0]['content_type'], self.TYPE_2) self.assertEquals(profiles[0]['profile'], self.PROFILE_2) expected_hash = UnitProfile.calculate_hash(self.PROFILE_2) self.assertEqual(profiles[0]['profile_hash'], expected_hash)
def put(self, request, consumer_id, content_type): """ Update the association of a profile with a consumer by content type ID. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param consumer_id: A consumer ID. :type consumer_id: str :param content_type: A content unit type ID. :type content_type: str :return: Response representing the updated profile :rtype: django.http.HttpResponse """ body = request.body_as_json profile = body.get('profile') manager = factory.consumer_profile_manager() consumer = manager.update(consumer_id, content_type, profile) add_link_profile(consumer) return generate_json_response_with_pulp_encoder(consumer)