-
Notifications
You must be signed in to change notification settings - Fork 116
/
repository.py
252 lines (208 loc) · 8.45 KB
/
repository.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import itertools
from gettext import gettext as _
from django_filters import Filter
from django_filters.rest_framework import DjangoFilterBackend, filters
from drf_spectacular.utils import extend_schema
from rest_framework import mixins, serializers
from rest_framework.decorators import action
from rest_framework.filters import OrderingFilter
from pulpcore.app import tasks
from pulpcore.app.models import (
Content,
Remote,
Repository,
RepositoryContent,
RepositoryVersion,
)
from pulpcore.app.response import OperationPostponedResponse
from pulpcore.app.serializers import (
AsyncOperationResponseSerializer,
RemoteSerializer,
RepairSerializer,
RepositorySerializer,
RepositoryVersionSerializer,
)
from pulpcore.app.viewsets import (
AsyncRemoveMixin,
AsyncUpdateMixin,
BaseFilterSet,
NamedModelViewSet,
)
from pulpcore.app.viewsets.base import DATETIME_FILTER_OPTIONS, NAME_FILTER_OPTIONS
from pulpcore.app.viewsets.custom_filters import IsoDateTimeFilter
from pulpcore.tasking.tasks import enqueue_with_reservation
class RepositoryFilter(BaseFilterSet):
name = filters.CharFilter()
class Meta:
model = Repository
fields = {"name": NAME_FILTER_OPTIONS}
class ImmutableRepositoryViewSet(
NamedModelViewSet,
mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.ListModelMixin,
AsyncRemoveMixin,
):
"""
An immutable repository ViewSet that does not allow the usage of the methods PATCH and PUT.
"""
queryset = Repository.objects.all().order_by("name")
serializer_class = RepositorySerializer
endpoint_name = "repositories"
router_lookup = "repository"
filterset_class = RepositoryFilter
class RepositoryViewSet(ImmutableRepositoryViewSet, AsyncUpdateMixin):
"""
A ViewSet for an ordinary repository.
"""
class RepositoryVersionContentFilter(Filter):
"""
Filter used to get the repository versions where some given content can be found.
Given a content_href, this filter will:
1. Get the RepositoryContent that the content can be found in
2. Get a list of version_added and version_removed where the content was
changed on the repository
3. Calculate and return the versions that the content can be found on
"""
def __init__(self, *args, **kwargs):
kwargs.setdefault("help_text", _("Content Unit referenced by HREF"))
super().__init__(*args, **kwargs)
def filter(self, qs, value):
"""
Args:
qs (django.db.models.query.QuerySet): The RepositoryVersion Queryset
value (string): of content href to filter
Returns:
Queryset of the RepositoryVersions containing the specified content
"""
if value is None:
# user didn't supply a value
return qs
if not value:
raise serializers.ValidationError(detail=_("No value supplied for content filter"))
# Get the content object from the content_href
content = NamedModelViewSet.get_resource(value, Content)
# Get the repository from the parent request.
repository_pk = self.parent.request.parser_context["kwargs"]["repository_pk"]
repository = Repository.objects.get(pk=repository_pk)
repository_content_set = RepositoryContent.objects.filter(
content=content, repository=repository
)
# Get the sorted list of version_added and version_removed.
version_added = list(repository_content_set.values_list("version_added__number", flat=True))
# None values have to be filtered out from version_removed,
# in order for zip_longest to pass it a default fillvalue
version_removed = list(
filter(
None.__ne__,
repository_content_set.values_list("version_removed__number", flat=True),
)
)
# The range finding should work as long as both lists are sorted
# Why it works: https://gist.github.com/werwty/6867f83ae5adbae71e452c28ecd9c444
version_added.sort()
version_removed.sort()
# Match every version_added to a version_removed, if len(version_removed)
# is shorter than len(version_added), pad out the remaining space with the current
# repository version +1 (the +1 is to the current version gets included when we
# calculate range)
version_tuples = itertools.zip_longest(
version_added, version_removed, fillvalue=repository.next_version
)
# Get the ranges between paired version_added and version_removed to get all
# the versions the content is present in.
versions = [list(range(added, removed)) for (added, removed) in version_tuples]
# Flatten the list of lists
versions = list(itertools.chain.from_iterable(versions))
return qs.filter(number__in=versions)
class RepositoryVersionFilter(BaseFilterSet):
# e.g.
# /?number=4
# /?number__range=4,6
# /?pulp_created__gte=2018-04-12T19:45
# /?pulp_created__range=2018-04-12T19:45,2018-04-13T20:00
# /?content=/pulp/api/v3/content/file/fb8ad2d0-03a8-4e36-a209-77763d4ed16c/
number = filters.NumberFilter()
pulp_created = IsoDateTimeFilter()
content = RepositoryVersionContentFilter()
content__in = RepositoryVersionContentFilter(field_name="content", lookup_expr="in")
class Meta:
model = RepositoryVersion
fields = {
"number": ["exact", "lt", "lte", "gt", "gte", "range"],
"pulp_created": DATETIME_FILTER_OPTIONS,
}
class RepositoryVersionViewSet(
NamedModelViewSet, mixins.RetrieveModelMixin, mixins.ListModelMixin, mixins.DestroyModelMixin
):
endpoint_name = "versions"
nest_prefix = "repositories"
router_lookup = "version"
lookup_field = "number"
parent_viewset = RepositoryViewSet
parent_lookup_kwargs = {"repository_pk": "repository__pk"}
serializer_class = RepositoryVersionSerializer
queryset = RepositoryVersion.objects.exclude(complete=False)
filterset_class = RepositoryVersionFilter
filter_backends = (OrderingFilter, DjangoFilterBackend)
ordering = ("-number",)
@extend_schema(
description="Trigger an asynchronous task to delete a repository version.",
responses={202: AsyncOperationResponseSerializer},
)
def destroy(self, request, repository_pk, number):
"""
Queues a task to handle deletion of a RepositoryVersion
"""
version = self.get_object()
if version.number == 0:
raise serializers.ValidationError(detail=_("Cannot delete repository version 0."))
async_result = enqueue_with_reservation(
tasks.repository.delete_version, [version.repository], kwargs={"pk": version.pk}
)
return OperationPostponedResponse(async_result, request)
@extend_schema(
description="Trigger an asynchronous task to repair a repository version.",
responses={202: AsyncOperationResponseSerializer},
)
@action(detail=True, methods=["post"])
def repair(self, request, repository_pk, number):
"""
Queues a task to repair corrupted artifacts corresponding to a RepositoryVersion
"""
version = self.get_object()
serializer = RepairSerializer(data=request.data)
serializer.is_valid()
verify_checksums = serializer.validated_data["verify_checksums"]
async_result = enqueue_with_reservation(
tasks.repository.repair_version,
[version.repository],
args=[version.pk, verify_checksums],
)
return OperationPostponedResponse(async_result, request)
class RemoteFilter(BaseFilterSet):
"""
Plugin remote filter should:
- inherit from this class
- add any specific filters if needed
- define a `Meta` class which should:
- specify a plugin remote model for which filter is defined
- extend `fields` with specific ones
"""
name = filters.CharFilter()
pulp_last_updated = IsoDateTimeFilter()
class Meta:
model = Remote
fields = {"name": NAME_FILTER_OPTIONS, "pulp_last_updated": DATETIME_FILTER_OPTIONS}
class RemoteViewSet(
NamedModelViewSet,
mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.ListModelMixin,
AsyncUpdateMixin,
AsyncRemoveMixin,
):
endpoint_name = "remotes"
serializer_class = RemoteSerializer
queryset = Remote.objects.all()
filterset_class = RemoteFilter