v0.0.21 | NHN Cloud 멀티 프로젝트 지원 추가
Some checks failed
Build And Test / build-and-push (push) Failing after 34s
Some checks failed
Build And Test / build-and-push (push) Failing after 34s
- NHNCloudProject 모델 추가 (사용자별 여러 프로젝트 관리) - 프로젝트 목록/추가/삭제/활성화 API 추가 - 프로젝트별 비밀번호 복호화 API 추가 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
210
users/views.py
210
users/views.py
@ -8,7 +8,7 @@ from rest_framework.permissions import IsAuthenticated, BasePermission
|
||||
from rest_framework_simplejwt.views import TokenObtainPairView
|
||||
from rest_framework import generics
|
||||
from .serializers import RegisterSerializer, CustomTokenObtainPairSerializer, UserListSerializer
|
||||
from .models import CustomUser
|
||||
from .models import CustomUser, NHNCloudProject
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
tracer = trace.get_tracer(__name__) # ✅ 트레이서 생성
|
||||
@ -430,3 +430,211 @@ class NHNCloudPasswordView(APIView):
|
||||
f"[NHN PASSWORD GET] user={email} | status=fail | reason=exception | IP={ip} | UA={ua}"
|
||||
)
|
||||
return Response({"error": f"복호화 실패: {str(e)}"}, status=500)
|
||||
|
||||
|
||||
# ============================================
|
||||
# NHN Cloud 프로젝트 관리 API (멀티 프로젝트 지원)
|
||||
# ============================================
|
||||
|
||||
class NHNCloudProjectListView(APIView):
|
||||
"""NHN Cloud 프로젝트 목록 조회 및 추가"""
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get(self, request):
|
||||
"""프로젝트 목록 조회"""
|
||||
with tracer.start_as_current_span("NHNCloudProjectListView GET") as span:
|
||||
email, ip, ua = get_request_info(request)
|
||||
projects = NHNCloudProject.objects.filter(user=request.user)
|
||||
|
||||
logger.debug(f"[NHN PROJECT LIST] user={email} | count={projects.count()} | IP={ip} | UA={ua}")
|
||||
span.add_event("NHN projects listed", attributes={"email": email, "count": projects.count()})
|
||||
|
||||
data = [{
|
||||
"id": p.id,
|
||||
"name": p.name,
|
||||
"tenant_id": p.tenant_id,
|
||||
"username": p.username,
|
||||
"storage_account": p.storage_account or "",
|
||||
"is_active": p.is_active,
|
||||
"created_at": p.created_at.isoformat(),
|
||||
} for p in projects]
|
||||
|
||||
return Response(data)
|
||||
|
||||
def post(self, request):
|
||||
"""프로젝트 추가"""
|
||||
with tracer.start_as_current_span("NHNCloudProjectListView POST") as span:
|
||||
email, ip, ua = get_request_info(request)
|
||||
|
||||
name = request.data.get("name")
|
||||
tenant_id = request.data.get("tenant_id")
|
||||
username = request.data.get("username")
|
||||
password = request.data.get("password")
|
||||
storage_account = request.data.get("storage_account", "")
|
||||
|
||||
if not name or not tenant_id or not username or not password:
|
||||
logger.warning(
|
||||
f"[NHN PROJECT CREATE] user={email} | status=fail | reason=missing_fields | IP={ip} | UA={ua}"
|
||||
)
|
||||
return Response(
|
||||
{"error": "name, tenant_id, username, password는 필수입니다."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
# 중복 체크
|
||||
if NHNCloudProject.objects.filter(user=request.user, tenant_id=tenant_id).exists():
|
||||
logger.warning(
|
||||
f"[NHN PROJECT CREATE] user={email} | status=fail | reason=duplicate | IP={ip} | UA={ua}"
|
||||
)
|
||||
return Response(
|
||||
{"error": "이미 등록된 Tenant ID입니다."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
try:
|
||||
# 첫 프로젝트면 자동 활성화
|
||||
is_first = not NHNCloudProject.objects.filter(user=request.user).exists()
|
||||
|
||||
project = NHNCloudProject(
|
||||
user=request.user,
|
||||
name=name,
|
||||
tenant_id=tenant_id,
|
||||
username=username,
|
||||
storage_account=storage_account,
|
||||
is_active=is_first,
|
||||
)
|
||||
project.save_credentials(password)
|
||||
|
||||
logger.info(
|
||||
f"[NHN PROJECT CREATE] user={email} | project={name} | is_active={is_first} | IP={ip} | UA={ua}"
|
||||
)
|
||||
span.add_event("NHN project created", attributes={"email": email, "project": name})
|
||||
|
||||
return Response({
|
||||
"id": project.id,
|
||||
"name": project.name,
|
||||
"tenant_id": project.tenant_id,
|
||||
"username": project.username,
|
||||
"storage_account": project.storage_account or "",
|
||||
"is_active": project.is_active,
|
||||
"created_at": project.created_at.isoformat(),
|
||||
}, status=status.HTTP_201_CREATED)
|
||||
|
||||
except Exception as e:
|
||||
logger.exception(
|
||||
f"[NHN PROJECT CREATE] user={email} | status=fail | reason=exception | IP={ip} | UA={ua}"
|
||||
)
|
||||
return Response({"error": f"프로젝트 생성 실패: {str(e)}"}, status=500)
|
||||
|
||||
|
||||
class NHNCloudProjectDetailView(APIView):
|
||||
"""NHN Cloud 프로젝트 상세 (삭제)"""
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get_project(self, request, project_id):
|
||||
"""프로젝트 조회 (본인 것만)"""
|
||||
try:
|
||||
return NHNCloudProject.objects.get(id=project_id, user=request.user)
|
||||
except NHNCloudProject.DoesNotExist:
|
||||
return None
|
||||
|
||||
def delete(self, request, project_id):
|
||||
"""프로젝트 삭제"""
|
||||
with tracer.start_as_current_span("NHNCloudProjectDetailView DELETE") as span:
|
||||
email, ip, ua = get_request_info(request)
|
||||
|
||||
project = self.get_project(request, project_id)
|
||||
if not project:
|
||||
logger.warning(
|
||||
f"[NHN PROJECT DELETE] user={email} | status=fail | reason=not_found | IP={ip} | UA={ua}"
|
||||
)
|
||||
return Response({"error": "프로젝트를 찾을 수 없습니다."}, status=404)
|
||||
|
||||
project_name = project.name
|
||||
was_active = project.is_active
|
||||
project.delete()
|
||||
|
||||
# 삭제된 프로젝트가 활성이었으면 다른 프로젝트 활성화
|
||||
if was_active:
|
||||
other_project = NHNCloudProject.objects.filter(user=request.user).first()
|
||||
if other_project:
|
||||
other_project.is_active = True
|
||||
other_project.save(update_fields=['is_active'])
|
||||
|
||||
logger.info(
|
||||
f"[NHN PROJECT DELETE] user={email} | project={project_name} | IP={ip} | UA={ua}"
|
||||
)
|
||||
span.add_event("NHN project deleted", attributes={"email": email, "project": project_name})
|
||||
|
||||
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
|
||||
class NHNCloudProjectActivateView(APIView):
|
||||
"""NHN Cloud 프로젝트 활성화"""
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def patch(self, request, project_id):
|
||||
"""프로젝트 활성화 (기존 활성 해제)"""
|
||||
with tracer.start_as_current_span("NHNCloudProjectActivateView PATCH") as span:
|
||||
email, ip, ua = get_request_info(request)
|
||||
|
||||
try:
|
||||
project = NHNCloudProject.objects.get(id=project_id, user=request.user)
|
||||
except NHNCloudProject.DoesNotExist:
|
||||
logger.warning(
|
||||
f"[NHN PROJECT ACTIVATE] user={email} | status=fail | reason=not_found | IP={ip} | UA={ua}"
|
||||
)
|
||||
return Response({"error": "프로젝트를 찾을 수 없습니다."}, status=404)
|
||||
|
||||
# 기존 활성 프로젝트 비활성화
|
||||
NHNCloudProject.objects.filter(user=request.user, is_active=True).update(is_active=False)
|
||||
|
||||
# 새 프로젝트 활성화
|
||||
project.is_active = True
|
||||
project.save(update_fields=['is_active'])
|
||||
|
||||
logger.info(
|
||||
f"[NHN PROJECT ACTIVATE] user={email} | project={project.name} | IP={ip} | UA={ua}"
|
||||
)
|
||||
span.add_event("NHN project activated", attributes={"email": email, "project": project.name})
|
||||
|
||||
return Response({
|
||||
"message": "프로젝트가 활성화되었습니다.",
|
||||
"id": project.id,
|
||||
"name": project.name,
|
||||
})
|
||||
|
||||
|
||||
class NHNCloudProjectPasswordView(APIView):
|
||||
"""NHN Cloud 프로젝트 비밀번호 조회 (복호화)"""
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get(self, request, project_id):
|
||||
"""복호화된 비밀번호 조회"""
|
||||
with tracer.start_as_current_span("NHNCloudProjectPasswordView GET") as span:
|
||||
email, ip, ua = get_request_info(request)
|
||||
|
||||
try:
|
||||
project = NHNCloudProject.objects.get(id=project_id, user=request.user)
|
||||
except NHNCloudProject.DoesNotExist:
|
||||
logger.warning(
|
||||
f"[NHN PROJECT PASSWORD] user={email} | status=fail | reason=not_found | IP={ip} | UA={ua}"
|
||||
)
|
||||
return Response({"error": "프로젝트를 찾을 수 없습니다."}, status=404)
|
||||
|
||||
try:
|
||||
decrypted_password = project.decrypt_password()
|
||||
logger.info(f"[NHN PROJECT PASSWORD] user={email} | project={project.name} | IP={ip} | UA={ua}")
|
||||
span.add_event("NHN project password retrieved", attributes={"email": email, "project": project.name})
|
||||
|
||||
return Response({
|
||||
"tenant_id": project.tenant_id,
|
||||
"username": project.username,
|
||||
"password": decrypted_password,
|
||||
"storage_account": project.storage_account or "",
|
||||
})
|
||||
except Exception as e:
|
||||
logger.exception(
|
||||
f"[NHN PROJECT PASSWORD] user={email} | status=fail | reason=exception | IP={ip} | UA={ua}"
|
||||
)
|
||||
return Response({"error": f"복호화 실패: {str(e)}"}, status=500)
|
||||
|
||||
Reference in New Issue
Block a user