def create_class(self): klass_limit = AdminLimit.get_for_admin(get_current_instance().owner_id).get_classes_count() if Klass.objects.count() >= klass_limit: raise KlassCountExceeded(klass_limit) klass_data = {'name': self.name, 'description': 'Class created as a dependency of ' 'socket: "{}".'.format(self.socket.name, self.name), 'schema': self.dependency['schema'], 'ignored_target_classes': self.ignored_class_names(), 'metadata': self.dependency['metadata']} # Run validation first serializer = KlassSerializer(data=klass_data) serializer.is_valid(raise_exception=True) fields = {} field_props = {} for field in self.dependency['schema']: fields[field['name']] = [self.socket.pk] field_props[field['name']] = {f_prop: [self.socket.pk] for f_prop, val in field.items() if val is True} refs = { 'managed_by': [self.socket.pk], 'fields': fields, 'props': field_props, } return serializer.save(refs=refs)
def create_schedule(self): # Create a fresh schedule if it does not exist yet try: script = self.get_script() except CodeBox.DoesNotExist: script = self.create_script() schedule_limit = AdminLimit.get_for_admin( get_current_instance().owner_id).get_schedules_count() if CodeBoxSchedule.objects.count() >= schedule_limit: raise ScheduleCountExceeded(schedule_limit) schedule_data = { 'label': 'Script dependency of {}'.format(self.socket.name), 'description': 'Schedule created as a dependency of ' 'socket: "{}".'.format(self.socket.name), 'script': script.pk, } schedule_data.update(self.schedule_params) schedule_serializer = CodeBoxScheduleSerializer(data=schedule_data) schedule_serializer.is_valid(raise_exception=True) return schedule_serializer.save( socket=self.socket, codebox=script, event_handler=self.dependency['handler_name'])
def perform_create(self, serializer): # Lock on instance for the duration of transaction to avoid race conditions with Instance.lock(self.request.instance.pk): klass_limit = AdminLimit.get_for_admin(self.request.instance.owner_id).get_classes_count() if Klass.objects.count() >= klass_limit: raise KlassCountExceeded(klass_limit) serializer.save()
def perform_create(self, serializer): # Lock on instance for the duration of transaction to avoid race conditions with Instance.lock(self.request.instance.pk): socket_limit = AdminLimit.get_for_admin(self.request.instance.owner_id).get_sockets_count() if SocketEnvironment.objects.count() >= socket_limit: raise SocketCountExceeded(socket_limit) serializer.save()
def perform_create(self, serializer): # Lock on admin for the duration of transaction to avoid race conditions admin = Admin.objects.select_for_update().get(pk=self.request.user.id) instances_limit = AdminLimit.get_for_admin( self.request.user.id).get_instances_count() if Instance.objects.filter(owner=admin).count() >= instances_limit: raise InstanceCountExceeded(instances_limit) serializer.save()
def perform_create(self, serializer): # Lock on instance for the duration of transaction to avoid race conditions with Instance.lock(self.request.instance.pk): schedule_limit = AdminLimit.get_for_admin(self.request.instance.owner_id).get_schedules_count() if CodeBoxSchedule.objects.count() >= schedule_limit: raise ScheduleCountExceeded(schedule_limit) object = serializer.save() object.schedule_next()
def create_run_spec(cls, instance, codebox, additional_args, meta, dumps=True, socket=None): custom_timeout = codebox.config.pop('timeout', None) timeout = cls.default_timeout if custom_timeout and isinstance(custom_timeout, (int, float)) and custom_timeout > 0: timeout = min(cls.max_timeout, custom_timeout) if dumps: additional_args = json.dumps(additional_args or {}) else: additional_args = additional_args or "{}" config = instance.config.copy() if socket: config.update(socket.config) meta['socket'] = socket.name config.update(codebox.config) # Add token if allow_full_access is True if config.pop('allow_full_access', False) is True: meta['token'] = make_token(instance) meta['api_host'] = settings.API_DOMAIN meta['space_host'] = settings.SPACE_DOMAIN return { 'instance': instance.name, 'runtime_name': codebox.runtime_name, 'original_source': codebox.source, 'config': json.dumps(config), 'timeout': timeout, 'additional_args': additional_args, 'meta': json.dumps(meta), 'concurrency_limit': AdminLimit.get_for_admin( instance.owner_id).get_codebox_concurrency(), }
def validate(self, attrs): has_archive = bool(attrs.get('archive', False)) has_backup = bool(attrs.get('backup', False)) if has_backup and has_archive or (not has_backup and not has_archive): raise ValidationError( 'You have to provide either backup or archive.') if has_archive and not self.context['request'].user.is_staff: raise PermissionDenied() if has_backup: backup = attrs['backup'] cross_region_limit = AdminLimit.get_for_admin( self.context['view'].request.instance.owner_id ).get_backup_cross_region_limit() if backup.location != settings.LOCATION and backup.size > cross_region_limit and cross_region_limit >= 0: raise ValidationError( 'You cannot restore cross region backup of size > {}.'. format(cross_region_limit)) return super().validate(attrs)
def get_instance_rate(self, request, view): return AdminLimit.get_for_admin(request.instance.owner_id).get_poll_rate()