Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): support integration in import cmd #1372

Merged
merged 5 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 52 additions & 8 deletions server/cmd/reearth-cms/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
"github.com/reearth/reearth-cms/server/internal/usecase/interfaces"
"github.com/reearth/reearth-cms/server/internal/usecase/repo"
"github.com/reearth/reearth-cms/server/pkg/id"
integration2 "github.com/reearth/reearth-cms/server/pkg/integration"
"github.com/reearth/reearth-cms/server/pkg/integrationapi"
"github.com/reearth/reearth-cms/server/pkg/schema"
"github.com/reearth/reearth-cms/server/pkg/value"
"github.com/reearth/reearthx/account/accountdomain"
"github.com/reearth/reearthx/account/accountdomain/user"
"github.com/reearth/reearthx/account/accountdomain/workspace"
"github.com/reearth/reearthx/account/accountusecase"
Expand All @@ -39,6 +41,7 @@ func main() {
if len(os.Args) >= 3 && os.Args[1] == "item" && os.Args[2] == "import" {
importCmd := flag.NewFlagSet("import", flag.ExitOnError)
uIdStr := importCmd.String("userId", "", "")
iIdStr := importCmd.String("integrationId", "", "")
mIdStr := importCmd.String("modelId", "", "")
aIdStr := importCmd.String("assetId", "", "")
format := importCmd.String("format", "", "")
Expand All @@ -51,10 +54,6 @@ func main() {
return
}

uId, err := user.IDFrom(*uIdStr)
if err != nil {
log.Fatal("invalid user id")
}
mId := id.ModelIDFromRef(mIdStr)
if mId == nil {
log.Fatal("invalid model id")
Expand Down Expand Up @@ -94,8 +93,13 @@ func main() {
})

// get op from user id
op, err := generateUserOperator(ctx, uId, repos, acRepos)
if err != nil {
var op *usecase.Operator
if uIdStr != nil && *uIdStr != "" {
op, err = generateUserOperator(ctx, *uIdStr, repos, acRepos)
} else if iIdStr != nil && *iIdStr != "" {
op, err = generateIntegrationOperator(ctx, *iIdStr, repos, acRepos)
}
if err != nil || op == nil {
log.Fatalf("failed to generate operator: %v", err)
}

Expand Down Expand Up @@ -275,8 +279,11 @@ func isAssignable(vt1, vt2 value.Type) bool {
return false
}

func generateUserOperator(ctx context.Context, uId user.ID, repo *repo.Container, accRepo *accountrepo.Container) (*usecase.Operator, error) {

func generateUserOperator(ctx context.Context, uIdStr string, repo *repo.Container, accRepo *accountrepo.Container) (*usecase.Operator, error) {
uId, err := user.IDFrom(uIdStr)
if err != nil {
return nil, err
}
w, err := accRepo.Workspace.FindByUser(ctx, uId)
if err != nil {
return nil, err
Expand Down Expand Up @@ -313,6 +320,43 @@ func generateUserOperator(ctx context.Context, uId user.ID, repo *repo.Container
}, nil
}

func generateIntegrationOperator(ctx context.Context, iIdStr string, repo *repo.Container, accRepo *accountrepo.Container) (*usecase.Operator, error) {
iId, err := accountdomain.IntegrationIDFrom(iIdStr)
if err != nil {
return nil, err
}
w, err := accRepo.Workspace.FindByIntegration(ctx, iId)
if err != nil {
return nil, err
}

rw := w.FilterByIntegrationRole(iId, workspace.RoleReader).IDs()
ww := w.FilterByIntegrationRole(iId, workspace.RoleWriter).IDs()
mw := w.FilterByIntegrationRole(iId, workspace.RoleMaintainer).IDs()
ow := w.FilterByIntegrationRole(iId, workspace.RoleOwner).IDs()

rp, wp, mp, op, err := operatorProjects(ctx, repo, w, rw, ww, mw, ow)
if err != nil {
return nil, err
}

return &usecase.Operator{
AcOperator: &accountusecase.Operator{
User: nil,
ReadableWorkspaces: rw,
WritableWorkspaces: ww,
MaintainableWorkspaces: mw,
OwningWorkspaces: ow,
},
Integration: integration2.IDFromRef(iId.StringRef()),
Lang: language.English.String(),
ReadableProjects: rp,
WritableProjects: wp,
MaintainableProjects: mp,
OwningProjects: op,
}, nil
}

func operatorProjects(ctx context.Context, g *repo.Container, w workspace.List, rw, ww, mw, ow user.WorkspaceIDList) (id.ProjectIDList, id.ProjectIDList, id.ProjectIDList, id.ProjectIDList, error) {
rp := id.ProjectIDList{}
wp := id.ProjectIDList{}
Expand Down
1 change: 1 addition & 0 deletions server/internal/infrastructure/gcp/taskrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ func importItems(ctx context.Context, p task.Payload, conf *TaskConfig) error {
"item",
"import",
"-userId=" + p.Import.UserId,
"-integrationId=" + p.Import.IntegrationId,
"-modelId=" + p.Import.ModelId,
"-assetId=" + p.Import.AssetId,
"-format=" + p.Import.Format,
Expand Down
11 changes: 10 additions & 1 deletion server/internal/usecase/interactor/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,20 +634,29 @@ func (i Item) Import(ctx context.Context, param interfaces.ImportItemsParam, ope
}

func (i Item) Import2(ctx context.Context, aId id.AssetID, mId id.ModelID, format, strategy, geoFieldKey string, mutateSchema bool, operator *usecase.Operator) error {
if operator.AcOperator.User == nil && operator.Integration == nil {
return interfaces.ErrInvalidOperator
}

if i.gateways.TaskRunner == nil {
log.Info("item: import skipped because task runner is not configured")
return nil
}

taskPayload := task.ImportPayload{
UserId: operator.AcOperator.User.String(),
ModelId: mId.String(),
AssetId: aId.String(),
Format: format,
GeometryFieldKey: geoFieldKey,
Strategy: strategy,
MutateSchema: mutateSchema,
}
if operator.AcOperator.User != nil {
taskPayload.UserId = operator.AcOperator.User.String()
}
if operator.Integration != nil {
taskPayload.IntegrationId = operator.Integration.String()
}

if err := i.gateways.TaskRunner.Run(ctx, taskPayload.Payload()); err != nil {
return fmt.Errorf("failed to trigger copy event: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion server/pkg/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const (

type ImportPayload struct {
UserId string
IntegrationId string
ModelId string
AssetId string
Format string
Expand All @@ -86,7 +87,7 @@ type ImportPayload struct {
}

func (p *ImportPayload) Validate() bool {
return p != nil && p.UserId != "" && p.ModelId != "" && p.AssetId != "" && p.Format != "" && p.GeometryFieldKey != "" && p.Strategy != ""
return p != nil && (p.UserId != "" || p.IntegrationId != "") && p.ModelId != "" && p.AssetId != "" && p.Format != "" && p.GeometryFieldKey != "" && p.Strategy != ""
}

func (p *ImportPayload) Payload() Payload {
Expand Down
Loading