Skip to content

Commit 0e2932b

Browse files
committed
add vms and clusters per user while listing all
1 parent 8f86730 commit 0e2932b

4 files changed

Lines changed: 146 additions & 168 deletions

File tree

server/app/admin_handler.go

Lines changed: 28 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ type SetPricesInput struct {
5353
PublicIP float64 `json:"public_ip"`
5454
}
5555

56-
type ListDeploymentsResponse struct {
56+
type UserResponse struct {
57+
*models.User
5758
VMs []models.VM `json:"vms"`
5859
K8S []models.K8sCluster `json:"k8s"`
5960
}
@@ -66,7 +67,7 @@ type ListDeploymentsResponse struct {
6667
// @Accept json
6768
// @Produce json
6869
// @Security BearerAuth
69-
// @Success 200 {object} []models.User
70+
// @Success 200 {object} []UserResponse
7071
// @Failure 400 {object} Response
7172
// @Failure 401 {object} Response
7273
// @Failure 404 {object} Response
@@ -86,9 +87,33 @@ func (a *App) GetAllUsersHandler(req *http.Request) (interface{}, Response) {
8687
return nil, InternalServerError(errors.New(internalServerErrorMsg))
8788
}
8889

90+
var allUsers []UserResponse
91+
92+
for _, user := range users {
93+
// vms
94+
vms, err := a.db.GetAllVms(user.ID.String())
95+
if err != nil && err != gorm.ErrRecordNotFound {
96+
log.Error().Err(err).Send()
97+
return nil, InternalServerError(errors.New(internalServerErrorMsg))
98+
}
99+
100+
// k8s clusters
101+
clusters, err := a.db.GetAllK8s(user.ID.String())
102+
if err != nil && err != gorm.ErrRecordNotFound {
103+
log.Error().Err(err).Send()
104+
return nil, InternalServerError(errors.New(internalServerErrorMsg))
105+
}
106+
107+
allUsers = append(allUsers, UserResponse{
108+
User: &user,
109+
VMs: vms,
110+
K8S: clusters,
111+
})
112+
}
113+
89114
return ResponseMsg{
90115
Message: "Users are found",
91-
Data: users,
116+
Data: allUsers,
92117
}, Ok()
93118
}
94119

@@ -319,70 +344,6 @@ func (a *App) DeleteAllDeploymentsHandler(req *http.Request) (interface{}, Respo
319344
}, Ok()
320345
}
321346

322-
// ListDeploymentsHandler lists all users' deployments
323-
// Example endpoint: List all users' deployments
324-
// @Summary List all users' deployments
325-
// @Description List all users' deployments
326-
// @Tags Admin
327-
// @Accept json
328-
// @Produce json
329-
// @Security BearerAuth
330-
// @Success 200 {object} ListDeploymentsResponse
331-
// @Failure 400 {object} Response
332-
// @Failure 401 {object} Response
333-
// @Failure 404 {object} Response
334-
// @Failure 500 {object} Response
335-
// @Router /deployments [get]
336-
func (a *App) ListDeploymentsHandler(req *http.Request) (interface{}, Response) {
337-
users, err := a.db.ListAllUsers()
338-
if err == gorm.ErrRecordNotFound || len(users) == 0 {
339-
return ResponseMsg{
340-
Message: "Users are not found",
341-
}, Ok()
342-
}
343-
344-
if err != nil {
345-
log.Error().Err(err).Send()
346-
return nil, InternalServerError(errors.New(internalServerErrorMsg))
347-
}
348-
349-
var allVMs []models.VM
350-
var allClusters []models.K8sCluster
351-
352-
for _, user := range users {
353-
// vms
354-
vms, err := a.db.GetAllVms(user.ID.String())
355-
if err == gorm.ErrRecordNotFound || len(vms) == 0 {
356-
log.Error().Err(err).Str("userID", user.ID.String()).Msg("Virtual machines are not found")
357-
continue
358-
}
359-
if err != nil {
360-
log.Error().Err(err).Send()
361-
return nil, InternalServerError(errors.New(internalServerErrorMsg))
362-
}
363-
364-
allVMs = append(allVMs, vms...)
365-
366-
// k8s clusters
367-
clusters, err := a.db.GetAllK8s(user.ID.String())
368-
if err == gorm.ErrRecordNotFound || len(clusters) == 0 {
369-
log.Error().Err(err).Str("userID", user.ID.String()).Msg("Kubernetes clusters are not found")
370-
continue
371-
}
372-
if err != nil {
373-
log.Error().Err(err).Send()
374-
return nil, InternalServerError(errors.New(internalServerErrorMsg))
375-
}
376-
377-
allClusters = append(allClusters, clusters...)
378-
}
379-
380-
return ResponseMsg{
381-
Message: "Deployments are listed successfully",
382-
Data: ListDeploymentsResponse{VMs: allVMs, K8S: allClusters},
383-
}, Ok()
384-
}
385-
386347
// UpdateMaintenanceHandler updates maintenance flag
387348
// Example endpoint: Updates maintenance flag
388349
// @Summary Updates maintenance flag

server/app/app.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ func (a *App) registerHandlers() {
190190
balanceRouter.HandleFunc("", WrapFunc(a.GetBalanceHandler)).Methods("GET", "OPTIONS")
191191
maintenanceRouter.HandleFunc("", WrapFunc(a.UpdateMaintenanceHandler)).Methods("PUT", "OPTIONS")
192192
deploymentsRouter.HandleFunc("", WrapFunc(a.DeleteAllDeploymentsHandler)).Methods("DELETE", "OPTIONS")
193-
deploymentsRouter.HandleFunc("", WrapFunc(a.ListDeploymentsHandler)).Methods("GET", "OPTIONS")
194193
deploymentsRouter.HandleFunc("/count", WrapFunc(a.GetDlsCountHandler)).Methods("GET", "OPTIONS")
195194
nextLaunchRouter.HandleFunc("", WrapFunc(a.UpdateNextLaunchHandler)).Methods("PUT", "OPTIONS")
196195

server/docs/docs.go

Lines changed: 70 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -117,48 +117,6 @@ const docTemplate = `{
117117
}
118118
},
119119
"/deployments": {
120-
"get": {
121-
"security": [
122-
{
123-
"BearerAuth": []
124-
}
125-
],
126-
"description": "List all users' deployments",
127-
"consumes": [
128-
"application/json"
129-
],
130-
"produces": [
131-
"application/json"
132-
],
133-
"tags": [
134-
"Admin"
135-
],
136-
"summary": "List all users' deployments",
137-
"responses": {
138-
"200": {
139-
"description": "OK",
140-
"schema": {
141-
"$ref": "#/definitions/app.ListDeploymentsResponse"
142-
}
143-
},
144-
"400": {
145-
"description": "Bad Request",
146-
"schema": {}
147-
},
148-
"401": {
149-
"description": "Unauthorized",
150-
"schema": {}
151-
},
152-
"404": {
153-
"description": "Not Found",
154-
"schema": {}
155-
},
156-
"500": {
157-
"description": "Internal Server Error",
158-
"schema": {}
159-
}
160-
}
161-
},
162120
"delete": {
163121
"security": [
164122
{
@@ -1402,7 +1360,7 @@ const docTemplate = `{
14021360
"schema": {
14031361
"type": "array",
14041362
"items": {
1405-
"$ref": "#/definitions/models.User"
1363+
"$ref": "#/definitions/app.UserResponse"
14061364
}
14071365
}
14081366
},
@@ -2805,23 +2763,6 @@ const docTemplate = `{
28052763
}
28062764
}
28072765
},
2808-
"app.ListDeploymentsResponse": {
2809-
"type": "object",
2810-
"properties": {
2811-
"k8s": {
2812-
"type": "array",
2813-
"items": {
2814-
"$ref": "#/definitions/models.K8sCluster"
2815-
}
2816-
},
2817-
"vms": {
2818-
"type": "array",
2819-
"items": {
2820-
"$ref": "#/definitions/models.VM"
2821-
}
2822-
}
2823-
}
2824-
},
28252766
"app.PayInvoiceInput": {
28262767
"type": "object",
28272768
"required": [
@@ -2995,6 +2936,75 @@ const docTemplate = `{
29952936
}
29962937
}
29972938
},
2939+
"app.UserResponse": {
2940+
"type": "object",
2941+
"required": [
2942+
"email",
2943+
"first_name",
2944+
"hashed_password",
2945+
"last_name"
2946+
],
2947+
"properties": {
2948+
"admin": {
2949+
"description": "checks if user type is admin",
2950+
"type": "boolean"
2951+
},
2952+
"balance": {
2953+
"type": "number"
2954+
},
2955+
"code": {
2956+
"type": "integer"
2957+
},
2958+
"email": {
2959+
"type": "string"
2960+
},
2961+
"first_name": {
2962+
"type": "string"
2963+
},
2964+
"hashed_password": {
2965+
"type": "array",
2966+
"items": {
2967+
"type": "integer"
2968+
}
2969+
},
2970+
"id": {
2971+
"type": "string"
2972+
},
2973+
"k8s": {
2974+
"type": "array",
2975+
"items": {
2976+
"$ref": "#/definitions/models.K8sCluster"
2977+
}
2978+
},
2979+
"last_name": {
2980+
"type": "string"
2981+
},
2982+
"ssh_key": {
2983+
"type": "string"
2984+
},
2985+
"stripe_customer_id": {
2986+
"type": "string"
2987+
},
2988+
"stripe_default_payment_id": {
2989+
"type": "string"
2990+
},
2991+
"updated_at": {
2992+
"type": "string"
2993+
},
2994+
"verified": {
2995+
"type": "boolean"
2996+
},
2997+
"vms": {
2998+
"type": "array",
2999+
"items": {
3000+
"$ref": "#/definitions/models.VM"
3001+
}
3002+
},
3003+
"voucher_balance": {
3004+
"type": "number"
3005+
}
3006+
}
3007+
},
29983008
"app.VerifyCodeInput": {
29993009
"type": "object",
30003010
"required": [

0 commit comments

Comments
 (0)