guardian
guardian copied to clipboard
Support dry-run option for create & update in policy & provider
Describe the solution you'd like
- accept
dry_run
flag in payload
message CreatePolicyRequest {
Policy policy = 1;
+ bool dry_run = 2;
}
message UpdatePolicyRequest {
string id = 1;
Policy policy = 2;
+ bool dry_run = 3;
}
message CreateProviderRequest {
ProviderConfig config = 1;
+ bool dry_run = 2;
}
message UpdateProviderRequest {
string id = 1;
ProviderConfig config = 2;
+ bool dry_run = 2;
}
- add support to policy service and provider service to accept dry_run info from context
// core/policy/service.go
func WithDryRun(ctx context.Context) context.Context {
return context.WithValue(ctx, "dry_run")
}
func isDryRun(ctx) bool {
return ctx.Value("dry_run").(bool)
}
...
func (s *Service) Create(ctx context.Context, p *domain.Policy) error {
...
dryRun := isDryRun(ctx)
if !dryRun {
if err := s.policyRepository.Create(p); err != nil {
return err
}
}
return nil
}
- pass
dry_run
flag to service in grpc handler
// api/handler/v1beta1/grpc.go GRPCServer.CreatePolicy
...
if req.GetDryRun() {
ctx = policy.WithDryRun(ctx)
}
if err := s.policyService.Create(ctx, policy); err != nil {
return nil, status.Errorf(codes.Internal, "failed to create policy: %v", err)
}
...