Skip to content

Commit

Permalink
feat: create ns,eb,sub with id (#610)
Browse files Browse the repository at this point in the history
Signed-off-by: xdlbdy <xdlbdy@gmail.com>
Co-authored-by: James Yin <ywhjames@hotmail.com>
  • Loading branch information
xdlbdy and ifplusor authored Aug 10, 2023
1 parent 8bbfce9 commit 2735bb4
Show file tree
Hide file tree
Showing 8 changed files with 837 additions and 1,054 deletions.
48 changes: 36 additions & 12 deletions internal/controller/eventbus/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,32 @@ func (ctrl *controller) CreateSystemEventbus(
return ctrl.createEventbus(ctx, req)
}

func (ctrl *controller) isEventbusExist(ctx context.Context, req *ctrlpb.CreateEventbusRequest) (bool, error) {
id := vanus.NewIDFromUint64(req.Id)
if !id.Equals(vanus.EmptyID()) {
// check id exist
_, err := ctrl.getEventbus(id)
if err == nil {
return true, nil
}
if !errors.Is(err, errors.ErrResourceNotFound) {
return false, err
}
}
// check name exist
_, err := ctrl.GetEventbusWithHumanFriendly(ctx, &ctrlpb.GetEventbusWithHumanFriendlyRequest{
NamespaceId: req.NamespaceId,
EventbusName: req.Name,
})
if err == nil {
return true, nil
}
if !errors.Is(err, errors.ErrResourceNotFound) {
return false, err
}
return false, nil
}

func (ctrl *controller) createEventbus(
ctx context.Context, req *ctrlpb.CreateEventbusRequest,
) (*metapb.Eventbus, error) {
Expand All @@ -238,22 +264,20 @@ func (ctrl *controller) createEventbus(
return nil, errors.ErrInvalidRequest.WithMessage(fmt.Sprintf("the number of eventlog exceeded,"+
" maximum is %d", maximumEventlogNum))
}

pb, err := ctrl.GetEventbusWithHumanFriendly(ctx, &ctrlpb.GetEventbusWithHumanFriendlyRequest{
NamespaceId: req.NamespaceId,
EventbusName: req.Name,
})
if err != nil && !errors.Is(err, errors.ErrResourceNotFound) {
ebExist, err := ctrl.isEventbusExist(ctx, req)
if err != nil {
return nil, err
}
if pb != nil {
if ebExist {
return nil, errors.ErrResourceAlreadyExist.WithMessage("the eventbus already exists")
}

id, err := vanus.NewID()
if err != nil {
log.Warn(ctx).Err(err).Msg("failed to create eventbus ID")
return nil, err
id := vanus.NewIDFromUint64(req.Id)
if id.Equals(vanus.EmptyID()) {
id, err = vanus.NewID()
if err != nil {
log.Warn(ctx).Err(err).Msg("failed to create eventbus ID")
return nil, err
}
}
eb := &metadata.Eventbus{
ID: id,
Expand Down
12 changes: 7 additions & 5 deletions internal/controller/tenant/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,17 @@ func (ctrl *controller) CreateNamespace(ctx context.Context,
}

func (ctrl *controller) createNamespace(ctx context.Context, ns *metadata.Namespace) error {
id, err := vanus.NewID()
if err != nil {
return err
if ns.ID.Equals(vanus.EmptyID()) {
id, err := vanus.NewID()
if err != nil {
return err
}
ns.ID = id
}
ns.ID = id
now := time.Now()
ns.CreatedAt = now
ns.UpdatedAt = now
err = ctrl.namespaceManager.AddNamespace(ctx, ns)
err := ctrl.namespaceManager.AddNamespace(ctx, ns)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions internal/controller/tenant/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

func FromPbCreateNamespace(ns *ctrlpb.CreateNamespaceRequest) *metadata.Namespace {
to := &metadata.Namespace{
ID: vanus.NewIDFromUint64(ns.Id),
Name: ns.Name,
Description: ns.Description,
}
Expand Down
17 changes: 13 additions & 4 deletions internal/controller/trigger/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,25 @@ func (ctrl *controller) CreateSubscription(
log.Info(ctx).Err(err).Msg("Invalid subscription.")
return nil, err
}
subID := vanus.NewIDFromUint64(request.Id)
if !subID.Equals(vanus.EmptyID()) {
sub := ctrl.subscriptionManager.GetSubscription(ctx, subID)
if sub != nil {
return nil, errors.ErrResourceAlreadyExist.WithMessage("subscription exist")
}
} else {
subID, err = vanus.NewID()
if err != nil {
return nil, err
}
}
sub, err := convert.FromPbSubscriptionRequest(request.Subscription)
if err != nil {
log.Info(ctx).Err(err).Msg("Invalid subscription.")
return nil, errors.ErrInvalidRequest.WithMessage(err.Error())
}

sub.ID, err = vanus.NewID()
if err != nil {
return nil, err
}
sub.ID = subID
sub.CreatedAt = time.Now()
sub.UpdatedAt = time.Now()
if request.Subscription.Disable {
Expand Down
41 changes: 34 additions & 7 deletions proto/pkg/controller/controller.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions proto/pkg/controller/controller_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2735bb4

Please sign in to comment.