Skip to content

Commit

Permalink
refactor: expose presetcreator
Browse files Browse the repository at this point in the history
  • Loading branch information
donch1989 committed Sep 25, 2024
1 parent e2d0b1d commit c1694fd
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 44 deletions.
56 changes: 15 additions & 41 deletions cmd/init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,27 @@ import (
"time"

"github.com/gofrs/uuid"
"github.com/instill-ai/mgmt-backend/config"
"github.com/instill-ai/mgmt-backend/pkg/acl"
"github.com/instill-ai/mgmt-backend/pkg/constant"
"github.com/instill-ai/mgmt-backend/pkg/datamodel"
"github.com/instill-ai/mgmt-backend/pkg/logger"
"github.com/instill-ai/mgmt-backend/pkg/repository"
"github.com/instill-ai/mgmt-backend/pkg/service"
"github.com/redis/go-redis/v9"
"go.opentelemetry.io/otel"
"golang.org/x/crypto/bcrypt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gorm.io/gorm"

grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
openfga "github.com/openfga/go-sdk/client"

"github.com/instill-ai/mgmt-backend/cmd/init/preset"
"github.com/instill-ai/mgmt-backend/config"
"github.com/instill-ai/mgmt-backend/pkg/acl"
"github.com/instill-ai/mgmt-backend/pkg/constant"
"github.com/instill-ai/mgmt-backend/pkg/datamodel"
"github.com/instill-ai/mgmt-backend/pkg/logger"
"github.com/instill-ai/mgmt-backend/pkg/repository"
"github.com/instill-ai/mgmt-backend/pkg/service"

database "github.com/instill-ai/mgmt-backend/pkg/db"
custom_otel "github.com/instill-ai/mgmt-backend/pkg/logger/otel"
customotel "github.com/instill-ai/mgmt-backend/pkg/logger/otel"
mgmtPB "github.com/instill-ai/protogen-go/core/mgmt/v1beta"
)

Expand Down Expand Up @@ -101,34 +103,6 @@ func createDefaultUser(ctx context.Context, r repository.Repository) error {
return nil
}

func createPresetOrg(ctx context.Context, r repository.Repository) error {

// In Instill Core, we provide a "preset" namespace for storing preset
// resources, such as preset pipelines.
presetOrg := &datamodel.Owner{
Base: datamodel.Base{UID: uuid.FromStringOrNil(constant.PresetOrgUID)},
ID: constant.PresetOrgID,
OwnerType: sql.NullString{String: service.PBUserType2DBUserType[mgmtPB.OwnerType_OWNER_TYPE_ORGANIZATION], Valid: true},
DisplayName: sql.NullString{String: constant.PresetOrgDisplayName, Valid: true},
}

_, err := r.GetOrganization(context.Background(), constant.PresetOrgID, false)
if err == nil {
return nil
}

if !errors.Is(err, gorm.ErrRecordNotFound) {
return status.Errorf(codes.Internal, "error %v", err)
}

// Create the default preset organization
err = r.CreateOrganization(ctx, presetOrg)
if err != nil {
return err
}
return nil
}

func main() {
if err := config.Init(config.ParseConfigFlag()); err != nil {
log.Fatal(err.Error())
Expand All @@ -137,7 +111,7 @@ func main() {
// setup tracing and metrics
ctx, cancel := context.WithCancel(context.Background())

if tp, err := custom_otel.SetupTracing(ctx, "mgmt-backend-init"); err != nil {
if tp, err := customotel.SetupTracing(ctx, "mgmt-backend-init"); err != nil {
panic(err)
} else {
defer func() {
Expand All @@ -156,7 +130,7 @@ func main() {
// can't handle the error due to https://github.com/uber-go/zap/issues/880
_ = logger.Sync()
}()
grpc_zap.ReplaceGrpcLoggerV2(logger)
grpczap.ReplaceGrpcLoggerV2(logger)

db := database.GetConnection(&config.Config.Database)
defer database.Close(db)
Expand All @@ -170,8 +144,8 @@ func main() {
logger.Fatal(err.Error())
}

// Create template organization
if err := createPresetOrg(ctx, r); err != nil {
// Create preset organization
if err := preset.CreatePresetOrg(ctx, r); err != nil {
logger.Fatal(err.Error())
}

Expand Down
47 changes: 47 additions & 0 deletions cmd/init/preset/presetcreator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package preset

import (
"context"
"database/sql"
"errors"

"github.com/gofrs/uuid"
"github.com/gogo/status"
"google.golang.org/grpc/codes"
"gorm.io/gorm"

"github.com/instill-ai/mgmt-backend/pkg/constant"
"github.com/instill-ai/mgmt-backend/pkg/datamodel"
"github.com/instill-ai/mgmt-backend/pkg/repository"
"github.com/instill-ai/mgmt-backend/pkg/service"

mgmtPB "github.com/instill-ai/protogen-go/core/mgmt/v1beta"
)

func CreatePresetOrg(ctx context.Context, r repository.Repository) error {

// In Instill Core, we provide a "preset" namespace for storing preset
// resources, such as preset pipelines.
presetOrg := &datamodel.Owner{
Base: datamodel.Base{UID: uuid.FromStringOrNil(constant.PresetOrgUID)},
ID: constant.PresetOrgID,
OwnerType: sql.NullString{String: service.PBUserType2DBUserType[mgmtPB.OwnerType_OWNER_TYPE_ORGANIZATION], Valid: true},
DisplayName: sql.NullString{String: constant.PresetOrgDisplayName, Valid: true},
}

_, err := r.GetOrganization(context.Background(), constant.PresetOrgID, false)
if err == nil {
return nil
}

if !errors.Is(err, gorm.ErrRecordNotFound) {
return status.Errorf(codes.Internal, "error %v", err)
}

// Create the default preset organization
err = r.CreateOrganization(ctx, presetOrg)
if err != nil {
return err
}
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/gabriel-vasile/mimetype v1.4.3
github.com/go-redis/redismock/v9 v9.2.0
github.com/gofrs/uuid v4.4.0+incompatible
github.com/gogo/status v1.1.1
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1
Expand Down Expand Up @@ -68,7 +69,6 @@ require (
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/gogo/status v1.1.1 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions pkg/repository/influx.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (i *influxDB) QueryPipelineTriggerTableRecords(ctx context.Context, owner s
// TODO: design better filter expression to flux transpiler
expr, err := i.transpileFilter(filter)
if err != nil {
return nil, 0, "", status.Errorf(codes.Internal, err.Error())
return nil, 0, "", status.Error(codes.Internal, err.Error())
}

if expr != "" {
Expand Down Expand Up @@ -320,7 +320,7 @@ func (i *influxDB) QueryPipelineTriggerChartRecords(ctx context.Context, owner s
// TODO: design better filter expression to flux transpiler
expr, err := i.transpileFilter(filter)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
return nil, status.Error(codes.Internal, err.Error())
}

if expr != "" {
Expand Down

0 comments on commit c1694fd

Please sign in to comment.