showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 1 | from django import http |
| 2 | from autotest_lib.frontend.shared import query_lib, resource_lib |
| 3 | from autotest_lib.frontend.afe import control_file, models, rpc_utils |
| 4 | from autotest_lib.frontend import thread_local |
| 5 | from autotest_lib.client.common_lib import host_protections |
| 6 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 7 | class EntryWithInvalid(resource_lib.InstanceEntry): |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 8 | def put(self): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 9 | if self.instance.invalid: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 10 | raise http.Http404('%s has been deleted' % self.instance) |
| 11 | return super(EntryWithInvalid, self).put() |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 12 | |
| 13 | |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 14 | def delete(self): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 15 | if self.instance.invalid: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 16 | raise http.Http404('%s has already been deleted' % self.instance) |
| 17 | return super(EntryWithInvalid, self).delete() |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 18 | |
| 19 | |
| 20 | class AtomicGroupClass(EntryWithInvalid): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 21 | model = models.AtomicGroup |
| 22 | |
| 23 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 24 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 25 | def from_uri_args(cls, request, ag_name, **kwargs): |
| 26 | return cls(request, models.AtomicGroup.objects.get(name=ag_name)) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 30 | return {'ag_name': self.instance.name} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 31 | |
| 32 | |
| 33 | def short_representation(self): |
| 34 | rep = super(AtomicGroupClass, self).short_representation() |
| 35 | rep['name'] = self.instance.name |
| 36 | return rep |
| 37 | |
| 38 | |
| 39 | def full_representation(self): |
| 40 | rep = super(AtomicGroupClass, self).full_representation() |
| 41 | rep.update({'max_number_of_machines': |
| 42 | self.instance.max_number_of_machines, |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 43 | 'labels': |
| 44 | AtomicLabelTaggingCollection(fixed_entry=self).link()}) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 45 | return rep |
| 46 | |
| 47 | |
| 48 | @classmethod |
| 49 | def create_instance(cls, input_dict, containing_collection): |
| 50 | cls._check_for_required_fields(input_dict, ('name',)) |
| 51 | return models.AtomicGroup.add_object(name=input_dict['name']) |
| 52 | |
| 53 | |
| 54 | def update(self, input_dict): |
| 55 | data = {'max_number_of_machines': |
| 56 | input_dict.get('max_number_of_machines')} |
| 57 | data = input_dict.remove_unspecified_fields(data) |
| 58 | self.instance.update_object(**data) |
| 59 | |
| 60 | |
| 61 | class AtomicGroupClassCollection(resource_lib.Collection): |
| 62 | queryset = models.AtomicGroup.valid_objects.all() |
| 63 | entry_class = AtomicGroupClass |
| 64 | |
| 65 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 66 | class Label(EntryWithInvalid): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 67 | model = models.Label |
| 68 | |
| 69 | @classmethod |
| 70 | def add_query_selectors(cls, query_processor): |
| 71 | query_processor.add_field_selector('name') |
| 72 | query_processor.add_field_selector( |
| 73 | 'is_platform', field='platform', |
| 74 | value_transform=query_processor.read_boolean) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 78 | def from_uri_args(cls, request, label_name, **kwargs): |
| 79 | return cls(request, models.Label.objects.get(name=label_name)) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 80 | |
| 81 | |
| 82 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 83 | return {'label_name': self.instance.name} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 84 | |
| 85 | |
| 86 | def short_representation(self): |
| 87 | rep = super(Label, self).short_representation() |
| 88 | rep.update({'name': self.instance.name, |
| 89 | 'is_platform': bool(self.instance.platform)}) |
| 90 | return rep |
| 91 | |
| 92 | |
| 93 | def full_representation(self): |
| 94 | rep = super(Label, self).full_representation() |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 95 | atomic_group_class = AtomicGroupClass.from_optional_instance( |
| 96 | self._request, self.instance.atomic_group) |
| 97 | rep.update({'atomic_group_class': |
| 98 | atomic_group_class.short_representation(), |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 99 | 'hosts': HostLabelingCollection(fixed_entry=self).link()}) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 100 | return rep |
| 101 | |
| 102 | |
| 103 | @classmethod |
| 104 | def create_instance(cls, input_dict, containing_collection): |
| 105 | cls._check_for_required_fields(input_dict, ('name',)) |
| 106 | return models.Label.add_object(name=input_dict['name']) |
| 107 | |
| 108 | |
| 109 | def update(self, input_dict): |
| 110 | # TODO update atomic group |
| 111 | raise NotImplementedError |
| 112 | |
| 113 | |
| 114 | class LabelCollection(resource_lib.Collection): |
| 115 | queryset = models.Label.valid_objects.all() |
| 116 | entry_class = Label |
| 117 | |
| 118 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 119 | class AtomicLabelTagging(resource_lib.Relationship): |
| 120 | related_classes = {'label': Label, 'atomic_group_class': AtomicGroupClass} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 121 | |
| 122 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 123 | class AtomicLabelTaggingCollection(resource_lib.RelationshipCollection): |
| 124 | entry_class = AtomicLabelTagging |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 125 | |
| 126 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 127 | class User(resource_lib.InstanceEntry): |
| 128 | model = models.User |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 129 | _permitted_methods = ('GET,') |
| 130 | |
| 131 | |
| 132 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 133 | def from_uri_args(cls, request, username, **kwargs): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 134 | if username == '@me': |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 135 | username = models.User.current_user().login |
| 136 | return cls(request, models.User.objects.get(login=username)) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 137 | |
| 138 | |
| 139 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 140 | return {'username': self.instance.login} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 141 | |
| 142 | |
| 143 | def short_representation(self): |
| 144 | rep = super(User, self).short_representation() |
| 145 | rep['username'] = self.instance.login |
| 146 | return rep |
| 147 | |
| 148 | |
| 149 | def full_representation(self): |
| 150 | rep = super(User, self).full_representation() |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 151 | accessible_hosts = HostCollection(self._request) |
| 152 | accessible_hosts.set_query_parameters(accessible_by=self.instance.login) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 153 | rep.update({'jobs': 'TODO', |
| 154 | 'recurring_runs': 'TODO', |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 155 | 'acls': |
| 156 | UserAclMembershipCollection(fixed_entry=self).link(), |
| 157 | 'accessible_hosts': accessible_hosts.link()}) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 158 | return rep |
| 159 | |
| 160 | |
| 161 | class UserCollection(resource_lib.Collection): |
| 162 | _permitted_methods = ('GET',) |
| 163 | queryset = models.User.objects.all() |
| 164 | entry_class = User |
| 165 | |
| 166 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 167 | class Acl(resource_lib.InstanceEntry): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 168 | _permitted_methods = ('GET',) |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 169 | model = models.AclGroup |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 170 | |
| 171 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 172 | def from_uri_args(cls, request, acl_name, **kwargs): |
| 173 | return cls(request, models.AclGroup.objects.get(name=acl_name)) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 174 | |
| 175 | |
| 176 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 177 | return {'acl_name': self.instance.name} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 178 | |
| 179 | |
| 180 | def short_representation(self): |
| 181 | rep = super(Acl, self).short_representation() |
| 182 | rep['name'] = self.instance.name |
| 183 | return rep |
| 184 | |
| 185 | |
| 186 | def full_representation(self): |
| 187 | rep = super(Acl, self).full_representation() |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 188 | rep.update({'users': |
| 189 | UserAclMembershipCollection(fixed_entry=self).link(), |
| 190 | 'hosts': |
| 191 | HostAclMembershipCollection(fixed_entry=self).link()}) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 192 | return rep |
| 193 | |
| 194 | |
| 195 | @classmethod |
| 196 | def create_instance(cls, input_dict, containing_collection): |
| 197 | cls._check_for_required_fields(input_dict, ('name',)) |
| 198 | return models.AclGroup.add_object(name=input_dict['name']) |
| 199 | |
| 200 | |
| 201 | def update(self, input_dict): |
| 202 | pass |
| 203 | |
| 204 | |
| 205 | class AclCollection(resource_lib.Collection): |
| 206 | queryset = models.AclGroup.objects.all() |
| 207 | entry_class = Acl |
| 208 | |
| 209 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 210 | class UserAclMembership(resource_lib.Relationship): |
| 211 | related_classes = {'user': User, 'acl': Acl} |
| 212 | |
| 213 | # TODO: check permissions |
| 214 | # TODO: check for and add/remove "Everyone" |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 215 | |
| 216 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 217 | class UserAclMembershipCollection(resource_lib.RelationshipCollection): |
| 218 | entry_class = UserAclMembership |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 219 | |
| 220 | |
| 221 | class Host(EntryWithInvalid): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 222 | model = models.Host |
| 223 | |
| 224 | @classmethod |
| 225 | def add_query_selectors(cls, query_processor): |
| 226 | query_processor.add_field_selector('hostname') |
| 227 | query_processor.add_field_selector( |
| 228 | 'locked', value_transform=query_processor.read_boolean) |
| 229 | query_processor.add_field_selector( |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 230 | 'locked_by', field='locked_by__login', |
| 231 | doc='Username of user who locked this host, if locked') |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 232 | query_processor.add_field_selector('status') |
| 233 | query_processor.add_field_selector( |
| 234 | 'protection_level', field='protection', |
| 235 | doc='Verify/repair protection level', |
| 236 | value_transform=cls._read_protection) |
| 237 | query_processor.add_field_selector( |
| 238 | 'accessible_by', field='aclgroup__users__login', |
| 239 | doc='Username of user with access to this host') |
| 240 | query_processor.add_related_existence_selector( |
| 241 | 'has_label', models.Label, 'name') |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 242 | |
| 243 | |
| 244 | @classmethod |
| 245 | def _read_protection(cls, protection_input): |
| 246 | return host_protections.Protection.get_value(protection_input) |
| 247 | |
| 248 | |
| 249 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 250 | def from_uri_args(cls, request, hostname, **kwargs): |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 251 | return cls(request, models.Host.objects.get(hostname=hostname)) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 252 | |
| 253 | |
| 254 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 255 | return {'hostname': self.instance.hostname} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 256 | |
| 257 | |
| 258 | def short_representation(self): |
| 259 | rep = super(Host, self).short_representation() |
| 260 | # TODO calling platform() over and over is inefficient |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 261 | platform_rep = (Label.from_optional_instance(self._request, |
| 262 | self.instance.platform()) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 263 | .short_representation()) |
| 264 | rep.update({'hostname': self.instance.hostname, |
| 265 | 'locked': bool(self.instance.locked), |
| 266 | 'status': self.instance.status, |
| 267 | 'platform': platform_rep}) |
| 268 | return rep |
| 269 | |
| 270 | |
| 271 | def full_representation(self): |
| 272 | rep = super(Host, self).full_representation() |
| 273 | protection = host_protections.Protection.get_string( |
| 274 | self.instance.protection) |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 275 | locked_by = (User.from_optional_instance(self._request, |
| 276 | self.instance.locked_by) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 277 | .short_representation()) |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 278 | labels = HostLabelingCollection(fixed_entry=self) |
| 279 | acls = HostAclMembershipCollection(fixed_entry=self) |
| 280 | queue_entries = QueueEntryCollection(self._request) |
| 281 | queue_entries.set_query_parameters(host=self.instance.hostname) |
| 282 | health_tasks = HealthTaskCollection(self._request) |
| 283 | health_tasks.set_query_parameters(host=self.instance.hostname) |
| 284 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 285 | rep.update({'locked_by': locked_by, |
| 286 | 'locked_on': self._format_datetime(self.instance.lock_time), |
| 287 | 'invalid': self.instance.invalid, |
| 288 | 'protection_level': protection, |
| 289 | # TODO make these efficient |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 290 | 'labels': labels.full_representation(), |
| 291 | 'acls': acls.full_representation(), |
| 292 | 'queue_entries': queue_entries.link(), |
| 293 | 'health_tasks': health_tasks.link()}) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 294 | return rep |
| 295 | |
| 296 | |
| 297 | @classmethod |
| 298 | def create_instance(cls, input_dict, containing_collection): |
| 299 | cls._check_for_required_fields(input_dict, ('hostname',)) |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 300 | # include locked here, rather than waiting for update(), to avoid race |
| 301 | # conditions |
| 302 | host = models.Host.add_object(hostname=input_dict['hostname'], |
| 303 | locked=input_dict.get('locked', False)) |
| 304 | return host |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 305 | |
| 306 | def update(self, input_dict): |
| 307 | data = {'locked': input_dict.get('locked'), |
| 308 | 'protection': input_dict.get('protection_level')} |
| 309 | data = input_dict.remove_unspecified_fields(data) |
| 310 | |
| 311 | if 'protection' in data: |
| 312 | data['protection'] = self._read_protection(data['protection']) |
| 313 | |
| 314 | self.instance.update_object(**data) |
| 315 | |
| 316 | if 'platform' in input_dict: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 317 | label = self.resolve_link(input_dict['platform']) .instance |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 318 | if not label.platform: |
| 319 | raise BadRequest('Label %s is not a platform' % label.name) |
| 320 | for label in self.instance.labels.filter(platform=True): |
| 321 | self.instance.labels.remove(label) |
| 322 | self.instance.labels.add(label) |
| 323 | |
| 324 | |
| 325 | class HostCollection(resource_lib.Collection): |
| 326 | queryset = models.Host.valid_objects.all() |
| 327 | entry_class = Host |
| 328 | |
| 329 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 330 | class HostLabeling(resource_lib.Relationship): |
| 331 | related_classes = {'host': Host, 'label': Label} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 332 | |
| 333 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 334 | class HostLabelingCollection(resource_lib.RelationshipCollection): |
| 335 | entry_class = HostLabeling |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 336 | |
| 337 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 338 | class HostAclMembership(resource_lib.Relationship): |
| 339 | related_classes = {'host': Host, 'acl': Acl} |
| 340 | |
| 341 | # TODO: acl.check_for_acl_violation_acl_group() |
| 342 | # TODO: models.AclGroup.on_host_membership_change() |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 343 | |
| 344 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 345 | class HostAclMembershipCollection(resource_lib.RelationshipCollection): |
| 346 | entry_class = HostAclMembership |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 347 | |
| 348 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 349 | class Test(resource_lib.InstanceEntry): |
| 350 | model = models.Test |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 351 | |
| 352 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 353 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 354 | def from_uri_args(cls, request, test_name, **kwargs): |
| 355 | return cls(request, models.Test.objects.get(name=test_name)) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 356 | |
| 357 | |
| 358 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 359 | return {'test_name': self.instance.name} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 360 | |
| 361 | |
| 362 | def short_representation(self): |
| 363 | rep = super(Test, self).short_representation() |
| 364 | rep['name'] = self.instance.name |
| 365 | return rep |
| 366 | |
| 367 | |
| 368 | def full_representation(self): |
| 369 | rep = super(Test, self).full_representation() |
| 370 | rep.update({'author': self.instance.author, |
| 371 | 'class': self.instance.test_class, |
| 372 | 'control_file_type': |
| 373 | models.Test.Types.get_string(self.instance.test_type), |
| 374 | 'control_file_path': self.instance.path, |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 375 | 'dependencies': |
| 376 | TestDependencyCollection(fixed_entry=self).link(), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 377 | }) |
| 378 | return rep |
| 379 | |
| 380 | |
| 381 | @classmethod |
| 382 | def create_instance(cls, input_dict, containing_collection): |
| 383 | cls._check_for_required_fields(input_dict, |
| 384 | ('name', 'control_file_type', |
| 385 | 'control_file_path')) |
| 386 | test_type = models.Test.Type.get_value(input['control_file_type']) |
| 387 | return models.Test.add_object(name=input_dict['name'], |
| 388 | test_type=test_type, |
| 389 | path=input_dict['control_file_path']) |
| 390 | |
| 391 | |
| 392 | def update(self, input_dict): |
| 393 | data = {'test_type': input_dict.get('control_file_type'), |
| 394 | 'path': input_dict.get('control_file_path'), |
| 395 | 'class': input_dict.get('class'), |
| 396 | } |
| 397 | data = input_dict.remove_unspecified_fields(data) |
| 398 | self.instance.update_object(**data) |
| 399 | |
| 400 | |
| 401 | class TestCollection(resource_lib.Collection): |
| 402 | queryset = models.Test.objects.all() |
| 403 | entry_class = Test |
| 404 | |
| 405 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 406 | class TestDependency(resource_lib.Relationship): |
| 407 | related_classes = {'test': Test, 'label': Label} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 408 | |
| 409 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 410 | class TestDependencyCollection(resource_lib.RelationshipCollection): |
| 411 | entry_class = TestDependency |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 412 | |
| 413 | |
| 414 | # TODO profilers |
| 415 | |
| 416 | |
| 417 | class ExecutionInfo(resource_lib.Resource): |
| 418 | _permitted_methods = ('GET','POST') |
| 419 | _job_fields = models.Job.get_field_dict() |
| 420 | _DEFAULTS = { |
| 421 | 'control_file': '', |
| 422 | 'is_server': True, |
| 423 | 'dependencies': [], |
| 424 | 'machines_per_execution': 1, |
| 425 | 'run_verify': bool(_job_fields['run_verify'].default), |
| 426 | 'timeout_hrs': _job_fields['timeout'].default, |
| 427 | 'maximum_runtime_hrs': _job_fields['max_runtime_hrs'].default, |
| 428 | 'cleanup_before_job': |
| 429 | models.RebootBefore.get_string(models.DEFAULT_REBOOT_BEFORE), |
| 430 | 'cleanup_after_job': |
| 431 | models.RebootAfter.get_string(models.DEFAULT_REBOOT_AFTER), |
| 432 | } |
| 433 | |
| 434 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 435 | def _query_parameters_accepted(self): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 436 | return (('tests', 'Comma-separated list of test names to run'), |
| 437 | ('kernels', 'TODO'), |
| 438 | ('client_control_file', |
| 439 | 'Client control file segment to run after all specified ' |
| 440 | 'tests'), |
| 441 | ('profilers', |
| 442 | 'Comma-separated list of profilers to activate during the ' |
| 443 | 'job'), |
| 444 | ('use_container', 'TODO'), |
| 445 | ('profile_only', |
| 446 | 'If true, run only profiled iterations; otherwise, always run ' |
| 447 | 'at least one non-profiled iteration in addition to a ' |
| 448 | 'profiled iteration'), |
| 449 | ('upload_kernel_config', |
| 450 | 'If true, generate a server control file code that uploads ' |
| 451 | 'the kernel config file to the client and tells the client of ' |
| 452 | 'the new (local) path when compiling the kernel; the tests ' |
| 453 | 'must be server side tests')) |
| 454 | |
| 455 | |
| 456 | @classmethod |
| 457 | def execution_info_from_job(cls, job): |
| 458 | return {'control_file': job.control_file, |
| 459 | 'is_server': job.control_type == models.Job.ControlType.SERVER, |
| 460 | 'dependencies': [label.name for label |
| 461 | in job.dependency_labels.all()], |
| 462 | 'machines_per_execution': job.synch_count, |
| 463 | 'run_verify': bool(job.run_verify), |
| 464 | 'timeout_hrs': job.timeout, |
| 465 | 'maximum_runtime_hrs': job.max_runtime_hrs, |
| 466 | 'cleanup_before_job': |
| 467 | models.RebootBefore.get_string(job.reboot_before), |
| 468 | 'cleanup_after_job': |
| 469 | models.RebootAfter.get_string(job.reboot_after), |
| 470 | } |
| 471 | |
| 472 | |
| 473 | def _get_execution_info(self, input_dict): |
| 474 | tests = input_dict.get('tests', '') |
| 475 | client_control_file = input_dict.get('client_control_file', None) |
| 476 | if not tests and not client_control_file: |
| 477 | return self._DEFAULTS |
| 478 | |
| 479 | test_list = tests.split(',') |
| 480 | if 'profilers' in input_dict: |
| 481 | profilers_list = input_dict['profilers'].split(',') |
| 482 | else: |
| 483 | profilers_list = [] |
| 484 | kernels = input_dict.get('kernels', '') # TODO |
| 485 | if kernels: |
| 486 | kernels = [dict(version=kernel) for kernel in kernels.split(',')] |
| 487 | |
| 488 | cf_info, test_objects, profiler_objects, label = ( |
| 489 | rpc_utils.prepare_generate_control_file( |
| 490 | test_list, kernels, None, profilers_list)) |
| 491 | control_file_contents = control_file.generate_control( |
| 492 | tests=test_objects, kernels=kernels, |
| 493 | profilers=profiler_objects, is_server=cf_info['is_server'], |
| 494 | client_control_file=client_control_file, |
| 495 | profile_only=input_dict.get('profile_only', None), |
| 496 | upload_kernel_config=input_dict.get( |
| 497 | 'upload_kernel_config', None)) |
| 498 | return dict(self._DEFAULTS, |
| 499 | control_file=control_file_contents, |
| 500 | is_server=cf_info['is_server'], |
| 501 | dependencies=cf_info['dependencies'], |
| 502 | machines_per_execution=cf_info['synch_count']) |
| 503 | |
| 504 | |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 505 | def handle_request(self): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 506 | result = self.link() |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 507 | result['execution_info'] = self._get_execution_info( |
| 508 | self._request.REQUEST) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 509 | return self._basic_response(result) |
| 510 | |
| 511 | |
| 512 | class QueueEntriesRequest(resource_lib.Resource): |
| 513 | _permitted_methods = ('GET',) |
| 514 | |
| 515 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 516 | def _query_parameters_accepted(self): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 517 | return (('hosts', 'Comma-separated list of hostnames'), |
| 518 | ('one_time_hosts', |
| 519 | 'Comma-separated list of hostnames not already in the ' |
| 520 | 'Autotest system'), |
| 521 | ('meta_hosts', |
| 522 | 'Comma-separated list of label names; for each one, an entry ' |
| 523 | 'will be created and assigned at runtime to an available host ' |
| 524 | 'with that label'), |
| 525 | ('atomic_group_class', 'TODO')) |
| 526 | |
| 527 | |
| 528 | def _read_list(self, list_string): |
| 529 | if list_string: |
| 530 | return list_string.split(',') |
| 531 | return [] |
| 532 | |
| 533 | |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 534 | def handle_request(self): |
| 535 | request_dict = self._request.REQUEST |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 536 | hosts = self._read_list(request_dict.get('hosts')) |
| 537 | one_time_hosts = self._read_list(request_dict.get('one_time_hosts')) |
| 538 | meta_hosts = self._read_list(request_dict.get('meta_hosts')) |
| 539 | atomic_group_class = request_dict.get('atomic_group_class') |
| 540 | |
| 541 | # TODO: bring in all the atomic groups magic from create_job() |
| 542 | |
| 543 | entries = [] |
| 544 | for hostname in one_time_hosts: |
| 545 | models.Host.create_one_time_host(hostname) |
| 546 | for hostname in hosts: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 547 | entry = Host.from_uri_args(self._request, hostname) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 548 | entries.append({'host': entry.link()}) |
| 549 | for label_name in meta_hosts: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 550 | entry = Label.from_uri_args(self._request, label_name) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 551 | entries.append({'meta_host': entry.link()}) |
| 552 | |
| 553 | result = self.link() |
| 554 | result['queue_entries'] = entries |
| 555 | return self._basic_response(result) |
| 556 | |
| 557 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 558 | class Job(resource_lib.InstanceEntry): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 559 | _permitted_methods = ('GET',) |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 560 | model = models.Job |
| 561 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 562 | |
| 563 | class _StatusConstraint(query_lib.Constraint): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 564 | def apply_constraint(self, queryset, value, comparison_type, |
| 565 | is_inverse): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 566 | if comparison_type != 'equals' or is_inverse: |
| 567 | raise query_lib.ConstraintError('Can only use this selector ' |
| 568 | 'with equals') |
| 569 | non_queued_statuses = [ |
| 570 | status for status, _ |
| 571 | in models.HostQueueEntry.Status.choices() |
| 572 | if status != models.HostQueueEntry.Status.QUEUED] |
| 573 | if value == 'queued': |
| 574 | return queryset.exclude( |
| 575 | hostqueueentry__status__in=non_queued_statuses) |
| 576 | elif value == 'active': |
| 577 | return queryset.filter( |
| 578 | hostqueueentry__status__in=non_queued_statuses).filter( |
| 579 | hostqueueentry__complete=False).distinct() |
| 580 | elif value == 'complete': |
| 581 | return queryset.exclude(hostqueueentry__complete=False) |
| 582 | else: |
| 583 | raise query_lib.ConstraintError('Value must be one of queued, ' |
| 584 | 'active or complete') |
| 585 | |
| 586 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 587 | @classmethod |
| 588 | def add_query_selectors(cls, query_processor): |
| 589 | query_processor.add_field_selector('id') |
| 590 | query_processor.add_selector( |
| 591 | query_lib.Selector('status', |
| 592 | doc='One of queued, active or complete'), |
| 593 | Job._StatusConstraint()) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 594 | |
| 595 | |
| 596 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 597 | def from_uri_args(cls, request, job_id, **kwargs): |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 598 | return cls(request, models.Job.objects.get(id=job_id)) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 599 | |
| 600 | |
| 601 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 602 | return {'job_id': self.instance.id} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 603 | |
| 604 | |
| 605 | def short_representation(self): |
| 606 | rep = super(Job, self).short_representation() |
| 607 | rep.update({'id': self.instance.id, |
| 608 | 'owner': self.instance.owner, |
| 609 | 'name': self.instance.name, |
| 610 | 'priority': |
| 611 | models.Job.Priority.get_string(self.instance.priority), |
| 612 | 'created_on': |
| 613 | self._format_datetime(self.instance.created_on), |
| 614 | }) |
| 615 | return rep |
| 616 | |
| 617 | |
| 618 | def full_representation(self): |
| 619 | rep = super(Job, self).full_representation() |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 620 | queue_entries = QueueEntryCollection(self._request) |
| 621 | queue_entries.set_query_parameters(job=self.instance.id) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 622 | rep.update({'email_list': self.instance.email_list, |
| 623 | 'parse_failed_repair': |
| 624 | bool(self.instance.parse_failed_repair), |
| 625 | 'execution_info': |
| 626 | ExecutionInfo.execution_info_from_job(self.instance), |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 627 | 'queue_entries': queue_entries.link(), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 628 | }) |
| 629 | return rep |
| 630 | |
| 631 | |
| 632 | @classmethod |
| 633 | def create_instance(cls, input_dict, containing_collection): |
| 634 | cls._check_for_required_fields(input_dict, ('name', 'execution_info', |
| 635 | 'queue_entries')) |
| 636 | execution_info = input_dict['execution_info'] |
| 637 | cls._check_for_required_fields(execution_info, ('control_file', |
| 638 | 'is_server')) |
| 639 | |
| 640 | if execution_info['is_server']: |
| 641 | control_type = models.Job.ControlType.SERVER |
| 642 | else: |
| 643 | control_type = models.Job.ControlType.CLIENT |
| 644 | options = dict( |
| 645 | name=input_dict['name'], |
| 646 | priority=input_dict.get('priority', None), |
| 647 | control_file=execution_info['control_file'], |
| 648 | control_type=control_type, |
| 649 | is_template=input_dict.get('is_template', None), |
| 650 | timeout=execution_info.get('timeout_hrs'), |
| 651 | max_runtime_hrs=execution_info.get('maximum_runtime_hrs'), |
| 652 | synch_count=execution_info.get('machines_per_execution'), |
| 653 | run_verify=execution_info.get('run_verify'), |
| 654 | email_list=input_dict.get('email_list', None), |
| 655 | dependencies=execution_info.get('dependencies', ()), |
| 656 | reboot_before=execution_info.get('cleanup_before_job'), |
| 657 | reboot_after=execution_info.get('cleanup_after_job'), |
| 658 | parse_failed_repair=input_dict.get('parse_failed_repair', None), |
| 659 | keyvals=input_dict.get('keyvals', None)) |
| 660 | |
| 661 | host_objects, metahost_label_objects, atomic_group = [], [], None |
| 662 | for queue_entry in input_dict['queue_entries']: |
| 663 | if 'host' in queue_entry: |
| 664 | host = queue_entry['host'] |
| 665 | if host: # can be None, indicated a hostless job |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 666 | host_entry = containing_collection.resolve_link(host) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 667 | host_objects.append(host_entry.instance) |
| 668 | elif 'meta_host' in queue_entry: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 669 | label_entry = containing_collection.resolve_link( |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 670 | queue_entry['meta_host']) |
| 671 | metahost_label_objects.append(label_entry.instance) |
| 672 | if 'atomic_group' in queue_entry: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 673 | atomic_group_entry = containing_collection.resolve_link( |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 674 | queue_entry['atomic_group']) |
| 675 | if atomic_group: |
| 676 | assert atomic_group_entry.instance.id == atomic_group.id |
| 677 | else: |
| 678 | atomic_group = atomic_group_entry.instance |
| 679 | |
| 680 | job_id = rpc_utils.create_new_job( |
| 681 | owner=models.User.current_user().login, |
| 682 | options=options, |
| 683 | host_objects=host_objects, |
| 684 | metahost_objects=metahost_label_objects, |
| 685 | atomic_group=atomic_group) |
| 686 | return models.Job.objects.get(id=job_id) |
| 687 | |
| 688 | |
| 689 | def update(self, input_dict): |
| 690 | # Required for POST, doesn't actually support PUT |
| 691 | pass |
| 692 | |
| 693 | |
| 694 | class JobCollection(resource_lib.Collection): |
| 695 | queryset = models.Job.objects.order_by('-id') |
| 696 | entry_class = Job |
| 697 | |
| 698 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 699 | class QueueEntry(resource_lib.InstanceEntry): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 700 | _permitted_methods = ('GET', 'PUT') |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 701 | model = models.HostQueueEntry |
| 702 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 703 | |
| 704 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 705 | def add_query_selectors(cls, query_processor): |
| 706 | query_processor.add_field_selector('host', field='host__hostname') |
| 707 | query_processor.add_field_selector('job', field='job__id') |
| 708 | |
| 709 | |
| 710 | @classmethod |
| 711 | def from_uri_args(cls, request, queue_entry_id): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 712 | instance = models.HostQueueEntry.objects.get(id=queue_entry_id) |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 713 | return cls(request, instance) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 714 | |
| 715 | |
| 716 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 717 | return {'queue_entry_id': self.instance.id} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 718 | |
| 719 | |
| 720 | def short_representation(self): |
| 721 | rep = super(QueueEntry, self).short_representation() |
| 722 | if self.instance.host: |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 723 | host = (Host(self._request, self.instance.host) |
| 724 | .short_representation()) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 725 | else: |
| 726 | host = None |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 727 | job = Job(self._request, self.instance.job) |
| 728 | host = Host.from_optional_instance(self._request, self.instance.host) |
| 729 | label = Label.from_optional_instance(self._request, |
| 730 | self.instance.meta_host) |
| 731 | atomic_group_class = AtomicGroupClass.from_optional_instance( |
| 732 | self._request, self.instance.atomic_group) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 733 | rep.update( |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 734 | {'job': job.short_representation(), |
| 735 | 'host': host.short_representation(), |
| 736 | 'label': label.short_representation(), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 737 | 'atomic_group_class': |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 738 | atomic_group_class.short_representation(), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 739 | 'status': self.instance.status, |
| 740 | 'execution_path': self.instance.execution_subdir, |
| 741 | 'started_on': self._format_datetime(self.instance.started_on), |
| 742 | 'aborted': bool(self.instance.aborted)}) |
| 743 | return rep |
| 744 | |
| 745 | |
| 746 | def update(self, input_dict): |
| 747 | if 'aborted' in input_dict: |
| 748 | if input_dict['aborted'] != True: |
| 749 | raise BadRequest('"aborted" can only be set to true') |
| 750 | query = models.HostQueueEntry.objects.filter(pk=self.instance.pk) |
| 751 | models.AclGroup.check_abort_permissions(query) |
| 752 | rpc_utils.check_abort_synchronous_jobs(query) |
| 753 | self.instance.abort(thread_local.get_user()) |
| 754 | |
| 755 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 756 | class QueueEntryCollection(resource_lib.Collection): |
| 757 | queryset = models.HostQueueEntry.objects.order_by('-id') |
| 758 | entry_class = QueueEntry |
| 759 | |
| 760 | |
| 761 | class HealthTask(resource_lib.InstanceEntry): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 762 | _permitted_methods = ('GET',) |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 763 | model = models.SpecialTask |
| 764 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 765 | |
| 766 | @classmethod |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 767 | def add_query_selectors(cls, query_processor): |
| 768 | query_processor.add_field_selector('host', field='host__hostname') |
| 769 | |
| 770 | |
| 771 | @classmethod |
| 772 | def from_uri_args(cls, request, task_id): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 773 | instance = models.SpecialTask.objects.get(id=task_id) |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 774 | return cls(request, instance) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 775 | |
| 776 | |
| 777 | def _uri_args(self): |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 778 | return {'task_id': self.instance.id} |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 779 | |
| 780 | |
| 781 | def short_representation(self): |
| 782 | rep = super(HealthTask, self).short_representation() |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 783 | host = Host(self._request, self.instance.host) |
| 784 | queue_entry = QueueEntry.from_optional_instance( |
| 785 | self._request, self.instance.queue_entry) |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 786 | rep.update( |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 787 | {'host': host.short_representation(), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 788 | 'task_type': self.instance.task, |
| 789 | 'started_on': |
| 790 | self._format_datetime(self.instance.time_started), |
| 791 | 'status': self.instance.status, |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 792 | 'queue_entry': queue_entry.short_representation() |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 793 | }) |
| 794 | return rep |
| 795 | |
| 796 | |
| 797 | @classmethod |
| 798 | def create_instance(cls, input_dict, containing_collection): |
| 799 | cls._check_for_required_fields(input_dict, ('task_type',)) |
| 800 | host = containing_collection.base_entry.instance |
| 801 | models.AclGroup.check_for_acl_violation_hosts((host,)) |
| 802 | return models.SpecialTask.schedule_special_task(host, |
| 803 | input_dict['task_type']) |
| 804 | |
| 805 | |
| 806 | def update(self, input_dict): |
| 807 | # Required for POST, doesn't actually support PUT |
| 808 | pass |
| 809 | |
| 810 | |
jamesren | 3981f44 | 2010-02-16 19:27:59 +0000 | [diff] [blame^] | 811 | class HealthTaskCollection(resource_lib.Collection): |
| 812 | entry_class = HealthTask |
| 813 | |
| 814 | |
| 815 | def _fresh_queryset(self): |
| 816 | return models.SpecialTask.objects.order_by('-id') |
| 817 | |
| 818 | |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 819 | class ResourceDirectory(resource_lib.Resource): |
| 820 | _permitted_methods = ('GET',) |
| 821 | |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 822 | def handle_request(self): |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 823 | result = self.link() |
| 824 | result.update({ |
showard | f46ad4c | 2010-02-03 20:28:59 +0000 | [diff] [blame] | 825 | 'atomic_group_classes': |
| 826 | AtomicGroupClassCollection(self._request).link(), |
| 827 | 'labels': LabelCollection(self._request).link(), |
| 828 | 'users': UserCollection(self._request).link(), |
| 829 | 'acl_groups': AclCollection(self._request).link(), |
| 830 | 'hosts': HostCollection(self._request).link(), |
| 831 | 'tests': TestCollection(self._request).link(), |
| 832 | 'execution_info': ExecutionInfo(self._request).link(), |
| 833 | 'queue_entries_request': |
| 834 | QueueEntriesRequest(self._request).link(), |
| 835 | 'jobs': JobCollection(self._request).link(), |
showard | f828c77 | 2010-01-25 21:49:42 +0000 | [diff] [blame] | 836 | }) |
| 837 | return self._basic_response(result) |