diff --git a/api/controller/application_config_group.go b/api/controller/application_config_group.go index 18b1dfb29..d3fbf82b4 100644 --- a/api/controller/application_config_group.go +++ b/api/controller/application_config_group.go @@ -21,14 +21,14 @@ func (a *ApplicationStruct) AddConfigGroup(w http.ResponseWriter, r *http.Reques // Get the application bound serviceIDs var availableServiceIDs []string - availableServices := db.GetManager().TenantServiceDao().GetServicesIDAndNameByAppID(appID) + availableServices := db.GetManager().TenantServiceDao().GetServiceIDsByAppID(appID) for _, s := range availableServices { availableServiceIDs = append(availableServiceIDs, s.ServiceID) } // Judge whether the requested service ID is correct - for _, sID := range configReq.ServiceIDs { - if !MapKeyInStringSlice(availableServiceIDs, sID) { - httputil.ReturnBcodeError(r, w, bcode.ErrServiceIDNotFound) + for _, sid := range configReq.ServiceIDs { + if !MapKeyInStringSlice(availableServiceIDs, sid) { + httputil.ReturnBcodeError(r, w, bcode.ErrServiceNotFound) return } } diff --git a/api/handler/application_config_group.go b/api/handler/application_config_group.go index c149de00c..e441602c6 100644 --- a/api/handler/application_config_group.go +++ b/api/handler/application_config_group.go @@ -10,35 +10,34 @@ import ( // AddConfigGroup - func (a *ApplicationAction) AddConfigGroup(appID string, req *model.ApplicationConfigGroup) (*model.ApplicationConfigGroupResp, error) { - serviceConfigGroup := &dbmodel.ServiceConfigGroup{ - AppID: appID, - ConfigGroupName: req.ConfigGroupName, - } - configItem := &dbmodel.ConfigItem{ - AppID: appID, - ConfigGroupName: req.ConfigGroupName, - } - config := &dbmodel.ApplicationConfigGroup{ - AppID: appID, - ConfigGroupName: req.ConfigGroupName, - DeployType: req.DeployType, - } - - // Create application ConfigGroup - for _, sID := range req.ServiceIDs { - serviceConfigGroup.ServiceID = sID - if err := db.GetManager().ServiceConfigGroupDao().AddModel(serviceConfigGroup); err != nil { + var serviceResp []dbmodel.ServiceConfigGroup + // Create application configGroup-services + for _, sid := range req.ServiceIDs { + services, _ := db.GetManager().TenantServiceDao().GetServiceByID(sid) + serviceConfigGroup := dbmodel.ServiceConfigGroup{ + AppID: appID, + ConfigGroupName: req.ConfigGroupName, + ServiceID: sid, + ServiceAlias: services.ServiceAlias, + } + serviceResp = append(serviceResp, serviceConfigGroup) + if err := db.GetManager().ServiceConfigGroupDao().AddModel(&serviceConfigGroup); err != nil { if err == bcode.ErrServiceConfigGroupExist { logrus.Warningf("config group \"%s\" under this service \"%s\" already exists.", serviceConfigGroup.ConfigGroupName, serviceConfigGroup.ServiceID) continue } return nil, err } - serviceConfigGroup.ID++ } + + // Create application configGroup-configItem for _, it := range req.ConfigItems { - configItem.ItemKey = it.ItemKey - configItem.ItemValue = it.ItemValue + configItem := &dbmodel.ConfigItem{ + AppID: appID, + ConfigGroupName: req.ConfigGroupName, + ItemKey: it.ItemKey, + ItemValue: it.ItemValue, + } if err := db.GetManager().ConfigItemDao().AddModel(configItem); err != nil { if err == bcode.ErrConfigItemExist { logrus.Warningf("config item \"%s\" under this config group \"%s\" already exists.", configItem.ItemKey, configItem.ConfigGroupName) @@ -46,21 +45,18 @@ func (a *ApplicationAction) AddConfigGroup(appID string, req *model.ApplicationC } return nil, err } - configItem.ID++ + } + + // Create application configGroup + config := &dbmodel.ApplicationConfigGroup{ + AppID: appID, + ConfigGroupName: req.ConfigGroupName, + DeployType: req.DeployType, } if err := db.GetManager().ApplicationConfigDao().AddModel(config); err != nil { return nil, err } - var serviceResult []dbmodel.ServiceIDAndNameResult - services, _ := db.GetManager().TenantServiceDao().GetServiceByIDs(req.ServiceIDs) - for _, service := range services { - s := dbmodel.ServiceIDAndNameResult{ - ServiceID: service.ServiceID, - ServiceName: service.ServiceName, - } - serviceResult = append(serviceResult, s) - } appconfig, _ := db.GetManager().ApplicationConfigDao().GetConfigByID(appID, req.ConfigGroupName) var resp *model.ApplicationConfigGroupResp resp = &model.ApplicationConfigGroupResp{ @@ -69,7 +65,7 @@ func (a *ApplicationAction) AddConfigGroup(appID string, req *model.ApplicationC ConfigGroupName: appconfig.ConfigGroupName, DeployType: appconfig.DeployType, ConfigItems: req.ConfigItems, - Services: serviceResult, + Services: serviceResp, } return resp, nil } diff --git a/api/handler/application_config_group_test.go b/api/handler/application_config_group_test.go index 27c23e4a8..29c14f127 100644 --- a/api/handler/application_config_group_test.go +++ b/api/handler/application_config_group_test.go @@ -33,14 +33,19 @@ func TestAddAppConfigGroup(t *testing.T) { }, }, mockFunc: func(manager *db.MockManager, ctrl *gomock.Controller) { - serviceResult := []*dbmodel.TenantServices{ - {ServiceID: "sid1", ServiceName: "sid1_name"}, + serviceResult := &dbmodel.TenantServices{ + ServiceID: "sid1", + ServiceAlias: "sid1_name", } config := &dbmodel.ApplicationConfigGroup{ AppID: "appID1", ConfigGroupName: "configName1", DeployType: "env", } + tenantServiceDao := daomock.NewMockTenantServiceDao(ctrl) + tenantServiceDao.EXPECT().GetServiceByID(gomock.Any()).Return(serviceResult, nil) + manager.EXPECT().TenantServiceDao().Return(tenantServiceDao) + serviceConfigGroupDao := daomock.NewMockServiceConfigGroupDao(ctrl) serviceConfigGroupDao.EXPECT().AddModel(gomock.Any()).Return(nil).AnyTimes() manager.EXPECT().ServiceConfigGroupDao().Return(serviceConfigGroupDao).AnyTimes() @@ -53,10 +58,6 @@ func TestAddAppConfigGroup(t *testing.T) { applicationConfigDao.EXPECT().AddModel(gomock.Any()).Return(nil) applicationConfigDao.EXPECT().GetConfigByID(gomock.Any(), gomock.Any()).Return(config, nil) manager.EXPECT().ApplicationConfigDao().Return(applicationConfigDao).AnyTimes() - - tenantServiceDao := daomock.NewMockTenantServiceDao(ctrl) - tenantServiceDao.EXPECT().GetServiceByIDs(gomock.Any()).Return(serviceResult, nil) - manager.EXPECT().TenantServiceDao().Return(tenantServiceDao) }, wanterr: false, }, @@ -73,6 +74,14 @@ func TestAddAppConfigGroup(t *testing.T) { }, }, mockFunc: func(manager *db.MockManager, ctrl *gomock.Controller) { + serviceResult := &dbmodel.TenantServices{ + ServiceID: "sid1", + ServiceAlias: "sid1_name", + } + tenantServiceDao := daomock.NewMockTenantServiceDao(ctrl) + tenantServiceDao.EXPECT().GetServiceByID(gomock.Any()).Return(serviceResult, nil) + manager.EXPECT().TenantServiceDao().Return(tenantServiceDao) + serviceConfigGroupDao := daomock.NewMockServiceConfigGroupDao(ctrl) serviceConfigGroupDao.EXPECT().AddModel(gomock.Any()).Return(errors.New("add service config failed")).AnyTimes() manager.EXPECT().ServiceConfigGroupDao().Return(serviceConfigGroupDao).AnyTimes() @@ -92,6 +101,14 @@ func TestAddAppConfigGroup(t *testing.T) { }, }, mockFunc: func(manager *db.MockManager, ctrl *gomock.Controller) { + serviceResult := &dbmodel.TenantServices{ + ServiceID: "sid1", + ServiceAlias: "sid1_name", + } + tenantServiceDao := daomock.NewMockTenantServiceDao(ctrl) + tenantServiceDao.EXPECT().GetServiceByID(gomock.Any()).Return(serviceResult, nil) + manager.EXPECT().TenantServiceDao().Return(tenantServiceDao) + serviceConfigGroupDao := daomock.NewMockServiceConfigGroupDao(ctrl) serviceConfigGroupDao.EXPECT().AddModel(gomock.Any()).Return(nil).AnyTimes() manager.EXPECT().ServiceConfigGroupDao().Return(serviceConfigGroupDao).AnyTimes() @@ -115,6 +132,14 @@ func TestAddAppConfigGroup(t *testing.T) { }, }, mockFunc: func(manager *db.MockManager, ctrl *gomock.Controller) { + serviceResult := &dbmodel.TenantServices{ + ServiceID: "sid1", + ServiceAlias: "sid1_name", + } + tenantServiceDao := daomock.NewMockTenantServiceDao(ctrl) + tenantServiceDao.EXPECT().GetServiceByID(gomock.Any()).Return(serviceResult, nil) + manager.EXPECT().TenantServiceDao().Return(tenantServiceDao) + serviceConfigGroupDao := daomock.NewMockServiceConfigGroupDao(ctrl) serviceConfigGroupDao.EXPECT().AddModel(gomock.Any()).Return(nil).AnyTimes() manager.EXPECT().ServiceConfigGroupDao().Return(serviceConfigGroupDao).AnyTimes() diff --git a/api/model/model.go b/api/model/model.go index 87981e568..55f83e66d 100644 --- a/api/model/model.go +++ b/api/model/model.go @@ -1659,7 +1659,7 @@ type ServiceConfigGroup struct { AppID string `json:"app_id"` ConfigGroupName string `json:"config_group_name"` ServiceID string `json:"service_id"` - ServiceName string `json:"service_name"` + ServiceAlias string `json:"service_alias"` } // ConfigItem - @@ -1681,10 +1681,10 @@ type ApplicationConfigGroup struct { // ApplicationConfigGroupResp - type ApplicationConfigGroupResp struct { - CreateTime time.Time `json:"create_time"` - AppID string `json:"app_id"` - ConfigGroupName string `json:"config_group_name"` - DeployType string `json:"deploy_type"` - Services []dbmodel.ServiceIDAndNameResult `json:"services"` - ConfigItems []ConfigItem `json:"config_items"` + CreateTime time.Time `json:"create_time"` + AppID string `json:"app_id"` + ConfigGroupName string `json:"config_group_name"` + DeployType string `json:"deploy_type"` + Services []dbmodel.ServiceConfigGroup `json:"services"` + ConfigItems []ConfigItem `json:"config_items"` } diff --git a/api/util/bcode/application.go b/api/util/bcode/application.go index 06de741cc..88caa5d13 100644 --- a/api/util/bcode/application.go +++ b/api/util/bcode/application.go @@ -5,7 +5,7 @@ var ( //ErrApplicationNotFound - ErrApplicationNotFound = newByMessage(404, 11001, "application not found") //ErrApplicationExist - - ErrApplicationExist = newByMessage(400, 11002, "application already exist") + ErrApplicationExist = newByMessage(409, 11002, "application already exist") //ErrCreateNeedCorrectAppID ErrCreateNeedCorrectAppID = newByMessage(404, 11003, "create service need correct application ID") //ErrUpdateNeedCorrectAppID @@ -17,11 +17,11 @@ var ( // tenant application 11100~11199 var ( //ErrApplicationConfigGroupExist - - ErrApplicationConfigGroupExist = newByMessage(400, 11101, "application config group already exist") + ErrApplicationConfigGroupExist = newByMessage(409, 11101, "application config group already exist") //ErrServiceConfigGroupExist - - ErrServiceConfigGroupExist = newByMessage(400, 11102, "config group under this service already exists") + ErrServiceConfigGroupExist = newByMessage(409, 11102, "config group under this service already exists") //ErrConfigItemExist - - ErrConfigItemExist = newByMessage(400, 11103, "config item under this config group already exist") - //ErrServiceIDNotFound - - ErrServiceIDNotFound = newByMessage(404, 11104, "this service ID cannot be found under this application") + ErrConfigItemExist = newByMessage(409, 11103, "config item under this config group already exist") + //ErrServiceNotFound - + ErrServiceNotFound = newByMessage(404, 11104, "this service ID cannot be found under this application") ) diff --git a/db/dao/dao.go b/db/dao/dao.go index 19c1957c4..5b1674848 100644 --- a/db/dao/dao.go +++ b/db/dao/dao.go @@ -122,7 +122,7 @@ type TenantServiceDao interface { GetServicesAllInfoByTenantID(tenantID string) ([]*model.TenantServices, error) GetServicesInfoByAppID(appID string, page, pageSize int) ([]*model.TenantServices, int64, error) CountServiceByAppID(appID string) (int64, error) - GetServicesIDAndNameByAppID(appID string) (re []model.ServiceIDAndNameResult) + GetServiceIDsByAppID(appID string) (re []model.ServiceID) DeleteServiceByServiceID(serviceID string) error GetServiceMemoryByTenantIDs(tenantIDs, serviceIDs []string) (map[string]map[string]interface{}, error) GetServiceMemoryByServiceIDs(serviceIDs []string) (map[string]map[string]interface{}, error) diff --git a/db/dao/dao_mock.go b/db/dao/dao_mock.go index 5e417d504..f22800608 100644 --- a/db/dao/dao_mock.go +++ b/db/dao/dao_mock.go @@ -1078,18 +1078,18 @@ func (mr *MockTenantServiceDaoMockRecorder) CountServiceByAppID(appID interface{ return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CountServiceByAppID", reflect.TypeOf((*MockTenantServiceDao)(nil).CountServiceByAppID), appID) } -// GetServicesIDAndNameByAppID mocks base method. -func (m *MockTenantServiceDao) GetServicesIDAndNameByAppID(appID string) []model.ServiceIDAndNameResult { +// GetServiceIDsByAppID mocks base method. +func (m *MockTenantServiceDao) GetServiceIDsByAppID(appID string) []model.ServiceID { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetServicesIDAndNameByAppID", appID) - ret0, _ := ret[0].([]model.ServiceIDAndNameResult) + ret := m.ctrl.Call(m, "GetServiceIDsByAppID", appID) + ret0, _ := ret[0].([]model.ServiceID) return ret0 } -// GetServicesIDAndNameByAppID indicates an expected call of GetServicesIDAndNameByAppID. -func (mr *MockTenantServiceDaoMockRecorder) GetServicesIDAndNameByAppID(appID interface{}) *gomock.Call { +// GetServiceIDsByAppID indicates an expected call of GetServiceIDsByAppID. +func (mr *MockTenantServiceDaoMockRecorder) GetServiceIDsByAppID(appID interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetServicesIDAndNameByAppID", reflect.TypeOf((*MockTenantServiceDao)(nil).GetServicesIDAndNameByAppID), appID) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetServiceIDsByAppID", reflect.TypeOf((*MockTenantServiceDao)(nil).GetServiceIDsByAppID), appID) } // DeleteServiceByServiceID mocks base method. diff --git a/db/model/application.go b/db/model/application.go index 50a74ce87..646597a4e 100644 --- a/db/model/application.go +++ b/db/model/application.go @@ -16,15 +16,15 @@ func (t *Application) TableName() string { // ServiceConfigGroup - type ServiceConfigGroup struct { Model - AppID string `gorm:"column:app_id" json:"app_id"` - ConfigGroupName string `gorm:"column:config_group_name" json:"config_group_name"` + AppID string `gorm:"column:app_id" json:"-"` + ConfigGroupName string `gorm:"column:config_group_name" json:"-"` ServiceID string `gorm:"column:service_id" json:"service_id"` - ServiceName string `gorm:"column:service_name" json:"service_name"` + ServiceAlias string `gorm:"column:service_alias" json:"service_alias"` } // TableName return tableName "application" func (t *ServiceConfigGroup) TableName() string { - return "application_config_service" + return "app_config_group_service" } // ConfigItem - @@ -38,7 +38,7 @@ type ConfigItem struct { // TableName return tableName "application" func (t *ConfigItem) TableName() string { - return "application_config_item" + return "app_config_group_item" } // ApplicationConfigGroup - @@ -51,5 +51,5 @@ type ApplicationConfigGroup struct { // TableName return tableName "application" func (t *ApplicationConfigGroup) TableName() string { - return "application_config_group" + return "app_config_group" } diff --git a/db/model/tenant.go b/db/model/tenant.go index 552df6020..a7c87cf95 100644 --- a/db/model/tenant.go +++ b/db/model/tenant.go @@ -604,8 +604,7 @@ func (t *TenantServiceScalingRecords) TableName() string { return "tenant_services_scaling_records" } -// ServiceIDAndNameResult - -type ServiceIDAndNameResult struct { - ServiceID string `gorm:"column:service_id" json:"-"` - ServiceName string `gorm:"column:service_name" json:"-"` +// ServiceID - +type ServiceID struct { + ServiceID string `gorm:"column:service_id" json:"-"` } diff --git a/db/mysql/dao/tenants.go b/db/mysql/dao/tenants.go index 6bda56f04..058084012 100644 --- a/db/mysql/dao/tenants.go +++ b/db/mysql/dao/tenants.go @@ -482,11 +482,11 @@ func (t *TenantServicesDaoImpl) CountServiceByAppID(appID string) (int64, error) return total, nil } -// GetServicesIDAndNameByAppID get ServiceID and ServiceName by AppID -func (t *TenantServicesDaoImpl) GetServicesIDAndNameByAppID(appID string) (re []model.ServiceIDAndNameResult) { - if err := t.DB.Raw("SELECT service_id,service_name FROM tenant_services WHERE app_id=?", appID). +// GetServiceIDsByAppID get ServiceIDs by AppID +func (t *TenantServicesDaoImpl) GetServiceIDsByAppID(appID string) (re []model.ServiceID) { + if err := t.DB.Raw("SELECT service_id FROM tenant_services WHERE app_id=?", appID). Scan(&re).Error; err != nil { - logrus.Errorf("select service_id and service_name failure %s", err.Error()) + logrus.Errorf("select service_id failure %s", err.Error()) return } return