2024-04-09 17:21:21 +00:00
|
|
|
package action
|
2024-02-15 05:39:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/muhlemmer/gu"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc/object/v2"
|
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
2024-04-09 17:21:21 +00:00
|
|
|
action "github.com/zitadel/zitadel/pkg/grpc/action/v3alpha"
|
2024-02-15 05:39:10 +00:00
|
|
|
)
|
|
|
|
|
2024-04-09 17:21:21 +00:00
|
|
|
func (s *Server) CreateTarget(ctx context.Context, req *action.CreateTargetRequest) (*action.CreateTargetResponse, error) {
|
|
|
|
if err := checkExecutionEnabled(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-15 05:39:10 +00:00
|
|
|
add := createTargetToCommand(req)
|
|
|
|
details, err := s.command.AddTarget(ctx, add, authz.GetInstance(ctx).InstanceID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-09 17:21:21 +00:00
|
|
|
return &action.CreateTargetResponse{
|
2024-02-15 05:39:10 +00:00
|
|
|
Id: add.AggregateID,
|
|
|
|
Details: object.DomainToDetailsPb(details),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-04-09 17:21:21 +00:00
|
|
|
func (s *Server) UpdateTarget(ctx context.Context, req *action.UpdateTargetRequest) (*action.UpdateTargetResponse, error) {
|
|
|
|
if err := checkExecutionEnabled(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-15 05:39:10 +00:00
|
|
|
details, err := s.command.ChangeTarget(ctx, updateTargetToCommand(req), authz.GetInstance(ctx).InstanceID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-09 17:21:21 +00:00
|
|
|
return &action.UpdateTargetResponse{
|
2024-02-15 05:39:10 +00:00
|
|
|
Details: object.DomainToDetailsPb(details),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-04-09 17:21:21 +00:00
|
|
|
func (s *Server) DeleteTarget(ctx context.Context, req *action.DeleteTargetRequest) (*action.DeleteTargetResponse, error) {
|
|
|
|
if err := checkExecutionEnabled(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-15 05:39:10 +00:00
|
|
|
details, err := s.command.DeleteTarget(ctx, req.GetTargetId(), authz.GetInstance(ctx).InstanceID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-09 17:21:21 +00:00
|
|
|
return &action.DeleteTargetResponse{
|
2024-02-15 05:39:10 +00:00
|
|
|
Details: object.DomainToDetailsPb(details),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-04-09 17:21:21 +00:00
|
|
|
func createTargetToCommand(req *action.CreateTargetRequest) *command.AddTarget {
|
2024-05-04 09:55:57 +00:00
|
|
|
var (
|
|
|
|
targetType domain.TargetType
|
|
|
|
interruptOnError bool
|
|
|
|
)
|
2024-02-15 05:39:10 +00:00
|
|
|
switch t := req.GetTargetType().(type) {
|
2024-04-09 17:21:21 +00:00
|
|
|
case *action.CreateTargetRequest_RestWebhook:
|
2024-02-15 05:39:10 +00:00
|
|
|
targetType = domain.TargetTypeWebhook
|
2024-05-04 09:55:57 +00:00
|
|
|
interruptOnError = t.RestWebhook.InterruptOnError
|
|
|
|
case *action.CreateTargetRequest_RestCall:
|
|
|
|
targetType = domain.TargetTypeCall
|
|
|
|
interruptOnError = t.RestCall.InterruptOnError
|
|
|
|
case *action.CreateTargetRequest_RestAsync:
|
|
|
|
targetType = domain.TargetTypeAsync
|
2024-02-15 05:39:10 +00:00
|
|
|
}
|
|
|
|
return &command.AddTarget{
|
|
|
|
Name: req.GetName(),
|
|
|
|
TargetType: targetType,
|
2024-05-04 09:55:57 +00:00
|
|
|
Endpoint: req.GetEndpoint(),
|
2024-02-15 05:39:10 +00:00
|
|
|
Timeout: req.GetTimeout().AsDuration(),
|
2024-05-04 09:55:57 +00:00
|
|
|
InterruptOnError: interruptOnError,
|
2024-02-15 05:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 17:21:21 +00:00
|
|
|
func updateTargetToCommand(req *action.UpdateTargetRequest) *command.ChangeTarget {
|
2024-02-15 05:39:10 +00:00
|
|
|
if req == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
target := &command.ChangeTarget{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: req.GetTargetId(),
|
|
|
|
},
|
2024-05-04 09:55:57 +00:00
|
|
|
Name: req.Name,
|
|
|
|
Endpoint: req.Endpoint,
|
2024-02-15 05:39:10 +00:00
|
|
|
}
|
2024-05-04 09:55:57 +00:00
|
|
|
if req.TargetType != nil {
|
|
|
|
switch t := req.GetTargetType().(type) {
|
|
|
|
case *action.UpdateTargetRequest_RestWebhook:
|
|
|
|
target.TargetType = gu.Ptr(domain.TargetTypeWebhook)
|
|
|
|
target.InterruptOnError = gu.Ptr(t.RestWebhook.InterruptOnError)
|
|
|
|
case *action.UpdateTargetRequest_RestCall:
|
|
|
|
target.TargetType = gu.Ptr(domain.TargetTypeCall)
|
|
|
|
target.InterruptOnError = gu.Ptr(t.RestCall.InterruptOnError)
|
|
|
|
case *action.UpdateTargetRequest_RestAsync:
|
|
|
|
target.TargetType = gu.Ptr(domain.TargetTypeAsync)
|
|
|
|
target.InterruptOnError = gu.Ptr(false)
|
|
|
|
}
|
2024-02-15 05:39:10 +00:00
|
|
|
}
|
|
|
|
if req.Timeout != nil {
|
|
|
|
target.Timeout = gu.Ptr(req.GetTimeout().AsDuration())
|
|
|
|
}
|
|
|
|
return target
|
|
|
|
}
|