Skip to content

Commit

Permalink
feat: added environment
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Jun 7, 2023
1 parent ed9c369 commit d165022
Show file tree
Hide file tree
Showing 18 changed files with 173 additions and 0 deletions.
1 change: 1 addition & 0 deletions controllers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
ControllerIdEnvDeps
ControllerIdNotification
ControllerIdFilter
ControllerIdEnvironment

ControllerIdVersion
ControllerIdI18n
Expand Down
9 changes: 9 additions & 0 deletions controllers/binder_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func (b *JsonBinder) Bind(c *gin.Context) (res interfaces.Model, err error) {
case ControllerIdPermission:
err = c.ShouldBindJSON(&m.Permission)
return &m.Permission, nil
case ControllerIdEnvironment:
err = c.ShouldBindJSON(&m.Environment)
return &m.Environment, nil
default:
return nil, errors.ErrorControllerInvalidControllerId
}
Expand Down Expand Up @@ -134,6 +137,9 @@ func (b *JsonBinder) BindList(c *gin.Context) (res interface{}, err error) {
case ControllerIdRole:
err = c.ShouldBindJSON(&m.Roles)
return m.Roles, nil
case ControllerIdEnvironment:
err = c.ShouldBindJSON(&m.Environments)
return m.Environments, nil
default:
return nil, errors.ErrorControllerInvalidControllerId
}
Expand Down Expand Up @@ -202,6 +208,9 @@ func (b *JsonBinder) BindBatchRequestPayloadWithStringData(c *gin.Context) (payl
case ControllerIdPlugin:
err = json.Unmarshal([]byte(payload.Data), &m.Plugin)
return payload, &m.Plugin, err
case ControllerIdEnvironment:
err = json.Unmarshal([]byte(payload.Data), &m.Environment)
return payload, &m.Environment, err
default:
return payload, nil, errors.ErrorControllerInvalidControllerId
}
Expand Down
65 changes: 65 additions & 0 deletions controllers/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package controllers

import (
"github.com/crawlab-team/crawlab-core/interfaces"
"github.com/crawlab-team/crawlab-core/models/service"
"github.com/crawlab-team/crawlab-core/user"
"go.uber.org/dig"
)

var EnvironmentController *environmentController

var EnvironmentActions []Action

type environmentController struct {
ListActionControllerDelegate
d ListActionControllerDelegate
ctx *environmentContext
}

type environmentContext struct {
modelSvc service.ModelService
userSvc interfaces.UserService
}

func newEnvironmentContext() *environmentContext {
// context
ctx := &environmentContext{}

// dependency injection
c := dig.New()
if err := c.Provide(service.NewService); err != nil {
panic(err)
}
if err := c.Provide(user.ProvideGetUserService()); err != nil {
panic(err)
}
if err := c.Invoke(func(
modelSvc service.ModelService,
userSvc interfaces.UserService,
) {
ctx.modelSvc = modelSvc
ctx.userSvc = userSvc
}); err != nil {
panic(err)
}

return ctx
}

func newEnvironmentController() *environmentController {
modelSvc, err := service.GetService()
if err != nil {
panic(err)
}

ctr := NewListPostActionControllerDelegate(ControllerIdEnvironment, modelSvc.GetBaseService(interfaces.ModelIdEnvironment), EnvironmentActions)
d := NewListPostActionControllerDelegate(ControllerIdEnvironment, modelSvc.GetBaseService(interfaces.ModelIdEnvironment), EnvironmentActions)
ctx := newEnvironmentContext()

return &environmentController{
ListActionControllerDelegate: *ctr,
d: *d,
ctx: ctx,
}
}
1 change: 1 addition & 0 deletions controllers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func InitControllers() (err error) {
NotificationController = NewActionControllerDelegate(ControllerIdNotification, getNotificationActions())
FilterController = NewActionControllerDelegate(ControllerIdFilter, getFilterActions())
DataSourceController = newDataSourceController()
EnvironmentController = newEnvironmentController()

return nil
}
2 changes: 2 additions & 0 deletions grpc/server/model_delegate_binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func (b *ModelDelegateBinder) Bind() (res interface{}, err error) {
return b.process(&m.Permission)
case interfaces.ModelIdRolePermission:
return b.process(&m.RolePermission)
case interfaces.ModelIdEnvironment:
return b.process(&m.Environment)
default:
return nil, errors.ErrorModelInvalidModelId
}
Expand Down
2 changes: 2 additions & 0 deletions interfaces/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
ModelIdUserRole
ModelIdPermission
ModelIdRolePermission
ModelIdEnvironment
)

const (
Expand Down Expand Up @@ -68,6 +69,7 @@ const (
ModelColNameUserRole = "user_roles"
ModelColNamePermission = "permissions"
ModelColNameRolePermission = "role_permissions"
ModelColNameEnvironment = "environments"
)

type ModelWithTags interface {
Expand Down
2 changes: 2 additions & 0 deletions models/client/binder_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ func (b *BasicBinder) Bind() (res interfaces.Model, err error) {
return b.Process(&m.Permission)
case interfaces.ModelIdRolePermission:
return b.Process(&m.RolePermission)
case interfaces.ModelIdEnvironment:
return b.Process(&m.Environment)
default:
return nil, errors.ErrorModelInvalidModelId
}
Expand Down
2 changes: 2 additions & 0 deletions models/client/binder_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ func (b *ListBinder) Bind() (l interfaces.List, err error) {
return b.Process(&m.PermissionList)
case interfaces.ModelIdRolePermission:
return b.Process(&m.RolePermissionList)
case interfaces.ModelIdEnvironment:
return b.Process(&m.Environments)
default:
return l, errors.ErrorModelInvalidModelId
}
Expand Down
2 changes: 2 additions & 0 deletions models/client/model_delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func NewModelDelegate(doc interfaces.Model, opts ...ModelDelegateOption) interfa
return newModelDelegate(interfaces.ModelIdPermission, doc, opts...)
case *models.RolePermission:
return newModelDelegate(interfaces.ModelIdRolePermission, doc, opts...)
case *models.Environment:
return newModelDelegate(interfaces.ModelIdEnvironment, doc, opts...)
default:
_ = trace.TraceError(errors.ErrorModelInvalidType)
return nil
Expand Down
2 changes: 2 additions & 0 deletions models/delegate/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func NewModelDelegate(doc interfaces.Model, args ...interface{}) interfaces.Mode
return newModelDelegate(interfaces.ModelIdPermission, doc, args...)
case *models.RolePermission:
return newModelDelegate(interfaces.ModelIdRolePermission, doc, args...)
case *models.Environment:
return newModelDelegate(interfaces.ModelIdEnvironment, doc, args...)
default:
_ = trace.TraceError(errors2.ErrorModelInvalidType)
return nil
Expand Down
30 changes: 30 additions & 0 deletions models/models/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package models

import (
"github.com/crawlab-team/crawlab-core/interfaces"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type Environment struct {
Id primitive.ObjectID `json:"_id" bson:"_id"`
Key string `json:"key" bson:"key"`
Value string `json:"value" bson:"value"`
}

func (e *Environment) GetId() (id primitive.ObjectID) {
return e.Id
}

func (e *Environment) SetId(id primitive.ObjectID) {
e.Id = id
}

type EnvironmentList []Environment

func (l *EnvironmentList) GetModels() (res []interfaces.Model) {
for i := range *l {
d := (*l)[i]
res = append(res, &d)
}
return res
}
3 changes: 3 additions & 0 deletions models/models/utils_model_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ModelMap struct {
UserRole UserRole
Permission Permission
RolePermission RolePermission
Environment Environment
}

type ModelListMap struct {
Expand Down Expand Up @@ -58,6 +59,7 @@ type ModelListMap struct {
UserRoles UserRoleList
PermissionList PermissionList
RolePermissionList RolePermissionList
Environments EnvironmentList
}

func NewModelMap() (m *ModelMap) {
Expand Down Expand Up @@ -92,5 +94,6 @@ func NewModelListMap() (m *ModelListMap) {
Roles: RoleList{},
PermissionList: PermissionList{},
RolePermissionList: RolePermissionList{},
Environments: EnvironmentList{},
}
}
2 changes: 2 additions & 0 deletions models/service/binder_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ func (b *BasicBinder) Bind() (res interfaces.Model, err error) {
return b.Process(&m.Permission)
case interfaces.ModelIdRolePermission:
return b.Process(&m.RolePermission)
case interfaces.ModelIdEnvironment:
return b.Process(&m.Environment)
default:
return nil, errors.ErrorModelInvalidModelId
}
Expand Down
2 changes: 2 additions & 0 deletions models/service/binder_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ func (b *ListBinder) Bind() (l interfaces.List, err error) {
return b.Process(&m.PermissionList)
case interfaces.ModelIdRolePermission:
return b.Process(&m.RolePermissionList)
case interfaces.ModelIdEnvironment:
return b.Process(&m.Environments)
default:
return l, errors.ErrorModelInvalidModelId
}
Expand Down
40 changes: 40 additions & 0 deletions models/service/environment_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package service

import (
"github.com/crawlab-team/crawlab-core/errors"
"github.com/crawlab-team/crawlab-core/interfaces"
models2 "github.com/crawlab-team/crawlab-core/models/models"
"github.com/crawlab-team/crawlab-db/mongo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)

func convertTypeEnvironment(d interface{}, err error) (res *models2.Environment, err2 error) {
if err != nil {
return nil, err
}
res, ok := d.(*models2.Environment)
if !ok {
return nil, errors.ErrorModelInvalidType
}
return res, nil
}

func (svc *Service) GetEnvironmentById(id primitive.ObjectID) (res *models2.Environment, err error) {
d, err := svc.GetBaseService(interfaces.ModelIdEnvironment).GetById(id)
return convertTypeEnvironment(d, err)
}

func (svc *Service) GetEnvironment(query bson.M, opts *mongo.FindOptions) (res *models2.Environment, err error) {
d, err := svc.GetBaseService(interfaces.ModelIdEnvironment).Get(query, opts)
return convertTypeEnvironment(d, err)
}

func (svc *Service) GetEnvironmentList(query bson.M, opts *mongo.FindOptions) (res []models2.Environment, err error) {
l, err := svc.GetBaseService(interfaces.ModelIdEnvironment).GetList(query, opts)
for _, doc := range l.GetModels() {
d := doc.(*models2.Environment)
res = append(res, *d)
}
return res, nil
}
3 changes: 3 additions & 0 deletions models/service/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,7 @@ type ModelService interface {
GetRolePermissionList(query bson.M, opts *mongo.FindOptions) (res []models.RolePermission, err error)
GetRolePermissionListByRoleId(id primitive.ObjectID, opts *mongo.FindOptions) (res []models.RolePermission, err error)
GetRolePermissionListByPermissionId(id primitive.ObjectID, opts *mongo.FindOptions) (res []models.RolePermission, err error)
GetEnvironmentById(id primitive.ObjectID) (res *models.Environment, err error)
GetEnvironment(query bson.M, opts *mongo.FindOptions) (res *models.Environment, err error)
GetEnvironmentList(query bson.M, opts *mongo.FindOptions) (res []models.Environment, err error)
}
3 changes: 3 additions & 0 deletions routes/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ func registerRoutesAuthGroup(svc *RouterService, groups *RouterGroups) {

// data sources
svc.RegisterListActionControllerToGroup(groups.AuthGroup, "/data-sources", controllers.DataSourceController)

// environments
svc.RegisterListActionControllerToGroup(groups.AuthGroup, "/environments", controllers.EnvironmentController)
}

func registerRoutesFilterGroup(svc *RouterService, groups *RouterGroups) {
Expand Down
2 changes: 2 additions & 0 deletions utils/binders/binder_col_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func (b *ColNameBinder) Bind() (res interface{}, err error) {
return interfaces.ModelColNamePermission, nil
case interfaces.ModelIdRolePermission:
return interfaces.ModelColNameRolePermission, nil
case interfaces.ModelIdEnvironment:
return interfaces.ModelColNameEnvironment, nil

// invalid
default:
Expand Down

0 comments on commit d165022

Please sign in to comment.