2024-07-31 12:42:12 +00:00
|
|
|
package action
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/muhlemmer/gu"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
resource_object "github.com/zitadel/zitadel/internal/api/grpc/resources/object/v3alpha"
|
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
object "github.com/zitadel/zitadel/pkg/grpc/object/v3alpha"
|
|
|
|
action "github.com/zitadel/zitadel/pkg/grpc/resources/action/v3alpha"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) CreateTarget(ctx context.Context, req *action.CreateTargetRequest) (*action.CreateTargetResponse, error) {
|
2024-08-12 20:32:01 +00:00
|
|
|
if err := checkActionsEnabled(ctx); err != nil {
|
2024-07-31 12:42:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
add := createTargetToCommand(req)
|
2024-08-12 20:32:01 +00:00
|
|
|
instanceID := authz.GetInstance(ctx).InstanceID()
|
|
|
|
details, err := s.command.AddTarget(ctx, add, instanceID)
|
2024-07-31 12:42:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &action.CreateTargetResponse{
|
2024-08-12 20:32:01 +00:00
|
|
|
Details: resource_object.DomainToDetailsPb(details, object.OwnerType_OWNER_TYPE_INSTANCE, instanceID),
|
2024-07-31 12:42:12 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) PatchTarget(ctx context.Context, req *action.PatchTargetRequest) (*action.PatchTargetResponse, error) {
|
2024-08-12 20:32:01 +00:00
|
|
|
if err := checkActionsEnabled(ctx); err != nil {
|
2024-07-31 12:42:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-12 20:32:01 +00:00
|
|
|
instanceID := authz.GetInstance(ctx).InstanceID()
|
|
|
|
details, err := s.command.ChangeTarget(ctx, patchTargetToCommand(req), instanceID)
|
2024-07-31 12:42:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &action.PatchTargetResponse{
|
2024-08-12 20:32:01 +00:00
|
|
|
Details: resource_object.DomainToDetailsPb(details, object.OwnerType_OWNER_TYPE_INSTANCE, instanceID),
|
2024-07-31 12:42:12 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) DeleteTarget(ctx context.Context, req *action.DeleteTargetRequest) (*action.DeleteTargetResponse, error) {
|
2024-08-12 20:32:01 +00:00
|
|
|
if err := checkActionsEnabled(ctx); err != nil {
|
2024-07-31 12:42:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-12 20:32:01 +00:00
|
|
|
instanceID := authz.GetInstance(ctx).InstanceID()
|
|
|
|
details, err := s.command.DeleteTarget(ctx, req.GetId(), instanceID)
|
2024-07-31 12:42:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &action.DeleteTargetResponse{
|
2024-08-12 20:32:01 +00:00
|
|
|
Details: resource_object.DomainToDetailsPb(details, object.OwnerType_OWNER_TYPE_INSTANCE, instanceID),
|
2024-07-31 12:42:12 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTargetToCommand(req *action.CreateTargetRequest) *command.AddTarget {
|
|
|
|
reqTarget := req.GetTarget()
|
|
|
|
var (
|
|
|
|
targetType domain.TargetType
|
|
|
|
interruptOnError bool
|
|
|
|
)
|
|
|
|
switch t := reqTarget.GetTargetType().(type) {
|
|
|
|
case *action.Target_RestWebhook:
|
|
|
|
targetType = domain.TargetTypeWebhook
|
|
|
|
interruptOnError = t.RestWebhook.InterruptOnError
|
|
|
|
case *action.Target_RestCall:
|
|
|
|
targetType = domain.TargetTypeCall
|
|
|
|
interruptOnError = t.RestCall.InterruptOnError
|
|
|
|
case *action.Target_RestAsync:
|
|
|
|
targetType = domain.TargetTypeAsync
|
|
|
|
}
|
|
|
|
return &command.AddTarget{
|
|
|
|
Name: reqTarget.GetName(),
|
|
|
|
TargetType: targetType,
|
|
|
|
Endpoint: reqTarget.GetEndpoint(),
|
|
|
|
Timeout: reqTarget.GetTimeout().AsDuration(),
|
|
|
|
InterruptOnError: interruptOnError,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func patchTargetToCommand(req *action.PatchTargetRequest) *command.ChangeTarget {
|
|
|
|
reqTarget := req.GetTarget()
|
|
|
|
if reqTarget == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
target := &command.ChangeTarget{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: req.GetId(),
|
|
|
|
},
|
|
|
|
Name: reqTarget.Name,
|
|
|
|
Endpoint: reqTarget.Endpoint,
|
|
|
|
}
|
|
|
|
if reqTarget.TargetType != nil {
|
|
|
|
switch t := reqTarget.GetTargetType().(type) {
|
|
|
|
case *action.PatchTarget_RestWebhook:
|
|
|
|
target.TargetType = gu.Ptr(domain.TargetTypeWebhook)
|
|
|
|
target.InterruptOnError = gu.Ptr(t.RestWebhook.InterruptOnError)
|
|
|
|
case *action.PatchTarget_RestCall:
|
|
|
|
target.TargetType = gu.Ptr(domain.TargetTypeCall)
|
|
|
|
target.InterruptOnError = gu.Ptr(t.RestCall.InterruptOnError)
|
|
|
|
case *action.PatchTarget_RestAsync:
|
|
|
|
target.TargetType = gu.Ptr(domain.TargetTypeAsync)
|
|
|
|
target.InterruptOnError = gu.Ptr(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if reqTarget.Timeout != nil {
|
|
|
|
target.Timeout = gu.Ptr(reqTarget.GetTimeout().AsDuration())
|
|
|
|
}
|
|
|
|
return target
|
|
|
|
}
|