def get(self, request, *args, **kwargs):
        """
        添加特性获取可用标签
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        # 节点添加的标签和数据中心查询回来的标签才可被组件使用
        node_labels = node_label_repo.get_all_labels()
        labels_list = list()
        labels_name_list = list()
        if node_labels:
            node_labels_id_list = [label.label_id for label in node_labels]
            label_obj_list = label_repo.get_labels_by_label_ids(
                node_labels_id_list)
            for label_obj in label_obj_list:
                labels_name_list.append(label_obj.label_name)
        # 查询数据中心可使用的标签
        try:
            code, msg, data_list = label_service.get_region_labels(
                self.tenant, self.service)
            if code == 200 and data_list:
                label_name_list = []
                labels = label_repo.get_all_labels()
                if labels:
                    for label in labels:
                        label_name_list.append(label.label_name)
                for label_name in data_list:
                    if label_name not in label_name_list:
                        label_repo.create_label(label_name, label_name)
                    labels_name_list.append(label_name)
        except Exception as e:
            logger.exception(e)

        # 去除该组件已绑定的标签
        service_labels = service_label_repo.get_service_labels(
            self.service.service_id)
        if service_labels:
            service_labels_id_list = [
                label.label_id for label in service_labels
            ]
            label_obj_list = label_repo.get_labels_by_label_ids(
                service_labels_id_list)
            service_labels_name_list = [
                label.label_name for label in label_obj_list
            ]
            for service_labels_name in service_labels_name_list:
                if service_labels_name in labels_name_list:
                    labels_name_list.remove(service_labels_name)
        for labels_name in labels_name_list:
            label_dict = dict()
            label_oj = label_repo.get_labels_by_label_name(labels_name)
            label_dict["label_id"] = label_oj.label_id
            label_dict["label_alias"] = label_oj.label_alias
            labels_list.append(label_dict)

        result = general_message(200, "success", "查询成功", list=labels_list)
        return Response(result, status=result["code"])
Example #2
0
    def get(self, request, *args, **kwargs):
        """
        添加特性获取可用标签
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        try:
            # 节点添加的标签和数据中心查询回来的标签才可被组件使用
            node_labels = node_label_repo.get_all_labels()
            labels_list = list()
            labels_name_list = list()
            if node_labels:
                node_labels_id_list = [label.label_id for label in node_labels]
                label_obj_list = label_repo.get_labels_by_label_ids(
                    node_labels_id_list)
                for label_obj in label_obj_list:
                    labels_name_list.append(label_obj.label_name)
            # 查询数据中心可使用的标签
            try:
                code, msg, data_list = label_service.get_region_labels(
                    self.tenant, self.service)
                if code == 200:
                    if data_list:
                        label_name_list = []
                        labels = label_repo.get_all_labels()
                        if labels:
                            for label in labels:
                                label_name_list.append(label.label_name)
                        for label_name in data_list:
                            if label_name not in label_name_list:
                                label_id = make_uuid("labels")
                                create_time = datetime.datetime.now().strftime(
                                    '%Y-%m-%d %H:%M:%S')
                                label = Labels(label_id=label_id,
                                               label_name=label_name,
                                               label_alias=label_name,
                                               create_time=create_time)
                                label.save()
                            labels_name_list.append(label_name)

            except Exception as e:
                logger.exception(e)
                pass

            # 去除该组件已绑定的标签
            service_labels = service_label_repo.get_service_labels(
                self.service.service_id)
            if service_labels:
                service_labels_id_list = [l.label_id for l in service_labels]
                label_obj_list = label_repo.get_labels_by_label_ids(
                    service_labels_id_list)
                service_labels_name_list = [
                    l.label_name for l in label_obj_list
                ]
                for service_labels_name in service_labels_name_list:
                    if service_labels_name in labels_name_list:
                        labels_name_list.remove(service_labels_name)
            for labels_name in labels_name_list:
                label_dict = dict()
                label_oj = label_repo.get_labels_by_label_name(labels_name)
                label_dict["label_id"] = label_oj.label_id
                label_dict["label_alias"] = label_oj.label_alias
                labels_list.append(label_dict)

            result = general_message(200, "success", "查询成功", list=labels_list)
        except Exception as e:
            logger.exception(e)
            result = error_message(e.message)
        return Response(result, status=result["code"])