Rainbond/api/api_routers/version2/v2Routers.go
2019-08-22 16:07:26 +08:00

297 lines
16 KiB
Go

// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package version2
import (
"github.com/go-chi/chi"
"github.com/goodrain/rainbond/api/controller"
"github.com/goodrain/rainbond/api/middleware"
)
//V2 v2
type V2 struct{}
//Routes routes
func (v2 *V2) Routes() chi.Router {
r := chi.NewRouter()
r.Get("/show", controller.GetManager().Show)
r.Post("/show", controller.GetManager().Show)
r.Mount("/tenants", v2.tenantRouter())
r.Mount("/nodes", v2.nodesRouter())
r.Mount("/notificationEvent", v2.notificationEventRouter())
r.Mount("/cluster", v2.clusterRouter())
r.Mount("/resources", v2.resourcesRouter())
r.Mount("/prometheus", v2.prometheusRouter())
r.Get("/event", controller.GetManager().Event)
r.Mount("/app", v2.appRouter())
r.Get("/health", controller.GetManager().Health)
r.Post("/alertmanager-webhook", controller.GetManager().AlertManagerWebHook)
r.Get("/version", controller.GetManager().Version)
r.Mount("/port", v2.portRouter())
r.Mount("/events", v2.eventsRouter())
return r
}
func (v2 *V2) eventsRouter() chi.Router {
r := chi.NewRouter()
// get target's event list with page
r.Post("/{target}/{targetID}", controller.GetManager().EventsByTarget)
// get target's event log
r.Get("/{eventID}/log", controller.GetManager().EventLogByEventID)
return r
}
func (v2 *V2) tenantRouter() chi.Router {
r := chi.NewRouter()
r.Post("/", controller.GetManager().Tenants)
r.Mount("/{tenant_name}", v2.tenantNameRouter())
r.Get("/", controller.GetManager().Tenants)
r.Get("/services-count", controller.GetManager().ServicesCount)
return r
}
func (v2 *V2) tenantNameRouter() chi.Router {
r := chi.NewRouter()
//初始化租户和服务信
r.Use(middleware.InitTenant)
r.Put("/", controller.GetManager().Tenant)
r.Get("/", controller.GetManager().Tenant)
r.Delete("/", controller.GetManager().Tenant)
//租户中的日志
r.Post("/event-log", controller.GetManager().TenantLogByAction)
r.Get("/protocols", controller.GetManager().GetSupportProtocols)
//插件预安装
r.Post("/transplugins", controller.GetManager().TransPlugins)
//代码检测
r.Post("/code-check", controller.GetManager().CheckCode)
r.Post("/servicecheck", controller.Check)
r.Get("/servicecheck/{uuid}", controller.GetServiceCheckInfo)
r.Get("/resources", controller.GetManager().SingleTenantResources)
r.Get("/services", controller.GetManager().ServicesInfo)
//创建应用
r.Post("/services", controller.GetManager().CreateService)
r.Post("/plugin", controller.GetManager().PluginAction)
r.Post("/plugins/{plugin_id}/share", controller.GetManager().SharePlugin)
r.Get("/plugins/{plugin_id}/share/{share_id}", controller.GetManager().SharePluginResult)
r.Get("/plugin", controller.GetManager().PluginAction)
r.Post("/services_status", controller.GetManager().StatusServiceList)
r.Mount("/services/{service_alias}", v2.serviceRouter())
r.Mount("/plugin/{plugin_id}", v2.pluginRouter())
r.Get("/event", controller.GetManager().Event)
r.Get("/chargesverify", controller.ChargesVerifyController)
//get some service pod info
r.Get("/pods", controller.Pods)
//app backup
r.Get("/groupapp/backups", controller.Backups)
r.Post("/groupapp/backups", controller.NewBackups)
r.Post("/groupapp/backupcopy", controller.BackupCopy)
r.Get("/groupapp/backups/{backup_id}", controller.GetBackup)
r.Delete("/groupapp/backups/{backup_id}", controller.DeleteBackup)
r.Post("/groupapp/backups/{backup_id}/restore", controller.Restore)
r.Get("/groupapp/backups/{backup_id}/restore/{restore_id}", controller.RestoreResult)
r.Post("/deployversions", controller.GetManager().GetManyDeployVersion)
//团队资源限制
r.Post("/limit_memory", controller.GetManager().LimitTenantMemory)
r.Get("/limit_memory", controller.GetManager().TenantResourcesStatus)
// Gateway
r.Post("/http-rule", controller.GetManager().HTTPRule)
r.Delete("/http-rule", controller.GetManager().HTTPRule)
r.Put("/http-rule", controller.GetManager().HTTPRule)
r.Post("/tcp-rule", controller.GetManager().TCPRule)
r.Delete("/tcp-rule", controller.GetManager().TCPRule)
r.Put("/tcp-rule", controller.GetManager().TCPRule)
//batch operation
r.Post("/batchoperation", controller.BatchOperation)
return r
}
func (v2 *V2) serviceRouter() chi.Router {
r := chi.NewRouter()
//初始化应用信息
r.Use(middleware.InitService)
//应用部署(act)
//r.Post("/deploy", controller.GetManager().DeployService)
r.Put("/", middleware.WrapEL(controller.GetManager().UpdateService, middleware.ST, "update-service", middleware.SYNEVENTTYPE))
//应用构建(act)
r.Post("/build", middleware.WrapEL(controller.GetManager().BuildService, middleware.ST, "build-service", middleware.ASYNEVENTTYPE))
//应用起停
r.Post("/start", middleware.WrapEL(controller.GetManager().StartService, middleware.ST, "start-service", middleware.ASYNEVENTTYPE))
r.Post("/stop", middleware.WrapEL(controller.GetManager().StopService, middleware.ST, "stop-service", middleware.ASYNEVENTTYPE))
r.Post("/restart", middleware.WrapEL(controller.GetManager().RestartService, middleware.ST, "restart-service", middleware.ASYNEVENTTYPE))
//应用伸缩
r.Put("/vertical", middleware.WrapEL(controller.GetManager().VerticalService, middleware.ST, "vertical-service", middleware.ASYNEVENTTYPE))
r.Put("/horizontal", middleware.WrapEL(controller.GetManager().HorizontalService, middleware.ST, "horizontal-service", middleware.ASYNEVENTTYPE))
//设置应用语言(act)
r.Post("/language", middleware.WrapEL(controller.GetManager().SetLanguage, middleware.ST, "set-language", middleware.SYNEVENTTYPE))
//应用信息获取修改与删除(source)
r.Get("/", controller.GetManager().SingleServiceInfo)
r.Delete("/", middleware.WrapEL(controller.GetManager().SingleServiceInfo, middleware.ST, "delete-service", middleware.SYNEVENTTYPE))
//应用升级(act)
r.Post("/upgrade", middleware.WrapEL(controller.GetManager().UpgradeService, middleware.ST, "upgrade-service", middleware.SYNEVENTTYPE))
//应用状态获取(act)
r.Get("/status", controller.GetManager().StatusService)
//构建版本列表
r.Get("/build-list", controller.GetManager().BuildList)
//构建版本操作
r.Get("/build-version/{build_version}", controller.GetManager().BuildVersionInfo)
r.Get("/deployversion", controller.GetManager().GetDeployVersion)
r.Delete("/build-version/{build_version}", middleware.WrapEL(controller.GetManager().BuildVersionInfo, middleware.ST, "delete-buildversion", middleware.SYNEVENTTYPE))
//应用分享
r.Post("/share", middleware.WrapEL(controller.GetManager().Share, middleware.ST, "share-service", middleware.ASYNEVENTTYPE))
r.Get("/share/{share_id}", controller.GetManager().ShareResult)
//应用日志相关
r.Post("/log", controller.GetManager().Logs)
r.Get("/log-file", controller.GetManager().LogList)
//r.Get("/log-file/{fileName}", controller.GetManager().LogFile)
r.Get("/log-instance", controller.GetManager().LogSocket)
r.Post("/event-log", controller.GetManager().LogByAction)
//应用依赖关系增加与删除(source)
r.Post("/dependency", middleware.WrapEL(controller.GetManager().Dependency, middleware.ST, "add-service-dependency", middleware.SYNEVENTTYPE))
r.Delete("/dependency", middleware.WrapEL(controller.GetManager().Dependency, middleware.ST, "delete-service-dependency", middleware.SYNEVENTTYPE))
//环境变量增删改(source)
r.Post("/env", middleware.WrapEL(controller.GetManager().Env, middleware.ST, "add-service-env", middleware.SYNEVENTTYPE))
r.Put("/env", middleware.WrapEL(controller.GetManager().Env, middleware.ST, "update-service-env", middleware.SYNEVENTTYPE))
r.Delete("/env", middleware.WrapEL(controller.GetManager().Env, middleware.ST, "delete-service-env", middleware.SYNEVENTTYPE))
//端口变量增删改(source)
r.Post("/ports", middleware.WrapEL(controller.GetManager().Ports, middleware.ST, "add-service-port", middleware.SYNEVENTTYPE))
r.Put("/ports", middleware.WrapEL(controller.GetManager().PutPorts, middleware.ST, "update-service-port-old", middleware.SYNEVENTTYPE))
r.Put("/ports/{port}", middleware.WrapEL(controller.GetManager().Ports, middleware.ST, "update-service-port", middleware.SYNEVENTTYPE))
r.Delete("/ports/{port}", middleware.WrapEL(controller.GetManager().Ports, middleware.ST, "delete-service-port", middleware.SYNEVENTTYPE))
r.Put("/ports/{port}/outer", middleware.WrapEL(controller.GetManager().PortOuterController, middleware.ST, "handle-service-outerport", middleware.SYNEVENTTYPE))
r.Put("/ports/{port}/inner", middleware.WrapEL(controller.GetManager().PortInnerController, middleware.ST, "handle-service-innerport", middleware.SYNEVENTTYPE))
r.Put("/ports/{port}/changelbport", middleware.WrapEL(controller.GetManager().ChangeLBPort, middleware.ST, "change-service-lbport", middleware.SYNEVENTTYPE))
//应用版本回滚(act)
r.Post("/rollback", middleware.WrapEL(controller.GetManager().RollBack, middleware.ST, "rollback-service", middleware.ASYNEVENTTYPE))
//持久化信息API v2.1 支持多种持久化格式
r.Post("/volumes", middleware.WrapEL(controller.AddVolume, middleware.ST, "add-service-volume", middleware.SYNEVENTTYPE))
r.Put("/volumes", middleware.WrapEL(controller.GetManager().UpdVolume, middleware.ST, "update-service-volume", middleware.SYNEVENTTYPE))
r.Get("/volumes", controller.GetVolume)
r.Delete("/volumes/{volume_name}", middleware.WrapEL(controller.DeleteVolume, middleware.ST, "delete-service-volume", middleware.SYNEVENTTYPE))
r.Post("/depvolumes", middleware.WrapEL(controller.AddVolumeDependency, middleware.ST, "add-service-depvolume", middleware.SYNEVENTTYPE))
r.Delete("/depvolumes", middleware.WrapEL(controller.DeleteVolumeDependency, middleware.ST, "delete-service-depvolume", middleware.SYNEVENTTYPE))
r.Get("/depvolumes", controller.GetDepVolume)
//持久化信息API v2
r.Post("/volume-dependency", middleware.WrapEL(controller.GetManager().VolumeDependency, middleware.ST, "add-service-depvolume", middleware.SYNEVENTTYPE))
r.Delete("/volume-dependency", middleware.WrapEL(controller.GetManager().VolumeDependency, middleware.ST, "delete-service-depvolume", middleware.SYNEVENTTYPE))
r.Post("/volume", middleware.WrapEL(controller.GetManager().AddVolume, middleware.ST, "add-service-volume", middleware.SYNEVENTTYPE))
r.Delete("/volume", middleware.WrapEL(controller.GetManager().DeleteVolume, middleware.ST, "delete-service-volume", middleware.SYNEVENTTYPE))
//获取应用实例情况(source)
r.Get("/pods", controller.GetManager().Pods)
//应用探针 增 删 改(surce)
r.Post("/probe", middleware.WrapEL(controller.GetManager().Probe, middleware.ST, "add-service-probe", middleware.SYNEVENTTYPE))
r.Put("/probe", middleware.WrapEL(controller.GetManager().Probe, middleware.ST, "update-service-probe", middleware.SYNEVENTTYPE))
r.Delete("/probe", middleware.WrapEL(controller.GetManager().Probe, middleware.ST, "delete-service-probe", middleware.SYNEVENTTYPE))
r.Post("/label", middleware.WrapEL(controller.GetManager().Label, middleware.ST, "add-service-label", middleware.SYNEVENTTYPE))
r.Put("/label", middleware.WrapEL(controller.GetManager().Label, middleware.ST, "update-service-label", middleware.SYNEVENTTYPE))
r.Delete("/label", middleware.WrapEL(controller.GetManager().Label, middleware.ST, "delete-service-label", middleware.SYNEVENTTYPE))
//插件
r.Mount("/plugin", v2.serviceRelatePluginRouter())
//rule
r.Mount("/net-rule", v2.rulesRouter())
r.Get("/deploy-info", controller.GetServiceDeployInfo)
// third-party service
r.Post("/endpoints", middleware.WrapEL(controller.GetManager().Endpoints, middleware.ST, "add-thirdpart-service", middleware.ASYNEVENTTYPE))
r.Put("/endpoints", middleware.WrapEL(controller.GetManager().Endpoints, middleware.ST, "update-thirdpart-service", middleware.ASYNEVENTTYPE))
r.Delete("/endpoints", middleware.WrapEL(controller.GetManager().Endpoints, middleware.ST, "delete-thirdpart-service", middleware.ASYNEVENTTYPE))
r.Get("/endpoints", controller.GetManager().Endpoints)
// gateway
r.Put("/rule-config", middleware.WrapEL(controller.GetManager().RuleConfig, middleware.ST, "update-service-gateway-rule", middleware.ASYNEVENTTYPE))
// app restore
r.Post("/app-restore/envs", middleware.WrapEL(controller.GetManager().RestoreEnvs, middleware.ST, "app-restore-envs", middleware.SYNEVENTTYPE))
r.Post("/app-restore/ports", middleware.WrapEL(controller.GetManager().RestorePorts, middleware.ST, "app-restore-ports", middleware.SYNEVENTTYPE))
r.Post("/app-restore/volumes", middleware.WrapEL(controller.GetManager().RestoreVolumes, middleware.ST, "app-restore-volumes", middleware.SYNEVENTTYPE))
r.Post("/app-restore/probe", middleware.WrapEL(controller.GetManager().RestoreProbe, middleware.ST, "app-restore-probe", middleware.SYNEVENTTYPE))
r.Post("/app-restore/deps", middleware.WrapEL(controller.GetManager().RestoreDeps, middleware.ST, "app-restore-deps", middleware.SYNEVENTTYPE))
r.Post("/app-restore/depvols", middleware.WrapEL(controller.GetManager().RestoreDepVols, middleware.ST, "app-restore-depvols", middleware.SYNEVENTTYPE))
r.Post("/app-restore/plugins", middleware.WrapEL(controller.GetManager().RestorePlugins, middleware.ST, "app-restore-plugins", middleware.SYNEVENTTYPE))
return r
}
func (v2 *V2) clusterRouter() chi.Router {
r := chi.NewRouter()
return r
}
func (v2 *V2) resourcesRouter() chi.Router {
r := chi.NewRouter()
r.Get("/labels", controller.GetManager().Labels)
r.Post("/tenants", controller.GetManager().TenantResources)
r.Post("/services", controller.GetManager().ServiceResources)
r.Get("/tenants/sum", controller.GetManager().SumTenants)
//tenants's resource
r.Get("/tenants/res", controller.GetManager().TenantsWithResource)
r.Get("/tenants/res/page/{curPage}/size/{pageLen}", controller.GetManager().TenantsWithResource)
r.Get("/tenants/query/{tenant_name}", controller.GetManager().TenantsQuery)
r.Get("/tenants/{tenant_name}/res", controller.GetManager().TenantsGetByName)
return r
}
func (v2 *V2) prometheusRouter() chi.Router {
r := chi.NewRouter()
return r
}
func (v2 *V2) appRouter() chi.Router {
r := chi.NewRouter()
r.Post("/export", controller.GetManager().ExportApp)
r.Get("/export/{eventID}", controller.GetManager().ExportApp)
r.Get("/download/{format}/{fileName}", controller.GetManager().Download)
r.Post("/upload/{eventID}", controller.GetManager().NewUpload)
r.Options("/upload/{eventID}", controller.GetManager().NewUpload)
r.Post("/import/ids/{eventID}", controller.GetManager().ImportID)
r.Get("/import/ids/{eventID}", controller.GetManager().ImportID)
r.Delete("/import/ids/{eventID}", controller.GetManager().ImportID)
r.Post("/import", controller.GetManager().ImportApp)
r.Get("/import/{eventID}", controller.GetManager().ImportApp)
r.Delete("/import/{eventID}", controller.GetManager().ImportApp)
return r
}
func (v2 *V2) notificationEventRouter() chi.Router {
r := chi.NewRouter()
r.Get("/", controller.GetNotificationEvents)
r.Put("/{serviceAlias}", controller.HandleNotificationEvent)
r.Get("/{hash}", controller.GetNotificationEvent)
return r
}
func (v2 *V2) portRouter() chi.Router {
r := chi.NewRouter()
r.Get("/avail-port", controller.GetManager().GetAvailablePort)
return r
}