Skip to content

Commit

Permalink
Merge branch 'develop' into project_k8s_info
Browse files Browse the repository at this point in the history
  • Loading branch information
ktkfree authored Feb 26, 2024
2 parents 0c952d4 + a25802a commit 82b42c4
Show file tree
Hide file tree
Showing 7 changed files with 1,071 additions and 4 deletions.
2 changes: 2 additions & 0 deletions hack/endpoint-codegen.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

package main

import (
Expand Down
139 changes: 139 additions & 0 deletions internal/delivery/http/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ func (p ProjectHandler) UpdateProject(w http.ResponseWriter, r *http.Request) {
}

func (p ProjectHandler) DeleteProject(w http.ResponseWriter, r *http.Request) {
//ToDo: to donggyu. implement cleanup logic for k8s & keycloak

//TODO implement me
}

Expand Down Expand Up @@ -498,6 +500,17 @@ func (p ProjectHandler) AddProjectMember(w http.ResponseWriter, r *http.Request)
return
}

pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId)
if err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
stackIds := make(map[string]struct{})
for _, pn := range pns {
stackIds[pn.StackId] = struct{}{}
}

now := time.Now()
for _, pmr := range projectMemberReq.ProjectMemberRequests {
pu, err := p.usecase.GetProjectUser(pmr.ProjectUserId)
Expand Down Expand Up @@ -534,6 +547,15 @@ func (p ProjectHandler) AddProjectMember(w http.ResponseWriter, r *http.Request)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}

// tasks for keycloak & k8s
for stackId := range stackIds {
if err := p.usecase.AssignKeycloakClientRoleToMember(organizationId, projectId, stackId, pmId); err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
}
}

out := domain.CommonProjectResponse{Result: "OK"}
Expand Down Expand Up @@ -769,6 +791,26 @@ func (p ProjectHandler) RemoveProjectMember(w http.ResponseWriter, r *http.Reque
"C_INVALID_PROJECT_MEMBER_ID", ""))
return
}

// tasks for keycloak & k8s
pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId)
if err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
stackIds := make(map[string]struct{})
for _, pn := range pns {
stackIds[pn.StackId] = struct{}{}
}
for stackId := range stackIds {
if err := p.usecase.UnassignKeycloakClientRoleToMember(organizationId, projectId, stackId, projectMemberId); err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
}

if err := p.usecase.RemoveProjectMember(projectMemberId); err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
Expand Down Expand Up @@ -813,8 +855,29 @@ func (p ProjectHandler) RemoveProjectMembers(w http.ResponseWriter, r *http.Requ
return
}

// tasks for keycloak & k8s
pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId)
if err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
stackIds := make(map[string]struct{})
for _, pn := range pns {
stackIds[pn.StackId] = struct{}{}
}

// TODO: change multi row delete
for _, pm := range projectMemberReq.ProjectMember {
// tasks for keycloak & k8s
for stackId := range stackIds {
if err := p.usecase.UnassignKeycloakClientRoleToMember(organizationId, projectId, stackId, pm.ProjectMemberId); err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
}

if err := p.usecase.RemoveProjectMember(pm.ProjectMemberId); err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
Expand Down Expand Up @@ -880,6 +943,25 @@ func (p ProjectHandler) UpdateProjectMemberRole(w http.ResponseWriter, r *http.R
return
}

pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId)
if err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
stackIds := make(map[string]struct{})
for _, pn := range pns {
stackIds[pn.StackId] = struct{}{}
}
// tasks for keycloak & k8s. Unassign old role
for stackId := range stackIds {
if err := p.usecase.UnassignKeycloakClientRoleToMember(organizationId, projectId, stackId, projectMemberId); err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
}

pm.ProjectRoleId = pmrReq.ProjectRoleId
pm.ProjectUser = nil
pm.ProjectRole = nil
Expand All @@ -889,6 +971,14 @@ func (p ProjectHandler) UpdateProjectMemberRole(w http.ResponseWriter, r *http.R
ErrorJSON(w, r, err)
return
}
// tasks for keycloak & k8s. Assign new role
for stackId := range stackIds {
if err := p.usecase.AssignKeycloakClientRoleToMember(organizationId, projectId, stackId, projectMemberId); err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
}

ResponseJSON(w, r, http.StatusOK, domain.CommonProjectResponse{Result: "OK"})
}
Expand Down Expand Up @@ -930,6 +1020,17 @@ func (p ProjectHandler) UpdateProjectMembersRole(w http.ResponseWriter, r *http.
return
}

pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId)
if err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
stackIds := make(map[string]struct{})
for _, pn := range pns {
stackIds[pn.StackId] = struct{}{}
}

for _, pmr := range projectMemberReq.ProjectMemberRoleRequests {
pm, err := p.usecase.GetProjectMember(pmr.ProjectMemberId)
if err != nil {
Expand All @@ -943,6 +1044,14 @@ func (p ProjectHandler) UpdateProjectMembersRole(w http.ResponseWriter, r *http.
return
}

for stackId := range stackIds {
if err := p.usecase.UnassignKeycloakClientRoleToMember(organizationId, projectId, stackId, pm.ID); err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
}

pm.ProjectRoleId = pmr.ProjectRoleId
pm.ProjectUser = nil
pm.ProjectRole = nil
Expand All @@ -952,6 +1061,14 @@ func (p ProjectHandler) UpdateProjectMembersRole(w http.ResponseWriter, r *http.
ErrorJSON(w, r, err)
return
}

for stackId := range stackIds {
if err := p.usecase.AssignKeycloakClientRoleToMember(organizationId, projectId, stackId, pm.ID); err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
}
}

ResponseJSON(w, r, http.StatusOK, domain.CommonProjectResponse{Result: "OK"})
Expand Down Expand Up @@ -1001,6 +1118,16 @@ func (p ProjectHandler) CreateProjectNamespace(w http.ResponseWriter, r *http.Re
CreatedAt: now,
}

// tasks for keycloak & k8s
if err := p.usecase.EnsureRequiredSetupForCluster(organizationId, projectId, projectNamespaceReq.StackId); err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
if err := p.usecase.CreateK8SNSRoleBinding(organizationId, projectId, projectNamespaceReq.StackId, projectNamespaceReq.Namespace); err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}

if err := p.usecase.CreateProjectNamespace(organizationId, pn); err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
Expand Down Expand Up @@ -1268,6 +1395,18 @@ func (p ProjectHandler) UpdateProjectNamespace(w http.ResponseWriter, r *http.Re
// @Router /organizations/{organizationId}/projects/{projectId}/namespaces/{projectNamespace}/stacks/{stackId} [delete]
// @Security JWT
func (p ProjectHandler) DeleteProjectNamespace(w http.ResponseWriter, r *http.Request) {

//ToDo: from donggyu. uncomment lines below after implementing usecase.DeleteProjectNamespace.
// tasks for keycloak & k8s
/*if err := p.usecase.DeleteK8SNSRoleBinding(organizationId, projectId, stackId, projectNamespace); err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
if err := p.usecase.MayRemoveRequiredSetupForCluster(organizationId, projectId, stackId); err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}*/

//TODO implement me

//vars := mux.Vars(r)
Expand Down
Loading

0 comments on commit 82b42c4

Please sign in to comment.