Skip to content

Commit

Permalink
feature. add stack resource info to fetching stacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
ktkfree committed Sep 25, 2023
1 parent 43bdb05 commit f1c09ba
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 18 deletions.
3 changes: 3 additions & 0 deletions api/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5814,6 +5814,9 @@ const docTemplate = `{
"primaryCluster": {
"type": "boolean"
},
"resource": {
"$ref": "#/definitions/domain.DashboardStackResponse"
},
"stackTemplate": {
"$ref": "#/definitions/domain.SimpleStackTemplateResponse"
},
Expand Down
3 changes: 3 additions & 0 deletions api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -5807,6 +5807,9 @@
"primaryCluster": {
"type": "boolean"
},
"resource": {
"$ref": "#/definitions/domain.DashboardStackResponse"
},
"stackTemplate": {
"$ref": "#/definitions/domain.SimpleStackTemplateResponse"
},
Expand Down
2 changes: 2 additions & 0 deletions api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,8 @@ definitions:
type: string
primaryCluster:
type: boolean
resource:
$ref: '#/definitions/domain.DashboardStackResponse'
stackTemplate:
$ref: '#/definitions/domain.SimpleStackTemplateResponse'
status:
Expand Down
3 changes: 3 additions & 0 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func migrateSchema(db *gorm.DB) error {
if err := db.AutoMigrate(&repository.ClusterFavorite{}); err != nil {
return err
}
if err := db.AutoMigrate(&repository.ClusterHost{}); err != nil {
return err
}

// Services
if err := db.AutoMigrate(&repository.AppGroup{}); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/repository/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Cluster struct {
StackTemplateId uuid.UUID
StackTemplate StackTemplate `gorm:"foreignKey:StackTemplateId"`
Favorites *[]ClusterFavorite
ByohHosts *[]ClusterByohHost
ByohHosts *[]ClusterHost
ClusterType domain.ClusterType `gorm:"default:0"`
TksCpNode int
TksCpNodeMax int
Expand Down Expand Up @@ -94,7 +94,7 @@ type ClusterFavorite struct {
User User `gorm:"foreignKey:UserId"`
}

type ClusterByohHost struct {
type ClusterHost struct {
ID uuid.UUID `gorm:"primarykey"`
ClusterId domain.ClusterId
Cluster Cluster `gorm:"foreignKey:ClusterId"`
Expand Down
24 changes: 12 additions & 12 deletions internal/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,6 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa
r.Handle(API_PREFIX+API_VERSION+"/stack-templates/{stackTemplateId}", authMiddleware.Handle(http.HandlerFunc(stackTemplateHandler.UpdateStackTemplate))).Methods(http.MethodPut)
r.Handle(API_PREFIX+API_VERSION+"/stack-templates/{stackTemplateId}", authMiddleware.Handle(http.HandlerFunc(stackTemplateHandler.DeleteStackTemplate))).Methods(http.MethodDelete)

stackHandler := delivery.NewStackHandler(usecase.NewStackUsecase(repoFactory, argoClient))
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks", authMiddleware.Handle(http.HandlerFunc(stackHandler.GetStacks))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks", authMiddleware.Handle(http.HandlerFunc(stackHandler.CreateStack))).Methods(http.MethodPost)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/name/{name}/existence", authMiddleware.Handle(http.HandlerFunc(stackHandler.CheckStackName))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}", authMiddleware.Handle(http.HandlerFunc(stackHandler.GetStack))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}", authMiddleware.Handle(http.HandlerFunc(stackHandler.UpdateStack))).Methods(http.MethodPut)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}", authMiddleware.Handle(http.HandlerFunc(stackHandler.DeleteStack))).Methods(http.MethodDelete)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/kube-config", authMiddleware.Handle(http.HandlerFunc(stackHandler.GetStackKubeConfig))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/status", authMiddleware.Handle(http.HandlerFunc(stackHandler.GetStackStatus))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/favorite", authMiddleware.Handle(http.HandlerFunc(stackHandler.SetFavorite))).Methods(http.MethodPost)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/favorite", authMiddleware.Handle(http.HandlerFunc(stackHandler.DeleteFavorite))).Methods(http.MethodDelete)

dashboardHandler := delivery.NewDashboardHandler(usecase.NewDashboardUsecase(repoFactory, cache))
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboard/charts", authMiddleware.Handle(http.HandlerFunc(dashboardHandler.GetCharts))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboard/charts/{chartType}", authMiddleware.Handle(http.HandlerFunc(dashboardHandler.GetChart))).Methods(http.MethodGet)
Expand All @@ -184,6 +172,18 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/alerts/{alertId}/actions", authMiddleware.Handle(http.HandlerFunc(alertHandler.CreateAlertAction))).Methods(http.MethodPost)
//r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/alerts/{alertId}/actions/status", authMiddleware.Handle(http.HandlerFunc(alertHandler.UpdateAlertActionStatus))).Methods(http.MethodPatch)

stackHandler := delivery.NewStackHandler(usecase.NewStackUsecase(repoFactory, argoClient, usecase.NewDashboardUsecase(repoFactory, cache)))
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks", authMiddleware.Handle(http.HandlerFunc(stackHandler.GetStacks))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks", authMiddleware.Handle(http.HandlerFunc(stackHandler.CreateStack))).Methods(http.MethodPost)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/name/{name}/existence", authMiddleware.Handle(http.HandlerFunc(stackHandler.CheckStackName))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}", authMiddleware.Handle(http.HandlerFunc(stackHandler.GetStack))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}", authMiddleware.Handle(http.HandlerFunc(stackHandler.UpdateStack))).Methods(http.MethodPut)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}", authMiddleware.Handle(http.HandlerFunc(stackHandler.DeleteStack))).Methods(http.MethodDelete)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/kube-config", authMiddleware.Handle(http.HandlerFunc(stackHandler.GetStackKubeConfig))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/status", authMiddleware.Handle(http.HandlerFunc(stackHandler.GetStackStatus))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/favorite", authMiddleware.Handle(http.HandlerFunc(stackHandler.SetFavorite))).Methods(http.MethodPost)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/favorite", authMiddleware.Handle(http.HandlerFunc(stackHandler.DeleteFavorite))).Methods(http.MethodDelete)

r.HandleFunc(API_PREFIX+API_VERSION+"/alerttest", alertHandler.CreateAlert).Methods(http.MethodPost)
// assets
r.PathPrefix("/api/").HandlerFunc(http.NotFound)
Expand Down
6 changes: 3 additions & 3 deletions internal/usecase/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ func (u *DashboardUsecase) GetStacks(ctx context.Context, organizationId string)
cpu := u.getStackCpu(stackCpu.Data.Result, cluster.ID.String())

if cpu != "" {
cpu = cpu + " %"
cpu = cpu + "%"
}
if memory != "" {
memory = memory + " %"
memory = memory + "%"
}
if disk != "" {
disk = disk + " %"
disk = disk + "%"
}

dashboardStack.Cpu = cpu
Expand Down
15 changes: 14 additions & 1 deletion internal/usecase/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ type StackUsecase struct {
stackTemplateRepo repository.IStackTemplateRepository
appServeAppRepo repository.IAppServeAppRepository
argo argowf.ArgoClient
dashbordUsecase IDashboardUsecase
}

func NewStackUsecase(r repository.Repository, argoClient argowf.ArgoClient) IStackUsecase {
func NewStackUsecase(r repository.Repository, argoClient argowf.ArgoClient, dashbordUsecase IDashboardUsecase) IStackUsecase {
return &StackUsecase{
clusterRepo: r.Cluster,
appGroupRepo: r.AppGroup,
Expand All @@ -54,6 +55,7 @@ func NewStackUsecase(r repository.Repository, argoClient argowf.ArgoClient) ISta
stackTemplateRepo: r.StackTemplate,
appServeAppRepo: r.AppServeApp,
argo: argoClient,
dashbordUsecase: dashbordUsecase,
}
}

Expand Down Expand Up @@ -224,6 +226,8 @@ func (u *StackUsecase) Fetch(ctx context.Context, organizationId string, pg *pag
return out, err
}

stackResources, err := u.dashbordUsecase.GetStacks(ctx, organizationId)

for _, cluster := range clusters {
appGroups, err := u.appGroupRepo.Fetch(cluster.ID, nil)
if err != nil {
Expand All @@ -246,6 +250,15 @@ func (u *StackUsecase) Fetch(ctx context.Context, organizationId string, pg *pag
}
}
}

for _, resource := range stackResources {
if resource.ID == domain.StackId(cluster.ID) {
if err := serializer.Map(resource, &outStack.Resource); err != nil {
log.Error(err)
}
}
}

out = append(out, outStack)
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/domain/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type Stack = struct {
UpdatedAt time.Time
Favorited bool
AdminClusterUrl string
Resource DashboardStackResponse
}

type StackConf struct {
Expand Down Expand Up @@ -163,6 +164,7 @@ type StackResponse struct {
Updator SimpleUserResponse `json:"updator,omitempty"`
Favorited bool `json:"favorited"`
AdminClusterUrl string `json:"adminClusterUrl,omitempty"`
Resource DashboardStackResponse `json:"resource,omitempty"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
Expand Down

0 comments on commit f1c09ba

Please sign in to comment.