def update(self, request, project_id, namespace_id, is_validate_perm=True): """修改命名空间 不允许修改命名空间信息,只能修改变量信息 TODO: 在wesley提供集群下使用的命名空间后,允许命名空间修改名称 """ serializer = slz.UpdateNSVariableSLZ(data=request.data, context={ 'request': request, 'project_id': project_id, 'ns_id': namespace_id }) serializer.is_valid(raise_exception=True) data = serializer.validated_data result = {'code': 0, 'data': data, 'message': _("更新成功")} # 更新成功后需要保存变量信息 if data.get('ns_vars'): res, not_exist_vars = NameSpaceVariable.batch_save( namespace_id, data['ns_vars']) if not_exist_vars: not_exist_show_msg = [ '%s[id:%s]' % (i['key'], i['id']) for i in not_exist_vars ] result['message'] = _("以下变量不存在:{}").format( ";".join(not_exist_show_msg)) result['data']['ns_vars'] = NameSpaceVariable.get_ns_vars( namespace_id, project_id) return response.Response(result)
def update(self, request, project_id, namespace_id, is_validate_perm=True): """修改命名空间下变量""" # TODO 增加权限控制 serializer = slz.UpdateNSVariableSLZ(data=request.data, context={ 'request': request, 'project_id': project_id, 'ns_id': namespace_id }) serializer.is_valid(raise_exception=True) data = serializer.validated_data result = {'code': 0, 'data': data, 'message': _("更新成功")} # 更新成功后需要保存变量信息 if data.get('ns_vars'): res, not_exist_vars = NameSpaceVariable.batch_save( namespace_id, data['ns_vars']) if not_exist_vars: not_exist_show_msg = [ '%s[id:%s]' % (i['key'], i['id']) for i in not_exist_vars ] result['message'] = _("以下变量不存在:{}").format( ";".join(not_exist_show_msg)) result['data']['ns_vars'] = NameSpaceVariable.get_ns_vars( namespace_id, project_id) request.audit_ctx.update_fields(resource=namespace_id, description=_("调整命名空间({})下的变量").format( namespace_id, extra=data)) return response.Response(result)
def create_flow(self, request, project_id, data, perm): access_token = request.user.token.access_token project_kind = request.project.kind project_code = request.project.english_name ns_name = data['name'] cluster_id = data['cluster_id'] if ClusterType.get(project_kind) == 'Kubernetes': # k8s 集群需要调用 bcs api 初始化数据 self.init_namespace_by_bcs(access_token, project_id, project_code, data) has_image_secret = None else: self.init_mesos_ns_by_bcs(access_token, project_id, project_code, cluster_id, ns_name) has_image_secret = True result = paas_cc.create_namespace( access_token, project_id, cluster_id, ns_name, None, # description 现在没有用到 request.user.username, data['env_type'], has_image_secret, ) if result.get('code') != 0: if ClusterType.get(project_kind) != 'Kubernetes': self.delete_secret_for_mesos(access_token, project_id, cluster_id, ns_name) if 'Duplicate entry' in result.get('message', ''): message = _("创建失败,namespace名称已经在其他项目存在") else: message = result.get('message', '') return response.Response({ 'code': result['code'], 'data': None, 'message': message }) else: # 注册资源到权限中心 perm.register(result['data']['id'], f'{ns_name}({cluster_id})') # 创建成功后需要保存变量信息 result_data = result.get('data') if data.get('ns_vars') and result_data: ns_id = result_data.get('id') res, not_exist_vars = NameSpaceVariable.batch_save( ns_id, data['ns_vars']) if not_exist_vars: not_exist_show_msg = [ f'{i["key"]}[id:{i["id"]}]' for i in not_exist_vars ] result['message'] = _("以下变量不存在:{}").format( ';'.join(not_exist_show_msg)) result['data']['ns_vars'] = NameSpaceVariable.get_ns_vars( ns_id, project_id) return result
def create_flow(self, request, project_id, data): access_token = request.user.token.access_token project_code = request.project.english_name ns_name = data['name'] cluster_id = data['cluster_id'] # k8s 集群需要调用 bcs api 初始化数据 self.init_namespace_by_bcs(access_token, project_id, project_code, data) has_image_secret = None result = paas_cc.create_namespace( access_token, project_id, cluster_id, ns_name, None, # description 现在没有用到 request.user.username, data['env_type'], has_image_secret, ) if result.get('code') != 0: if 'Duplicate entry' in result.get('message', ''): message = _("创建失败,namespace名称已经在其他项目存在") else: message = result.get('message', '') return response.Response({ 'code': result['code'], 'data': None, 'message': message }) else: self.iam_perm.grant_resource_creator_actions( NamespaceCreatorAction(project_id=project_id, cluster_id=cluster_id, creator=request.user.username, name=ns_name), ) # 创建成功后需要保存变量信息 result_data = result.get('data') if data.get('ns_vars') and result_data: ns_id = result_data.get('id') res, not_exist_vars = NameSpaceVariable.batch_save( ns_id, data['ns_vars']) if not_exist_vars: not_exist_show_msg = [ f'{i["key"]}[id:{i["id"]}]' for i in not_exist_vars ] result['message'] = _("以下变量不存在:{}").format( ';'.join(not_exist_show_msg)) result['data']['ns_vars'] = NameSpaceVariable.get_ns_vars( ns_id, project_id) return result
def get_namespace_variables(project_id, namespace_id): # 仅能拿到用户自定义的变量 """ project_var = [{ 'id': _v.id, 'key': _v.key, 'name': _v.name, 'default_value': _v.get_default_value, 'ns_values': ns_values }] ns_vars = NameSpaceVariable.get_ns_vars(namespace_id, project_id) ns_vars = [{ 'id': _v.id, 'key': _v.key, 'name': _v.name, 'value': _ns_value if _ns_value else '' }] """ project_var = NameSpaceVariable.get_project_ns_vars(project_id) namespace_vars = [] for _var in project_var: _ns_values = _var['ns_values'] _ns_value_ids = _ns_values.keys() namespace_vars.append({ 'id': _var['id'], 'key': _var['key'], 'name': _var['name'], 'value': _ns_values.get(namespace_id) if namespace_id in _ns_value_ids else _var['default_value'], }) ns_vars = NameSpaceVariable.get_ns_vars(namespace_id, project_id) namespace_vars += ns_vars variable = {item["key"]: item["value"] for item in namespace_vars} logger.info("get_namespace_variables %s:%s \n %s", project_id, namespace_id, json.dumps(variable)) return variable