Пример #1
0
def update_resources(template, show_version, template_files):
    updator = template.updator
    template_id = template.id
    show_version_name = show_version['name']
    show_version_id = show_version['show_version_id']

    try:
        old_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})')

    ventity = models.VersionedEntity.objects.get(id=old_show_version.real_version_id)
    entity = update_entity(updator, template_id, ventity.get_entity(), template_files)
    ventity = models.VersionedEntity.objects.create(
        template_id=template_id, version=models.get_default_version(), entity=entity, creator=updator
    )

    if old_show_version.name == show_version_name:
        old_show_version.update_real_version_id(ventity.id, updator=updator)
        return

    try:
        old_show_version = models.ShowVersion.default_objects.get(template_id=template.id, name=show_version_name)
    except models.ShowVersion.DoesNotExist:
        models.ShowVersion.objects.create(
            template_id=template.id, name=show_version_name, real_version_id=ventity.id, history=[ventity.id],
            creator=updator, updator=updator
        )
    else:
        old_show_version.update_real_version_id(ventity.id, updator=updator, is_deleted=False, deleted_time=None)
Пример #2
0
def create_resources(template, show_version, template_files):
    creator = template.creator
    entity = create_entity(creator, template.id, template_files)
    ventity = models.VersionedEntity.objects.create(
        template_id=template.id, version=models.get_default_version(), entity=entity, creator=creator
    )
    models.ShowVersion.objects.create(
        template_id=template.id, name=show_version['name'],
        real_version_id=ventity.id, history=json.dumps([ventity.id]),
        creator=creator, updator=creator
    )
Пример #3
0
 def _create_resource_entity(self, resource_name, template_id, create_data):
     resource_class = get_model_class_by_resource_name(resource_name)
     resource = resource_class.perform_create(**create_data)
     # 创建新的模板集版本, 只保存各类资源的第一个
     resource_entity = {resource_name: str(resource.id)}
     ventity = VersionedEntity.objects.create(
         template_id=template_id,
         version=get_default_version(),
         entity=resource_entity,
         creator=self.request.user.username)
     return {
         'id': resource.id,
         'version': ventity.id,
         'template_id': template_id,
         'resource_data': resource.get_res_config(is_simple=True)
     }
Пример #4
0
def init_template(project_id, project_code, project_kind, access_token,
                  username):
    """创建项目时,初始化示例模板集
    request.project.english_name
    """
    # 判断模板集是否已经创建, 如果已经创建, 删除旧模板
    exit_templates = Template.objects.filter(project_id=project_id,
                                             name=TEMPLATE_NAME)
    if exit_templates.exists():
        _delete_old_init_templates(exit_templates, project_id, project_code,
                                   access_token, username)

    template_data = {}
    if project_kind == K8S_KIND:
        template_data = K8S_TEMPLATE.get('data', {})
    elif project_kind == MESOS_KIND:
        template_data = MESOS_TEMPLATE.get('data', {})

    logger.info(
        f'init_template [begin] project_id: {project_id}, project_kind: {ClusterType.get(project_kind)}'
    )
    # 新建模板集
    init_template = Template.objects.create(
        project_id=project_id,
        name=TEMPLATE_NAME,
        desc=TEMPLATE_DESC,
        creator=username,
        updator=username,
    )

    new_entity = {}
    for cate in template_data:
        new_item_id_list = []
        data_list = template_data[cate]
        for _data in data_list:
            _save_data = {}
            for _d_key in _data:
                # 新建,忽略 id 字段
                if _d_key == 'id':
                    continue
                # 目前只有 dict、list这两类非字符格式
                _d_value = _data[_d_key]
                if isinstance(_d_value, list) or isinstance(_d_value, dict):
                    _save_data[_d_key] = json.dumps(_d_value)
                else:
                    _save_data[_d_key] = _d_value
            _ins = MODULE_DICT.get(cate).objects.create(**_save_data)
            new_item_id_list.append(str(_ins.id))
        new_entity[cate] = ','.join(new_item_id_list)

    # 新建version
    new_ver = VersionedEntity.objects.create(
        template_id=init_template.id,
        entity=json.dumps(new_entity),
        version=get_default_version(),
        creator=username,
        updator=username,
    )
    # 新建可见版本
    ShowVersion.objects.create(
        template_id=init_template.id,
        real_version_id=new_ver.id,
        name='init_version',
    )

    # 将模板集注册到权限中心
    request = RequestClass(username=username,
                           access_token=access_token,
                           project_code=project_code)
    perm = bcs_perm.Templates(request, project_id, bcs_perm.NO_RES)
    perm.register(str(init_template.id), str(init_template.name))

    logger.info(
        f'init_template [end] project_id: {project_id}, project_kind: {ClusterType.get(project_kind)}'
    )
    return