diff --git a/controller/service/iml.go b/controller/service/iml.go index c782bad4..aac8c32b 100644 --- a/controller/service/iml.go +++ b/controller/service/iml.go @@ -6,6 +6,9 @@ import ( "net/http" "strings" + application_authorization "github.com/APIParkLab/APIPark/module/application-authorization" + application_authorization_dto "github.com/APIParkLab/APIPark/module/application-authorization/dto" + "github.com/APIParkLab/APIPark/model/plugin_model" "github.com/APIParkLab/APIPark/service/api" @@ -271,7 +274,8 @@ func (i *imlServiceController) SaveServiceDoc(ctx *gin.Context, id string, input } type imlAppController struct { - module service.IAppModule `autowired:""` + module service.IAppModule `autowired:""` + authModule application_authorization.IAuthorizationModule `autowired:""` } func (i *imlAppController) Search(ctx *gin.Context, teamId string, keyword string) ([]*service_dto.AppItem, error) { @@ -279,7 +283,25 @@ func (i *imlAppController) Search(ctx *gin.Context, teamId string, keyword strin } func (i *imlAppController) CreateApp(ctx *gin.Context, teamID string, input *service_dto.CreateApp) (*service_dto.App, error) { - return i.module.CreateApp(ctx, teamID, input) + app, err := i.module.CreateApp(ctx, teamID, input) + if err != nil { + return nil, err + } + _, err = i.authModule.AddAuthorization(ctx, app.Id, &application_authorization_dto.CreateAuthorization{ + Name: "Default API Key", + Driver: "apikey", + Position: "Header", + TokenName: "Authorization", + ExpireTime: 0, + Config: map[string]interface{}{ + "apikey": uuid.New().String(), + }, + }) + if err != nil { + i.module.DeleteApp(ctx, app.Id) + return nil, err + } + return app, nil } func (i *imlAppController) UpdateApp(ctx *gin.Context, appId string, input *service_dto.UpdateApp) (*service_dto.App, error) { return i.module.UpdateApp(ctx, appId, input) diff --git a/controller/system/iml.go b/controller/system/iml.go index 72e0de8f..a11b2e23 100644 --- a/controller/system/iml.go +++ b/controller/system/iml.go @@ -3,11 +3,23 @@ package system import ( "archive/zip" "bytes" + "context" "encoding/json" "fmt" "net/http" "time" + catalogue_dto "github.com/APIParkLab/APIPark/module/catalogue/dto" + + application_authorization_dto "github.com/APIParkLab/APIPark/module/application-authorization/dto" + service_dto "github.com/APIParkLab/APIPark/module/service/dto" + team_dto "github.com/APIParkLab/APIPark/module/team/dto" + "github.com/eolinker/eosc/log" + "github.com/eolinker/go-common/register" + "github.com/eolinker/go-common/server" + "github.com/eolinker/go-common/utils" + "github.com/google/uuid" + system_dto "github.com/APIParkLab/APIPark/module/system/dto" application_authorization "github.com/APIParkLab/APIPark/module/application-authorization" @@ -185,3 +197,70 @@ func (i *imlSettingController) Get(ctx *gin.Context) (*system_dto.Setting, error func (i *imlSettingController) Set(ctx *gin.Context, input *system_dto.InputSetting) error { return i.settingModule.Set(ctx, input) } + +type imlInitController struct { + teamModule team.ITeamModule `autowired:""` + appModule service.IAppModule `autowired:""` + applicationAuthorizationModule application_authorization.IAuthorizationModule `autowired:""` + catalogueModule catalogue.ICatalogueModule `autowired:""` +} + +func (i *imlInitController) OnInit() { + register.Handle(func(v server.Server) { + ctx := utils.SetUserId(context.Background(), "admin") + teams, err := i.teamModule.Search(ctx, "") + if err != nil { + log.Error("get teams error: %v", err) + return + } + if len(teams) == 0 { + info, err := i.teamModule.Create(ctx, &team_dto.CreateTeam{ + Name: "Default Team", + Description: "Auto created By APIPark", + }) + if err != nil { + log.Error("create default team error: %v", err) + return + } + app, err := i.appModule.CreateApp(ctx, info.Id, &service_dto.CreateApp{ + Name: "Demo Application", + Description: "Auto created By APIPark", + }) + if err != nil { + i.teamModule.Delete(ctx, info.Id) + log.Error("create default app error: %v", err) + return + } + _, err = i.applicationAuthorizationModule.AddAuthorization(ctx, app.Id, &application_authorization_dto.CreateAuthorization{ + Name: "Default API Key", + Driver: "apikey", + Position: "Header", + TokenName: "Authorization", + ExpireTime: 0, + Config: map[string]interface{}{ + "apikey": uuid.New().String(), + }, + }) + if err != nil { + i.teamModule.Delete(ctx, info.Id) + i.appModule.DeleteApp(ctx, app.Id) + log.Error("create default api key error: %v", err) + return + } + } + items, err := i.catalogueModule.Search(ctx, "") + if err != nil { + log.Error("get catalogue error: %v", err) + return + } + if len(items) == 0 { + err = i.catalogueModule.Create(ctx, &catalogue_dto.CreateCatalogue{ + Name: "Default Catalogue", + }) + if err != nil { + log.Error("create default catalogue error: %v", err) + return + } + } + }) +} diff --git a/controller/system/system.go b/controller/system/system.go index 008bf5d0..43a9a609 100644 --- a/controller/system/system.go +++ b/controller/system/system.go @@ -21,6 +21,9 @@ type ISettingController interface { Set(ctx *gin.Context, input *system_dto.InputSetting) error } +type IInitController interface { +} + func init() { autowire.Auto[IExportConfigController](func() reflect.Value { return reflect.ValueOf(new(imlExportConfigController)) @@ -33,4 +36,8 @@ func init() { autowire.Auto[ISettingController](func() reflect.Value { return reflect.ValueOf(new(imlSettingController)) }) + + autowire.Auto[IInitController](func() reflect.Value { + return reflect.ValueOf(new(imlInitController)) + }) } diff --git a/module/cluster/impl.go b/module/cluster/impl.go index 06d3b0af..db37f50e 100644 --- a/module/cluster/impl.go +++ b/module/cluster/impl.go @@ -2,16 +2,18 @@ package cluster import ( "context" - + + "github.com/APIParkLab/APIPark/service/setting" + cluster_dto "github.com/APIParkLab/APIPark/module/cluster/dto" - + "github.com/APIParkLab/APIPark/gateway/admin" "github.com/eolinker/eosc/log" - + "github.com/eolinker/go-common/store" - + "github.com/APIParkLab/APIPark/gateway" - + "github.com/APIParkLab/APIPark/service/cluster" "github.com/eolinker/ap-account/service/account" "github.com/eolinker/go-common/utils" @@ -23,6 +25,7 @@ var ( type imlClusterModule struct { clusterService cluster.IClusterService `autowired:""` + settingService setting.ISettingService `autowired:""` userNameService account.IAccountService `autowired:""` transaction store.ITransaction `autowired:""` } @@ -42,12 +45,12 @@ func (m *imlClusterModule) CheckCluster(ctx context.Context, address ...string) } }) nodeStatus(ctx, nodesOut) - + return nodesOut, nil } func (m *imlClusterModule) ResetCluster(ctx context.Context, clusterId string, address string) ([]*cluster_dto.Node, error) { - + nodes, err := m.clusterService.UpdateAddress(ctx, clusterId, address) if err != nil { return nil, err @@ -65,7 +68,10 @@ func (m *imlClusterModule) ResetCluster(ctx context.Context, clusterId string, a Gateways: i.Server, } }) - + v, has := m.settingService.Get(ctx, setting.KeyInvokeAddress) + if (!has || v == "") && len(nodesOut) > 0 && len(nodesOut[0].Gateways) > 0 { + m.settingService.Set(ctx, setting.KeyInvokeAddress, nodesOut[0].Gateways[0], utils.UserId(ctx)) + } nodeStatus(ctx, nodesOut) return nodesOut, nil } @@ -83,7 +89,7 @@ func (m *imlClusterModule) initGateway(ctx context.Context, clusterId string) er return gateway.InitGateway(ctx, clusterId, client) } func (m *imlClusterModule) ClusterNodes(ctx context.Context, clusterId string) ([]*cluster_dto.Node, error) { - + nodes, err := m.clusterService.Nodes(ctx) if err != nil { return nil, err @@ -98,7 +104,7 @@ func (m *imlClusterModule) ClusterNodes(ctx context.Context, clusterId string) ( } }) nodeStatus(ctx, nodesOut) - + return nodesOut, nil } diff --git a/module/team/iml.go b/module/team/iml.go index c02e5e00..a61f9854 100644 --- a/module/team/iml.go +++ b/module/team/iml.go @@ -161,19 +161,3 @@ func (m *imlTeamModule) Delete(ctx context.Context, id string) error { }) return err } - -func (m *imlTeamModule) OnInit() { - ctx := context.Background() - list, err := m.service.List(ctx) - if err != nil { - return - } - if len(list) == 0 { - teamId := uuid.New().String() - err = m.service.Create(ctx, &team.CreateTeam{ - Id: teamId, - Name: "Default Team", - Description: "Auto create default team", - }) - } -} diff --git a/plugins/core/core.go b/plugins/core/core.go index 2be3ebef..e194b191 100644 --- a/plugins/core/core.go +++ b/plugins/core/core.go @@ -83,6 +83,7 @@ type plugin struct { importConfigController system.IImportConfigController `autowired:""` aiProviderController ai.IProviderController `autowired:""` settingController system.ISettingController `autowired:""` + initController system.IInitController `autowired:""` apis []pm3.Api } diff --git a/resources/access/access.yaml b/resources/access/access.yaml index 9585978a..4ba49f1b 100644 --- a/resources/access/access.yaml +++ b/resources/access/access.yaml @@ -1,6 +1,8 @@ system: - name: organization cname: '组织管理' + i18n: + zh_CN: 组织管理 value: 'organization' children: - name: member @@ -190,23 +192,6 @@ system: - "PUT:/api/v1/dynamic/{name}/offline" dependents: - system.devops.log_configuration.view - - name: monitor - cname: 监控 - value: 'monitor' - children: - - name: view - cname: 查看 - value: 'view' - guest_allow: true - apis: - - "GET:/api/v1/monitor/config" - - name: manager - cname: 管理 - value: 'manager' - apis: - - "POST:/api/v1/monitor/config" - dependents: - - system.devops.monitor.view - name: ai_provider cname: AI 模型供应商 value: 'ai_provider' diff --git a/resources/access/role.yaml b/resources/access/role.yaml index 750f1650..8cc1f5b3 100644 --- a/resources/access/role.yaml +++ b/resources/access/role.yaml @@ -2,25 +2,27 @@ system: - name: supper_admin cname: 超级管理员 permits: - - system.api_market.service_classification.manager - - system.api_market.service_classification.view - system.dashboard.run_view.view - - system.devops.ai_provider.view - system.devops.ai_provider.manager + - system.devops.ai_provider.view - system.devops.cluster.manager - system.devops.cluster.view + - system.devops.data_source.manager + - system.devops.data_source.view - system.devops.log_configuration.manager - system.devops.log_configuration.view - system.devops.ssl_certificate.manager - system.devops.ssl_certificate.view - - system.devops.data_source.manager - - system.devops.data_source.view - system.organization.member.manager - system.organization.member.view - system.organization.role.view_system_role - system.organization.role.view_team_role - system.organization.team.manager - system.organization.team.view + - system.settings.general.manager + - system.settings.general.view + - system.settings.service_classification.manager + - system.settings.service_classification.view - system.workspace.api_market.view - system.workspace.application.view_all - system.workspace.service.view_all @@ -39,23 +41,25 @@ system: - name: devops_admin cname: 运维管理员 permits: - - system.api_market.service_classification.manager - - system.api_market.service_classification.view - system.dashboard.run_view.view - - system.devops.ai_provider.view - system.devops.ai_provider.manager + - system.devops.ai_provider.view - system.devops.cluster.manager - system.devops.cluster.view + - system.devops.data_source.manager + - system.devops.data_source.view - system.devops.log_configuration.manager - system.devops.log_configuration.view - system.devops.ssl_certificate.manager - system.devops.ssl_certificate.view - - system.devops.data_source.manager - - system.devops.data_source.view - system.workspace.api_market.view - system.workspace.application.view_all - system.workspace.service.view_all - system.workspace.team.view_all + - system.settings.general.manager + - system.settings.general.view + - system.settings.service_classification.manager + - system.settings.service_classification.view - name: member cname: 普通成员 permits: @@ -64,7 +68,7 @@ system: - name: guest cname: 只读成员 permits: - - system.api_market.service_classification.view + - system.settings.service_classification.view - system.devops.cluster.view - system.devops.log_configuration.view - system.devops.ssl_certificate.view