def create_namespace(self, request, project_id_or_code, cluster_id): project_id = request.project.project_id slz = CreateNamespaceParamsSLZ(data=request.data, context={"project_id": project_id}) slz.is_valid(raise_exception=True) data = slz.data access_token = request.user.token.access_token username = request.user.username namespace = self._create_kubernetes_namespace(access_token, username, project_id, cluster_id, data["name"]) # 创建命名空间下的变量值 ns_id = namespace.get("namespace_id") or namespace.get("id") namespace["id"] = ns_id NameSpaceVariable.batch_save(ns_id, data["variables"]) namespace["variables"] = data["variables"] # 命名空间权限Client ns_perm_client = bcs_perm.Namespace(request, project_id, bcs_perm.NO_RES, cluster_id) ns_perm_client.register(namespace["id"], f"{namespace['name']}({cluster_id})") return Response(namespace)
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(self, request, project_id_or_code, cluster_id): project_id = request.project.project_id params = self.params_validate(CreateNamespaceSLZ, context={'project_id': project_id}) ns_name, variables = params['name'], params['variables'] if ns_name in BCS_RESERVED_NAMESPACES: raise error_codes.ValidateError('不允许创建 BCS 保留的命名空间') # 更新操作审计信息 request.audit_ctx.update_fields(resource_type=ResourceType.Namespace, resource=ns_name) namespace = Namespace(request.ctx_cluster).get_or_create_cc_namespace( ns_name, request.user.username, params['labels'], params['annotations'] ) # 创建命名空间下的变量值 ns_id = namespace['namespace_id'] namespace['id'] = ns_id NameSpaceVariable.batch_save(ns_id, variables) namespace['variables'] = variables return Response(namespace)
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 create_namespace(self, request, project_id_or_code, cluster_id): project_id = request.project.project_id slz = CreateNamespaceParamsSLZ(data=request.data, context={"project_id": project_id}) slz.is_valid(raise_exception=True) data = slz.data access_token = request.user.token.access_token username = request.user.username project_id = request.project.project_id project_kind_name = ProjectKind.get_choice_label(request.project.kind) namespace = getattr(self, f"create_{project_kind_name.lower()}_namespace")( access_token, username, project_id, cluster_id, data["name"]) # 创建命名空间下的变量值 ns_id = namespace["id"] NameSpaceVariable.batch_save(ns_id, data["variables"]) namespace["variables"] = data["variables"] return Response(namespace)