예제 #1
0
    def create(self, request, path):

        serializer = CollectionUploadSerializer(data=request.data,
                                                context={'request': request})
        serializer.is_valid(raise_exception=True)

        data = serializer.validated_data
        filename = data['filename']

        try:
            namespace = models.Namespace.objects.get(name=filename.namespace)
        except models.Namespace.DoesNotExist:
            raise ValidationError('Namespace "{0}" does not exist.'.format(
                filename.namespace))

        self.check_object_permissions(request, namespace)

        try:
            response = super(CollectionUploadViewSet,
                             self).create(request, path)
        except ValidationError:
            log.exception(
                'Failed to publish artifact %s (namespace=%s, sha256=%s)',  # noqa
                data['file'].name,
                namespace,
                data.get('sha256'))
            raise

        task_href = response.data['task']

        # icky, have to extract task id from the task_href url
        task_id = task_href.strip("/").split("/")[-1]

        task_detail = Task.objects.get(pk=task_id)

        models.CollectionImport.objects.create(
            task_id=task_detail.pulp_id,
            created_at=task_detail.pulp_created,
            namespace=namespace,
            name=data['filename'].name,
            version=data['filename'].version,
        )

        # TODO: CollectionImport.get_absolute_url() should be able to generate this, but
        #       it needs the  repo/distro base_path for the <path> part of url
        import_obj_url = reverse("galaxy:api:content:v3:collection-import",
                                 kwargs={
                                     'pk': str(task_detail.pulp_id),
                                     'path': path
                                 })

        log.debug('import_obj_url: %s', import_obj_url)
        return Response(data={'task': import_obj_url},
                        status=response.status_code)
예제 #2
0
    def post(self, request, *args, **kwargs):
        metrics.collection_import_attempts.inc()
        serializer = CollectionUploadSerializer(data=request.data,
                                                context={'request': request})
        serializer.is_valid(raise_exception=True)

        data = serializer.validated_data
        filename = data['filename']

        try:
            namespace = models.Namespace.objects.get(name=filename.namespace)
        except models.Namespace.DoesNotExist:
            raise ValidationError('Namespace "{0}" does not exist.'.format(
                filename.namespace))

        self.check_object_permissions(request, namespace)
        api = pulp.get_client()
        url = '{host}/{prefix}/{path}'.format(
            host=api.configuration.host,
            path='v3/artifacts/collections/',
            prefix=settings.X_PULP_API_PREFIX)
        headers = {}
        headers.update(api.default_headers)
        headers.update({'Content-Type': 'multipart/form-data'})

        api.update_params_for_auth(headers, tuple(),
                                   api.configuration.auth_settings())

        post_params = self._prepare_post_params(data)
        try:
            upload_response = api.request(
                'POST',
                url,
                headers=headers,
                post_params=post_params,
            )
        except galaxy_pulp.ApiException:
            log.exception(
                'Failed to publish artifact %s (namespace=%s, sha256=%s) to pulp at url=%s',  # noqa
                data['file'].name,
                namespace,
                data.get('sha256'),
                url)
            raise

        upload_response_data = json.loads(upload_response.data)

        task_detail = api.call_api(
            upload_response_data['task'],
            'GET',
            auth_settings=['BasicAuth'],
            response_type='CollectionImport',
            _return_http_data_only=True,
        )

        log.info(
            'Publishing of artifact %s to namespace=%s by user=%s created pulp import task_id=%s',  # noqa
            data['file'].name,
            namespace,
            request.user,
            task_detail.id)

        import_obj = models.CollectionImport.objects.create(
            task_id=task_detail.id,
            created_at=task_detail.created_at,
            namespace=namespace,
            name=data['filename'].name,
            version=data['filename'].version,
        )

        metrics.collection_import_successes.inc()
        return Response(data={'task': import_obj.get_absolute_url()},
                        status=upload_response.status)
예제 #3
0
    def _get_data(self, request):
        serializer = CollectionUploadSerializer(data=request.data,
                                                context={'request': request})
        serializer.is_valid(raise_exception=True)

        return serializer.validated_data