2024-04-09 17:21:21 +00:00
|
|
|
package action
|
2024-02-26 10:49:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"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"
|
2024-05-04 09:55:57 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/execution"
|
2024-04-09 17:21:21 +00:00
|
|
|
action "github.com/zitadel/zitadel/pkg/grpc/action/v3alpha"
|
2024-02-26 10:49:43 +00:00
|
|
|
)
|
|
|
|
|
2024-04-09 17:21:21 +00:00
|
|
|
func (s *Server) ListExecutionFunctions(_ context.Context, _ *action.ListExecutionFunctionsRequest) (*action.ListExecutionFunctionsResponse, error) {
|
|
|
|
return &action.ListExecutionFunctionsResponse{
|
2024-02-26 10:49:43 +00:00
|
|
|
Functions: s.ListActionFunctions(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-04-09 17:21:21 +00:00
|
|
|
func (s *Server) ListExecutionMethods(_ context.Context, _ *action.ListExecutionMethodsRequest) (*action.ListExecutionMethodsResponse, error) {
|
|
|
|
return &action.ListExecutionMethodsResponse{
|
2024-02-26 10:49:43 +00:00
|
|
|
Methods: s.ListGRPCMethods(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-04-09 17:21:21 +00:00
|
|
|
func (s *Server) ListExecutionServices(_ context.Context, _ *action.ListExecutionServicesRequest) (*action.ListExecutionServicesResponse, error) {
|
|
|
|
return &action.ListExecutionServicesResponse{
|
2024-02-26 10:49:43 +00:00
|
|
|
Services: s.ListGRPCServices(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-04-09 17:21:21 +00:00
|
|
|
func (s *Server) SetExecution(ctx context.Context, req *action.SetExecutionRequest) (*action.SetExecutionResponse, error) {
|
|
|
|
if err := checkExecutionEnabled(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-04 09:55:57 +00:00
|
|
|
targets := make([]*execution.Target, len(req.Targets))
|
|
|
|
for i, target := range req.Targets {
|
|
|
|
switch t := target.GetType().(type) {
|
|
|
|
case *action.ExecutionTargetType_Include:
|
|
|
|
include, err := conditionToInclude(t.Include)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
targets[i] = &execution.Target{Type: domain.ExecutionTargetTypeInclude, Target: include}
|
|
|
|
case *action.ExecutionTargetType_Target:
|
|
|
|
targets[i] = &execution.Target{Type: domain.ExecutionTargetTypeTarget, Target: t.Target}
|
|
|
|
}
|
|
|
|
}
|
2024-02-26 10:49:43 +00:00
|
|
|
set := &command.SetExecution{
|
2024-05-04 09:55:57 +00:00
|
|
|
Targets: targets,
|
2024-02-26 10:49:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var details *domain.ObjectDetails
|
|
|
|
switch t := req.GetCondition().GetConditionType().(type) {
|
2024-04-09 17:21:21 +00:00
|
|
|
case *action.Condition_Request:
|
2024-05-04 09:55:57 +00:00
|
|
|
cond := executionConditionFromRequest(t.Request)
|
2024-02-26 10:49:43 +00:00
|
|
|
details, err = s.command.SetExecutionRequest(ctx, cond, set, authz.GetInstance(ctx).InstanceID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-09 17:21:21 +00:00
|
|
|
case *action.Condition_Response:
|
2024-05-04 09:55:57 +00:00
|
|
|
cond := executionConditionFromResponse(t.Response)
|
2024-02-26 10:49:43 +00:00
|
|
|
details, err = s.command.SetExecutionResponse(ctx, cond, set, authz.GetInstance(ctx).InstanceID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-09 17:21:21 +00:00
|
|
|
case *action.Condition_Event:
|
2024-05-04 09:55:57 +00:00
|
|
|
cond := executionConditionFromEvent(t.Event)
|
2024-02-26 10:49:43 +00:00
|
|
|
details, err = s.command.SetExecutionEvent(ctx, cond, set, authz.GetInstance(ctx).InstanceID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-09 17:21:21 +00:00
|
|
|
case *action.Condition_Function:
|
2024-05-04 09:55:57 +00:00
|
|
|
details, err = s.command.SetExecutionFunction(ctx, command.ExecutionFunctionCondition(t.Function.GetName()), set, authz.GetInstance(ctx).InstanceID())
|
2024-02-26 10:49:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2024-04-09 17:21:21 +00:00
|
|
|
return &action.SetExecutionResponse{
|
2024-02-26 10:49:43 +00:00
|
|
|
Details: object.DomainToDetailsPb(details),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-05-04 09:55:57 +00:00
|
|
|
func conditionToInclude(cond *action.Condition) (string, error) {
|
|
|
|
switch t := cond.GetConditionType().(type) {
|
|
|
|
case *action.Condition_Request:
|
|
|
|
cond := executionConditionFromRequest(t.Request)
|
|
|
|
if err := cond.IsValid(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return cond.ID(domain.ExecutionTypeRequest), nil
|
|
|
|
case *action.Condition_Response:
|
|
|
|
cond := executionConditionFromResponse(t.Response)
|
|
|
|
if err := cond.IsValid(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return cond.ID(domain.ExecutionTypeRequest), nil
|
|
|
|
case *action.Condition_Event:
|
|
|
|
cond := executionConditionFromEvent(t.Event)
|
|
|
|
if err := cond.IsValid(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return cond.ID(), nil
|
|
|
|
case *action.Condition_Function:
|
|
|
|
cond := command.ExecutionFunctionCondition(t.Function.GetName())
|
|
|
|
if err := cond.IsValid(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return cond.ID(), nil
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2024-04-09 17:21:21 +00:00
|
|
|
func (s *Server) DeleteExecution(ctx context.Context, req *action.DeleteExecutionRequest) (*action.DeleteExecutionResponse, error) {
|
|
|
|
if err := checkExecutionEnabled(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-26 10:49:43 +00:00
|
|
|
var err error
|
|
|
|
var details *domain.ObjectDetails
|
|
|
|
switch t := req.GetCondition().GetConditionType().(type) {
|
2024-04-09 17:21:21 +00:00
|
|
|
case *action.Condition_Request:
|
2024-05-04 09:55:57 +00:00
|
|
|
cond := executionConditionFromRequest(t.Request)
|
2024-02-26 10:49:43 +00:00
|
|
|
details, err = s.command.DeleteExecutionRequest(ctx, cond, authz.GetInstance(ctx).InstanceID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-09 17:21:21 +00:00
|
|
|
case *action.Condition_Response:
|
2024-05-04 09:55:57 +00:00
|
|
|
cond := executionConditionFromResponse(t.Response)
|
2024-02-26 10:49:43 +00:00
|
|
|
details, err = s.command.DeleteExecutionResponse(ctx, cond, authz.GetInstance(ctx).InstanceID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-09 17:21:21 +00:00
|
|
|
case *action.Condition_Event:
|
2024-05-04 09:55:57 +00:00
|
|
|
cond := executionConditionFromEvent(t.Event)
|
2024-02-26 10:49:43 +00:00
|
|
|
details, err = s.command.DeleteExecutionEvent(ctx, cond, authz.GetInstance(ctx).InstanceID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-09 17:21:21 +00:00
|
|
|
case *action.Condition_Function:
|
2024-05-04 09:55:57 +00:00
|
|
|
details, err = s.command.DeleteExecutionFunction(ctx, command.ExecutionFunctionCondition(t.Function.GetName()), authz.GetInstance(ctx).InstanceID())
|
2024-02-26 10:49:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2024-04-09 17:21:21 +00:00
|
|
|
return &action.DeleteExecutionResponse{
|
2024-02-26 10:49:43 +00:00
|
|
|
Details: object.DomainToDetailsPb(details),
|
|
|
|
}, nil
|
|
|
|
}
|
2024-05-04 09:55:57 +00:00
|
|
|
|
|
|
|
func executionConditionFromRequest(request *action.RequestExecution) *command.ExecutionAPICondition {
|
|
|
|
return &command.ExecutionAPICondition{
|
|
|
|
Method: request.GetMethod(),
|
|
|
|
Service: request.GetService(),
|
|
|
|
All: request.GetAll(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func executionConditionFromResponse(response *action.ResponseExecution) *command.ExecutionAPICondition {
|
|
|
|
return &command.ExecutionAPICondition{
|
|
|
|
Method: response.GetMethod(),
|
|
|
|
Service: response.GetService(),
|
|
|
|
All: response.GetAll(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func executionConditionFromEvent(event *action.EventExecution) *command.ExecutionEventCondition {
|
|
|
|
return &command.ExecutionEventCondition{
|
|
|
|
Event: event.GetEvent(),
|
|
|
|
Group: event.GetGroup(),
|
|
|
|
All: event.GetAll(),
|
|
|
|
}
|
|
|
|
}
|