From d4ee7d6bd555d4013e03188fb4f5fcc1c80b357f Mon Sep 17 00:00:00 2001 From: "taekyu.kang" Date: Wed, 21 Feb 2024 15:20:48 +0900 Subject: [PATCH] feature. refactoring usecase --- api/swagger/docs.go | 52 ++++++++++++++++++++++++ api/swagger/swagger.json | 52 ++++++++++++++++++++++++ api/swagger/swagger.yaml | 33 +++++++++++++++ internal/delivery/http/alert.go | 4 +- internal/delivery/http/app-group.go | 4 +- internal/delivery/http/app-serve-app.go | 14 +++---- internal/delivery/http/auth.go | 4 +- internal/delivery/http/cloud-account.go | 4 +- internal/delivery/http/cluster.go | 4 +- internal/delivery/http/dashboard.go | 4 +- internal/delivery/http/organization.go | 6 +-- internal/delivery/http/project.go | 11 ++--- internal/delivery/http/stack-template.go | 4 +- internal/delivery/http/stack.go | 4 +- internal/delivery/http/user.go | 4 +- internal/route/route.go | 51 +++++++++++++++-------- internal/usecase/usecase.go | 32 ++++++--------- 17 files changed, 218 insertions(+), 69 deletions(-) diff --git a/api/swagger/docs.go b/api/swagger/docs.go index 45cd7d55..0520123d 100644 --- a/api/swagger/docs.go +++ b/api/swagger/docs.go @@ -3334,6 +3334,50 @@ const docTemplate = `{ } } }, + "/organizations/{organizationId}/projects/{projectId}/kubeconfig": { + "get": { + "security": [ + { + "JWT": [] + } + ], + "description": "Get project kubeconfig", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Projects" + ], + "summary": "Get project kubeconfig", + "parameters": [ + { + "type": "string", + "description": "Organization ID", + "name": "organizationId", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "Project ID", + "name": "projectId", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/domain.GetProjectKubeconfigResponse" + } + } + } + } + }, "/organizations/{organizationId}/projects/{projectId}/members": { "get": { "security": [ @@ -7093,6 +7137,14 @@ const docTemplate = `{ } } }, + "domain.GetProjectKubeconfigResponse": { + "type": "object", + "properties": { + "kubeconfig": { + "type": "string" + } + } + }, "domain.GetProjectMemberCountResponse": { "type": "object", "properties": { diff --git a/api/swagger/swagger.json b/api/swagger/swagger.json index 4cacc82f..a9f7dbc0 100644 --- a/api/swagger/swagger.json +++ b/api/swagger/swagger.json @@ -3328,6 +3328,50 @@ } } }, + "/organizations/{organizationId}/projects/{projectId}/kubeconfig": { + "get": { + "security": [ + { + "JWT": [] + } + ], + "description": "Get project kubeconfig", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Projects" + ], + "summary": "Get project kubeconfig", + "parameters": [ + { + "type": "string", + "description": "Organization ID", + "name": "organizationId", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "Project ID", + "name": "projectId", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/domain.GetProjectKubeconfigResponse" + } + } + } + } + }, "/organizations/{organizationId}/projects/{projectId}/members": { "get": { "security": [ @@ -7087,6 +7131,14 @@ } } }, + "domain.GetProjectKubeconfigResponse": { + "type": "object", + "properties": { + "kubeconfig": { + "type": "string" + } + } + }, "domain.GetProjectMemberCountResponse": { "type": "object", "properties": { diff --git a/api/swagger/swagger.yaml b/api/swagger/swagger.yaml index 04aa35d6..8f0537c5 100644 --- a/api/swagger/swagger.yaml +++ b/api/swagger/swagger.yaml @@ -1314,6 +1314,11 @@ definitions: type: string type: object type: object + domain.GetProjectKubeconfigResponse: + properties: + kubeconfig: + type: string + type: object domain.GetProjectMemberCountResponse: properties: projectLeaderCount: @@ -4425,6 +4430,34 @@ paths: summary: Check duplicate appServeAppName tags: - AppServeApps + /organizations/{organizationId}/projects/{projectId}/kubeconfig: + get: + consumes: + - application/json + description: Get project kubeconfig + parameters: + - description: Organization ID + in: path + name: organizationId + required: true + type: string + - description: Project ID + in: path + name: projectId + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/domain.GetProjectKubeconfigResponse' + security: + - JWT: [] + summary: Get project kubeconfig + tags: + - Projects /organizations/{organizationId}/projects/{projectId}/members: delete: consumes: diff --git a/internal/delivery/http/alert.go b/internal/delivery/http/alert.go index b03d8f3b..3591c4dd 100644 --- a/internal/delivery/http/alert.go +++ b/internal/delivery/http/alert.go @@ -21,9 +21,9 @@ type AlertHandler struct { usecase usecase.IAlertUsecase } -func NewAlertHandler(h usecase.IAlertUsecase) *AlertHandler { +func NewAlertHandler(h usecase.Usecase) *AlertHandler { return &AlertHandler{ - usecase: h, + usecase: h.Alert, } } diff --git a/internal/delivery/http/app-group.go b/internal/delivery/http/app-group.go index 14aa7da6..1efd767a 100644 --- a/internal/delivery/http/app-group.go +++ b/internal/delivery/http/app-group.go @@ -18,9 +18,9 @@ type AppGroupHandler struct { usecase usecase.IAppGroupUsecase } -func NewAppGroupHandler(h usecase.IAppGroupUsecase) *AppGroupHandler { +func NewAppGroupHandler(h usecase.Usecase) *AppGroupHandler { return &AppGroupHandler{ - usecase: h, + usecase: h.AppGroup, } } diff --git a/internal/delivery/http/app-serve-app.go b/internal/delivery/http/app-serve-app.go index 578e69fc..b2bd74d0 100644 --- a/internal/delivery/http/app-serve-app.go +++ b/internal/delivery/http/app-serve-app.go @@ -71,9 +71,9 @@ type AppServeAppHandler struct { usecase usecase.IAppServeAppUsecase } -func NewAppServeAppHandler(h usecase.IAppServeAppUsecase) *AppServeAppHandler { +func NewAppServeAppHandler(h usecase.Usecase) *AppServeAppHandler { return &AppServeAppHandler{ - usecase: h, + usecase: h.AppServeApp, } } @@ -289,10 +289,10 @@ func (h *AppServeAppHandler) GetAppServeApps(w http.ResponseWriter, r *http.Requ // @Router /organizations/{organizationId}/projects/{projectId}/app-serve-apps/{appId} [get] // @Security JWT func (h *AppServeAppHandler) GetAppServeApp(w http.ResponseWriter, r *http.Request) { - ////////////////////////////////////////////////////////////////////////////////////////// - // TODO: this API will'be deprecated soon once the new task-related API's are verified. - // Until then, this is available (except for stage info) just for backward compatibility. - ////////////////////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////////////////////// + // TODO: this API will'be deprecated soon once the new task-related API's are verified. + // Until then, this is available (except for stage info) just for backward compatibility. + ////////////////////////////////////////////////////////////////////////////////////////// vars := mux.Vars(r) @@ -333,7 +333,7 @@ func (h *AppServeAppHandler) GetAppServeApp(w http.ResponseWriter, r *http.Reque var out domain.GetAppServeAppResponse out.AppServeApp = *app - // NOTE: makeStages function's been changed to use task instead of app + // NOTE: makeStages function's been changed to use task instead of app //out.Stages = makeStages(app) ResponseJSON(w, r, http.StatusOK, out) diff --git a/internal/delivery/http/auth.go b/internal/delivery/http/auth.go index c04dd331..1b84dbde 100644 --- a/internal/delivery/http/auth.go +++ b/internal/delivery/http/auth.go @@ -30,9 +30,9 @@ type AuthHandler struct { usecase usecase.IAuthUsecase } -func NewAuthHandler(h usecase.IAuthUsecase) IAuthHandler { +func NewAuthHandler(h usecase.Usecase) IAuthHandler { return &AuthHandler{ - usecase: h, + usecase: h.Auth, } } diff --git a/internal/delivery/http/cloud-account.go b/internal/delivery/http/cloud-account.go index 64b6edab..dfce7a78 100644 --- a/internal/delivery/http/cloud-account.go +++ b/internal/delivery/http/cloud-account.go @@ -20,9 +20,9 @@ type CloudAccountHandler struct { usecase usecase.ICloudAccountUsecase } -func NewCloudAccountHandler(h usecase.ICloudAccountUsecase) *CloudAccountHandler { +func NewCloudAccountHandler(h usecase.Usecase) *CloudAccountHandler { return &CloudAccountHandler{ - usecase: h, + usecase: h.CloudAccount, } } diff --git a/internal/delivery/http/cluster.go b/internal/delivery/http/cluster.go index e2bba0f2..5cd1848a 100644 --- a/internal/delivery/http/cluster.go +++ b/internal/delivery/http/cluster.go @@ -18,9 +18,9 @@ type ClusterHandler struct { usecase usecase.IClusterUsecase } -func NewClusterHandler(h usecase.IClusterUsecase) *ClusterHandler { +func NewClusterHandler(h usecase.Usecase) *ClusterHandler { return &ClusterHandler{ - usecase: h, + usecase: h.Cluster, } } diff --git a/internal/delivery/http/dashboard.go b/internal/delivery/http/dashboard.go index 46ae2f07..babc03ae 100644 --- a/internal/delivery/http/dashboard.go +++ b/internal/delivery/http/dashboard.go @@ -17,9 +17,9 @@ type DashboardHandler struct { usecase usecase.IDashboardUsecase } -func NewDashboardHandler(h usecase.IDashboardUsecase) *DashboardHandler { +func NewDashboardHandler(h usecase.Usecase) *DashboardHandler { return &DashboardHandler{ - usecase: h, + usecase: h.Dashboard, } } diff --git a/internal/delivery/http/organization.go b/internal/delivery/http/organization.go index 67c7e252..1d90f6a0 100644 --- a/internal/delivery/http/organization.go +++ b/internal/delivery/http/organization.go @@ -19,10 +19,10 @@ type OrganizationHandler struct { userUsecase usecase.IUserUsecase } -func NewOrganizationHandler(o usecase.IOrganizationUsecase, u usecase.IUserUsecase) *OrganizationHandler { +func NewOrganizationHandler(u usecase.Usecase) *OrganizationHandler { return &OrganizationHandler{ - usecase: o, - userUsecase: u, + usecase: u.Organization, + userUsecase: u.User, } } diff --git a/internal/delivery/http/project.go b/internal/delivery/http/project.go index fffd35ab..557ebc91 100644 --- a/internal/delivery/http/project.go +++ b/internal/delivery/http/project.go @@ -2,13 +2,14 @@ package http import ( "fmt" - "github.com/google/uuid" - "github.com/openinfradev/tks-api/internal/middleware/auth/request" - "github.com/openinfradev/tks-api/internal/serializer" "net/http" "strings" "time" + "github.com/google/uuid" + "github.com/openinfradev/tks-api/internal/middleware/auth/request" + "github.com/openinfradev/tks-api/internal/serializer" + "github.com/gorilla/mux" "github.com/openinfradev/tks-api/internal/usecase" "github.com/openinfradev/tks-api/pkg/domain" @@ -54,9 +55,9 @@ type ProjectHandler struct { usecase usecase.IProjectUsecase } -func NewProjectHandler(u usecase.IProjectUsecase) IProjectHandler { +func NewProjectHandler(u usecase.Usecase) IProjectHandler { return &ProjectHandler{ - usecase: u, + usecase: u.Project, } } diff --git a/internal/delivery/http/stack-template.go b/internal/delivery/http/stack-template.go index c2ad628c..4e699d35 100644 --- a/internal/delivery/http/stack-template.go +++ b/internal/delivery/http/stack-template.go @@ -20,9 +20,9 @@ type StackTemplateHandler struct { usecase usecase.IStackTemplateUsecase } -func NewStackTemplateHandler(h usecase.IStackTemplateUsecase) *StackTemplateHandler { +func NewStackTemplateHandler(h usecase.Usecase) *StackTemplateHandler { return &StackTemplateHandler{ - usecase: h, + usecase: h.StackTemplate, } } diff --git a/internal/delivery/http/stack.go b/internal/delivery/http/stack.go index d92f6d1e..1ec8987d 100644 --- a/internal/delivery/http/stack.go +++ b/internal/delivery/http/stack.go @@ -18,9 +18,9 @@ type StackHandler struct { usecase usecase.IStackUsecase } -func NewStackHandler(h usecase.IStackUsecase) *StackHandler { +func NewStackHandler(h usecase.Usecase) *StackHandler { return &StackHandler{ - usecase: h, + usecase: h.Stack, } } diff --git a/internal/delivery/http/user.go b/internal/delivery/http/user.go index 2144ec0a..85ad3ce4 100644 --- a/internal/delivery/http/user.go +++ b/internal/delivery/http/user.go @@ -37,9 +37,9 @@ type UserHandler struct { usecase usecase.IUserUsecase } -func NewUserHandler(h usecase.IUserUsecase) IUserHandler { +func NewUserHandler(h usecase.Usecase) IUserHandler { return &UserHandler{ - usecase: h, + usecase: h.User, } } diff --git a/internal/route/route.go b/internal/route/route.go index 951cb554..448e9361 100644 --- a/internal/route/route.go +++ b/internal/route/route.go @@ -4,13 +4,14 @@ import ( "bytes" "context" "fmt" - internalApi "github.com/openinfradev/tks-api/internal/delivery/api" - "github.com/openinfradev/tks-api/internal/middleware/audit" - "github.com/openinfradev/tks-api/internal/middleware/auth/requestRecoder" "io" "net/http" "time" + internalApi "github.com/openinfradev/tks-api/internal/delivery/api" + "github.com/openinfradev/tks-api/internal/middleware/audit" + "github.com/openinfradev/tks-api/internal/middleware/auth/requestRecoder" + "github.com/google/uuid" "github.com/gorilla/handlers" "github.com/gorilla/mux" @@ -52,6 +53,8 @@ func (r *StatusRecorder) WriteHeader(status int) { func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloak, asset http.Handler) http.Handler { r := mux.NewRouter() + cache := gcache.New(5*time.Minute, 10*time.Minute) + repoFactory := repository.Repository{ Auth: repository.NewAuthRepository(db), User: repository.NewUserRepository(db), @@ -64,20 +67,34 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa Alert: repository.NewAlertRepository(db), Project: repository.NewProjectRepository(db), } + + usecaseFactory := usecase.Usecase{ + Auth: usecase.NewAuthUsecase(repoFactory, kc), + User: usecase.NewUserUsecase(repoFactory, kc), + Cluster: usecase.NewClusterUsecase(repoFactory, argoClient, cache), + Organization: usecase.NewOrganizationUsecase(repoFactory, argoClient, kc), + AppGroup: usecase.NewAppGroupUsecase(repoFactory, argoClient), + AppServeApp: usecase.NewAppServeAppUsecase(repoFactory, argoClient), + CloudAccount: usecase.NewCloudAccountUsecase(repoFactory, argoClient), + StackTemplate: usecase.NewStackTemplateUsecase(repoFactory), + Dashboard: usecase.NewDashboardUsecase(repoFactory, cache), + Alert: usecase.NewAlertUsecase(repoFactory), + Stack: usecase.NewStackUsecase(repoFactory, argoClient, usecase.NewDashboardUsecase(repoFactory, cache)), + Project: usecase.NewProjectUsecase(repoFactory, argoClient), + } + customMiddleware := internalMiddleware.NewMiddleware( authenticator.NewAuthenticator(authKeycloak.NewKeycloakAuthenticator(kc)), authorizer.NewDefaultAuthorization(repoFactory), requestRecoder.NewDefaultRequestRecoder(), audit.NewDefaultAudit(repoFactory)) - cache := gcache.New(5*time.Minute, 10*time.Minute) - r.Use(loggingMiddleware) // [TODO] Transaction //r.Use(transactionMiddleware(db)) - authHandler := delivery.NewAuthHandler(usecase.NewAuthUsecase(repoFactory, kc)) + authHandler := delivery.NewAuthHandler(usecaseFactory) r.HandleFunc(API_PREFIX+API_VERSION+"/auth/login", authHandler.Login).Methods(http.MethodPost) r.HandleFunc(API_PREFIX+API_VERSION+"/auth/ping", authHandler.PingToken).Methods(http.MethodPost) r.Handle(API_PREFIX+API_VERSION+"/auth/logout", customMiddleware.Handle(internalApi.Logout, http.HandlerFunc(authHandler.Logout))).Methods(http.MethodPost) @@ -90,7 +107,7 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa //r.HandleFunc(API_PREFIX+API_VERSION+"/cookie-test", authHandler.CookieTest).Methods(http.MethodPost) //r.HandleFunc(API_PREFIX+API_VERSION+"/auth/callback", authHandler.CookieTestCallback).Methods(http.MethodGet) - userHandler := delivery.NewUserHandler(usecase.NewUserUsecase(repoFactory, kc)) + userHandler := delivery.NewUserHandler(usecaseFactory) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/users", customMiddleware.Handle(internalApi.CreateUser, http.HandlerFunc(userHandler.Create))).Methods(http.MethodPost) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/users", customMiddleware.Handle(internalApi.ListUser, http.HandlerFunc(userHandler.List))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/users/{accountId}", customMiddleware.Handle(internalApi.GetUser, http.HandlerFunc(userHandler.Get))).Methods(http.MethodGet) @@ -106,7 +123,7 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/my-profile/next-password-change", customMiddleware.Handle(internalApi.RenewPasswordExpiredDate, http.HandlerFunc(userHandler.RenewPasswordExpiredDate))).Methods(http.MethodPut) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/my-profile", customMiddleware.Handle(internalApi.DeleteMyProfile, http.HandlerFunc(userHandler.DeleteMyProfile))).Methods(http.MethodDelete) - organizationHandler := delivery.NewOrganizationHandler(usecase.NewOrganizationUsecase(repoFactory, argoClient, kc), usecase.NewUserUsecase(repoFactory, kc)) + organizationHandler := delivery.NewOrganizationHandler(usecaseFactory) r.Handle(API_PREFIX+API_VERSION+"/organizations", customMiddleware.Handle(internalApi.CreateOrganization, http.HandlerFunc(organizationHandler.CreateOrganization))).Methods(http.MethodPost) r.Handle(API_PREFIX+API_VERSION+"/organizations", customMiddleware.Handle(internalApi.GetOrganizations, http.HandlerFunc(organizationHandler.GetOrganizations))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}", customMiddleware.Handle(internalApi.GetOrganization, http.HandlerFunc(organizationHandler.GetOrganization))).Methods(http.MethodGet) @@ -114,7 +131,7 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}", customMiddleware.Handle(internalApi.UpdateOrganization, http.HandlerFunc(organizationHandler.UpdateOrganization))).Methods(http.MethodPut) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/primary-cluster", customMiddleware.Handle(internalApi.UpdatePrimaryCluster, http.HandlerFunc(organizationHandler.UpdatePrimaryCluster))).Methods(http.MethodPatch) - clusterHandler := delivery.NewClusterHandler(usecase.NewClusterUsecase(repoFactory, argoClient, cache)) + clusterHandler := delivery.NewClusterHandler(usecaseFactory) r.Handle(API_PREFIX+API_VERSION+"/clusters", customMiddleware.Handle(internalApi.CreateCluster, http.HandlerFunc(clusterHandler.CreateCluster))).Methods(http.MethodPost) r.Handle(API_PREFIX+API_VERSION+"/clusters", customMiddleware.Handle(internalApi.GetClusters, http.HandlerFunc(clusterHandler.GetClusters))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/clusters/import", customMiddleware.Handle(internalApi.ImportCluster, http.HandlerFunc(clusterHandler.ImportCluster))).Methods(http.MethodPost) @@ -126,7 +143,7 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa r.Handle(API_PREFIX+API_VERSION+"/clusters/{clusterId}/bootstrap-kubeconfig", customMiddleware.Handle(internalApi.GetBootstrapKubeconfig, http.HandlerFunc(clusterHandler.GetBootstrapKubeconfig))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/clusters/{clusterId}/nodes", customMiddleware.Handle(internalApi.GetNodes, http.HandlerFunc(clusterHandler.GetNodes))).Methods(http.MethodGet) - appGroupHandler := delivery.NewAppGroupHandler(usecase.NewAppGroupUsecase(repoFactory, argoClient)) + appGroupHandler := delivery.NewAppGroupHandler(usecaseFactory) r.Handle(API_PREFIX+API_VERSION+"/app-groups", customMiddleware.Handle(internalApi.CreateAppgroup, http.HandlerFunc(appGroupHandler.CreateAppGroup))).Methods(http.MethodPost) r.Handle(API_PREFIX+API_VERSION+"/app-groups", customMiddleware.Handle(internalApi.GetAppgroups, http.HandlerFunc(appGroupHandler.GetAppGroups))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/app-groups/{appGroupId}", customMiddleware.Handle(internalApi.GetAppgroup, http.HandlerFunc(appGroupHandler.GetAppGroup))).Methods(http.MethodGet) @@ -134,7 +151,7 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa r.Handle(API_PREFIX+API_VERSION+"/app-groups/{appGroupId}/applications", customMiddleware.Handle(internalApi.GetApplications, http.HandlerFunc(appGroupHandler.GetApplications))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/app-groups/{appGroupId}/applications", customMiddleware.Handle(internalApi.CreateApplication, http.HandlerFunc(appGroupHandler.CreateApplication))).Methods(http.MethodPost) - appServeAppHandler := delivery.NewAppServeAppHandler(usecase.NewAppServeAppUsecase(repoFactory, argoClient)) + appServeAppHandler := delivery.NewAppServeAppHandler(usecaseFactory) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects/{projectId}/app-serve-apps", customMiddleware.Handle(internalApi.CreateAppServeApp, http.HandlerFunc(appServeAppHandler.CreateAppServeApp))).Methods(http.MethodPost) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects/{projectId}/app-serve-apps", customMiddleware.Handle(internalApi.GetAppServeApps, http.HandlerFunc(appServeAppHandler.GetAppServeApps))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects/{projectId}/app-serve-apps/count", customMiddleware.Handle(internalApi.GetNumOfAppsOnStack, http.HandlerFunc(appServeAppHandler.GetNumOfAppsOnStack))).Methods(http.MethodGet) @@ -150,7 +167,7 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects/{projectId}/app-serve-apps/{appId}/endpoint", customMiddleware.Handle(internalApi.UpdateAppServeAppEndpoint, http.HandlerFunc(appServeAppHandler.UpdateAppServeAppEndpoint))).Methods(http.MethodPatch) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects/{projectId}/app-serve-apps/{appId}/rollback", customMiddleware.Handle(internalApi.RollbackAppServeApp, http.HandlerFunc(appServeAppHandler.RollbackAppServeApp))).Methods(http.MethodPost) - cloudAccountHandler := delivery.NewCloudAccountHandler(usecase.NewCloudAccountUsecase(repoFactory, argoClient)) + cloudAccountHandler := delivery.NewCloudAccountHandler(usecaseFactory) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/cloud-accounts", customMiddleware.Handle(internalApi.GetCloudAccounts, http.HandlerFunc(cloudAccountHandler.GetCloudAccounts))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/cloud-accounts", customMiddleware.Handle(internalApi.CreateCloudAccount, http.HandlerFunc(cloudAccountHandler.CreateCloudAccount))).Methods(http.MethodPost) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/cloud-accounts/name/{name}/existence", customMiddleware.Handle(internalApi.CheckCloudAccountName, http.HandlerFunc(cloudAccountHandler.CheckCloudAccountName))).Methods(http.MethodGet) @@ -161,20 +178,20 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/cloud-accounts/{cloudAccountId}/error", customMiddleware.Handle(internalApi.DeleteForceCloudAccount, http.HandlerFunc(cloudAccountHandler.DeleteForceCloudAccount))).Methods(http.MethodDelete) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/cloud-accounts/{cloudAccountId}/quotas", customMiddleware.Handle(internalApi.GetResourceQuota, http.HandlerFunc(cloudAccountHandler.GetResourceQuota))).Methods(http.MethodGet) - stackTemplateHandler := delivery.NewStackTemplateHandler(usecase.NewStackTemplateUsecase(repoFactory)) + stackTemplateHandler := delivery.NewStackTemplateHandler(usecaseFactory) r.Handle(API_PREFIX+API_VERSION+"/stack-templates", customMiddleware.Handle(internalApi.GetStackTemplates, http.HandlerFunc(stackTemplateHandler.GetStackTemplates))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/stack-templates", customMiddleware.Handle(internalApi.CreateStackTemplate, http.HandlerFunc(stackTemplateHandler.CreateStackTemplate))).Methods(http.MethodPost) r.Handle(API_PREFIX+API_VERSION+"/stack-templates/{stackTemplateId}", customMiddleware.Handle(internalApi.GetStackTemplate, http.HandlerFunc(stackTemplateHandler.GetStackTemplate))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/stack-templates/{stackTemplateId}", customMiddleware.Handle(internalApi.UpdateStackTemplate, http.HandlerFunc(stackTemplateHandler.UpdateStackTemplate))).Methods(http.MethodPut) r.Handle(API_PREFIX+API_VERSION+"/stack-templates/{stackTemplateId}", customMiddleware.Handle(internalApi.DeleteStackTemplate, http.HandlerFunc(stackTemplateHandler.DeleteStackTemplate))).Methods(http.MethodDelete) - dashboardHandler := delivery.NewDashboardHandler(usecase.NewDashboardUsecase(repoFactory, cache)) + dashboardHandler := delivery.NewDashboardHandler(usecaseFactory) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboard/charts", customMiddleware.Handle(internalApi.GetChartsDashboard, http.HandlerFunc(dashboardHandler.GetCharts))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboard/charts/{chartType}", customMiddleware.Handle(internalApi.GetChartDashboard, http.HandlerFunc(dashboardHandler.GetChart))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboard/stacks", customMiddleware.Handle(internalApi.GetStacksDashboard, http.HandlerFunc(dashboardHandler.GetStacks))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboard/resources", customMiddleware.Handle(internalApi.GetResourcesDashboard, http.HandlerFunc(dashboardHandler.GetResources))).Methods(http.MethodGet) - alertHandler := delivery.NewAlertHandler(usecase.NewAlertUsecase(repoFactory)) + alertHandler := delivery.NewAlertHandler(usecaseFactory) r.HandleFunc(SYSTEM_API_PREFIX+SYSTEM_API_VERSION+"/alerts", alertHandler.CreateAlert).Methods(http.MethodPost) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/alerts", customMiddleware.Handle(internalApi.GetAlerts, http.HandlerFunc(alertHandler.GetAlerts))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/alerts/{alertId}", customMiddleware.Handle(internalApi.GetAlert, http.HandlerFunc(alertHandler.GetAlert))).Methods(http.MethodGet) @@ -183,7 +200,7 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/alerts/{alertId}/actions", customMiddleware.Handle(internalApi.CreateAlertAction, http.HandlerFunc(alertHandler.CreateAlertAction))).Methods(http.MethodPost) //r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/alerts/{alertId}/actions/status", customMiddleware.Handle(http.HandlerFunc(alertHandler.UpdateAlertActionStatus))).Methods(http.MethodPatch) - stackHandler := delivery.NewStackHandler(usecase.NewStackUsecase(repoFactory, argoClient, usecase.NewDashboardUsecase(repoFactory, cache))) + stackHandler := delivery.NewStackHandler(usecaseFactory) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks", customMiddleware.Handle(internalApi.GetStacks, http.HandlerFunc(stackHandler.GetStacks))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks", customMiddleware.Handle(internalApi.CreateStack, http.HandlerFunc(stackHandler.CreateStack))).Methods(http.MethodPost) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/name/{name}/existence", customMiddleware.Handle(internalApi.CheckStackName, http.HandlerFunc(stackHandler.CheckStackName))).Methods(http.MethodGet) @@ -196,7 +213,7 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/favorite", customMiddleware.Handle(internalApi.DeleteFavoriteStack, http.HandlerFunc(stackHandler.DeleteFavorite))).Methods(http.MethodDelete) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/install", customMiddleware.Handle(internalApi.InstallStack, http.HandlerFunc(stackHandler.InstallStack))).Methods(http.MethodPost) - projectHandler := delivery.NewProjectHandler(usecase.NewProjectUsecase(repoFactory, argoClient)) + projectHandler := delivery.NewProjectHandler(usecaseFactory) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects", customMiddleware.Handle(internalApi.CreateProject, http.HandlerFunc(projectHandler.CreateProject))).Methods(http.MethodPost) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects", customMiddleware.Handle(internalApi.GetProjects, http.HandlerFunc(projectHandler.GetProjects))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects/{projectId}", customMiddleware.Handle(internalApi.GetProject, http.HandlerFunc(projectHandler.GetProject))).Methods(http.MethodGet) diff --git a/internal/usecase/usecase.go b/internal/usecase/usecase.go index d9a67132..b8838933 100644 --- a/internal/usecase/usecase.go +++ b/internal/usecase/usecase.go @@ -1,22 +1,16 @@ package usecase -import ( - kube "github.com/openinfradev/tks-api/internal/kubernetes" - gcache "github.com/patrickmn/go-cache" - "k8s.io/client-go/kubernetes" -) - -func GetKubeClient(cache *gcache.Cache, clusterId string) (*kubernetes.Clientset, error) { - const prefix = "CACHE_KEY_KUBE_CLIENT_" - value, found := cache.Get(prefix + clusterId) - if found { - return value.(*kubernetes.Clientset), nil - } - client, err := kube.GetClientFromClusterId(clusterId) - if err != nil { - return nil, err - } - - cache.Set(prefix+clusterId, client, gcache.DefaultExpiration) - return client, nil +type Usecase struct { + Auth IAuthUsecase + User IUserUsecase + Cluster IClusterUsecase + Organization IOrganizationUsecase + AppGroup IAppGroupUsecase + AppServeApp IAppServeAppUsecase + CloudAccount ICloudAccountUsecase + StackTemplate IStackTemplateUsecase + Dashboard IDashboardUsecase + Alert IAlertUsecase + Stack IStackUsecase + Project IProjectUsecase }