This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
/
viewsets.py
96 lines (85 loc) · 3.2 KB
/
viewsets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from django_filters.rest_framework import filters
from drf_yasg.utils import swagger_auto_schema
from rest_framework import mixins
from rest_framework.decorators import action
from pulpcore.app.viewsets.base import DATETIME_FILTER_OPTIONS
from pulpcore.app.viewsets.custom_filters import (
HyperlinkRelatedFilter,
IsoDateTimeFilter
)
from pulpcore.plugin.serializers import AsyncOperationResponseSerializer
from pulpcore.plugin.tasking import enqueue_with_reservation
from pulpcore.plugin.viewsets import (
BaseFilterSet,
NamedModelViewSet,
OperationPostponedResponse,
)
from .constants import PULP_2TO3_MIGRATION_RESOURCE
from .models import MigrationPlan, Pulp2Content
from .serializers import (
MigrationPlanSerializer,
MigrationPlanRunSerializer,
Pulp2ContentSerializer
)
from .tasks.migrate import migrate_from_pulp2
class MigrationPlanViewSet(NamedModelViewSet,
mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin):
"""
MigrationPlan ViewSet.
"""
endpoint_name = 'migration-plans'
queryset = MigrationPlan.objects.all()
serializer_class = MigrationPlanSerializer
@swagger_auto_schema(
operation_summary="Run migration plan",
operation_description="Trigger an asynchronous task to run a migration from Pulp 2.",
responses={202: AsyncOperationResponseSerializer}
)
@action(detail=True, methods=('post',), serializer_class=MigrationPlanRunSerializer)
def run(self, request, pk):
"""Run the migration plan."""
migration_plan = self.get_object()
serializer = MigrationPlanRunSerializer(
data=request.data,
context={'request': request}
)
serializer.is_valid(raise_exception=True)
validate = serializer.validated_data.get('validate', False)
dry_run = serializer.validated_data.get('dry_run', False)
result = enqueue_with_reservation(
migrate_from_pulp2,
[PULP_2TO3_MIGRATION_RESOURCE],
kwargs={
'migration_plan_pk': migration_plan.pk,
'validate': validate,
'dry_run': dry_run
}
)
return OperationPostponedResponse(result, request)
class Pulp2ContentFilter(BaseFilterSet):
"""
Filter for Pulp2Content ViewSet.
"""
pulp2_id = filters.CharFilter()
pulp2_content_type_id = filters.CharFilter()
pulp2_last_updated = IsoDateTimeFilter(field_name='pulp2_last_updated')
pulp3_content = HyperlinkRelatedFilter()
class Meta:
model = Pulp2Content
fields = {
'pulp2_id': ['exact', 'in'],
'pulp2_content_type_id': ['exact', 'in'],
'pulp2_last_updated': DATETIME_FILTER_OPTIONS,
'pulp3_content': ['exact']
}
class Pulp2ContentViewSet(NamedModelViewSet, mixins.RetrieveModelMixin, mixins.ListModelMixin):
"""
ViewSet for Pulp2Content model.
"""
endpoint_name = 'pulp2content'
queryset = Pulp2Content.objects.all()
serializer_class = Pulp2ContentSerializer
filterset_class = Pulp2ContentFilter