def update(self, request, project_id, namespace_id, is_validate_perm=True): """修改命名空间 不允许修改命名空间信息,只能修改变量信息 TODO: 在wesley提供集群下使用的命名空间后,允许命名空间修改名称 """ serializer = UpdateNSVariableSLZ(data=request.data, context={ 'request': request, 'project_id': project_id, 'ns_id': namespace_id }) serializer.is_valid(raise_exception=True) data = serializer.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 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 get_variables(self, request, project_id, ns_id): """获取命名空间下所有的变量信息""" variables = NameSpaceVariable.get_ns_vars(ns_id, project_id) return Response({ "code": 0, "message": "OK", "count": len(variables), "data": variables })
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
def create(self, request, project_id, is_validate_perm=True): """新建命名空间 k8s 流程:新建namespace配置文件并下发 -> 新建包含仓库账号信息的sercret配置文件并下发 -> 在paas-cc上注册 mesos流程:新建包含仓库账号信息的sercret配置文件并下发 -> 在paas-cc上注册 """ serializer = NamespaceSLZ(data=request.data, context={ 'request': request, 'project_id': project_id }) serializer.is_valid(raise_exception=True) data = serializer.data # 判断权限 perm = bcs_perm.Namespace(request, project_id, bcs_perm.NO_RES, data['cluster_id']) perm.can_create(raise_exception=is_validate_perm) data = serializer.data access_token = request.user.token.access_token project_kind = request.project.kind project_code = request.project.english_name 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: ns_name = data['name'] cluster_id = data['cluster_id'] 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, data['cluster_id'], data['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'], result['data']['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 = [ '%s[id:%s]' % (i['key'], i['id']) for i in not_exist_vars ] result['message'] = u"以下变量不存在:%s" % ";".join( not_exist_show_msg) result['data']['ns_vars'] = NameSpaceVariable.get_ns_vars( ns_id, project_id) return response.Response(result)