2018-03-14 14:12:26 +08:00
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
2017-11-24 18:23:35 +08:00
// RAINBOND, Application Management Platform
2018-03-14 14:33:31 +08:00
2017-11-24 18:23:35 +08:00
// 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.
2018-03-14 14:33:31 +08:00
2017-11-24 18:23:35 +08:00
// 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.
2018-03-14 14:33:31 +08:00
2017-11-24 18:23:35 +08:00
// 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 dao
import (
"fmt"
2019-04-30 14:50:33 +08:00
"github.com/goodrain/rainbond/db/errors"
2018-04-24 16:44:59 +08:00
"github.com/goodrain/rainbond/db/model"
2017-11-24 18:23:35 +08:00
"github.com/jinzhu/gorm"
2020-11-25 16:39:38 +08:00
"github.com/sirupsen/logrus"
2017-11-24 18:23:35 +08:00
)
//PluginDaoImpl PluginDaoImpl
type PluginDaoImpl struct {
DB * gorm . DB
}
//AddModel 创建插件
func ( t * PluginDaoImpl ) AddModel ( mo model . Interface ) error {
plugin := mo . ( * model . TenantPlugin )
var oldPlugin model . TenantPlugin
2018-01-18 13:45:24 +08:00
if ok := t . DB . Where ( "plugin_id = ? and tenant_id = ?" , plugin . PluginID , plugin . TenantID ) . Find ( & oldPlugin ) . RecordNotFound ( ) ; ok {
2017-11-24 18:23:35 +08:00
if err := t . DB . Create ( plugin ) . Error ; err != nil {
return err
}
} else {
2019-10-14 17:21:31 +08:00
logrus . Infof ( "plugin id: %s; tenant id: %s; tenant plugin already exist" , plugin . PluginID , plugin . TenantID )
return errors . ErrRecordAlreadyExist
2017-11-24 18:23:35 +08:00
}
return nil
}
//UpdateModel 更新插件
func ( t * PluginDaoImpl ) UpdateModel ( mo model . Interface ) error {
plugin := mo . ( * model . TenantPlugin )
if err := t . DB . Save ( plugin ) . Error ; err != nil {
return err
}
return nil
}
//GetPluginByID GetPluginByID
2018-01-18 13:45:24 +08:00
func ( t * PluginDaoImpl ) GetPluginByID ( id , tenantID string ) ( * model . TenantPlugin , error ) {
2017-11-24 18:23:35 +08:00
var plugin model . TenantPlugin
2018-01-18 13:45:24 +08:00
if err := t . DB . Where ( "plugin_id = ? and tenant_id = ?" , id , tenantID ) . Find ( & plugin ) . Error ; err != nil {
2017-11-24 18:23:35 +08:00
return nil , err
}
return & plugin , nil
}
2019-10-13 19:56:13 +08:00
// ListByIDs returns the list of plugins based on the given plugin ids.
func ( t * PluginDaoImpl ) ListByIDs ( ids [ ] string ) ( [ ] * model . TenantPlugin , error ) {
var plugins [ ] * model . TenantPlugin
if err := t . DB . Where ( "plugin_id in (?)" , ids ) . Find ( & plugins ) . Error ; err != nil {
if err == gorm . ErrRecordNotFound {
return nil , nil
}
return nil , err
}
return plugins , nil
}
2017-11-24 18:23:35 +08:00
//DeletePluginByID DeletePluginByID
2018-01-18 13:45:24 +08:00
func ( t * PluginDaoImpl ) DeletePluginByID ( id , tenantID string ) error {
2018-05-11 15:54:55 +08:00
var plugin model . TenantPlugin
if err := t . DB . Where ( "plugin_id=? and tenant_id=?" , id , tenantID ) . Delete ( & plugin ) . Error ; err != nil {
2017-11-24 18:23:35 +08:00
return err
}
return nil
}
//GetPluginsByTenantID GetPluginsByTenantID
func ( t * PluginDaoImpl ) GetPluginsByTenantID ( tenantID string ) ( [ ] * model . TenantPlugin , error ) {
var plugins [ ] * model . TenantPlugin
if err := t . DB . Where ( "tenant_id=?" , tenantID ) . Find ( & plugins ) . Error ; err != nil {
return nil , err
}
return plugins , nil
}
2019-11-05 11:43:10 +08:00
// ListByTenantID -
func ( t * PluginDaoImpl ) ListByTenantID ( tenantID string ) ( [ ] * model . TenantPlugin , error ) {
var plugins [ ] * model . TenantPlugin
if err := t . DB . Where ( "tenant_id=?" , tenantID ) . Find ( & plugins ) . Error ; err != nil {
return nil , err
}
return plugins , nil
}
2017-11-24 18:23:35 +08:00
//PluginDefaultENVDaoImpl PluginDefaultENVDaoImpl
type PluginDefaultENVDaoImpl struct {
DB * gorm . DB
}
//AddModel 添加插件默认变量
func ( t * PluginDefaultENVDaoImpl ) AddModel ( mo model . Interface ) error {
env := mo . ( * model . TenantPluginDefaultENV )
var oldENV model . TenantPluginDefaultENV
2017-12-12 12:52:29 +08:00
if ok := t . DB . Where ( "plugin_id=? and env_name = ? and version_id = ?" ,
env . PluginID ,
env . ENVName ,
env . VersionID ) . Find ( & oldENV ) . RecordNotFound ( ) ; ok {
2017-11-24 18:23:35 +08:00
if err := t . DB . Create ( env ) . Error ; err != nil {
return err
}
} else {
return fmt . Errorf ( "env %s is exist" , env . ENVName )
}
return nil
}
//UpdateModel 更新插件默认变量
func ( t * PluginDefaultENVDaoImpl ) UpdateModel ( mo model . Interface ) error {
env := mo . ( * model . TenantPluginDefaultENV )
if err := t . DB . Save ( env ) . Error ; err != nil {
return err
}
return nil
}
2017-12-12 12:52:29 +08:00
//GetALLMasterDefultENVs GetALLMasterDefultENVs
func ( t * PluginDefaultENVDaoImpl ) GetALLMasterDefultENVs ( pluginID string ) ( [ ] * model . TenantPluginDefaultENV , error ) {
var envs [ ] * model . TenantPluginDefaultENV
if err := t . DB . Where ( "plugin_id=? and version_id=?" , pluginID , "master_rb" ) . Find ( & envs ) . Error ; err != nil {
return nil , err
}
return envs , nil
}
2017-11-24 18:23:35 +08:00
//GetDefaultENVByName GetDefaultENVByName
2017-12-12 12:52:29 +08:00
func ( t * PluginDefaultENVDaoImpl ) GetDefaultENVByName ( pluginID , name , versionID string ) ( * model . TenantPluginDefaultENV , error ) {
2017-11-24 18:23:35 +08:00
var env model . TenantPluginDefaultENV
2017-12-12 12:52:29 +08:00
if err := t . DB . Where ( "plugin_id=? and env_name=? and version_id=?" ,
pluginID ,
name ,
versionID ) . Find ( & env ) . Error ; err != nil {
2017-11-24 18:23:35 +08:00
return nil , err
}
return & env , nil
}
//GetDefaultENVSByPluginID GetDefaultENVSByPluginID
2017-12-12 12:52:29 +08:00
func ( t * PluginDefaultENVDaoImpl ) GetDefaultENVSByPluginID ( pluginID , versionID string ) ( [ ] * model . TenantPluginDefaultENV , error ) {
2017-11-24 18:23:35 +08:00
var envs [ ] * model . TenantPluginDefaultENV
2017-12-12 12:52:29 +08:00
if err := t . DB . Where ( "plugin_id=? and version_id=?" , pluginID , versionID ) . Find ( & envs ) . Error ; err != nil {
2017-11-24 18:23:35 +08:00
return nil , err
}
return envs , nil
}
//DeleteDefaultENVByName DeleteDefaultENVByName
2017-12-12 12:52:29 +08:00
func ( t * PluginDefaultENVDaoImpl ) DeleteDefaultENVByName ( pluginID , name , versionID string ) error {
2017-11-24 18:23:35 +08:00
relation := & model . TenantPluginDefaultENV {
ENVName : name ,
}
2017-12-12 12:52:29 +08:00
if err := t . DB . Where ( "plugin_id=? and env_name=? and version_id=?" ,
pluginID , name , versionID ) . Delete ( relation ) . Error ; err != nil {
return err
}
return nil
}
//DeleteDefaultENVByPluginIDAndVersionID DeleteDefaultENVByPluginIDAndVersionID
func ( t * PluginDefaultENVDaoImpl ) DeleteDefaultENVByPluginIDAndVersionID ( pluginID , versionID string ) error {
relation := & model . TenantPluginDefaultENV {
PluginID : pluginID ,
}
if err := t . DB . Where ( "plugin_id=? and version_id=?" , pluginID , versionID ) . Delete ( relation ) . Error ; err != nil {
2017-11-24 18:23:35 +08:00
return err
}
return nil
}
//DeleteAllDefaultENVByPluginID DeleteAllDefaultENVByPluginID
2017-12-12 12:52:29 +08:00
func ( t * PluginDefaultENVDaoImpl ) DeleteAllDefaultENVByPluginID ( pluginID string ) error {
2017-11-24 18:23:35 +08:00
relation := & model . TenantPluginDefaultENV {
2017-12-12 12:52:29 +08:00
PluginID : pluginID ,
2017-11-24 18:23:35 +08:00
}
2017-12-12 12:52:29 +08:00
if err := t . DB . Where ( "plugin_id=?" , pluginID ) . Delete ( relation ) . Error ; err != nil {
2017-11-24 18:23:35 +08:00
return err
}
return nil
}
//GetDefaultEnvWhichCanBeSetByPluginID GetDefaultEnvWhichCanBeSetByPluginID
2017-12-12 12:52:29 +08:00
func ( t * PluginDefaultENVDaoImpl ) GetDefaultEnvWhichCanBeSetByPluginID ( pluginID , versionID string ) ( [ ] * model . TenantPluginDefaultENV , error ) {
2017-11-24 18:23:35 +08:00
var envs [ ] * model . TenantPluginDefaultENV
2018-04-26 14:57:25 +08:00
if err := t . DB . Where ( "plugin_id=? and is_change=? and version_id=?" , pluginID , true , versionID ) . Find ( & envs ) . Error ; err != nil {
2017-11-24 18:23:35 +08:00
return nil , err
}
return envs , nil
}
//PluginBuildVersionDaoImpl PluginBuildVersionDaoImpl
type PluginBuildVersionDaoImpl struct {
DB * gorm . DB
}
//AddModel 添加插件构建版本信息
func ( t * PluginBuildVersionDaoImpl ) AddModel ( mo model . Interface ) error {
version := mo . ( * model . TenantPluginBuildVersion )
var oldVersion model . TenantPluginBuildVersion
2018-05-30 14:41:27 +08:00
if ok := t . DB . Where ( "plugin_id =? and version_id = ? and deploy_version=?" , version . PluginID , version . VersionID , version . DeployVersion ) . Find ( & oldVersion ) . RecordNotFound ( ) ; ok {
2017-11-24 18:23:35 +08:00
if err := t . DB . Create ( version ) . Error ; err != nil {
return err
}
} else {
2019-10-14 17:21:31 +08:00
logrus . Infof ( "plugin id: %s; version_id: %s; deploy_version: %s; tenant plugin build versoin already exist" , version . PluginID , version . VersionID , version . DeployVersion )
return errors . ErrRecordAlreadyExist
2017-11-24 18:23:35 +08:00
}
return nil
}
//UpdateModel 更新插件默认变量
//主体信息一般不变更, 仅构建的本地镜像名与status需要变更
func ( t * PluginBuildVersionDaoImpl ) UpdateModel ( mo model . Interface ) error {
version := mo . ( * model . TenantPluginBuildVersion )
if version . ID == 0 {
return fmt . Errorf ( "id can not be empty when update build verion" )
}
if err := t . DB . Save ( version ) . Error ; err != nil {
return err
}
return nil
}
//DeleteBuildVersionByVersionID DeleteBuildVersionByVersionID
func ( t * PluginBuildVersionDaoImpl ) DeleteBuildVersionByVersionID ( versionID string ) error {
relation := & model . TenantPluginBuildVersion {
VersionID : versionID ,
}
if err := t . DB . Where ( "version_id=?" , versionID ) . Delete ( relation ) . Error ; err != nil {
return err
}
return nil
}
//DeleteBuildVersionByPluginID DeleteBuildVersionByPluginID
func ( t * PluginBuildVersionDaoImpl ) DeleteBuildVersionByPluginID ( pluginID string ) error {
relation := & model . TenantPluginBuildVersion {
PluginID : pluginID ,
}
if err := t . DB . Where ( "plugin_id=?" , pluginID ) . Delete ( relation ) . Error ; err != nil {
return err
}
return nil
}
//GetBuildVersionByPluginID GetBuildVersionByPluginID
func ( t * PluginBuildVersionDaoImpl ) GetBuildVersionByPluginID ( pluginID string ) ( [ ] * model . TenantPluginBuildVersion , error ) {
var versions [ ] * model . TenantPluginBuildVersion
if err := t . DB . Where ( "plugin_id = ? and status= ?" , pluginID , "complete" ) . Find ( & versions ) . Error ; err != nil {
return nil , err
}
return versions , nil
}
//GetBuildVersionByVersionID GetBuildVersionByVersionID
func ( t * PluginBuildVersionDaoImpl ) GetBuildVersionByVersionID ( pluginID , versionID string ) ( * model . TenantPluginBuildVersion , error ) {
var version model . TenantPluginBuildVersion
if err := t . DB . Where ( "plugin_id=? and version_id = ? " , pluginID , versionID ) . Find ( & version ) . Error ; err != nil {
return nil , err
}
return & version , nil
}
2018-05-29 12:51:10 +08:00
//GetBuildVersionByDeployVersion GetBuildVersionByDeployVersion
func ( t * PluginBuildVersionDaoImpl ) GetBuildVersionByDeployVersion ( pluginID , versionID , deployVersion string ) ( * model . TenantPluginBuildVersion , error ) {
var version model . TenantPluginBuildVersion
if err := t . DB . Where ( "plugin_id=? and version_id = ? and deploy_version=?" , pluginID , versionID , deployVersion ) . Find ( & version ) . Error ; err != nil {
return nil , err
}
return & version , nil
}
2019-10-13 19:56:13 +08:00
// ListSuccessfulOnesByPluginIDs returns the list of successful build versions,
func ( t * PluginBuildVersionDaoImpl ) ListSuccessfulOnesByPluginIDs ( pluginIDs [ ] string ) ( [ ] * model . TenantPluginBuildVersion , error ) {
var version [ ] * model . TenantPluginBuildVersion
if err := t . DB . Where ( "ID in (?) " , t . DB . Table ( "tenant_plugin_build_version" ) . Select ( "max(id)" ) . Where ( "plugin_id in (?) and status=?" , pluginIDs , "complete" ) . Group ( "plugin_id" ) . QueryExpr ( ) ) . Find ( & version ) . Error ; err != nil {
return nil , err
}
return version , nil
}
2018-05-29 12:51:10 +08:00
//GetLastBuildVersionByVersionID get last success build version
func ( t * PluginBuildVersionDaoImpl ) GetLastBuildVersionByVersionID ( pluginID , versionID string ) ( * model . TenantPluginBuildVersion , error ) {
var version model . TenantPluginBuildVersion
if err := t . DB . Where ( "plugin_id=? and version_id = ? and status=?" , pluginID , versionID , "complete" ) . Order ( "ID desc" ) . Limit ( "1" ) . Find ( & version ) . Error ; err != nil {
return nil , err
}
return & version , nil
}
2017-11-24 18:23:35 +08:00
//PluginVersionEnvDaoImpl PluginVersionEnvDaoImpl
type PluginVersionEnvDaoImpl struct {
DB * gorm . DB
}
//AddModel 添加插件默认变量
func ( t * PluginVersionEnvDaoImpl ) AddModel ( mo model . Interface ) error {
env := mo . ( * model . TenantPluginVersionEnv )
var oldENV model . TenantPluginVersionEnv
2017-12-28 14:40:32 +08:00
if ok := t . DB . Where ( "service_id=? and plugin_id=? and env_name = ?" , env . ServiceID , env . PluginID , env . EnvName ) . Find ( & oldENV ) . RecordNotFound ( ) ; ok {
2017-11-24 18:23:35 +08:00
if err := t . DB . Create ( env ) . Error ; err != nil {
return err
}
} else {
return fmt . Errorf ( "env %s is exist" , env . EnvName )
}
return nil
}
//UpdateModel 更新插件默认变量
func ( t * PluginVersionEnvDaoImpl ) UpdateModel ( mo model . Interface ) error {
env := mo . ( * model . TenantPluginVersionEnv )
if env . ID == 0 || env . ServiceID == "" || env . PluginID == "" {
return fmt . Errorf ( "id can not be empty when update plugin version env" )
}
if err := t . DB . Save ( env ) . Error ; err != nil {
return err
}
return nil
}
//DeleteEnvByEnvName 删除单个env
func ( t * PluginVersionEnvDaoImpl ) DeleteEnvByEnvName ( envName , pluginID , serviceID string ) error {
env := & model . TenantPluginVersionEnv {
PluginID : pluginID ,
EnvName : envName ,
ServiceID : serviceID ,
}
return t . DB . Where ( "env_name=? and plugin_id=? and service_id=?" , envName , pluginID , serviceID ) . Delete ( env ) . Error
}
//DeleteEnvByPluginID 删除插件依赖关系时, 需要操作删除对应env
func ( t * PluginVersionEnvDaoImpl ) DeleteEnvByPluginID ( serviceID , pluginID string ) error {
env := & model . TenantPluginVersionEnv {
PluginID : pluginID ,
ServiceID : serviceID ,
}
return t . DB . Where ( "plugin_id=? and service_id= ?" , pluginID , serviceID ) . Delete ( env ) . Error
}
//DeleteEnvByServiceID 删除应用时,需要进行此操作
func ( t * PluginVersionEnvDaoImpl ) DeleteEnvByServiceID ( serviceID string ) error {
env := & model . TenantPluginVersionEnv {
ServiceID : serviceID ,
}
return t . DB . Where ( "service_id=?" , serviceID ) . Delete ( env ) . Error
}
//GetVersionEnvByServiceID 获取该应用下使用的某个插件依赖的插件变量
func ( t * PluginVersionEnvDaoImpl ) GetVersionEnvByServiceID ( serviceID string , pluginID string ) ( [ ] * model . TenantPluginVersionEnv , error ) {
var envs [ ] * model . TenantPluginVersionEnv
if err := t . DB . Where ( "service_id=? and plugin_id=?" , serviceID , pluginID ) . Find ( & envs ) . Error ; err != nil {
return nil , err
}
return envs , nil
}
//GetVersionEnvByEnvName GetVersionEnvByEnvName
func ( t * PluginVersionEnvDaoImpl ) GetVersionEnvByEnvName ( serviceID , pluginID , envName string ) ( * model . TenantPluginVersionEnv , error ) {
2017-12-27 17:58:17 +08:00
var env model . TenantPluginVersionEnv
2017-11-24 18:23:35 +08:00
if err := t . DB . Where ( "service_id=? and plugin_id=? and env_name=?" , serviceID , pluginID , envName ) . Find ( & env ) . Error ; err != nil {
return nil , err
}
2017-12-27 17:58:17 +08:00
return & env , nil
2017-11-24 18:23:35 +08:00
}
2019-10-13 19:56:13 +08:00
// ListByServiceID returns the list of environment variables for the plugin via serviceID
func ( t * PluginVersionEnvDaoImpl ) ListByServiceID ( serviceID string ) ( [ ] * model . TenantPluginVersionEnv , error ) {
var envs [ ] * model . TenantPluginVersionEnv
if err := t . DB . Where ( "service_id=?" , serviceID ) . Find ( & envs ) . Error ; err != nil {
if err == gorm . ErrRecordNotFound {
return nil , nil
}
return nil , err
}
return envs , nil
}
2019-02-15 17:20:08 +08:00
//PluginVersionConfigDaoImpl PluginVersionEnvDaoImpl
type PluginVersionConfigDaoImpl struct {
DB * gorm . DB
}
//AddModel add or update service plugin config
func ( t * PluginVersionConfigDaoImpl ) AddModel ( mo model . Interface ) error {
config := mo . ( * model . TenantPluginVersionDiscoverConfig )
var oldconfig model . TenantPluginVersionDiscoverConfig
if ok := t . DB . Where ( "service_id=? and plugin_id=?" , config . ServiceID , config . PluginID ) . Find ( & oldconfig ) . RecordNotFound ( ) ; ok {
if err := t . DB . Create ( config ) . Error ; err != nil {
return err
}
} else {
config . ID = oldconfig . ID
2020-03-05 18:07:14 +08:00
config . CreatedAt = oldconfig . CreatedAt
return t . UpdateModel ( config )
2019-02-15 17:20:08 +08:00
}
return nil
}
//UpdateModel update service plugin config
func ( t * PluginVersionConfigDaoImpl ) UpdateModel ( mo model . Interface ) error {
env := mo . ( * model . TenantPluginVersionDiscoverConfig )
if env . ID == 0 || env . ServiceID == "" || env . PluginID == "" {
return fmt . Errorf ( "id can not be empty when update plugin version config" )
}
if err := t . DB . Save ( env ) . Error ; err != nil {
return err
}
return nil
}
//DeletePluginConfig delete service plugin config
func ( t * PluginVersionConfigDaoImpl ) DeletePluginConfig ( serviceID , pluginID string ) error {
var oldconfig model . TenantPluginVersionDiscoverConfig
if err := t . DB . Where ( "service_id=? and plugin_id=?" , serviceID , pluginID ) . Delete ( & oldconfig ) . Error ; err != nil {
return err
}
return nil
}
//DeletePluginConfigByServiceID Batch delete config by service id
func ( t * PluginVersionConfigDaoImpl ) DeletePluginConfigByServiceID ( serviceID string ) error {
var oldconfig model . TenantPluginVersionDiscoverConfig
if err := t . DB . Where ( "service_id=?" , serviceID ) . Delete ( & oldconfig ) . Error ; err != nil {
return err
}
return nil
}
//GetPluginConfig get service plugin config
func ( t * PluginVersionConfigDaoImpl ) GetPluginConfig ( serviceID , pluginID string ) ( * model . TenantPluginVersionDiscoverConfig , error ) {
var oldconfig model . TenantPluginVersionDiscoverConfig
if err := t . DB . Where ( "service_id=? and plugin_id=?" , serviceID , pluginID ) . Find ( & oldconfig ) . Error ; err != nil {
return nil , err
}
return & oldconfig , nil
}
2019-03-01 15:13:14 +08:00
//GetPluginConfigs get plugin configs
func ( t * PluginVersionConfigDaoImpl ) GetPluginConfigs ( serviceID string ) ( [ ] * model . TenantPluginVersionDiscoverConfig , error ) {
var oldconfigs [ ] * model . TenantPluginVersionDiscoverConfig
if err := t . DB . Where ( "service_id=?" , serviceID ) . Find ( & oldconfigs ) . Error ; err != nil {
return nil , err
}
return oldconfigs , nil
}
2017-11-24 18:23:35 +08:00
//TenantServicePluginRelationDaoImpl TenantServicePluginRelationDaoImpl
type TenantServicePluginRelationDaoImpl struct {
DB * gorm . DB
}
//AddModel 添加插件默认变量
func ( t * TenantServicePluginRelationDaoImpl ) AddModel ( mo model . Interface ) error {
relation := mo . ( * model . TenantServicePluginRelation )
var oldRelation model . TenantServicePluginRelation
if ok := t . DB . Where ( "service_id= ? and plugin_id=?" , relation . ServiceID , relation . PluginID ) . Find ( & oldRelation ) . RecordNotFound ( ) ; ok {
if err := t . DB . Create ( relation ) . Error ; err != nil {
return err
}
} else {
2019-04-30 14:50:33 +08:00
return errors . ErrRecordAlreadyExist
2017-11-24 18:23:35 +08:00
}
return nil
}
//UpdateModel 更新插件默认变量 更新依赖的version id
func ( t * TenantServicePluginRelationDaoImpl ) UpdateModel ( mo model . Interface ) error {
relation := mo . ( * model . TenantServicePluginRelation )
if relation . ID == 0 {
return fmt . Errorf ( "id can not be empty when update service plugin relation" )
}
if err := t . DB . Save ( relation ) . Error ; err != nil {
return err
}
return nil
}
//DeleteRelationByServiceIDAndPluginID 删除service plugin 对应关系
func ( t * TenantServicePluginRelationDaoImpl ) DeleteRelationByServiceIDAndPluginID ( serviceID , pluginID string ) error {
relation := & model . TenantServicePluginRelation {
ServiceID : serviceID ,
PluginID : pluginID ,
}
return t . DB . Where ( "plugin_id=? and service_id=?" ,
pluginID ,
serviceID ) . Delete ( relation ) . Error
}
2017-12-26 10:41:52 +08:00
//CheckSomeModelPluginByServiceID 检查是否绑定了某种插件且处于启用状态
2017-11-29 15:41:01 +08:00
func ( t * TenantServicePluginRelationDaoImpl ) CheckSomeModelPluginByServiceID ( serviceID , pluginModel string ) ( bool , error ) {
var relations [ ] * model . TenantServicePluginRelation
2018-04-26 14:57:25 +08:00
if err := t . DB . Where ( "service_id=? and plugin_model=? and switch=?" , serviceID , pluginModel , true ) . Find ( & relations ) . Error ; err != nil {
2017-11-30 19:22:28 +08:00
return false , err
2017-12-01 12:40:49 +08:00
}
if len ( relations ) == 1 {
return true , nil
}
return false , nil
}
//CheckSomeModelLikePluginByServiceID 检查是否绑定了某大类插件
func ( t * TenantServicePluginRelationDaoImpl ) CheckSomeModelLikePluginByServiceID ( serviceID , pluginModel string ) ( bool , error ) {
var relations [ ] * model . TenantServicePluginRelation
catePlugin := "%" + pluginModel + "%"
if err := t . DB . Where ( "service_id=? and plugin_model LIKE ?" , serviceID , catePlugin ) . Find ( & relations ) . Error ; err != nil {
return false , err
2017-11-29 15:41:01 +08:00
}
2017-11-30 19:22:28 +08:00
if len ( relations ) == 1 {
return true , nil
}
return false , nil
2017-11-29 15:41:01 +08:00
}
2017-11-24 18:23:35 +08:00
//DeleteALLRelationByServiceID 删除serviceID所有插件依赖 一般用于删除应用时使用
func ( t * TenantServicePluginRelationDaoImpl ) DeleteALLRelationByServiceID ( serviceID string ) error {
relation := & model . TenantServicePluginRelation {
ServiceID : serviceID ,
}
return t . DB . Where ( "service_id=?" , serviceID ) . Delete ( relation ) . Error
}
//DeleteALLRelationByPluginID 删除pluginID所有依赖 一般不要使用 会影响关联过的应用启动
func ( t * TenantServicePluginRelationDaoImpl ) DeleteALLRelationByPluginID ( pluginID string ) error {
relation := & model . TenantServicePluginRelation {
PluginID : pluginID ,
}
return t . DB . Where ( "plugin_id=?" , pluginID ) . Delete ( relation ) . Error
}
//GetALLRelationByServiceID 获取当前应用所有的插件依赖关系
func ( t * TenantServicePluginRelationDaoImpl ) GetALLRelationByServiceID ( serviceID string ) ( [ ] * model . TenantServicePluginRelation , error ) {
var relations [ ] * model . TenantServicePluginRelation
if err := t . DB . Where ( "service_id=?" , serviceID ) . Find ( & relations ) . Error ; err != nil {
return nil , err
}
return relations , nil
}
//GetRelateionByServiceIDAndPluginID GetRelateionByServiceIDAndPluginID
func ( t * TenantServicePluginRelationDaoImpl ) GetRelateionByServiceIDAndPluginID ( serviceID , pluginID string ) ( * model . TenantServicePluginRelation , error ) {
relation := & model . TenantServicePluginRelation {
PluginID : pluginID ,
ServiceID : serviceID ,
}
if err := t . DB . Where ( "plugin_id=? and service_id=?" , pluginID , serviceID ) . Find ( relation ) . Error ; err != nil {
return nil , err
}
return relation , nil
}
2017-11-30 15:03:10 +08:00
//TenantServicesStreamPluginPortDaoImpl TenantServicesStreamPluginPortDaoImpl
type TenantServicesStreamPluginPortDaoImpl struct {
DB * gorm . DB
}
//AddModel 添加插件端口映射信息
func ( t * TenantServicesStreamPluginPortDaoImpl ) AddModel ( mo model . Interface ) error {
port := mo . ( * model . TenantServicesStreamPluginPort )
var oldPort model . TenantServicesStreamPluginPort
if ok := t . DB . Where ( "service_id= ? and container_port= ? and plugin_model=? " ,
port . ServiceID ,
port . ContainerPort ,
port . PluginModel ) . Find ( & oldPort ) . RecordNotFound ( ) ; ok {
if err := t . DB . Create ( port ) . Error ; err != nil {
return err
}
} else {
return fmt . Errorf ( "plugin port %d mappint to %d is exist" , port . ContainerPort , port . PluginPort )
}
return nil
}
//UpdateModel 更新插件端口映射信息
func ( t * TenantServicesStreamPluginPortDaoImpl ) UpdateModel ( mo model . Interface ) error {
port := mo . ( * model . TenantServicesStreamPluginPort )
if port . ID == 0 {
return fmt . Errorf ( "id can not be empty when update plugin mapping port" )
}
if err := t . DB . Save ( port ) . Error ; err != nil {
return err
}
return nil
}
//GetPluginMappingPorts GetPluginMappingPorts 降序排列
func ( t * TenantServicesStreamPluginPortDaoImpl ) GetPluginMappingPorts (
2019-06-14 15:19:53 +08:00
serviceID string ) ( [ ] * model . TenantServicesStreamPluginPort , error ) {
2017-11-30 15:03:10 +08:00
var ports [ ] * model . TenantServicesStreamPluginPort
2019-06-14 15:19:53 +08:00
if err := t . DB . Where ( "service_id=?" , serviceID ) . Order ( "plugin_port asc" ) . Find ( & ports ) . Error ; err != nil {
2017-11-30 15:03:10 +08:00
return nil , err
}
return ports , nil
}
//GetPluginMappingPortByServiceIDAndContainerPort GetPluginMappingPortByServiceIDAndContainerPort
func ( t * TenantServicesStreamPluginPortDaoImpl ) GetPluginMappingPortByServiceIDAndContainerPort (
serviceID string ,
pluginModel string ,
containerPort int ,
) ( * model . TenantServicesStreamPluginPort , error ) {
var port model . TenantServicesStreamPluginPort
if err := t . DB . Where (
"service_id=? and plugin_model=? and container_port=?" ,
serviceID ,
pluginModel ,
containerPort ,
) . Find ( & port ) . Error ; err != nil {
return nil , err
}
return & port , nil
}
//SetPluginMappingPort SetPluginMappingPort
func ( t * TenantServicesStreamPluginPortDaoImpl ) SetPluginMappingPort (
tenantID string ,
serviceID string ,
pluginModel string ,
containerPort int ) ( int , error ) {
2019-06-14 15:19:53 +08:00
ports , err := t . GetPluginMappingPorts ( serviceID )
2017-11-30 15:03:10 +08:00
if err != nil {
return 0 , err
}
2018-04-14 14:09:37 +08:00
//if have been allocated,return
for _ , oldp := range ports {
if oldp . ContainerPort == containerPort {
return oldp . PluginPort , nil
}
}
//Distribution port range
2017-11-30 15:03:10 +08:00
minPort := 65301
maxPort := 65400
newPort := & model . TenantServicesStreamPluginPort {
TenantID : tenantID ,
ServiceID : serviceID ,
PluginModel : pluginModel ,
ContainerPort : containerPort ,
}
if len ( ports ) == 0 {
newPort . PluginPort = minPort
if err := t . AddModel ( newPort ) ; err != nil {
return 0 , err
}
return newPort . PluginPort , nil
}
oldMaxPort := ports [ len ( ports ) - 1 ]
//已分配端口+2大于最大端口限制则从原范围内扫描端口使用
if oldMaxPort . PluginPort > ( maxPort - 2 ) {
waitPort := minPort
for _ , p := range ports {
if p . PluginPort == waitPort {
waitPort ++
continue
}
newPort . PluginPort = waitPort
if err := t . AddModel ( newPort ) ; err != nil {
return 0 , nil
}
continue
}
}
//端口与预分配端口相同重新分配
if containerPort == ( oldMaxPort . PluginPort + 1 ) {
newPort . PluginPort = oldMaxPort . PluginPort + 2
if err := t . AddModel ( newPort ) ; err != nil {
return 0 , err
}
return newPort . PluginPort , nil
}
newPort . PluginPort = oldMaxPort . PluginPort + 1
if err := t . AddModel ( newPort ) ; err != nil {
return 0 , err
}
return newPort . PluginPort , nil
}
//DeletePluginMappingPortByContainerPort DeletePluginMappingPortByContainerPort
func ( t * TenantServicesStreamPluginPortDaoImpl ) DeletePluginMappingPortByContainerPort (
serviceID string ,
pluginModel string ,
containerPort int ) error {
relation := & model . TenantServicesStreamPluginPort {
ServiceID : serviceID ,
PluginModel : pluginModel ,
ContainerPort : containerPort ,
}
return t . DB . Where ( "service_id=? and plugin_model=? and container_port=?" ,
serviceID ,
pluginModel ,
containerPort ) . Delete ( relation ) . Error
}
//DeleteAllPluginMappingPortByServiceID DeleteAllPluginMappingPortByServiceID
func ( t * TenantServicesStreamPluginPortDaoImpl ) DeleteAllPluginMappingPortByServiceID ( serviceID string ) error {
relation := & model . TenantServicesStreamPluginPort {
ServiceID : serviceID ,
}
return t . DB . Where ( "service_id=?" , serviceID ) . Delete ( relation ) . Error
}
2019-10-13 19:56:13 +08:00
// ListByServiceID returns the list of environment variables for the plugin via serviceID
func ( t * TenantServicesStreamPluginPortDaoImpl ) ListByServiceID ( sid string ) ( [ ] * model . TenantServicesStreamPluginPort , error ) {
var result [ ] * model . TenantServicesStreamPluginPort
if err := t . DB . Where ( "service_id=?" , sid ) . Find ( & result ) . Error ; err != nil {
if err == gorm . ErrRecordNotFound {
return nil , nil
}
return nil , err
}
return result , nil
}