Skip to content

Commit

Permalink
feature: 全局变量改为后台实现,API接口优化 TencentBlueKing#130 (TencentBlueKing#172)
Browse files Browse the repository at this point in the history
* feature: 全局变量改为后台实现,API接口优化 TencentBlueKing#130

* improve: 代码优化

* improve: 代码优化
  • Loading branch information
pagezz-canway authored and homholueng committed Apr 19, 2019
1 parent d816d0b commit 38ed33f
Show file tree
Hide file tree
Showing 22 changed files with 224 additions and 182 deletions.
2 changes: 1 addition & 1 deletion config/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
'gcloud.periodictask',
'pipeline',
'pipeline.component_framework',
'pipeline.variables',
'pipeline.variable_framework',
'pipeline.engine',
'pipeline.log',
'pipeline.contrib.statistics',
Expand Down
10 changes: 5 additions & 5 deletions frontend/desktop/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ const api = {
* 获取自定义全局变量列表
*/
getCustomVarCollection () {
const prefixUrl = this.getPrefix('query_custom_variables_collection')
const prefixUrl = this.getPrefix('variable')
const opts = {
method: 'GET',
url: prefixUrl
Expand Down Expand Up @@ -1291,14 +1291,14 @@ const api = {
}
return request(opts)
},

/**
* 查询业务在 CMDB 的主机
* @param {Array} filels 主机查询字段
*/
loadHostInCC (fields) {
const prefixUrl = this.getPrefix('cc_search_host')

const opts = {
method: 'GET',
url: prefixUrl,
Expand All @@ -1313,7 +1313,7 @@ const api = {
*/
loadTopoTreeInCC () {
const prefixUrl = this.getPrefix('cc_search_topo_tree')

const opts = {
method: 'GET',
url: prefixUrl
Expand All @@ -1325,7 +1325,7 @@ const api = {
*/
loadTopoModelInCC () {
const prefixUrl = this.getPrefix('cc_get_mainline_object_topo')

const opts = {
method: 'GET',
url: prefixUrl
Expand Down
3 changes: 1 addition & 2 deletions frontend/desktop/src/api/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export function getUrlSetting (SITE_URL, BIZ_CC_ID) {
taskCreateMethod: SITE_URL + 'taskflow/api/get_task_create_method/',
cc_search_host: SITE_URL + 'pipeline/cc_search_host/' + BIZ_CC_ID + '/',
cc_search_topo_tree: SITE_URL + 'pipeline/cc_search_topo_tree/' + BIZ_CC_ID + '/',
cc_get_mainline_object_topo: SITE_URL + 'pipeline/cc_get_mainline_object_topo/' + BIZ_CC_ID + '/',
query_custom_variables_collection: SITE_URL + 'pipeline/query_custom_variables_collection/'
cc_get_mainline_object_topo: SITE_URL + 'pipeline/cc_get_mainline_object_topo/' + BIZ_CC_ID + '/'
}
}
42 changes: 19 additions & 23 deletions frontend/desktop/src/pages/template/TemplateEdit/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -357,28 +357,24 @@
this.customVarCollectionLoading = true
try {
const customVarCollection = await this.loadCustomVarCollection()
if (customVarCollection.result) {
const listData = [
{
name: gettext('普通变量'),
children: []
},
{
name: gettext('元变量'),
children: []
}
]
customVarCollection.data.forEach(item => {
if (item.type === 'general') {
listData[0].children.push(item)
} else {
listData[1].children.push(item)
}
})
this.variableTypeList = listData
} else {
errorHandler(customVarCollection, this)
}
const listData = [
{
name: gettext('普通变量'),
children: []
},
{
name: gettext('元变量'),
children: []
}
]
customVarCollection.forEach(item => {
if (item.type === 'general') {
listData[0].children.push(item)
} else {
listData[1].children.push(item)
}
})
this.variableTypeList = listData
} catch (e) {
errorHandler(e, this)
} finally {
Expand Down Expand Up @@ -521,7 +517,7 @@
atomGrouped[index].list.push(item)
}
})
this.subAtomGrouped = atomGrouped
},
toggleSettingPanel (isSettingPanelShow) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/desktop/src/store/modules/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ const template = {
return api.getTemplateData(data).then(response => response.data)
},
loadCustomVarCollection () {
return api.getCustomVarCollection().then(response => response.data)
return api.getCustomVarCollection().then(response => response.data.objects)
},
// 保存模板数据
saveTemplateData ({ state }, { templateId, ccId, common }) {
Expand Down
44 changes: 24 additions & 20 deletions gcloud/webservice3/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@

from pipeline.component_framework.library import ComponentLibrary
from pipeline.component_framework.models import ComponentModel
from pipeline.core.data.library import VariableLibrary
from pipeline.models import VariableModel
from pipeline.variable_framework.models import VariableModel
from gcloud import exceptions
from gcloud.core.models import Business
from gcloud.core.utils import (
Expand Down Expand Up @@ -340,7 +339,7 @@ class ComponentModelResource(ModelResource):
null=True)

class Meta:
queryset = ComponentModel.objects.filter(status=1).order_by('name')
queryset = ComponentModel.objects.filter(status=True).order_by('name')
resource_name = 'component'
excludes = ['status', 'id']
detail_uri_name = 'code'
Expand Down Expand Up @@ -376,27 +375,32 @@ def alter_detail_data_to_serialize(self, request, data):


class VariableModelResource(ModelResource):
name = fields.CharField(
attribute='name',
readonly=True,
null=True)
form = fields.CharField(
attribute='form',
readonly=True,
null=True)
type = fields.CharField(
attribute='type',
readonly=True,
null=True)
tag = fields.CharField(
attribute='tag',
readonly=True,
null=True)
meta_tag = fields.CharField(
attribute='meta_tag',
readonly=True,
null=True)

class Meta:
queryset = VariableModel.objects.filter(status=1)
queryset = VariableModel.objects.filter(status=True)
resource_name = 'variable'
excludes = ['status', 'id']
detail_uri_name = 'code'
ordering = ['id']
authorization = ReadOnlyAuthorization()
limit = 0

def alter_list_data_to_serialize(self, request, data):
for bundle in data['objects']:
var = VariableLibrary.get_var_class(bundle.data['code'])
bundle.data['form'] = var.form

return data

def alter_detail_data_to_serialize(self, request, data):
bundle = data
var = VariableLibrary.get_var_class(bundle.data['code'])
is_meta = request.GET.get('meta', False)
form = getattr(var, 'meta_form') if bool(int(is_meta)) else var.form
bundle.data['form'] = form

return data
7 changes: 0 additions & 7 deletions pipeline/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,3 @@ class PipelineInstanceAdmin(admin.ModelAdmin):
list_filter = ['is_started', 'is_finished', 'is_deleted']
search_fields = ['name']
raw_id_fields = ['template', 'snapshot', 'execution_snapshot']


@admin.register(models.VariableModel)
class VariableModelAdmin(admin.ModelAdmin):
list_display = ['id', 'code', 'status']
list_filter = ['status']
search_fields = ['code', 'status']
2 changes: 1 addition & 1 deletion pipeline/core/data/var.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def __new__(cls, name, bases, attrs):
raise exceptions.ConstantReferenceException("LazyVariable %s: code can't be empty."
% new_class.__name__)

pre_variable_register.send(sender=LazyVariable, variable_cls=new_class, variable_code=new_class.code)
pre_variable_register.send(sender=LazyVariable, variable_cls=new_class)

library.VariableLibrary.variables[new_class.code] = new_class

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
specific language governing permissions and limitations under the License.
"""

from django.http import JsonResponse
from django.views.decorators.http import require_GET
from __future__ import unicode_literals

from pipeline_plugins.variables.query import constants
from django.db import migrations


@require_GET
def query_custom_variables_collection(request):
ctx = {
'result': True,
'data': constants.VARIABLES_COLLECTION
}
return JsonResponse(ctx)
class Migration(migrations.Migration):

dependencies = [
('pipeline', '0018_set_has_subprocess'),
]

operations = [
migrations.DeleteModel(
name='VariableModel',
),
]
17 changes: 0 additions & 17 deletions pipeline/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,20 +775,3 @@ def calculate_tree_info(self, save=False):

if save:
self.save()


class VariableModel(models.Model):
"""
注册的变量
"""
code = models.CharField(_(u"变量编码"), max_length=255, unique=True)
status = models.BooleanField(_(u"变量是否可用"), default=True)

class Meta:
verbose_name = _(u"Variable变量")
verbose_name_plural = _(u"Variable变量")
ordering = ['-id']
app_label = 'pipeline'

def __unicode__(self):
return self.code
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,4 @@
specific language governing permissions and limitations under the License.
"""

import importlib

from django.conf import settings

site_constants = importlib.import_module('pipeline_plugins.variables.query.sites.%s.constants' % settings.RUN_VER)

VARIABLES_COLLECTION = getattr(site_constants, 'VARIABLES_COLLECTION')
default_app_config = 'pipeline.variable_framework.apps.VariableFrameworkConfig'
23 changes: 23 additions & 0 deletions pipeline/variable_framework/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
"""
Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community
Edition) available.
Copyright (C) 2017-2019 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""

from django.contrib import admin

from pipeline.variable_framework import models


@admin.register(models.VariableModel)
class VariableModelAdmin(admin.ModelAdmin):
list_display = ['id', 'code', 'status']
search_fields = ['code']
list_filter = ['status']
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
from pipeline.utils.register import autodiscover_collections


class VariablesConfig(AppConfig):
name = 'pipeline.variables'
verbose_name = 'PipelineVariables'
class VariableFrameworkConfig(AppConfig):
name = 'pipeline.variable_framework'
verbose_name = 'PipelineVariableFramework'

def ready(self):
"""
@summary: 注册公共部分和OPEN_VER下的变量到数据库
@summary: 注册公共部分和RUN_VER下的变量到数据库
@return:
"""
from pipeline.variables.signals.handlers import * # noqa
from pipeline.variable_framework.signals.handlers import * # noqa
for path in settings.VARIABLE_AUTO_DISCOVER_PATH:
autodiscover_collections(path)

from pipeline.models import VariableModel
from pipeline.variable_framework.models import VariableModel
from pipeline.core.data.library import VariableLibrary
try:
VariableModel.objects.exclude(code__in=VariableLibrary.variables.keys()).update(status=False)
Expand Down
39 changes: 39 additions & 0 deletions pipeline/variable_framework/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
"""
Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community
Edition) available.
Copyright (C) 2017-2019 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""

from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='VariableModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('code', models.CharField(max_length=255, unique=True, verbose_name='\u53d8\u91cf\u7f16\u7801')),
('status', models.BooleanField(default=True, verbose_name='\u53d8\u91cf\u662f\u5426\u53ef\u7528')),
],
options={
'verbose_name': 'Variable\u53d8\u91cf',
'verbose_name_plural': 'Variable\u53d8\u91cf',
},
),
]
File renamed without changes.
Loading

0 comments on commit 38ed33f

Please sign in to comment.