Rainbond/api/controller/application.go

193 lines
4.9 KiB
Go
Raw Normal View History

2020-09-17 15:45:46 +08:00
package controller
import (
"net/http"
2020-09-17 16:43:53 +08:00
"strconv"
2020-09-17 15:45:46 +08:00
2020-09-17 16:43:53 +08:00
"github.com/go-chi/chi"
2020-09-17 15:45:46 +08:00
"github.com/goodrain/rainbond/api/handler"
"github.com/goodrain/rainbond/api/middleware"
"github.com/goodrain/rainbond/api/model"
dbmodel "github.com/goodrain/rainbond/db/model"
httputil "github.com/goodrain/rainbond/util/http"
)
2020-09-27 16:27:46 +08:00
// ApplicationController -
type ApplicationController struct{}
2020-09-17 15:45:46 +08:00
// CreateApp -
2020-09-27 16:27:46 +08:00
func (a *ApplicationController) CreateApp(w http.ResponseWriter, r *http.Request) {
2020-09-17 15:45:46 +08:00
var tenantReq model.Application
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &tenantReq, nil) {
return
}
// get current tenant
tenant := r.Context().Value(middleware.ContextKey("tenant")).(*dbmodel.Tenants)
tenantReq.TenantID = tenant.UUID
// create app
app, err := handler.GetApplicationHandler().CreateApp(&tenantReq)
2020-09-18 11:39:05 +08:00
if err != nil {
2020-09-20 01:09:29 +08:00
httputil.ReturnBcodeError(r, w, err)
2020-09-18 11:39:05 +08:00
return
}
httputil.ReturnSuccess(r, w, app)
}
2020-11-09 15:48:35 +08:00
// BatchCreateApp -
func (a *ApplicationController) BatchCreateApp(w http.ResponseWriter, r *http.Request) {
2020-11-10 16:12:40 +08:00
var apps model.CreateAppRequest
2020-11-09 15:48:35 +08:00
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &apps, nil) {
return
}
// get current tenant
tenant := r.Context().Value(middleware.ContextKey("tenant")).(*dbmodel.Tenants)
2020-11-10 16:12:40 +08:00
respList, err := handler.GetApplicationHandler().BatchCreateApp(&apps, tenant.UUID)
if err != nil {
httputil.ReturnBcodeError(r, w, err)
return
2020-11-09 15:48:35 +08:00
}
httputil.ReturnSuccess(r, w, respList)
}
2020-09-18 11:39:05 +08:00
// UpdateApp -
2020-09-27 16:27:46 +08:00
func (a *ApplicationController) UpdateApp(w http.ResponseWriter, r *http.Request) {
2020-09-19 20:11:52 +08:00
var updateAppReq model.UpdateAppRequest
2020-09-18 11:39:05 +08:00
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &updateAppReq, nil) {
return
}
2020-09-19 20:11:52 +08:00
app := r.Context().Value(middleware.ContextKey("application")).(*dbmodel.Application)
2020-09-18 11:39:05 +08:00
2020-09-19 20:11:52 +08:00
// update app
app, err := handler.GetApplicationHandler().UpdateApp(app, updateAppReq)
2020-09-17 15:45:46 +08:00
if err != nil {
2020-09-20 01:09:29 +08:00
httputil.ReturnBcodeError(r, w, err)
2020-09-17 15:45:46 +08:00
return
}
httputil.ReturnSuccess(r, w, app)
}
2020-09-17 16:43:53 +08:00
// ListApps -
2020-09-27 16:27:46 +08:00
func (a *ApplicationController) ListApps(w http.ResponseWriter, r *http.Request) {
2020-09-20 02:07:10 +08:00
query := r.URL.Query()
appName := query.Get("app_name")
pageQuery := query.Get("page")
pageSizeQuery := query.Get("pageSize")
page, _ := strconv.Atoi(pageQuery)
2020-09-17 16:43:53 +08:00
if page == 0 {
page = 1
}
2020-09-20 02:07:10 +08:00
pageSize, _ := strconv.Atoi(pageSizeQuery)
2020-09-17 16:43:53 +08:00
if pageSize == 0 {
pageSize = 10
}
// get current tenantID
2020-09-19 21:17:40 +08:00
tenantID := r.Context().Value(middleware.ContextKey("tenant_id")).(string)
2020-09-17 16:43:53 +08:00
// List apps
resp, err := handler.GetApplicationHandler().ListApps(tenantID, appName, page, pageSize)
2020-09-17 16:43:53 +08:00
if err != nil {
2020-09-21 09:52:56 +08:00
httputil.ReturnBcodeError(r, w, err)
2020-09-17 16:43:53 +08:00
return
}
httputil.ReturnSuccess(r, w, resp)
}
2020-09-18 17:02:28 +08:00
// ListServices -
2020-09-27 16:27:46 +08:00
func (a *ApplicationController) ListServices(w http.ResponseWriter, r *http.Request) {
2020-09-18 17:02:28 +08:00
appID := chi.URLParam(r, "app_id")
2020-09-20 02:07:10 +08:00
query := r.URL.Query()
pageQuery := query.Get("page")
pageSizeQuery := query.Get("pageSize")
page, _ := strconv.Atoi(pageQuery)
2020-09-18 17:02:28 +08:00
if page == 0 {
page = 1
}
2020-09-20 02:07:10 +08:00
pageSize, _ := strconv.Atoi(pageSizeQuery)
2020-09-18 17:02:28 +08:00
if pageSize == 0 {
pageSize = 10
}
2020-09-20 01:09:29 +08:00
// List services
resp, err := handler.GetServiceManager().GetServicesByAppID(appID, page, pageSize)
2020-09-18 17:02:28 +08:00
if err != nil {
2020-09-21 09:52:56 +08:00
httputil.ReturnBcodeError(r, w, err)
2020-09-18 17:02:28 +08:00
return
}
httputil.ReturnSuccess(r, w, resp)
}
2020-09-18 18:08:32 +08:00
// DeleteApp -
2020-09-27 16:27:46 +08:00
func (a *ApplicationController) DeleteApp(w http.ResponseWriter, r *http.Request) {
2020-09-18 18:08:32 +08:00
appID := chi.URLParam(r, "app_id")
// Delete application
err := handler.GetApplicationHandler().DeleteApp(appID)
2020-09-18 18:08:32 +08:00
if err != nil {
2020-09-20 01:09:29 +08:00
httputil.ReturnBcodeError(r, w, err)
2020-09-18 18:08:32 +08:00
return
}
httputil.ReturnSuccess(r, w, nil)
}
2020-10-08 14:01:37 +08:00
2020-09-30 11:03:14 +08:00
func (a *ApplicationController) BatchUpdateComponentPorts(w http.ResponseWriter, r *http.Request) {
2020-09-27 16:27:46 +08:00
var appPorts []*model.AppPort
if err := httputil.ReadEntity(r, &appPorts); err != nil {
httputil.ReturnBcodeError(r, w, err)
return
}
for _, port := range appPorts {
if err := httputil.ValidateStruct(port); err != nil {
httputil.ReturnBcodeError(r, w, err)
return
}
}
appID := r.Context().Value(middleware.ContextKey("app_id")).(string)
2020-09-30 11:03:14 +08:00
if err := handler.GetApplicationHandler().BatchUpdateComponentPorts(appID, appPorts); err != nil {
2020-09-27 16:27:46 +08:00
httputil.ReturnBcodeError(r, w, err)
return
}
httputil.ReturnSuccess(r, w, nil)
}
2020-09-28 16:43:27 +08:00
func (a *ApplicationController) GetAppStatus(w http.ResponseWriter, r *http.Request) {
appID := r.Context().Value(middleware.ContextKey("app_id")).(string)
res, err := handler.GetApplicationHandler().GetStatus(appID)
if err != nil {
httputil.ReturnBcodeError(r, w, err)
return
}
httputil.ReturnSuccess(r, w, res)
}
2020-10-08 14:01:37 +08:00
// BatchBindService -
func (a *ApplicationController) BatchBindService(w http.ResponseWriter, r *http.Request) {
2020-10-08 14:01:37 +08:00
appID := chi.URLParam(r, "app_id")
var bindServiceReq model.BindServiceRequest
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &bindServiceReq, nil) {
return
}
// bind service
err := handler.GetApplicationHandler().BatchBindService(appID, bindServiceReq)
if err != nil {
httputil.ReturnBcodeError(r, w, err)
return
}
httputil.ReturnSuccess(r, w, nil)
}