示例#1
0
def check_resource_name(resource_name, data, name):
    # 校验配置信息中的变量名是否规范
    check_var_by_config(data['config'])

    if ('item_id' in data or 'resource_id' in data) and 'project_id' in data:
        if 'item_id' in data:
            result = models.Template.check_resource_name(
                data['project_id'], resource_name, data['item_id'], name,
                data['version_id'])
        else:
            result = models.Template.check_resource_name(
                data['project_id'], resource_name, data['resource_id'], name,
                data['version_id'])
        if not result:
            raise ValidationError(u"%s名称:%s已经在项目模板中被占用,请重新填写" %
                                  (resource_name, name))
    return data
示例#2
0
def check_resource_name(resource_name, data, name):
    # 校验配置信息中的变量名是否规范
    check_var_by_config(data["config"])

    if ("item_id" in data or "resource_id" in data) and "project_id" in data:
        if "item_id" in data:
            result = models.Template.check_resource_name(
                data["project_id"], resource_name, data["item_id"], name, data["version_id"]
            )
        else:
            result = models.Template.check_resource_name(
                data["project_id"], resource_name, data["resource_id"], name, data["version_id"]
            )
        if not result:
            raise ValidationError(_("{}名称:{}已经在项目模板中被占用,请重新填写").format(resource_name, name))

    return data
示例#3
0
def check_resource_name(resource_name, data, name):
    # 校验配置信息中的变量名是否规范
    check_var_by_config(data['config'])

    if ('item_id' in data or 'resource_id' in data) and 'project_id' in data:
        if 'item_id' in data:
            result = models.Template.check_resource_name(
                data['project_id'], resource_name, data['item_id'], name,
                data['version_id'])
        else:
            result = models.Template.check_resource_name(
                data['project_id'], resource_name, data['resource_id'], name,
                data['version_id'])
        if not result:
            raise ValidationError(
                '{resource_name}{prefix_msg}:{name}{suffix_msg}'.format(
                    resource_name=resource_name,
                    prefix_msg=_("名称"),
                    name=name,
                    suffix_msg=_("已经在项目模板中被占用,请重新填写")))
    return data
示例#4
0
    def post(self, request, project_id, version_id):
        version_entity = validate_version_id(project_id,
                                             version_id,
                                             is_version_entity_retrun=True)

        project_kind = request.project.kind
        self.slz = VariableNamespaceSLZ(data=request.data,
                                        context={'project_kind': project_kind})
        self.slz.is_valid(raise_exception=True)
        slz_data = self.slz.data

        if 'instance_entity' not in slz_data:
            raise ValidationError(_("请选择要实例化的模板"))
        instance_entity = slz_data['instance_entity']

        lb_services = []
        key_list = []
        for cate in instance_entity:
            cate_data = instance_entity[cate]
            cate_id_list = [i.get('id') for i in cate_data if i.get('id')]
            # 查询这些配置文件的变量名
            for _id in cate_id_list:
                if cate in ['metric']:
                    config = self.get_metric_confg(_id)
                else:
                    try:
                        resource = MODULE_DICT.get(cate).objects.get(id=_id)
                    except Exception:
                        continue
                    config = resource.config
                search_list = check_var_by_config(config)
                key_list.extend(search_list)
                # mesos Deployment 需要再查询 Application 的配置文件
                if cate in ['deployment']:
                    deployment_app = VersionedEntity.get_application_by_deployment_id(
                        version_id, _id)
                    deployment_app_config = deployment_app.config
                    _search_list = check_var_by_config(deployment_app_config)
                    key_list.extend(_search_list)

            # mesos service 查询需要lb的列表
            if project_kind == 2 and cate == 'service':
                lb_services = version_entity.get_lb_services_by_ids(
                    cate_id_list)

        key_list = list(set(key_list))
        variable_dict = {}
        if key_list:
            # 验证变量名是否符合规范,不符合抛出异常,否则后续用 django 模板渲染变量也会抛出异常

            var_objects = Variable.objects.filter(
                Q(project_id=project_id) | Q(project_id=0))

            access_token = request.user.token.access_token
            namespace_res = paas_cc.get_namespace_list(access_token,
                                                       project_id,
                                                       limit=ALL_LIMIT)
            namespace_data = namespace_res.get('data', {}).get('results') or []
            namespace_dict = {
                str(i['id']): i['cluster_id']
                for i in namespace_data
            }

            ns_list = slz_data['namespaces'].split(
                ',') if slz_data['namespaces'] else []
            for ns_id in ns_list:
                _v_list = []
                for _key in key_list:
                    key_obj = var_objects.filter(key=_key)
                    if key_obj.exists():
                        _obj = key_obj.first()
                        # 只显示自定义变量
                        if _obj.category == 'custom':
                            cluster_id = namespace_dict.get(ns_id, 0)
                            _v_list.append({
                                "key":
                                _obj.key,
                                "name":
                                _obj.name,
                                "value":
                                _obj.get_show_value(cluster_id, ns_id)
                            })
                    else:
                        _v_list.append({
                            "key": _key,
                            "name": _key,
                            "value": ""
                        })
                variable_dict[ns_id] = _v_list
        return Response({
            "code": 0,
            "message": "OK",
            "data": {
                "lb_services": lb_services,
                "variable_dict": variable_dict
            }
        })