def post(self, request): """ Creates an async task to regenerate content applicability data for given consumers. body {consumer_criteria:<dict>} :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :raises MissingValue: if some parameters are missing :raises InvalidValue: if some parameters are invalid :raises OperationPostponed: when an async operation is performed. """ body = request.body_as_json consumer_criteria = body.get('consumer_criteria', None) if consumer_criteria is None: raise MissingValue('consumer_criteria') try: consumer_criteria = Criteria.from_client_input(consumer_criteria) except: raise InvalidValue('consumer_criteria') task_tags = [tags.action_tag('content_applicability_regeneration')] async_result = regenerate_applicability_for_consumers.apply_async_with_reservation( tags.RESOURCE_REPOSITORY_PROFILE_APPLICABILITY_TYPE, tags.RESOURCE_ANY_ID, (consumer_criteria.as_dict(), ), tags=task_tags) raise OperationPostponed(async_result)
def create_schedule(cls, action, consumer_id, units, options, schedule, failure_threshold=None, enabled=True): """ Creates a new schedule for a consumer action :param action: a unique identified for an action, one of UNIT_INSTALL_ACTION, UNIT_UPDATE_ACTION, UNIT_UNINSTALL_ACTION :type action: basestring :param consumer_id: a unique ID for a consumer :type consumer_id: basestring :param units: A list of content units to be installed, each as a dict in the form: { type_id:<str>, unit_key:<dict> } :type units: list :param options: a dictionary that will be passed to the action-appropriate task as the "options" argument :type options: dict :param schedule: ISO8601 string representation of the schedule :type schedule: basestring :param failure_threshold: optional positive integer indicating how many times this schedule's execution can fail before being automatically disabled. :type failure_threshold: int or NoneType :param enabled: boolean indicating if this schedule should be actively loaded and executed by the scheduler. Defaults to True. :type enabled: bool :return: instance of the new ScheduledCal :rtype: pulp.server.db.models.dispatch.ScheduledCall :raise: pulp.server.exceptions.MissingValue """ cls._validate_consumer(consumer_id) utils.validate_initial_schedule_options(schedule, failure_threshold, enabled) if not units: raise MissingValue(['units']) task = ACTIONS_TO_TASKS[action] args = [consumer_id] kwargs = {'units': units, 'options': options} resource = Consumer.build_resource_tag(consumer_id) schedule = ScheduledCall(schedule, task, args=args, kwargs=kwargs, resource=resource, failure_threshold=failure_threshold, enabled=enabled) schedule.save() return schedule
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 POST(self, repo_group_id): params = self.params() distributor_id = params.get('id', None) overrides = params.get('override_config', None) if distributor_id is None: raise MissingValue(['id']) publish_manager = managers_factory.repo_group_publish_manager() tags = [resource_tag(dispatch_constants.RESOURCE_REPOSITORY_GROUP_TYPE, repo_group_id), resource_tag(dispatch_constants.RESOURCE_REPOSITORY_GROUP_DISTRIBUTOR_TYPE, distributor_id), action_tag('publish') ] weight = pulp_config.config.getint('tasks', 'publish_weight') call_request = CallRequest(publish_manager.publish, args=[repo_group_id, distributor_id], kwargs={'publish_config_override' : overrides}, tags=tags, weight=weight, archive=True) call_request.updates_resource(dispatch_constants.RESOURCE_REPOSITORY_GROUP_TYPE, repo_group_id) call_request.updates_resource(dispatch_constants.RESOURCE_REPOSITORY_GROUP_DISTRIBUTOR_TYPE, distributor_id) call_request.add_life_cycle_callback(dispatch_constants.CALL_ENQUEUE_LIFE_CYCLE_CALLBACK, publish_manager.prep_publish) return execution.execute_async(self, call_request)
def POST(self, repo_group_id): params = self.params() distributor_id = params.get('id', None) overrides = params.get('override_config', None) if distributor_id is None: raise MissingValue(['id']) # If a repo group does not exist, get_group raises a MissingResource exception manager = managers_factory.repo_group_query_manager() manager.get_group(repo_group_id) task_tags = [ tags.resource_tag(tags.RESOURCE_REPOSITORY_GROUP_TYPE, repo_group_id), tags.resource_tag(tags.RESOURCE_REPOSITORY_GROUP_DISTRIBUTOR_TYPE, distributor_id), tags.action_tag('publish') ] async_result = publish.apply_async_with_reservation( tags.RESOURCE_REPOSITORY_GROUP_TYPE, repo_group_id, args=[repo_group_id, distributor_id], kwargs={'publish_config_override': overrides}, tags=task_tags) raise OperationPostponed(async_result)
def POST(self, consumer_id): consumer_manager = managers.consumer_manager() consumer_manager.get_consumer(consumer_id) schedule_data = self.params() units = schedule_data.pop('units', None) uninstall_options = {'options': schedule_data.pop('options', {})} if not units: raise MissingValue(['units']) schedule_manager = managers.schedule_manager() weight = pulp_config.config.getint('tasks', 'create_weight') tags = [resource_tag(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id), action_tag('create_unit_uninstall_schedule')] call_request = CallRequest(schedule_manager.create_unit_uninstall_schedule, [consumer_id, units, uninstall_options, schedule_data], weight=weight, tags=tags, archive=True) call_request.reads_resource(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id) schedule_id = execution.execute_sync(call_request) scheduler = dispatch_factory.scheduler() scheduled_call = scheduler.get(schedule_id) scheduled_obj = serialization.dispatch.scheduled_unit_management_obj(scheduled_call) scheduled_obj.update(serialization.link.child_link_obj(schedule_id)) return self.created(scheduled_obj['_href'], scheduled_obj)
def validate(cls, config): """ Ensure that any required settings have non-empty values. :param config: config object for the sync :type config: pulp.plugins.config.PluginCallConfiguration :raises MissingValue: if any required sync setting is missing """ missing = [] for key in cls.required_settings: if not config.get(key): missing.append(key) if missing: raise MissingValue(missing)
def POST(self, repo_group_id): params = self.params() distributor_id = params.get('id', None) overrides = params.get('override_config', None) if distributor_id is None: raise MissingValue(['id']) task_tags = [tags.resource_tag(tags.RESOURCE_REPOSITORY_GROUP_TYPE, repo_group_id), tags.resource_tag(tags.RESOURCE_REPOSITORY_GROUP_DISTRIBUTOR_TYPE, distributor_id), tags.action_tag('publish')] async_result = publish.apply_async_with_reservation( tags.RESOURCE_REPOSITORY_GROUP_TYPE, repo_group_id, args=[repo_group_id, distributor_id], kwargs={'publish_config_override' : overrides}, tags=task_tags) raise OperationPostponed(async_result)
def update(id, delta): """ Updates metadata about the given consumer. Only the following fields may be updated through this call: * display-name * description * notes Other fields found in delta will be ignored. :param id: identifies the consumer :type id: str :param delta: list of attributes and their new values to change :type delta: dict :return: document from the database representing the updated consumer :rtype: dict :raises MissingResource: if there is no consumer with given id :raises InvalidValue: if notes are provided in unacceptable (non-dict) form :raises MissingValue: if delta provided is empty """ consumer = ConsumerManager.get_consumer(id) if delta is None: _logger.exception('Missing delta when updating consumer [%s]' % id) raise MissingValue('delta') if 'notes' in delta: if delta['notes'] is not None and not isinstance( delta['notes'], dict): raise InvalidValue("delta['notes']") else: consumer['notes'] = update_notes(consumer['notes'], delta['notes']) for key in ('display_name', 'description', 'rsa_pub'): if key in delta: consumer[key] = delta[key] Consumer.get_collection().save(consumer, safe=True) return consumer
def POST(self): """ Creates an async task to regenerate content applicability data for given consumers. body {consumer_criteria:<dict>} """ body = self.params() consumer_criteria = body.get('consumer_criteria', None) if consumer_criteria is None: raise MissingValue('consumer_criteria') try: consumer_criteria = Criteria.from_client_input(consumer_criteria) except: raise InvalidValue('consumer_criteria') task_tags = [tags.action_tag('content_applicability_regeneration')] async_result = regenerate_applicability_for_consumers.apply_async_with_reservation( tags.RESOURCE_REPOSITORY_PROFILE_APPLICABILITY_TYPE, tags.RESOURCE_ANY_ID, (consumer_criteria.as_dict(),), tags=task_tags) raise OperationPostponed(async_result)
def update(self, id, delta): """ Updates metadata about the given consumer. Only the following fields may be updated through this call: * display-name * description * notes Other fields found in delta will be ignored. @param id: identifies the consumer @type id: str @param delta: list of attributes and their new values to change @type delta: dict @raises MissingResource: if there is no consumer with given id @raises InvalidValue: if notes are provided in unacceptable (non-dict) form @raises MissingValue: if delta provided is empty """ consumer = self.get_consumer(id) if delta is None: _LOG.exception('Missing delta when updating consumer [%s]' % id) raise MissingValue('delta') if 'notes' in delta: if delta['notes'] is not None and not isinstance(delta['notes'], dict): raise InvalidValue("delta['notes']") else: consumer['notes'] = update_notes(consumer['notes'], delta['notes']) if 'display_name' in delta: consumer['display_name'] = delta['display_name'] if 'description' in delta: consumer['description'] = delta['description'] Consumer.get_collection().save(consumer, safe=True) return consumer
def post(self, request): """ Create a consumer and return a serialized object containing just created consumer. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :raises MissingValue: if ID is not provided :return: Response containing the consumer :rtype: django.http.HttpResponse """ params = request.body_as_json consumer_id = params.get('id') if consumer_id is None: raise MissingValue(['id']) display_name = params.get('display_name') description = params.get('description') notes = params.get('notes') rsa_pub = params.get('rsa_pub') manager = factory.consumer_manager() consumer, certificate = manager.register( consumer_id, display_name=display_name, description=description, notes=notes, rsa_pub=rsa_pub) link = add_link(consumer) document = { 'consumer': consumer, 'certificate': certificate } response = generate_json_response_with_pulp_encoder(document) redirect_response = generate_redirect_response(response, link['_href']) return redirect_response
def post(self, request, consumer_id, action): """ Install/update/uninstall content unit/s on the consumer. :param request: WSGI request object :type request: django.core.handlers.wsgi.WSGIRequest :param consumer_id: A consumer ID. :type consumer_id: str :param action: type of action to perform :type action: str :raises MissingResource: if consumer id does not exist :raises MissingValue: if some required values are missing :raises InvalidValue: if units are not a list of dictionaries """ method = getattr(self, action, None) if method: try: factory.consumer_manager().get_consumer(consumer_id) except MissingResource: raise MissingResource(consumer_id=consumer_id) else: body = request.body_as_json missing_params = [] units = body.get('units') if units is None: missing_params.append('units') elif not isinstance(units, list): raise InvalidValue('Units must be a list of dictionaries') options = body.get('options') if options is None: missing_params.append('options') if missing_params: raise MissingValue(missing_params) return method(request, consumer_id, units, options) else: return HttpResponseBadRequest('bad request')
def update(consumer_id, content_type, profile): """ Update a unit profile. Created if not already exists. :param consumer_id: uniquely identifies the consumer. :type consumer_id: str :param content_type: The profile (content) type ID. :type content_type: str :param profile: The unit profile :type profile: object """ consumer = factory.consumer_manager().get_consumer(consumer_id) try: profiler, config = plugin_api.get_profiler_by_type(content_type) except plugin_exceptions.PluginNotFound: # Not all profile types have a type specific profiler, so let's use the baseclass # Profiler profiler, config = (Profiler(), {}) # Allow the profiler a chance to update the profile before we save it if profile is None: raise MissingValue('profile') profile = profiler.update_profile(consumer, content_type, profile, config) try: p = ProfileManager.get_profile(consumer_id, content_type) p['profile'] = profile # We store the profile's hash anytime the profile gets altered p['profile_hash'] = UnitProfile.calculate_hash(profile) except MissingResource: p = UnitProfile(consumer_id, content_type, profile) collection = UnitProfile.get_collection() collection.save(p) history_manager = factory.consumer_history_manager() history_manager.record_event(consumer_id, 'unit_profile_changed', {'profile_content_type': content_type}) return p
def uninstall(self, consumer_id): """ Uninstall content (units) on a consumer. Expected body: {units:[], options:<dict>} where unit is: {type_id:<str>, unit_key={}} and the options is a dict of uninstall options. @param consumer_id: A consumer ID. @type consumer_id: str """ body = self.params() missing_params = [] units = body.get('units') if units is None: missing_params.append('units') options = body.get('options') if options is None: missing_params.append('options') if len(missing_params) > 0: raise MissingValue(missing_params) agent_manager = managers.consumer_agent_manager() task = agent_manager.uninstall_content(consumer_id, units, options) raise OperationPostponed(TaskResult.from_task_status_dict(task))