def post(self, request, project_id, template_id, resource_name): validate_resource_name(resource_name) if is_create_template(template_id): # template dict like {'desc': '', 'name': ''} tpl_args = request.data.get('template', {}) template = create_template_with_perm_check(request, project_id, tpl_args) else: template = get_template_by_project_and_id(project_id, template_id) self.can_edit_template(request, template) data = request.data or {} data.update({ 'version_id': 0, 'resource_id': 0, 'project_id': project_id }) serializer_class = serializers_new.get_slz_class_by_resource_name( resource_name) serializer = serializer_class(data=data) serializer.is_valid(raise_exception=True) validated_data = serializer.validated_data create_data = self._compose_create_data(validated_data) ret_data = self._create_resource_entity(resource_name, template.id, create_data) return Response(ret_data)
def validate(self, data): template_id = data["template_id"] data["template"] = models.get_template_by_project_and_id(data["project_id"], template_id) namespace_id = data["namespace_id"] release_id = data["release_id"] try: VersionInstance.objects.get(id=release_id, template_id=template_id, ns_id=namespace_id) except VersionInstance.DoesNotExist: raise ValidationError( f"release does not exist: release_id({release_id}), template_id({template_id}), " f"namespace_id({namespace_id})" ) name = data["name"] resource_name = data["resource_name"] try: InstanceConfig.objects.get( name=name, instance_id=release_id, namespace=namespace_id, category=resource_name ) except InstanceConfig.DoesNotExist: raise ValidationError( f"release does not exist: release_id({release_id}), name({name}), " f"namespace_id({namespace_id}), resource_name({resource_name})" ) data["variable_info"] = {namespace_id: data.get("namespace_variables", {})} return data
def validate(self, data): try: data["show_version_id"] = int(data["show_version_id"]) except Exception as e: raise ValidationError(e) template_id = data["template_id"] template = models.get_template_by_project_and_id( data["project_id"], template_id) data["template"] = template show_version_id = data["show_version_id"] if show_version_id == -1: data["show_version"] = None return data try: data["show_version"] = models.ShowVersion.objects.get( id=show_version_id, template_id=template_id) except models.ShowVersion.DoesNotExist: raise ValidationError( f"show version(id:{show_version_id}) does not exist or not belong to template(id:{template_id})" ) else: return data
def post(self, request, project_id, template_id): if is_create_template(template_id): # template dict like {'desc': '', 'name': ''} tpl_args = request.data.get('template', {}) template = create_template_with_perm_check(request, project_id, tpl_args) else: template = get_template_by_project_and_id(project_id, template_id) self.can_edit_template(request, template) serializer = serializers_new.TemplateDraftSLZ( template, data=request.data, context={'template_id': template.id}) serializer.is_valid(raise_exception=True) serializer.save(draft_updator=request.user.username) validated_data = serializer.validated_data # 记录操作日志 client.ContextActivityLogClient(project_id=template.project_id, user=request.user.username, resource_type='template', resource=template.name, resource_id=template.id, extra=json.dumps(validated_data), description=_("保存草稿")).log_modify() return Response({ 'template_id': template.id, 'show_version_id': -1, 'real_version_id': validated_data['real_version_id'], })
def validate(self, data): version_id = data['version_id'] try: ventity = models.VersionedEntity.objects.get(id=version_id) except models.VersionedEntity.DoesNotExist: raise ResNotFoundError('{prefix_msg}id:{version_id}{suffix_msg}'.format( prefix_msg=_("模板集版本"), version_id=version_id, suffix_msg=_("不存在") )) else: data['ventity'] = ventity project_id = data['project_id'] try: template = models.get_template_by_project_and_id(project_id, ventity.template_id) except ValidationError: raise ValidationError('{prefix_msg}id:{version_id}{suffix_msg}id:{project_id}'.format( prefix_msg=_("模板集版本"), version_id=version_id, suffix_msg=_("不属于该项目"), project_id=project_id )) else: data['template'] = template return data
def list_show_versions_for_instance(self, request, project_id, template_id): """根据模板集查询用户可见版本列表 实例化页面查询模板集版本列表 """ template = models.get_template_by_project_and_id(project_id, template_id) self.can_use_template(request, template) show_versions = models.ShowVersion.objects.filter(template_id=template.id) serializer = ListShowVersionISLZ(show_versions, many=True) return Response({'results': serializer.data})
def validate(self, data): template = models.get_template_by_project_and_id( data["project_id"], data["template_id"]) data["template"] = template data[ "show_version"] = models.ShowVersion.objects.get_latest_by_template( template.id) data["show_version_id"] = data["show_version"].id return data
def validate(self, data): template = models.get_template_by_project_and_id( data['project_id'], data['template_id']) data['template'] = template data[ 'show_version'] = models.ShowVersion.objects.get_latest_by_template( template.id) data['show_version_id'] = data['show_version'].id return data
def validate(self, data): real_version_id = data["real_version_id"] if real_version_id <= 0: raise ValidationError(_("请先填写模板内容,再保存")) template_id = data["template_id"] try: models.VersionedEntity.objects.get(id=real_version_id, template_id=template_id) except models.VersionedEntity.DoesNotExist: raise ValidationError(_("模板集版本(id:{})不属于该模板(id:{})").format(real_version_id, template_id)) template = models.get_template_by_project_and_id(data["project_id"], template_id) data["template"] = template return data
def lock_template(self, request, project_id, template_id): template = get_template_by_project_and_id(project_id, template_id) self.validate_template_locked(request, template) with client.ContextActivityLogClient(project_id=project_id, user=request.user.username, resource_type="template", resource=template.name, resource_id=template_id, description=_("加锁模板集")).log_add(): template.is_locked = True template.locker = request.user.username template.save() return Response(data={})
def list_show_versions(self, request, project_id, template_id): template = models.get_template_by_project_and_id(project_id, template_id) self.can_view_template(request, template) show_versions = models.ShowVersion.objects.filter(template_id=template.id) serializer = ListShowVersionSLZ(show_versions, many=True) show_version_list = list(serializer.data) if request.GET.get('is_filter_draft') != '1': if template.get_draft(): show_version_list.append(get_draft_show_version(template)) show_version_list.sort(key=lambda k: k['updated'], reverse=True) return Response(show_version_list)
def _filter_release(self, request, project_id, template_id, context): template = get_template_by_project_and_id(project_id, template_id) self.can_view_template(request, template) qsets = VersionInstance.objects.filter(template_id=template_id) namespace_id = request.query_params.get("namespace_id") if namespace_id: qsets = qsets.filter(ns_id=namespace_id) if context.get("latest"): serializer = serializers.TemplateReleaseSLZ( qsets.latest("created")) else: serializer = serializers.TemplateReleaseSLZ(qsets, many=True) return serializer.data
def validate(self, data): real_version_id = data['real_version_id'] if real_version_id <= 0: raise ValidationError("请先填写模板内容,再保存") template_id = data['template_id'] try: models.VersionedEntity.objects.get(id=real_version_id, template_id=template_id) except models.VersionedEntity.DoesNotExist: raise ValidationError( f"模板集版本(id:{real_version_id})不属于该模板(id:{template_id})") template = models.get_template_by_project_and_id( data['project_id'], template_id) data['template'] = template return data
def delete_show_version(self, request, project_id, template_id, show_version_id): template = models.get_template_by_project_and_id(project_id, template_id) # 已经实例化过的版本不能被删除 has_instance = has_instance_of_show_version(template.id, show_version_id) if has_instance: raise ValidationError("该版本已经被实例化过,不能被删除") self.can_edit_template(request, template) delete_data = { 'project_id': project_id, 'template': template, 'show_version_id': show_version_id, 'username': request.user.username } self._delete_show_version(delete_data) return Response({'show_version_id': show_version_id})
def validate(self, data): version_id = data['version_id'] try: ventity = models.VersionedEntity.objects.get(id=version_id) except models.VersionedEntity.DoesNotExist: raise ResNotFoundError(f"模板集版本(id:{version_id})不存在") else: data['ventity'] = ventity project_id = data['project_id'] try: template = models.get_template_by_project_and_id(project_id, ventity.template_id) except ValidationError: raise ValidationError(f"模板集版本(id:{version_id})不属于该项目(id:{project_id})") else: data['template'] = template return data
def update_template(self, request, project_id, template_id): """ request.data = { 'name': '', 'desc': '', 'show_version': { 'name': '', 'show_version_id': '', } 'template_files': [{ 'resource_name': 'Deployment', 'files': [{'name': 'nginx.yaml', 'content': 'Kind:Deployment', 'action': 'update', 'id': 3}] }] } """ template = get_template_by_project_and_id(project_id, template_id) data = self._template_data(request, project_id=project_id) serializer = serializers.UpdateTemplateSLZ(template, data=data, context={'request': request}) serializer.is_valid(raise_exception=True) template = serializer.save() return Response({'template_id': template.id})
def save_without_ventity(self, request, project_id, template_id): """仅仅创建可见版本 """ template = models.get_template_by_project_and_id(project_id, template_id) self.can_edit_template(request, template) serializer = ShowVersionCreateSLZ(data=request.data) serializer.is_valid(raise_exception=True) username = request.user.username show_version = models.ShowVersion.objects.create( template_id=template.id, name=serializer.data['name'], creator=username, updator=username, ) # model Template updated field need change when save version template.save(update_fields=['updated']) return Response({'show_version_id': show_version.id})